Skip to content

Commit

Permalink
Add support for uploading a coverage badge to gist
Browse files Browse the repository at this point in the history
  • Loading branch information
xoofx committed Feb 8, 2024
1 parent 1c2719e commit cb53623
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 4 deletions.
5 changes: 5 additions & 0 deletions src/DotNetReleaser.Tests/MockDevHosting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ public Task<bool> Connect()
return Task.FromResult(true);
}

public Task CreateOrUpdateGist(string gistId, string fileName, string content)
{
return Task.CompletedTask;
}

public Task<List<ReleaseVersion>> GetAllReleaseTags(string user, string repo, string tagPrefix)
{
throw new System.NotImplementedException();
Expand Down
12 changes: 12 additions & 0 deletions src/dotnet-releaser/Configuration/CoverageConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ public CoverageConfiguration()
{
Enable = true;

BadgeUploadToGist = false;
BadgeGistId = "https://gist.github.com";
Package = "coverlet.collector";
Version = "6.0.*";
SingleHit = false;
Expand All @@ -36,6 +38,16 @@ public CoverageConfiguration()
/// </summary>
public string Package { get; set; }

/// <summary>
/// Gets or sets a boolean indicating if a badge should be uploaded to gist. Default is disabled.
/// </summary>
public bool BadgeUploadToGist { get; set; }

/// <summary>
/// Gets or sets the gist id
/// </summary>
public string? BadgeGistId { get; set; }

/// <summary>
/// Gets or sets the version of the package that will be used for collecting.
/// </summary>
Expand Down
31 changes: 31 additions & 0 deletions src/dotnet-releaser/DevHosting/GitHubDevHosting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,37 @@ public async Task<List<ReleaseVersion>> GetAllReleaseTags(string user, string re

return result;
}

public async Task CreateOrUpdateGist(string gistId, string fileName, string content)
{
var gist = await _client.Gist.Get(gistId);
if (gist is null)
{
_log.Error($"The gist {gistId} for code coverage was not found");
return;
}

if (gist.Files.ContainsKey(fileName))
{
var existingContent = gist.Files[fileName].Content;
if (existingContent == content)
{
_log.Info($"No need to update the gist {gistId} as the content is the same.");
return;
}
}
else
{
_log.Warn($"Cannot update gist {gistId} as it does not contain the required file {fileName}");
return;
}

// Update the file
GistUpdate gistUpdate = new GistUpdate();
//gistUpdate.Files.Add();
gistUpdate.Files.Add(fileName, new GistFileUpdate() { NewFileName = fileName, Content = content });
await _client.Gist.Edit(gistId, gistUpdate);
}

private async Task<List<(RepositoryTag, NuGetVersion)>> GetAllReleaseTagsImpl(string user, string repo, string tagPrefix)
{
Expand Down
2 changes: 2 additions & 0 deletions src/dotnet-releaser/IDevHosting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public interface IDevHosting

Task<bool> Connect();

Task CreateOrUpdateGist(string gistId, string fileName, string content);

Task<List<ReleaseVersion>> GetAllReleaseTags(string user, string repo, string tagPrefix);

Task<ChangelogCollection?> GetChanges(string user, string repo, string tagPrefix, string version);
Expand Down
14 changes: 11 additions & 3 deletions src/dotnet-releaser/ReleaserApp.BuildAndTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public partial class ReleaserApp
{
private List<AssemblyCoverage> _assemblyCoverages;

private async Task<bool> BuildAndTest(BuildInformation buildInfo)
private async Task<bool> BuildAndTest(IDevHosting? devHosting, BuildInformation buildInfo)
{
var coverage = _config.Coverage.Enable && _config.Test.Enable;

Expand Down Expand Up @@ -81,7 +81,13 @@ private async Task<bool> BuildAndTest(BuildInformation buildInfo)
{
if (_config.Coverage.Enable)
{
LoadAndDisplayCoverageResults();
var coverageResult = LoadAndDisplayCoverageResults();

// Publish badge if requested
if (devHosting is not null)
{
await PublishCoverageToGist(devHosting, buildInfo, coverageResult);
}
}

_logger.LogEndGroup();
Expand Down Expand Up @@ -114,7 +120,7 @@ private static string FormatRate(HitCoverage rate)
return Math.Round(rate.Rate * 100, 2, MidpointRounding.AwayFromZero).ToString("##.00") + "%";
}

private void LoadAndDisplayCoverageResults()
private HitCoverage LoadAndDisplayCoverageResults()
{
LoadCoverageResults();

Expand Down Expand Up @@ -153,6 +159,8 @@ private void LoadAndDisplayCoverageResults()
table.Columns[4].Footer = new Text(FormatRate(totalMethodRate));

_logger.InfoMarkup("Coverage Results:", table);

return totalMethodRate;
}

private async Task<bool> Build(string projectFile, bool isTestProject)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,35 @@
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using DotNetReleaser.Coverage;
using DotNetReleaser.Coverage.Coveralls;
using DotNetReleaser.Helpers;

namespace DotNetReleaser;

public partial class ReleaserApp
{
private async Task PublishCoverageToGist(IDevHosting devHosting, BuildInformation buildInfo, HitCoverage coverage)
{
if (!_config.Coverage.BadgeUploadToGist) return;

var gistId = _config.Coverage.BadgeGistId;
if (string.IsNullOrWhiteSpace(gistId))
{
Warn("The 'coverage.badge_gist_id' is not set in the configuration file. The coverage badge will not be uploaded to a gist.");
return;
}

var rate = (int)(Math.Round((double)coverage.Rate) * 100);
var svg = $"""
<svg xmlns="http://www.w3.org/2000/svg" width="99" height="20"><linearGradient id="a" x2="0" y2="100%"><stop offset="0" stop-color="#bbb" stop-opacity=".1"/><stop offset="1" stop-opacity=".1"/></linearGradient><rect rx="3" width="99" height="20" fill="#555"/><rect rx="3" x="63" width="36" height="20" fill="#97CA00"/><path fill="#97CA00" d="M63 0h4v20h-4z"/><rect rx="3" width="99" height="20" fill="url(#a)"/><g fill="#fff" text-anchor="middle" font-family="DejaVu Sans,Verdana,Geneva,sans-serif" font-size="11"><text x="32.5" y="15" fill="#010101" fill-opacity=".3">coverage</text><text x="32.5" y="14">coverage</text><text x="80" y="15" fill="#010101" fill-opacity=".3">{rate:##}%</text><text x="80" y="14">{rate:##}%</text></g></svg>
""";

var fileName = $"dotnet-releaser-coverage-badge-{_config.GitHub.User}-{_config.GitHub.Repo}.svg";
Info($"Updating coverage badge with {rate:##}% result to gist {gistId} and file {fileName}");
await devHosting.CreateOrUpdateGist(gistId, fileName, svg);
}

private async Task PublishCoveralls(IDevHosting devHosting, BuildInformation buildInfo)
{
if (!_config.Coveralls.Publish || _assemblyCoverages.Count == 0) return;
Expand Down
2 changes: 1 addition & 1 deletion src/dotnet-releaser/ReleaserApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ private async Task<bool> RunImpl(string configurationFile, BuildKind buildKind,
// ------------------------------------------------------------------
// Build Projects (debug/release)
// ------------------------------------------------------------------
if (!await BuildAndTest(buildInformation)) return false;
if (!await BuildAndTest(devHosting, buildInformation)) return false;

// ------------------------------------------------------------------
// Build NuGet package
Expand Down

0 comments on commit cb53623

Please sign in to comment.