Skip to content

Commit 6e0fcb1

Browse files
committed
fix: don't fail on warnings
This follows the commitlint CLI behavior, which exits with success when there are only warnings. This behavior can be changed by passing 'true' to the parameter `failOnWarnings`.
1 parent 85fcead commit 6e0fcb1

File tree

4 files changed

+142
-27
lines changed

4 files changed

+142
-27
lines changed

README.md

+10-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ Alternatively, you can run on other event types such as `on: [push]`. In that ca
3030

3131
### `configFile`
3232

33-
The path to your commitlint config file. Default `commitlint.config.js`.
33+
The path to your commitlint config file.
34+
35+
Default: `commitlint.config.js`
3436

3537
### `firstParent`
3638

@@ -39,7 +41,13 @@ When set to true, we follow only the first parent commit when seeing a merge com
3941
This helps to ignore errors in commits that were already present in your default branch (e.g. `master`) before adding conventional commit checks.
4042
More info in [git-log docs](https://git-scm.com/docs/git-log#Documentation/git-log.txt---first-parent).
4143

42-
Default `true`
44+
Default: `true`
45+
46+
### `failOnWarnings`
47+
48+
Whether you want to fail on warnings or not.
49+
50+
Default: `false`
4351

4452
## About `extends` in your config file
4553

action.js

+39-12
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,41 @@ function getOptsFromConfig(config) {
101101
}
102102
}
103103

104+
const formatErrors = results =>
105+
format(
106+
{ results },
107+
{
108+
color: true,
109+
helpUrl:
110+
'https://github.com/conventional-changelog/commitlint/#what-is-commitlint',
111+
},
112+
)
113+
114+
const hasOnlyWarnings = results => {
115+
const resultsWithOnlyWarnings = results.filter(
116+
result => result.valid && result.warnings.length,
117+
)
118+
119+
return (
120+
resultsWithOnlyWarnings.length &&
121+
resultsWithOnlyWarnings.length === results.length
122+
)
123+
}
124+
125+
const setFailed = formattedResults => {
126+
core.setFailed(`You have commit messages with errors\n\n${formattedResults}`)
127+
}
128+
129+
const handleOnlyWarnings = formattedResults => {
130+
if (core.getInput('failOnWarnings') === 'true') {
131+
setFailed(formattedResults)
132+
} else {
133+
console.log(`You have commit messages with warnings\n\n${formattedResults}`)
134+
}
135+
}
136+
104137
const showLintResults = async ([from, to]) => {
138+
const failOnWarnings = core.getInput('failOnWarnings')
105139
const commits = await getHistoryCommits(from, to)
106140
const config = existsSync(configPath)
107141
? await load({}, { file: configPath })
@@ -110,19 +144,12 @@ const showLintResults = async ([from, to]) => {
110144
const results = await Promise.all(
111145
commits.map(commit => lint(commit, config.rules, opts)),
112146
)
113-
const formattedResults = format(
114-
{ results },
115-
{
116-
color: true,
117-
helpUrl:
118-
'https://github.com/conventional-changelog/commitlint/#what-is-commitlint',
119-
},
120-
)
147+
const formattedResults = formatErrors(results)
121148

122-
if (formattedResults) {
123-
core.setFailed(
124-
`You have commit messages with errors\n\n${formattedResults}`,
125-
)
149+
if (hasOnlyWarnings(results)) {
150+
handleOnlyWarnings(formattedResults)
151+
} else if (formattedResults) {
152+
setFailed(formattedResults)
126153
} else {
127154
console.log('Lint free! 🎉')
128155
}

action.test.js

+89-13
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,15 @@ const runAction = () => {
3737

3838
describe('Commit Linter action', () => {
3939
let core
40+
let cwd
4041

4142
beforeEach(() => {
4243
core = require('@actions/core')
4344
td.replace(core, 'getInput')
4445
td.replace(core, 'setFailed')
4546
td.when(core.getInput('configFile')).thenReturn('./commitlint.config.js')
4647
td.when(core.getInput('firstParent')).thenReturn('true')
48+
td.when(core.getInput('failOnWarnings')).thenReturn('false')
4749
})
4850

4951
afterEach(() => {
@@ -53,7 +55,7 @@ describe('Commit Linter action', () => {
5355
})
5456

5557
it('should fail for single push with incorrect message', async () => {
56-
const cwd = await git.bootstrap('fixtures/conventional')
58+
cwd = await git.bootstrap('fixtures/conventional')
5759
await gitEmptyCommit(cwd, 'wrong message')
5860
const [to] = await getCommitHashes(cwd)
5961
await createPushEventPayload(cwd, { to })
@@ -66,7 +68,7 @@ describe('Commit Linter action', () => {
6668
})
6769

6870
it('should pass for single push with correct message', async () => {
69-
const cwd = await git.bootstrap('fixtures/conventional')
71+
cwd = await git.bootstrap('fixtures/conventional')
7072
await gitEmptyCommit(cwd, 'chore: correct message')
7173
const [to] = await getCommitHashes(cwd)
7274
await createPushEventPayload(cwd, { to })
@@ -81,7 +83,7 @@ describe('Commit Linter action', () => {
8183
})
8284

8385
it('should fail for push range with wrong messages', async () => {
84-
const cwd = await git.bootstrap('fixtures/conventional')
86+
cwd = await git.bootstrap('fixtures/conventional')
8587
await gitEmptyCommit(cwd, 'message from before push')
8688
await gitEmptyCommit(cwd, 'wrong message 1')
8789
await gitEmptyCommit(cwd, 'wrong message 2')
@@ -97,7 +99,7 @@ describe('Commit Linter action', () => {
9799
})
98100

99101
it('should pass for push range with correct messages', async () => {
100-
const cwd = await git.bootstrap('fixtures/conventional')
102+
cwd = await git.bootstrap('fixtures/conventional')
101103
await gitEmptyCommit(cwd, 'message from before push')
102104
await gitEmptyCommit(cwd, 'chore: correct message 1')
103105
await gitEmptyCommit(cwd, 'chore: correct message 2')
@@ -114,7 +116,7 @@ describe('Commit Linter action', () => {
114116
})
115117

116118
it('should lint only last commit for forced push', async () => {
117-
const cwd = await git.bootstrap('fixtures/conventional')
119+
cwd = await git.bootstrap('fixtures/conventional')
118120
await gitEmptyCommit(cwd, 'message from before push')
119121
await gitEmptyCommit(cwd, 'wrong message 1')
120122
await gitEmptyCommit(cwd, 'wrong message 2')
@@ -137,7 +139,7 @@ describe('Commit Linter action', () => {
137139

138140
it('should lint only last commit when "before" field is an empty sha', async () => {
139141
const gitEmptySha = '0000000000000000000000000000000000000000'
140-
const cwd = await git.bootstrap('fixtures/conventional')
142+
cwd = await git.bootstrap('fixtures/conventional')
141143
await gitEmptyCommit(cwd, 'message from before push')
142144
await gitEmptyCommit(cwd, 'wrong message 1')
143145
await gitEmptyCommit(cwd, 'chore(WRONG): message 2')
@@ -153,7 +155,7 @@ describe('Commit Linter action', () => {
153155
})
154156

155157
it('should fail for commit with scope that is not a lerna package', async () => {
156-
const cwd = await git.bootstrap('fixtures/lerna-scopes')
158+
cwd = await git.bootstrap('fixtures/lerna-scopes')
157159
td.when(core.getInput('configFile')).thenReturn('./commitlint.config.yml')
158160
await gitEmptyCommit(cwd, 'chore(wrong): not including package scope')
159161
const [to] = await getCommitHashes(cwd)
@@ -169,7 +171,7 @@ describe('Commit Linter action', () => {
169171
})
170172

171173
it('should pass for scope that is a lerna package', async () => {
172-
const cwd = await git.bootstrap('fixtures/lerna-scopes')
174+
cwd = await git.bootstrap('fixtures/lerna-scopes')
173175
td.when(core.getInput('configFile')).thenReturn('./commitlint.config.yml')
174176
await gitEmptyCommit(cwd, 'chore(second-package): this works')
175177
const [to] = await getCommitHashes(cwd)
@@ -184,7 +186,7 @@ describe('Commit Linter action', () => {
184186
})
185187

186188
it("should fail for commit that doesn't comply with jira rules", async () => {
187-
const cwd = await git.bootstrap('fixtures/jira')
189+
cwd = await git.bootstrap('fixtures/jira')
188190
td.when(core.getInput('configFile')).thenReturn('./commitlint.config.js')
189191
await gitEmptyCommit(cwd, 'ib-21212121212121: without jira ticket')
190192
const [to] = await getCommitHashes(cwd)
@@ -217,7 +219,7 @@ describe('Commit Linter action', () => {
217219
})
218220

219221
it('should NOT consider commits from another branch', async () => {
220-
const cwd = await git.bootstrap('fixtures/conventional')
222+
cwd = await git.bootstrap('fixtures/conventional')
221223
await gitEmptyCommit(cwd, 'chore: commit before')
222224
await gitEmptyCommit(cwd, 'chore: correct message')
223225
await execa.command('git checkout -b another-branch', { cwd })
@@ -236,7 +238,7 @@ describe('Commit Linter action', () => {
236238
})
237239

238240
it('should consider commits from another branch when firstParent is false', async () => {
239-
const cwd = await git.bootstrap('fixtures/conventional')
241+
cwd = await git.bootstrap('fixtures/conventional')
240242
await gitEmptyCommit(cwd, 'chore: commit before')
241243
await gitEmptyCommit(cwd, 'chore: correct message')
242244
await execa.command('git checkout -b another-branch', { cwd })
@@ -255,7 +257,7 @@ describe('Commit Linter action', () => {
255257
})
256258

257259
it('should lint all commits from a pull request', async () => {
258-
const cwd = await git.bootstrap('fixtures/conventional')
260+
cwd = await git.bootstrap('fixtures/conventional')
259261
td.when(core.getInput('configFile')).thenReturn('./commitlint.config.js')
260262
await gitEmptyCommit(cwd, 'message from before push')
261263
await gitEmptyCommit(cwd, 'wrong message 1')
@@ -286,7 +288,7 @@ describe('Commit Linter action', () => {
286288
})
287289

288290
it('should show an error message when failing to fetch commits', async () => {
289-
const cwd = await git.bootstrap('fixtures/conventional')
291+
cwd = await git.bootstrap('fixtures/conventional')
290292
td.when(core.getInput('configFile')).thenReturn('./commitlint.config.js')
291293
await gitEmptyCommit(cwd, 'commit message')
292294
await createPullRequestEventPayload(cwd)
@@ -310,4 +312,78 @@ describe('Commit Linter action', () => {
310312
)
311313
td.verify(core.setFailed(contains('HttpError: Bad credentials')))
312314
})
315+
316+
describe('when all errors are just warnings', () => {
317+
beforeEach(async () => {
318+
cwd = await git.bootstrap('fixtures/conventional')
319+
await gitEmptyCommit(
320+
cwd,
321+
'chore: correct message\nsome context without leading blank line',
322+
)
323+
const [to] = await getCommitHashes(cwd)
324+
await createPushEventPayload(cwd, { to })
325+
updatePushEnvVars(cwd, to)
326+
td.replace(process, 'cwd', () => cwd)
327+
td.replace(console, 'log')
328+
})
329+
330+
it('should pass and show that warnings exist', async () => {
331+
await runAction()
332+
333+
td.verify(core.setFailed(), { times: 0, ignoreExtraArgs: true })
334+
td.verify(console.log(contains('You have commit messages with warnings')))
335+
})
336+
337+
describe('and failOnWarnings is set to true', () => {
338+
beforeEach(() => {
339+
td.when(core.getInput('failOnWarnings')).thenReturn('true')
340+
})
341+
342+
it('should fail', async () => {
343+
await runAction()
344+
345+
td.verify(
346+
core.setFailed(contains('You have commit messages with errors')),
347+
)
348+
})
349+
})
350+
})
351+
352+
describe('when a subset of errors are just warnings', () => {
353+
beforeEach(async () => {
354+
cwd = await git.bootstrap('fixtures/conventional')
355+
await gitEmptyCommit(
356+
cwd,
357+
'chore: correct message\nsome context without leading blank line',
358+
)
359+
await gitEmptyCommit(cwd, 'wrong message')
360+
const [before, to] = await getCommitHashes(cwd)
361+
await createPushEventPayload(cwd, { before, to })
362+
updatePushEnvVars(cwd, to)
363+
td.replace(process, 'cwd', () => cwd)
364+
td.replace(console, 'log')
365+
})
366+
367+
it('should fail', async () => {
368+
await runAction()
369+
370+
td.verify(
371+
core.setFailed(contains('You have commit messages with errors')),
372+
)
373+
})
374+
375+
describe('and failOnWarnings is set to true', () => {
376+
beforeEach(() => {
377+
td.when(core.getInput('failOnWarnings')).thenReturn('true')
378+
})
379+
380+
it('should fail', async () => {
381+
await runAction()
382+
383+
td.verify(
384+
core.setFailed(contains('You have commit messages with errors')),
385+
)
386+
})
387+
})
388+
})
313389
})

action.yml

+4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ inputs:
1010
description: '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'
1111
default: 'true'
1212
required: false
13+
failOnWarnings:
14+
description: 'whether you want to fail on warnings or not'
15+
default: 'false'
16+
required: false
1317
runs:
1418
using: 'docker'
1519
image: 'docker://wagoid/commitlint-github-action:1.3.2'

0 commit comments

Comments
 (0)