private async Task PostDiscardedCommitCommentAsync(
            GitHubProject baseProject,
            GitHubPullRequest pullRequestToUpdate,
            GitCommit oldCommit,
            GitHubClient 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);
        }
Exemple #2
0
        private async Task <string> CreateDiscardedCommitListBodyAsync(
            GitHubProject baseProject,
            GitHubPullRequest pullRequestToUpdate,
            GitCommit oldCommit,
            GitHubClient client)
        {
            // GitHub returns the HTML "commit" url, but we want "commits" so that CI results show.
            string oldCommitsUrl = oldCommit.HtmlUrl.Replace("commit", "commits");

            GitHubCombinedStatus combinedStatus = await client.GetStatusAsync(
                baseProject,
                oldCommit.Sha);

            string statusLines = combinedStatus
                                 .Statuses
                                 .OrderBy(s => s.State)
                                 .ThenBy(s => s.Context)
                                 .Select(GetStatusLine)
                                 .Aggregate(string.Empty, (acc, line) => acc + line + "\r\n");

            string oldCommitEntry =
                $" * [`{oldCommit.Sha.Substring(0, 7)}`]({oldCommitsUrl}) {oldCommit.Message}\r\n" +
                $"{statusLines}";

            // Find insertion point. GitHub always returns \r\n.
            string insertionMarker    = $"<{DiscardedCommitElementName}>\r\n\r\n";
            string endInsertionMarker = $"\r\n</{DiscardedCommitElementName}>";

            int elementBegin = pullRequestToUpdate.Body.IndexOf(
                insertionMarker,
                StringComparison.Ordinal);

            if (elementBegin != -1)
            {
                return(pullRequestToUpdate.Body.Insert(
                           elementBegin + insertionMarker.Length,
                           oldCommitEntry));
            }
            return(pullRequestToUpdate.Body +
                   "<details><summary>Discarded auto-update commits (click to expand)</summary>" +
                   $"{insertionMarker}{oldCommitEntry}{endInsertionMarker}" +
                   "</details>");
        }