diff --git a/src/CodeCoverageSummary/CommandLineOptions.cs b/src/CodeCoverageSummary/CommandLineOptions.cs index 012116b..4412032 100644 --- a/src/CodeCoverageSummary/CommandLineOptions.cs +++ b/src/CodeCoverageSummary/CommandLineOptions.cs @@ -9,5 +9,8 @@ namespace CodeCoverageSummary [Option(shortName: 'f', longName: "format", Required = false, HelpText = "Output Format.", Default = "text")] public string Format { get; set; } + + [Option(shortName: 'b', longName: "badge", Required = false, HelpText = "Include a badge in the output.", Default = false)] + public bool Badge { get; set; } } } diff --git a/src/CodeCoverageSummary/Program.cs b/src/CodeCoverageSummary/Program.cs index cb888be..fb4affa 100644 --- a/src/CodeCoverageSummary/Program.cs +++ b/src/CodeCoverageSummary/Program.cs @@ -26,12 +26,38 @@ namespace CodeCoverageSummary } else { + string badgeUrl = null; + + if (o.Badge) + { + string colour; + if (summary.LineRate < 0.5) + { + colour = "critical"; + } + else if (summary.LineRate < 0.7) + { + colour = "yellow"; + } + else + { + colour = "success"; + } + badgeUrl = $"https://img.shields.io/badge/Code%20Coverage-{summary.LineRate * 100:N0}%25-{colour}?style=flat"; + } + StringBuilder summaryText = new(); if (o.Format.Equals("text", StringComparison.OrdinalIgnoreCase)) { + if (string.IsNullOrWhiteSpace(badgeUrl)) + { + summaryText.AppendLine(badgeUrl); + } + summaryText.AppendLine($"Line Rate = {summary.LineRate * 100:N0}%, Lines Covered = {summary.LinesCovered} / {summary.LinesValid}") .AppendLine($"Branch Rate = {summary.BranchRate * 100:N0}%, Branches Covered = {summary.BranchesCovered} / {summary.BranchesValid}") .AppendLine($"Complexity = {summary.Complexity}"); + foreach (CodeCoverage package in summary.Packages) { summaryText.AppendLine($"{package.Name}: Line Rate = {package.LineRate * 100:N0}%, Branch Rate = {package.BranchRate * 100:N0}%, Complexity = {package.Complexity}"); @@ -39,12 +65,19 @@ namespace CodeCoverageSummary } else if (o.Format.Equals("md", StringComparison.OrdinalIgnoreCase) || o.Format.Equals("markdown", StringComparison.OrdinalIgnoreCase)) { + if (!string.IsNullOrWhiteSpace(badgeUrl)) + { + summaryText.AppendLine($"![Code Coverage]({badgeUrl})"); + } + summaryText.AppendLine("Package | Line Rate | Branch Rate | Complexity") .AppendLine("-------- | --------- | ----------- | ----------"); + foreach (CodeCoverage package in summary.Packages) { summaryText.AppendLine($"{package.Name} | {package.LineRate * 100:N0}% | {package.BranchRate * 100:N0}% | {package.Complexity}"); } + summaryText.Append($"**Summary** | **{summary.LineRate * 100:N0}%** ({summary.LinesCovered} / {summary.LinesValid}) | ") .AppendLine($"**{summary.BranchRate * 100:N0}%** ({summary.BranchesCovered} / {summary.BranchesValid}) | {summary.Complexity}"); }