private async Task PostDiscardedCommitCommentAsync( GitHubProject baseProject, GitHubPullRequest pullRequestToUpdate, GitCommit oldCommit, IGitHubClient client) { GitHubCombinedStatus combinedStatus = await client.GetStatusAsync( baseProject, oldCommit.Sha); CiStatusLine[] statuses = combinedStatus .Statuses .OrderBy(s => s.State) .ThenBy(s => s.Context) .Select(CiStatusLine.Create) .ToArray(); string statusLines = statuses .Aggregate(string.Empty, (acc, line) => acc + line.MarkdownLine + "\r\n"); string ciSummary = string.Join( " ", statuses .GroupBy(s => s.Emoticon) .Select(g => $"{g.Count()}{g.Key}") .ToArray()); string commentBody = $"Discarded [`{oldCommit.Sha.Substring(0, 7)}`]({oldCommit.HtmlUrl}): " + $"`{oldCommit.Message}`"; if (statuses.Any()) { commentBody += "\r\n\r\n" + "<details>" + "<summary>" + $"CI Status: {ciSummary} (click to expand)\r\n" + "</summary>" + $"\r\n\r\n{statusLines}\r\n" + "</details>"; } await client.PostCommentAsync( baseProject, pullRequestToUpdate.Number, commentBody); }
public async Task CreateOrUpdateAsync( string commitMessage, string title, string description, GitHubBranch baseBranch, GitHubProject origin, PullRequestOptions options, IGitHubClient client) { options = options ?? new PullRequestOptions(); client.AdjustOptionsToCapability(options); var upstream = baseBranch.Project; GitHubBranch originBranch = null; GitHubPullRequest pullRequestToUpdate = null; IUpdateBranchNamingStrategy namingStrategy = options.BranchNamingStrategy ?? new SingleBranchNamingStrategy("UpdateDependencies"); string upgradeBranchPrefix = namingStrategy.Prefix(baseBranch.Name); if (!options.ForceCreate) { string myAuthorId = await client.GetMyAuthorIdAsync(); pullRequestToUpdate = await client.SearchPullRequestsAsync( upstream, upgradeBranchPrefix, myAuthorId); if (pullRequestToUpdate == null) { Trace.TraceInformation($"No existing pull request found."); } else { Trace.TraceInformation( $"Pull request already exists for {upgradeBranchPrefix} in {upstream.Segments}. " + $"#{pullRequestToUpdate.Number}, '{pullRequestToUpdate.Title}'"); GitCommit headCommit = await client.GetCommitAsync( origin, pullRequestToUpdate.Head.Sha); string blockedReason = GetUpdateBlockedReason( pullRequestToUpdate, headCommit, upgradeBranchPrefix, origin, options); if (blockedReason == null) { if (options.TrackDiscardedCommits) { await PostDiscardedCommitCommentAsync( baseBranch.Project, pullRequestToUpdate, headCommit, client); } originBranch = new GitHubBranch( pullRequestToUpdate.Head.Ref, origin); } else { string comment = $"Couldn't update this pull request: {blockedReason}\n" + $"Would have applied '{commitMessage}'"; Trace.TraceInformation($"Sending comment to PR: {comment}"); await client.PostCommentAsync(upstream, pullRequestToUpdate.Number, comment); return; } } // No existing branch to update: push to a new one. if (originBranch == null) { string newBranchName = namingStrategy.Prefix(baseBranch.Name) + namingStrategy.CreateFreshBranchNameSuffix(baseBranch.Name); originBranch = new GitHubBranch(newBranchName, origin); } PushNewCommit(originBranch, commitMessage, client); if (pullRequestToUpdate != null) { await client.UpdateGitHubPullRequestAsync( upstream, pullRequestToUpdate.Number, title, description, maintainersCanModify : options.MaintainersCanModify); } else { await client.PostGitHubPullRequestAsync( title, description, originBranch, baseBranch, options.MaintainersCanModify); } } }