Пример #1
0
        public static async Task <string> ToMarkdownCommentAsync(
            this QuantifierResult quantifierResult,
            string repositoryLink,
            string contextFileLink,
            string pullRequestLink,
            string authorName,
            MarkdownCommentOptions markdownCommentOptions = null)
        {
            markdownCommentOptions ??= new MarkdownCommentOptions();

            var stubble = new StubbleBuilder()
                          .Configure(settings => { settings.SetIgnoreCaseOnKeyLookup(true); }).Build();

            using var stream = new StreamReader(
                      Assembly.GetExecutingAssembly().GetManifestResourceStream(
                          $"{typeof(QuantifierResultExtensions).Namespace}.QuantifierComment.mustache") !);

            var feedbackLinkThumbsUp = CreateFeedbackLink(
                "ThumbsUp",
                authorName,
                repositoryLink,
                pullRequestLink);
            var feedbackLinkNeutral = CreateFeedbackLink(
                "Neutral",
                authorName,
                repositoryLink,
                pullRequestLink);
            var feedbackLinkThumbsDown = CreateFeedbackLink(
                "ThumbsDown",
                authorName,
                repositoryLink,
                pullRequestLink);

            var comment = await stubble.RenderAsync(
                await stream.ReadToEndAsync(),
                new
            {
                quantifierResult.Color,
                quantifierResult.Label,
                quantifierResult.QuantifiedLinesAdded,
                quantifierResult.QuantifiedLinesDeleted,
                quantifierResult.PercentileAddition,
                quantifierResult.PercentileDeletion,
                quantifierResult.FormulaPercentile,
                Formula             = quantifierResult.Formula.ToString(),
                ContextFileLink     = contextFileLink,
                FeedbackLinkUp      = feedbackLinkThumbsUp,
                FeedbackLinkNeutral = feedbackLinkNeutral,
                FeedbackLinkDown    = feedbackLinkThumbsDown,
                TotalFilesChanged   = quantifierResult.QuantifierInput.Changes.Count,
                Details             = string.Join(
                    Environment.NewLine,
                    quantifierResult.QuantifierInput.Changes
                    .GroupBy(c => c.FileExtension)
                    .Select(
                        g =>
                        $"{g.Key} : +{g.Sum(v => v.QuantifiedLinesAdded)} -{g.Sum(v => v.QuantifiedLinesDeleted)}")),
                CollapseChangesSummarySection        = markdownCommentOptions.CollapseChangesSummarySection ? string.Empty : "open",
                CollapsePullRequestQuantifiedSection = markdownCommentOptions.CollapsePullRequestQuantifiedSection ? string.Empty : "open"
            });

            return(comment);
        }
        public static async Task <string> ToMarkdownCommentAsync(
            this QuantifierResult quantifierResult,
            string repositoryLink,
            string contextFileLink,
            string pullRequestLink,
            string authorName,
            bool anonymous = true,
            MarkdownCommentOptions markdownCommentOptions = null)
        {
            markdownCommentOptions ??= new MarkdownCommentOptions();

            var stubble = new StubbleBuilder()
                          .Configure(settings => { settings.SetIgnoreCaseOnKeyLookup(true); }).Build();

            using var stream = new StreamReader(
                      Assembly.GetExecutingAssembly().GetManifestResourceStream(
                          $"{typeof(QuantifierResultExtensions).Namespace}.Mustache.QuantifierComment.mustache") !);

            var feedbackLinkThumbsUp = CreateFeedbackLink(
                "ThumbsUp",
                authorName,
                repositoryLink,
                pullRequestLink,
                anonymous);
            var feedbackLinkNeutral = CreateFeedbackLink(
                "Neutral",
                authorName,
                repositoryLink,
                pullRequestLink,
                anonymous);
            var feedbackLinkThumbsDown = CreateFeedbackLink(
                "ThumbsDown",
                authorName,
                repositoryLink,
                pullRequestLink,
                anonymous);

            var contextFormulaPercentile =
                quantifierResult.Context.FormulaPercentile.First(f => f.Item1 == quantifierResult.Formula).Item2;

            var(idealSizeLowerBound, idealSizeUpperBound) = GetIdealChangeCountRange(contextFormulaPercentile);
            var detailsByFileExt = quantifierResult.QuantifierInput.Changes
                                   .Where(c => !string.IsNullOrEmpty(c.FileExtension))
                                   .GroupBy(c => c.FileExtension)
                                   .Select(
                g =>
                $"{g.Key} : +{g.Sum(v => v.QuantifiedLinesAdded)} -{g.Sum(v => v.QuantifiedLinesDeleted)}")
                                   .Concat(
                quantifierResult.QuantifierInput.Changes
                .Where(c => string.IsNullOrEmpty(c.FileExtension))
                .Select(c => $"{c.FilePath} : +{c.QuantifiedLinesAdded} -{c.QuantifiedLinesDeleted}"));
            var comment = await stubble.RenderAsync(
                await stream.ReadToEndAsync(),
                new
            {
                // lower th color str because of a bug in shield service
                // https://img.shields.io/static/v1?label=Quantified&message=Extra%20Small&color=Red is not respecting the red color,
                // but https://img.shields.io/static/v1?label=Quantified&message=Extra%20Small&color=red is working
                Color = quantifierResult.Color.ToLower(),
                quantifierResult.Label,

                // encode the label in case contains space
                EncodedLabel = Uri.EscapeUriString(quantifierResult.Label),
                quantifierResult.QuantifiedLinesAdded,
                quantifierResult.QuantifiedLinesDeleted,
                quantifierResult.FormulaLinesChanged,
                quantifierResult.PercentileAddition,
                quantifierResult.PercentileDeletion,
                quantifierResult.FormulaPercentile,
                IdealSizeLowerBound = idealSizeLowerBound,
                IdealSizeUpperBound = idealSizeUpperBound,
                IsIdealSize         = quantifierResult.FormulaLinesChanged >= idealSizeLowerBound && quantifierResult.FormulaLinesChanged <= idealSizeUpperBound,
                Formula             = quantifierResult.Formula.ToString(),
                ContextFileLink     = !string.IsNullOrWhiteSpace(contextFileLink)
                        ? contextFileLink :
                                      "https://github.com/microsoft/PullRequestQuantifier/blob/main/docs/prquantifier-yaml.md",
                FeedbackLinkUp      = feedbackLinkThumbsUp,
                FeedbackLinkNeutral = feedbackLinkNeutral,
                FeedbackLinkDown    = feedbackLinkThumbsDown,
                TotalFilesChanged   = quantifierResult.QuantifierInput.Changes.Count,
                Details             = string.Join(Environment.NewLine, detailsByFileExt),
                CollapsePullRequestQuantifiedSection = markdownCommentOptions.CollapsePullRequestQuantifiedSection ? string.Empty : "open"
            });

            return(comment);
        }