Skip to content

Commit

Permalink
Add support for failing the action below the threshold (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
kjagiello committed Jun 18, 2021
1 parent 7b96dd4 commit 61b3b14
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 0 deletions.
1 change: 1 addition & 0 deletions .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@ jobs:
path: coverage/cobertura-coverage.xml
repo_token: ${{ secrets.GITHUB_TOKEN }}
minimum_coverage: 75
fail_below_threshold: true
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ If files with 100% coverage should be ignored. Defaults to `true`.

The minimum allowed coverage percentage as an integer.

### `fail_below_threshold`

Fail the action when the minimum coverage was not met.

### `show_line`

Show line rate as specific column.
Expand Down
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ inputs:
minimum_coverage:
description: 'Minimum allowed coverage percentage as an integer.'
required: true
fail_below_threshold:
description: 'Fail the action when the minimum coverage was not met.'
required: false
default: false
show_line:
description: 'Show line rate as specific column.'
required: true
Expand Down
9 changes: 9 additions & 0 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15949,6 +15949,9 @@ async function action(payload) {
const minimumCoverage = parseInt(
core.getInput("minimum_coverage", { required: true })
);
const failBelowThreshold = JSON.parse(
core.getInput("fail_below_threshold", { required: false }) || "false"
);
const showClassNames = JSON.parse(
core.getInput("show_class_names", { required: true })
);
Expand Down Expand Up @@ -15982,6 +15985,12 @@ async function action(payload) {
reportName,
});
await addComment(pullRequestNumber, comment, reportName);

if (failBelowThreshold) {
if (reports.some((report) => Math.floor(report.total) < minimumCoverage)) {
core.setFailed("Minimum coverage requirement was not satisfied");
}
}
}

function markdownReport(reports, commit, options) {
Expand Down
9 changes: 9 additions & 0 deletions src/action.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ async function action(payload) {
const minimumCoverage = parseInt(
core.getInput("minimum_coverage", { required: true })
);
const failBelowThreshold = JSON.parse(
core.getInput("fail_below_threshold", { required: false }) || "false"
);
const showClassNames = JSON.parse(
core.getInput("show_class_names", { required: true })
);
Expand Down Expand Up @@ -59,6 +62,12 @@ async function action(payload) {
reportName,
});
await addComment(pullRequestNumber, comment, reportName);

if (failBelowThreshold) {
if (reports.some((report) => Math.floor(report.total) < minimumCoverage)) {
core.setFailed("Minimum coverage requirement was not satisfied");
}
}
}

function markdownReport(reports, commit, options) {
Expand Down
67 changes: 67 additions & 0 deletions src/action.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ const dummyReport = {
beforeEach(() => {
process.env["INPUT_REPO_TOKEN"] = "hunter2";
process.env["GITHUB_REPOSITORY"] = `${owner}/${repo}`;
process.exitCode = 0;
process.stdout.write = jest.fn();
});

test("action", async () => {
Expand Down Expand Up @@ -202,6 +204,71 @@ test("action with crop missing lines", async () => {
await action();
});

test("action failing on coverage below threshold", async () => {
const { action } = require("./action");
const prNumber = 123;
process.env["INPUT_PATH"] = "./src/fixtures/test-branch.xml";
process.env["INPUT_SKIP_COVERED"] = "true";
process.env["INPUT_SHOW_BRANCH"] = "false";
process.env["INPUT_SHOW_LINE"] = "false";
process.env["INPUT_MINIMUM_COVERAGE"] = "100";
process.env["INPUT_FAIL_BELOW_THRESHOLD"] = "true";
process.env["INPUT_SHOW_CLASS_NAMES"] = "false";
process.env["INPUT_SHOW_MISSING"] = "false";
process.env["INPUT_ONLY_CHANGED_FILES"] = "false";
process.env["INPUT_PULL_REQUEST_NUMBER"] = prNumber;
nock("https://api.github.com")
.post(`/repos/${owner}/${repo}/issues/${prNumber}/comments`)
.reply(200)
.get(`/repos/${owner}/${repo}/issues/${prNumber}/comments`)
.reply(200, [{ body: "some body", id: 123 }])
.get(`/repos/${owner}/${repo}/pulls/${prNumber}`)
.reply(200, {
head: {
sha: "deadbeef",
},
});
await action({
push: { ref: "master" },
});
expect(process.exitCode).toBe(1);
expect(process.stdout.write).toHaveBeenCalledTimes(1);
expect(process.stdout.write).toHaveBeenCalledWith(
"::error::Minimum coverage requirement was not satisfied\n"
);
});

test("action not failing on coverage above threshold", async () => {
const { action } = require("./action");
const prNumber = 123;
process.env["INPUT_PATH"] = "./src/fixtures/test-branch.xml";
process.env["INPUT_SKIP_COVERED"] = "true";
process.env["INPUT_SHOW_BRANCH"] = "false";
process.env["INPUT_SHOW_LINE"] = "false";
process.env["INPUT_MINIMUM_COVERAGE"] = "82";
process.env["INPUT_FAIL_BELOW_THRESHOLD"] = "true";
process.env["INPUT_SHOW_CLASS_NAMES"] = "false";
process.env["INPUT_SHOW_MISSING"] = "false";
process.env["INPUT_ONLY_CHANGED_FILES"] = "false";
process.env["INPUT_PULL_REQUEST_NUMBER"] = prNumber;
nock("https://api.github.com")
.post(`/repos/${owner}/${repo}/issues/${prNumber}/comments`)
.reply(200)
.get(`/repos/${owner}/${repo}/issues/${prNumber}/comments`)
.reply(200, [{ body: "some body", id: 123 }])
.get(`/repos/${owner}/${repo}/pulls/${prNumber}`)
.reply(200, {
head: {
sha: "deadbeef",
},
});
await action({
push: { ref: "master" },
});
expect(process.exitCode).toBe(0);
expect(process.stdout.write).toHaveBeenCalledTimes(0);
});

test("markdownReport", () => {
const { markdownReport } = require("./action");
const commit = "deadbeef";
Expand Down

0 comments on commit 61b3b14

Please sign in to comment.