From 83467da07bcf182bf7dead9d98090d358bb760d8 Mon Sep 17 00:00:00 2001 From: Cooper Date: Mon, 3 Apr 2023 16:46:31 -0400 Subject: [PATCH 1/6] feat(action): adds flag to optionally fail on errors --- src/action.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/action.js b/src/action.js index 8d34009..e40d6c7 100644 --- a/src/action.js +++ b/src/action.js @@ -16,6 +16,8 @@ const { GITHUB_EVENT_NAME, GITHUB_SHA } = process.env const configPath = resolve(process.env.GITHUB_WORKSPACE, getInput('configFile')) +const failOnErrors = resolve(process.env.GITHUB_WORKSPACE, getInput('failOnErrors', { required: false })) + const getCommitDepth = () => { const commitDepthString = getInput('commitDepth') if (!commitDepthString?.trim()) return null @@ -145,6 +147,12 @@ const showLintResults = async ([from, to]) => { if (hasOnlyWarnings(lintedCommits)) { handleOnlyWarnings(formattedResults) + } else if (formattedResults && (failOnErrors == false)) { + // https://github.com/actions/toolkit/tree/master/packages/core#exit-codes + // this would be a good place to implement the setNeutral() when it's eventually implimented. + // for now it can pass with a check mark. + console.log('Passing despite errors ✅') + console.log(`You have commit messages with errors\n\n${formattedResults}`) } else if (formattedResults) { setFailedAction(formattedResults) } else { @@ -156,6 +164,7 @@ const exitWithMessage = (message) => (error) => { setFailedAction(`${message}\n${error.message}\n${error.stack}`) } + const commitLinterAction = () => getRangeForEvent() .catch( From 2640e4c8bf1ef522aea0d9fa997c4f07ee95ba97 Mon Sep 17 00:00:00 2001 From: Cooper Date: Wed, 5 Apr 2023 11:23:02 -0400 Subject: [PATCH 2/6] fix(action): fix bug with value, add tests --- src/action.js | 13 ++++++------- src/action.test.js | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 7 deletions(-) diff --git a/src/action.js b/src/action.js index e40d6c7..1505de1 100644 --- a/src/action.js +++ b/src/action.js @@ -1,6 +1,6 @@ import { existsSync } from 'fs' import { resolve } from 'path' -import { getInput, setFailed } from '@actions/core' +import { getInput, setFailed, setOutput } from '@actions/core' import { context as eventContext, getOctokit } from '@actions/github' import lint from '@commitlint/lint' import { format } from '@commitlint/format' @@ -16,7 +16,7 @@ const { GITHUB_EVENT_NAME, GITHUB_SHA } = process.env const configPath = resolve(process.env.GITHUB_WORKSPACE, getInput('configFile')) -const failOnErrors = resolve(process.env.GITHUB_WORKSPACE, getInput('failOnErrors', { required: false })) +const failOnErrors = getInput('failOnErrors') const getCommitDepth = () => { const commitDepthString = getInput('commitDepth') @@ -147,12 +147,12 @@ const showLintResults = async ([from, to]) => { if (hasOnlyWarnings(lintedCommits)) { handleOnlyWarnings(formattedResults) - } else if (formattedResults && (failOnErrors == false)) { - // https://github.com/actions/toolkit/tree/master/packages/core#exit-codes + } else if (formattedResults && failOnErrors === 'false') { + // https://github.com/actions/toolkit/tree/master/packages/core#exit-codes // this would be a good place to implement the setNeutral() when it's eventually implimented. - // for now it can pass with a check mark. + // for now it can pass with a check mark. console.log('Passing despite errors ✅') - console.log(`You have commit messages with errors\n\n${formattedResults}`) + setOutput(`You have commit messages with errors\n\n${formattedResults}`) } else if (formattedResults) { setFailedAction(formattedResults) } else { @@ -164,7 +164,6 @@ const exitWithMessage = (message) => (error) => { setFailedAction(`${message}\n${error.message}\n${error.stack}`) } - const commitLinterAction = () => getRangeForEvent() .catch( diff --git a/src/action.test.js b/src/action.test.js index 7aef83b..9ac8a9b 100644 --- a/src/action.test.js +++ b/src/action.test.js @@ -110,10 +110,49 @@ describe('Commit Linter action', () => { await runAction() + console.log() td.verify(core.setFailed(contains('wrong message 1'))) td.verify(core.setFailed(contains('wrong message 2'))) }) + it('should pass for push range with wrong messages with failOnErrors set to false', async () => { + td.when(core.getInput('failOnErrors')).thenReturn('false') + cwd = await git.bootstrap('fixtures/conventional') + await gitEmptyCommit(cwd, 'message from before push') + await gitEmptyCommit(cwd, 'wrong message 1') + await gitEmptyCommit(cwd, 'wrong message 2') + const [before, , to] = await getCommitHashes(cwd) + await createPushEventPayload(cwd, { before, to }) + updatePushEnvVars(cwd, to) + td.replace(process, 'cwd', () => cwd) + td.replace(console, 'log') + + await runAction() + + td.verify(core.setFailed(), { times: 0, ignoreExtraArgs: true }) + td.verify(console.log(contains('Passing despite errors ✅'))) + td.verify(core.setOutput(contains('wrong message 1'))) + td.verify(core.setOutput(contains('wrong message 2'))) + }) + + it('should pass for push range with correct messages with failOnErrors set to false', async () => { + td.when(core.getInput('failOnErrors')).thenReturn('false') + cwd = await git.bootstrap('fixtures/conventional') + await gitEmptyCommit(cwd, 'message from before push') + await gitEmptyCommit(cwd, 'chore: correct message 1') + await gitEmptyCommit(cwd, 'chore: correct message 2') + const [before, , to] = await getCommitHashes(cwd) + await createPushEventPayload(cwd, { before, to }) + updatePushEnvVars(cwd, to) + td.replace(process, 'cwd', () => cwd) + td.replace(console, 'log') + + await runAction() + + td.verify(core.setFailed(), { times: 0, ignoreExtraArgs: true }) + td.verify(console.log('Lint free! 🎉')) + }) + it('should pass for push range with correct messages', async () => { cwd = await git.bootstrap('fixtures/conventional') await gitEmptyCommit(cwd, 'message from before push') From 7991e57b35c9a86cdda880119d9c4ca3b975b4fd Mon Sep 17 00:00:00 2001 From: Cooper Date: Wed, 5 Apr 2023 12:11:01 -0400 Subject: [PATCH 3/6] fix(action): refactor a value, add to docs --- README.md | 12 ++++++++++++ src/action.js | 4 +--- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 1aa3e9e..1715e64 100644 --- a/README.md +++ b/README.md @@ -52,6 +52,18 @@ Whether you want to fail on warnings or not. Default: `false` +### `failOnWarnings` + +Whether you want to fail on warnings or not. + +Default: `false` + +### `failOnErrors` + +Whether you want to fail on errors or not. Still outputs the results, just forces the action to pass even if errors are detected. + +Default: `true` + ### `helpURL` Link to a page explaining your commit message convention. diff --git a/src/action.js b/src/action.js index 1505de1..d1cf47f 100644 --- a/src/action.js +++ b/src/action.js @@ -16,8 +16,6 @@ const { GITHUB_EVENT_NAME, GITHUB_SHA } = process.env const configPath = resolve(process.env.GITHUB_WORKSPACE, getInput('configFile')) -const failOnErrors = getInput('failOnErrors') - const getCommitDepth = () => { const commitDepthString = getInput('commitDepth') if (!commitDepthString?.trim()) return null @@ -147,7 +145,7 @@ const showLintResults = async ([from, to]) => { if (hasOnlyWarnings(lintedCommits)) { handleOnlyWarnings(formattedResults) - } else if (formattedResults && failOnErrors === 'false') { + } else if (formattedResults && getInput('failOnErrors') === 'false') { // https://github.com/actions/toolkit/tree/master/packages/core#exit-codes // this would be a good place to implement the setNeutral() when it's eventually implimented. // for now it can pass with a check mark. From 6a9739b9a2f47cb6e51a850b6e82fa9c42a13f61 Mon Sep 17 00:00:00 2001 From: Cooper Date: Wed, 5 Apr 2023 12:13:54 -0400 Subject: [PATCH 4/6] fix(docs): remove dupe --- README.md | 6 ------ 1 file changed, 6 deletions(-) diff --git a/README.md b/README.md index 1715e64..613dc2f 100644 --- a/README.md +++ b/README.md @@ -52,12 +52,6 @@ Whether you want to fail on warnings or not. Default: `false` -### `failOnWarnings` - -Whether you want to fail on warnings or not. - -Default: `false` - ### `failOnErrors` Whether you want to fail on errors or not. Still outputs the results, just forces the action to pass even if errors are detected. From 8589bb7e04b6baba23e93083597321c592b18593 Mon Sep 17 00:00:00 2001 From: Cooper Date: Fri, 7 Apr 2023 11:30:10 -0400 Subject: [PATCH 5/6] fix(action): add field to action --- action.yml | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/action.yml b/action.yml index c93c712..1a0c217 100644 --- a/action.yml +++ b/action.yml @@ -3,7 +3,8 @@ description: Lints Pull Request commit messages with commitlint author: Wagner Santos inputs: configFile: - description: Commitlint config file. If the file doesn't exist, config-conventional settings will be + description: + Commitlint config file. If the file doesn't exist, config-conventional settings will be loaded as a fallback. default: ./commitlint.config.js required: false @@ -12,19 +13,23 @@ inputs: When set to true, we follow only the first parent commit when seeing a merge commit. More info in git-log docs https://git-scm.com/docs/git-log#Documentation/git-log.txt---first-parent - default: "true" + default: 'true' required: false failOnWarnings: description: Whether you want to fail on warnings or not - default: "false" + default: 'false' required: false + failOnErrors: + description: Whether you want to fail on errors or not + default: 'true' + required: true helpURL: description: Link to a page explaining your commit message convention default: https://github.com/conventional-changelog/commitlint/#what-is-commitlint required: false commitDepth: description: When set to a valid Integer value - X, considers only the latest X number of commits. - default: "" + default: '' required: false token: description: > From 097b726807006a63d6f34782aad667815f4217dc Mon Sep 17 00:00:00 2001 From: Cooper Date: Fri, 7 Apr 2023 11:32:04 -0400 Subject: [PATCH 6/6] fix(tests): remove log --- src/action.test.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/action.test.js b/src/action.test.js index 9ac8a9b..3c1ef94 100644 --- a/src/action.test.js +++ b/src/action.test.js @@ -109,8 +109,6 @@ describe('Commit Linter action', () => { td.replace(process, 'cwd', () => cwd) await runAction() - - console.log() td.verify(core.setFailed(contains('wrong message 1'))) td.verify(core.setFailed(contains('wrong message 2'))) })