private async Task <(int?, DateTimeOffset)> GetCommitInfo(GitHubInfo gitHubInfo, Build build) { var commitAge = DateTimeOffset.Now; int?commitDistance = null; if (gitHubInfo == null) { commitAge = build.DateProduced; } else { var comparison = await GetCommitsBehindAsync(gitHubInfo, build); foreach (var commit in comparison.Commits) { if (commit.Commit.Author.Date < commitAge) { commitAge = commit.Commit.Author.Date; } } if (comparison.Commits.Count == 0) { commitAge = build.DateProduced; } // We're using the branch as the "head" so "ahead by" is actually how far the branch (i.e. "master") is // ahead of the commit. So it's also how far **behind** the commit is from the branch head. commitDistance = comparison.AheadBy; } return(commitDistance, commitAge); }
private async Task <CompareResult> GetCommitsBehindAsync(GitHubInfo gitHubInfo, Build build) { try { var comparison = await _github.Repository.Commit.Compare(gitHubInfo.Owner, gitHubInfo.Repo, build.Commit, build.GitHubBranch); return(comparison); } catch (NotFoundException) { _logger.LogWarning("Failed to compare commit history for '{0}/{1}' between '{2}' and '{3}'.", gitHubInfo.Owner, gitHubInfo.Repo, build.Commit, build.GitHubBranch); return(null); } }
public async Task OnGet(int channelId, string owner, string repo) { var repoUrl = $"https://github.com/{owner}/{repo}"; var latest = await _client.GetLatestAsync(repoUrl, null, null, channelId, null, null, false, ApiVersion10._20190116); var graph = await _client.GetBuildGraphAsync(latest.Id, (ApiVersion9)ApiVersion40._20190116); latest = graph.Builds[latest.Id.ToString()]; var incoming = new List <IncomingRepo>(); foreach (var dep in latest.Dependencies) { var build = graph.Builds[dep.BuildId.ToString()]; GitHubInfo?gitHubInfo = null; if (!string.IsNullOrEmpty(build.GitHubRepository)) { var match = _repoParser.Match(build.GitHubRepository); if (match.Success) { gitHubInfo = new GitHubInfo( match.Groups["owner"].Value, match.Groups["repo"].Value); } } if (!IncludeRepo(gitHubInfo)) { continue; } var(commitDistance, commitAge) = await GetCommitInfo(gitHubInfo, build); incoming.Add(new IncomingRepo( build, shortName: gitHubInfo?.Repo ?? "", commitDistance, GetCommitUrl(build), buildUrl: $"https://dev.azure.com/{build.AzureDevOpsAccount}/{build.AzureDevOpsProject}/_build/results?buildId={build.AzureDevOpsBuildId}&view=results", commitAge )); } IncomingRepositories = incoming; CurrentRateLimit = _github.GetLastApiInfo().RateLimit; }
public GitHubInfo?GetGitHubInfo(Build?build) { GitHubInfo?gitHubInfo = null; if (!string.IsNullOrEmpty(build?.GitHubRepository)) { var match = _repoParser.Match(build.GitHubRepository); if (match.Success) { gitHubInfo = new GitHubInfo( match.Groups["owner"].Value, match.Groups["repo"].Value); } } return(gitHubInfo); }