diff --git a/README.md b/README.md index 328d0e0c..6ce44c44 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/action.yml b/action.yml index 4980e6b6..d59fb33e 100644 --- a/action.yml +++ b/action.yml @@ -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 diff --git a/src/github/api.js b/src/github/api.js index 4b6088ff..e8e57382 100644 --- a/src/github/api.js +++ b/src/github/api.js @@ -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 = [ @@ -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}`, diff --git a/src/index.js b/src/index.js index 36b2fb4d..0e4c6d8b 100644 --- a/src/index.js +++ b/src/index.js @@ -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"; @@ -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(); diff --git a/test/github/api.test.js b/test/github/api.test.js index 938833b6..94900bb9 100644 --- a/test/github/api.test.js +++ b/test/github/api.test.js @@ -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), ); });