Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ jobs:
needs: build
runs-on: ubuntu-latest
environment: build.cake
env:
COVERALLS_REPO_TOKEN: ${{ secrets.COVERALLS_REPO_TOKEN }}
GITHUB_SHA: ${{ github.sha }}
GITHUB_REF: ${{ github.ref }}
GITHUB_REF_NAME: ${{ github.ref_name }}
steps:
- name: .NET Version
run: dotnet --version
Expand Down Expand Up @@ -77,5 +82,26 @@ jobs:
uses: cake-build/cake-action@v3
with:
target: PullRequest
env:
COVERALLS_REPO_TOKEN: ${{ secrets.COVERALLS_REPO_TOKEN }}
- name: Prepare Coveralls
run: |
echo "GITHUB_REF is ${{ env.GITHUB_REF }}"
echo "GITHUB_REF_NAME is ${{ env.GITHUB_REF_NAME }}"
echo "GITHUB_SHA is ${{ env.GITHUB_SHA }}"
coverage_1st_folder=$(ls -d /home/runner/work/Ocelot/Ocelot/artifacts/UnitTests/*/ | head -1)
echo "Detected first folder : $coverage_1st_folder"
coverage_file="${coverage_1st_folder%/}/coverage.cobertura.xml"
echo "Detecting file $coverage_file ..."
if [ -f "$coverage_file" ]; then
echo "Coverage file exists."
echo "COVERALLS_coverage_file_exists=true" >> $GITHUB_ENV
echo "COVERALLS_coverage_file=$coverage_file" >> $GITHUB_ENV
else
echo "Coverage file DOES NOT exist!"
echo "COVERALLS_coverage_file_exists=false" >> $GITHUB_ENV
fi
- name: Coveralls
if: env.COVERALLS_coverage_file_exists == 'true'
uses: coverallsapp/github-action@v2
with:
file: ${{ env.COVERALLS_coverage_file }}
compare-ref: develop
91 changes: 47 additions & 44 deletions build.cake
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#tool dotnet:?package=GitVersion.Tool&version=6.2.0 // released on 1.04.2025 with TFMs net8.0 net9.0
#tool dotnet:?package=coveralls.net&version=4.0.1 // Outdated! released on 07.08.22 with TFM net6.0
// #tool dotnet:?package=coveralls.net&version=4.0.1 // Outdated! released on 07.08.22 with TFM net6.0
#tool nuget:?package=ReportGenerator&version=5.4.5 // released on 23.03.2025 with TFM netstandard2.0
#addin nuget:?package=Newtonsoft.Json&version=13.0.3 // Switch to a MS lib! Outdated! released on 08.03.23 with TFMs net6.0 netstandard2.0
#addin nuget:?package=System.Text.Encodings.Web&version=9.0.3 // released on 11.03.2025 with TFMs net8.0 net9.0 netstandard2.0
#addin nuget:?package=Cake.Coveralls&version=4.0.0 // Outdated! released on 9.07.2024 with TFMs net6.0 net7.0 net8.0
// #addin nuget:?package=Cake.Coveralls&version=4.0.0 // Outdated! released on 9.07.2024 with TFMs net6.0 net7.0 net8.0

#r "Spectre.Console"
using Spectre.Console
Expand Down Expand Up @@ -32,8 +32,6 @@ var artifactsDir = Directory("artifacts"); // build artifacts
// unit testing
var artifactsForUnitTestsDir = artifactsDir + Directory("UnitTests");
var unitTestAssemblies = @"./test/Ocelot.UnitTests/Ocelot.UnitTests.csproj";
var minCodeCoverage = 0.80d;
var coverallsRepo = "https://coveralls.io/github/ThreeMammals/Ocelot";

// acceptance testing
var artifactsForAcceptanceTestsDir = artifactsDir + Directory("AcceptanceTests");
Expand Down Expand Up @@ -507,56 +505,61 @@ Task("UnitTests")
EnsureDirectoryExists(artifactsForUnitTestsDir);
DotNetTest(unitTestAssemblies, settings); // sequential testing
}

Information("ArtifactsForUnitTestsDir = " + artifactsForUnitTestsDir);
var coverageSummaryFile = GetSubDirectories(artifactsForUnitTestsDir)
.First()
.CombineWithFilePath(File("coverage.cobertura.xml"));
Information("CoverageSummaryFile = " + coverageSummaryFile);
GenerateReport(coverageSummaryFile);

Information("##############################");
Information("# Coveralls & Code coverage");
Information("# Code coverage");
Information("#=============================");
if (IsRunningInCICD() && IsMainOrDevelop())
{
var repoToken = EnvironmentVariable("COVERALLS_REPO_TOKEN");
if (string.IsNullOrEmpty(repoToken))
{
var err = "# Coveralls repo token was not found! Set environment variable: COVERALLS_REPO_TOKEN !";
Warning(err);
throw new Exception(err);
}
Information($"# Uploading test coverage to {coverallsRepo}");
var gitHEAD = string.Join(string.Empty, GitHelper("rev-parse HEAD")); // git rev-parse HEAD
Information($"# HEAD commit is {gitHEAD}");
// git log -1 --pretty=format:'%an <%ae>'
var gitAuthor = string.Join(string.Empty, GitHelper("log -1 --pretty=format:%an"));
var gitEmail = string.Join(string.Empty, GitHelper("log -1 --pretty=format:%ae"));
var gitBranch = GetGitBranch();
var gitMessage = string.Join(string.Empty, GitHelper("log -1 --pretty=format:%s"));
CoverallsNet(coverageSummaryFile, CoverallsNetReportType.OpenCover, new CoverallsNetSettings()
{
RepoToken = repoToken,
CommitAuthor = gitAuthor,
CommitBranch = gitBranch,
CommitEmail = gitEmail,
CommitId = gitHEAD,
CommitMessage = gitMessage,
});
}
else
{
Information("# We are not running on the build server so we won't publish the coverage report to coveralls.io");
}
const string CoverallsRepo = "https://coveralls.io/github/ThreeMammals/Ocelot";
// if (IsRunningInCICD() && IsMainOrDevelop())
// {
// var repoToken = EnvironmentVariable("COVERALLS_REPO_TOKEN");
// if (string.IsNullOrEmpty(repoToken))
// {
// var err = "# Coveralls repo token was not found! Set environment variable: COVERALLS_REPO_TOKEN !";
// Warning(err);
// throw new Exception(err);
// }
// Information($"# Uploading test coverage to {CoverallsRepo}");
// var gitHEAD = string.Join(string.Empty, GitHelper("rev-parse HEAD")); // git rev-parse HEAD
// Information($"# HEAD commit is {gitHEAD}");
// // git log -1 --pretty=format:'%an <%ae>'
// var gitAuthor = string.Join(string.Empty, GitHelper("log -1 --pretty=format:%an"));
// var gitEmail = string.Join(string.Empty, GitHelper("log -1 --pretty=format:%ae"));
// var gitBranch = GetGitBranch();
// var gitMessage = string.Join(string.Empty, GitHelper("log -1 --pretty=format:%s"));
// CoverallsNet(coverageSummaryFile, CoverallsNetReportType.OpenCover, new CoverallsNetSettings()
// {
// RepoToken = repoToken,
// CommitAuthor = gitAuthor,
// CommitBranch = gitBranch,
// CommitEmail = gitEmail,
// CommitId = gitHEAD,
// CommitMessage = gitMessage,
// });
// }
// else
// {
Information($"# CoverallsNet uploading is disabled in favor of Coveralls step of GH Action workflows. So, we won't publish the coverage report to coveralls.io");
// }

var sequenceCoverage = XmlPeek(coverageSummaryFile, "//coverage/@line-rate");
var branchCoverage = XmlPeek(coverageSummaryFile, "//coverage/@line-rate");
Information("# Sequence Coverage: " + sequenceCoverage);
if (double.Parse(sequenceCoverage) < minCodeCoverage)
// Apply code coverage threshold
const double MinCodeCoverage = 0.80D; // consider definition of an env var in GitHub Environment vars
var lineCoverage = XmlPeek(coverageSummaryFile, "//coverage/@line-rate");
var branchCoverage = XmlPeek(coverageSummaryFile, "//coverage/@branch-rate");
Information("# Line Coverage: " + lineCoverage);
Information("# Branch Coverage: " + branchCoverage);
if (double.Parse(lineCoverage) < MinCodeCoverage)
{
var whereToCheck = !IsRunningInCICD() ? coverallsRepo : artifactsForUnitTestsDir;
Warning($"# Code coverage fell below the threshold of {minCodeCoverage}%. You can find the code coverage report at {whereToCheck}");
var whereToCheck = !IsRunningInCICD() ? CoverallsRepo : artifactsForUnitTestsDir;
var msg = $"# Code coverage fell below the threshold of {MinCodeCoverage}%. You can find the code coverage report at {whereToCheck}";
Warning(msg);
throw new Exception(msg); // fail the building job step in GitHub Actions
};
Information("##############################");
});
Expand Down
Loading