Example #1
0
        public GitHubLinkTask(ILogger <GitHubLinkTask> logger, IGitRepository repository, IGitHubClientFactory gitHubClientFactory)
        {
            m_Logger              = logger ?? throw new ArgumentNullException(nameof(logger));
            m_Repository          = repository ?? throw new ArgumentNullException(nameof(repository));
            m_GitHubClientFactory = gitHubClientFactory ?? throw new ArgumentNullException(nameof(gitHubClientFactory));

            // 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 && GitHubUrlParser.TryParseRemoteUrl(remote.Url, out var projectInfo))
            {
                m_ProjectInfo = projectInfo;
            }
            else
            {
                m_Logger.LogWarning("Failed to determine GitHub project name. Disabling GitHub integration");
            }
        }
Example #2
0
        private GitHubProjectInfo?GetProjectInfo()
        {
            var host  = m_Configuration.Integrations.GitHub.Host;
            var owner = m_Configuration.Integrations.GitHub.Owner;
            var repo  = m_Configuration.Integrations.GitHub.Repository;

            // if all required properties were specified in the configuration, return project info
            if (!String.IsNullOrWhiteSpace(host) && !String.IsNullOrWhiteSpace(owner) && !String.IsNullOrWhiteSpace(repo))
            {
                m_Logger.LogDebug("Using GitHub project information from configuration");
                return(new GitHubProjectInfo(host, owner, repo));
            }
            // otherwise try to determine the missing properties from the repository's remote url
            else
            {
                // get configured remote
                var remoteName = m_Configuration.Integrations.GitHub.RemoteName;
                m_Logger.LogDebug(
                    $"GitHub 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 (GitHubUrlParser.TryParseRemoteUrl(remote.Url, out var parsedProjectInfo))
                {
                    if (String.IsNullOrWhiteSpace(host))
                    {
                        m_Logger.LogDebug($"Using GitHub host '{parsedProjectInfo.Host}' from remote url.");
                        host = parsedProjectInfo.Host;
                    }

                    if (String.IsNullOrWhiteSpace(owner))
                    {
                        m_Logger.LogDebug($"Using GitHub owner '{parsedProjectInfo.Owner}' from remote url.");
                        owner = parsedProjectInfo.Owner;
                    }

                    if (String.IsNullOrWhiteSpace(repo))
                    {
                        m_Logger.LogDebug($"Using GitHub repository '{parsedProjectInfo.Repository}' from remote url.");
                        repo = parsedProjectInfo.Repository;
                    }

                    return(new GitHubProjectInfo(host, owner, repo));
                }
                else
                {
                    m_Logger.LogDebug($"Failed to determine GitHub project information from remote url '{remote.Url}'");
                }
            }

            return(null);
        }