Skip to content

Commit 3b6f002

Browse files
authored
Merge pull request #711 from cmitzel-ncino/failonerrors-cmitzel
feat(action): adds flag to optionally fail on errors
2 parents 09faa1a + 097b726 commit 3b6f002

File tree

4 files changed

+60
-6
lines changed

4 files changed

+60
-6
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ Whether you want to fail on warnings or not.
5252

5353
Default: `false`
5454

55+
### `failOnErrors`
56+
57+
Whether you want to fail on errors or not. Still outputs the results, just forces the action to pass even if errors are detected.
58+
59+
Default: `true`
60+
5561
### `helpURL`
5662

5763
Link to a page explaining your commit message convention.

action.yml

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ description: Lints Pull Request commit messages with commitlint
33
author: Wagner Santos
44
inputs:
55
configFile:
6-
description: Commitlint config file. If the file doesn't exist, config-conventional settings will be
6+
description:
7+
Commitlint config file. If the file doesn't exist, config-conventional settings will be
78
loaded as a fallback.
89
default: ./commitlint.config.js
910
required: false
@@ -12,19 +13,23 @@ inputs:
1213
When set to true, we follow only the first parent commit when seeing a merge commit.
1314
More info in git-log docs
1415
https://git-scm.com/docs/git-log#Documentation/git-log.txt---first-parent
15-
default: "true"
16+
default: 'true'
1617
required: false
1718
failOnWarnings:
1819
description: Whether you want to fail on warnings or not
19-
default: "false"
20+
default: 'false'
2021
required: false
22+
failOnErrors:
23+
description: Whether you want to fail on errors or not
24+
default: 'true'
25+
required: true
2126
helpURL:
2227
description: Link to a page explaining your commit message convention
2328
default: https://github.com/conventional-changelog/commitlint/#what-is-commitlint
2429
required: false
2530
commitDepth:
2631
description: When set to a valid Integer value - X, considers only the latest X number of commits.
27-
default: ""
32+
default: ''
2833
required: false
2934
token:
3035
description: >

src/action.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { existsSync } from 'fs'
22
import { resolve } from 'path'
3-
import { getInput, setFailed } from '@actions/core'
3+
import { getInput, setFailed, setOutput } from '@actions/core'
44
import { context as eventContext, getOctokit } from '@actions/github'
55
import lint from '@commitlint/lint'
66
import { format } from '@commitlint/format'
@@ -145,6 +145,12 @@ const showLintResults = async ([from, to]) => {
145145

146146
if (hasOnlyWarnings(lintedCommits)) {
147147
handleOnlyWarnings(formattedResults)
148+
} else if (formattedResults && getInput('failOnErrors') === 'false') {
149+
// https://github.com/actions/toolkit/tree/master/packages/core#exit-codes
150+
// this would be a good place to implement the setNeutral() when it's eventually implimented.
151+
// for now it can pass with a check mark.
152+
console.log('Passing despite errors ✅')
153+
setOutput(`You have commit messages with errors\n\n${formattedResults}`)
148154
} else if (formattedResults) {
149155
setFailedAction(formattedResults)
150156
} else {

src/action.test.js

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,48 @@ describe('Commit Linter action', () => {
109109
td.replace(process, 'cwd', () => cwd)
110110

111111
await runAction()
112-
113112
td.verify(core.setFailed(contains('wrong message 1')))
114113
td.verify(core.setFailed(contains('wrong message 2')))
115114
})
116115

116+
it('should pass for push range with wrong messages with failOnErrors set to false', async () => {
117+
td.when(core.getInput('failOnErrors')).thenReturn('false')
118+
cwd = await git.bootstrap('fixtures/conventional')
119+
await gitEmptyCommit(cwd, 'message from before push')
120+
await gitEmptyCommit(cwd, 'wrong message 1')
121+
await gitEmptyCommit(cwd, 'wrong message 2')
122+
const [before, , to] = await getCommitHashes(cwd)
123+
await createPushEventPayload(cwd, { before, to })
124+
updatePushEnvVars(cwd, to)
125+
td.replace(process, 'cwd', () => cwd)
126+
td.replace(console, 'log')
127+
128+
await runAction()
129+
130+
td.verify(core.setFailed(), { times: 0, ignoreExtraArgs: true })
131+
td.verify(console.log(contains('Passing despite errors ✅')))
132+
td.verify(core.setOutput(contains('wrong message 1')))
133+
td.verify(core.setOutput(contains('wrong message 2')))
134+
})
135+
136+
it('should pass for push range with correct messages with failOnErrors set to false', async () => {
137+
td.when(core.getInput('failOnErrors')).thenReturn('false')
138+
cwd = await git.bootstrap('fixtures/conventional')
139+
await gitEmptyCommit(cwd, 'message from before push')
140+
await gitEmptyCommit(cwd, 'chore: correct message 1')
141+
await gitEmptyCommit(cwd, 'chore: correct message 2')
142+
const [before, , to] = await getCommitHashes(cwd)
143+
await createPushEventPayload(cwd, { before, to })
144+
updatePushEnvVars(cwd, to)
145+
td.replace(process, 'cwd', () => cwd)
146+
td.replace(console, 'log')
147+
148+
await runAction()
149+
150+
td.verify(core.setFailed(), { times: 0, ignoreExtraArgs: true })
151+
td.verify(console.log('Lint free! 🎉'))
152+
})
153+
117154
it('should pass for push range with correct messages', async () => {
118155
cwd = await git.bootstrap('fixtures/conventional')
119156
await gitEmptyCommit(cwd, 'message from before push')

0 commit comments

Comments
 (0)