Skip to content

Commit

Permalink
Added the option to conclude the check run with neutral status (#192)
Browse files Browse the repository at this point in the history
* Added the option to conclude the check run with neutral status

* Fixed issues found by linter

* Fixed issues found by linter, 2nd try
  • Loading branch information
inket authored May 20, 2021
1 parent b803c84 commit 222b3d9
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 5 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,8 @@ jobs:

- **`check_name`**: Template for the [name of the check run](https://docs.github.com/en/rest/reference/checks#create-a-check-run). Use this to ensure unique names when the action is used more than once in a workflow. The `${linter}` and `${dir}` variables can be used to insert the name and directory of the linter. Default: `"${linter}"`

- **`neutral_check_on_warning`:** Whether the check run should conclude with a neutral status instead of success when the linter finds only warnings. Default: `false`

### Linter support

Some options are not be available for specific linters:
Expand Down
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ inputs:
description: 'Template for the name of the check run. The "${linter}" and "${dir}" variables can be used to insert the name and directory of the linter.'
required: false
default: "${linter}"
neutral_check_on_warning:
description: Whether the check run should conclude with a neutral status instead of success when the linter finds only warnings
required: false
default: "false"

# CSS

Expand Down
17 changes: 15 additions & 2 deletions src/github/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ const { capitalizeFirstLetter } = require("../utils/string");
* @param {import('./context').GithubContext} context - Information about the GitHub repository and
* action trigger event
* @param {import('../utils/lint-result').LintResult} lintResult - Parsed lint result
* @param {boolean} neutralCheckOnWarning - Whether the check run should conclude as neutral if
* there are only warnings
* @param {string} summary - Summary for the GitHub check
*/
async function createCheck(linterName, sha, context, lintResult, summary) {
async function createCheck(linterName, sha, context, lintResult, neutralCheckOnWarning, summary) {
let annotations = [];
for (const level of ["warning", "error"]) {
annotations = [
Expand All @@ -36,10 +38,21 @@ async function createCheck(linterName, sha, context, lintResult, summary) {
annotations = annotations.slice(0, 50);
}

let conclusion;
if (lintResult.isSuccess) {
if (annotations.length > 0 && neutralCheckOnWarning) {
conclusion = "neutral";
} else {
conclusion = "success";
}
} else {
conclusion = "failure";
}

const body = {
name: linterName,
head_sha: sha,
conclusion: lintResult.isSuccess ? "success" : "failure",
conclusion,
output: {
title: capitalizeFirstLetter(summary),
summary: `${linterName} found ${summary}`,
Expand Down
3 changes: 2 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ async function runAction() {
const gitEmail = core.getInput("git_email", { required: true });
const commitMessage = core.getInput("commit_message", { required: true });
const checkName = core.getInput("check_name", { required: true });
const neutralCheckOnWarning = core.getInput("neutral_check_on_warning") === "true";
const isPullRequest =
context.eventName === "pull_request" || context.eventName === "pull_request_target";

Expand Down Expand Up @@ -123,7 +124,7 @@ async function runAction() {
core.startGroup("Create check runs with commit annotations");
await Promise.all(
checks.map(({ lintCheckName, lintResult, summary }) =>
createCheck(lintCheckName, headSha, context, lintResult, summary),
createCheck(lintCheckName, headSha, context, lintResult, neutralCheckOnWarning, summary),
),
);
core.endGroup();
Expand Down
4 changes: 2 additions & 2 deletions test/github/api.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ describe("createCheck()", () => {

test("mocked request should be successful", async () => {
await expect(
createCheck("check-name", "sha", context, LINT_RESULT, "summary"),
createCheck("check-name", "sha", context, LINT_RESULT, false, "summary"),
).resolves.toEqual(undefined);
});

test("mocked request should fail when no lint results are provided", async () => {
await expect(createCheck("check-name", "sha", context, null, "summary")).rejects.toEqual(
await expect(createCheck("check-name", "sha", context, null, false, "summary")).rejects.toEqual(
expect.any(Error),
);
});
Expand Down

0 comments on commit 222b3d9

Please sign in to comment.