public GitLabLinkTask(ILogger <GitLabLinkTask> logger, IGitRepository repository, IGitLabClientFactory clientFactory) { m_Logger = logger ?? throw new ArgumentNullException(nameof(logger)); m_Repository = repository ?? throw new ArgumentNullException(nameof(repository)); m_ClientFactory = clientFactory ?? throw new ArgumentNullException(nameof(clientFactory)); // TODO: Allow configuration of remote name // TODO: Allow bypassing parsing by setting project info in the config file var remote = m_Repository.Remotes.FirstOrDefault(r => StringComparer.OrdinalIgnoreCase.Equals(r.Name, "origin")); if (remote != null && GitLabUrlParser.TryParseRemoteUrl(remote.Url, out var projectInfo)) { m_ProjectInfo = projectInfo; } else { m_Logger.LogWarning("Failed to determine GitLab project path. Disabling GitLab integration"); } }
private GitLabProjectInfo?GetProjectInfo() { var host = m_Configuration.Integrations.GitLab.Host; var @namespace = m_Configuration.Integrations.GitLab.Namespace; var project = m_Configuration.Integrations.GitLab.Project; // if all required properties were specified in the configuration, return project info if (!String.IsNullOrWhiteSpace(host) && !String.IsNullOrWhiteSpace(@namespace) && !String.IsNullOrWhiteSpace(project)) { m_Logger.LogDebug("Using GitLab project information from configuration"); return(new GitLabProjectInfo(host, @namespace, project)); } // otherwise, try to determine the missing properties from the repository's remote url else { // get configured remote var remoteName = m_Configuration.Integrations.GitLab.RemoteName; m_Logger.LogDebug( $"GitLab project information from configuration is incomplete. " + $"Trying to get missing properties from git remote '{remoteName}'"); var remote = m_Repository.Remotes.FirstOrDefault(r => StringComparer.OrdinalIgnoreCase.Equals(r.Name, remoteName) ); if (remote == null) { m_Logger.LogWarning($"Remote '{remoteName}' does not exist in the git repository."); return(null); } // if remote url could be parsed, replace missing properties with value from remote url if (GitLabUrlParser.TryParseRemoteUrl(remote.Url, out var parsedProjectInfo)) { if (String.IsNullOrWhiteSpace(host)) { m_Logger.LogDebug($"Using GitLab host '{parsedProjectInfo.Host}' from remote url."); host = parsedProjectInfo.Host; } if (String.IsNullOrWhiteSpace(@namespace)) { m_Logger.LogDebug($"Using GitLab namespace '{parsedProjectInfo.Namespace}' from remote url."); @namespace = parsedProjectInfo.Namespace; } if (String.IsNullOrWhiteSpace(project)) { m_Logger.LogDebug($"Using GitLab project name '{parsedProjectInfo.Project}' from remote url."); project = parsedProjectInfo.Project; } return(new GitLabProjectInfo(host, @namespace, project)); } else { m_Logger.LogDebug($"Failed to determine GitLab project information from remote url '{remote.Url}'"); } } return(null); }