Skip to content

Commit 550792f

Browse files
committed
feat: add results output
Resolves #39
1 parent 4b08865 commit 550792f

7 files changed

+137
-5
lines changed

README.md

+44
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,50 @@ Link to a page explaining your commit message convention.
5454

5555
default: `https://github.com/conventional-changelog/commitlint/#what-is-commitlint`
5656

57+
## Outputs
58+
59+
### `results`
60+
61+
The error and warning messages for each one of the analyzed commits. This is useful if you want to use the commitlint results in a JSON format in other jobs. See [the documentation](https://docs.github.com/en/actions/reference/context-and-expression-syntax-for-github-actions#fromjson) on how to read JSON information from outputs.
62+
63+
Below you can see an example text output together with its corresponding JSON output:
64+
65+
```
66+
You have commit messages with errors
67+
68+
⧗ input: wrong message
69+
✖ subject may not be empty [subject-empty]
70+
✖ type may not be empty [type-empty]
71+
72+
✖ found 2 problems, 0 warnings
73+
ⓘ Get help: https://github.com/conventional-changelog/commitlint/#what-is-commitlint
74+
75+
⧗ input: chore: my message
76+
⚠ body must have leading blank line [body-leading-blank]
77+
78+
⚠ found 0 problems, 1 warnings
79+
ⓘ Get help: https://github.com/conventional-changelog/commitlint/#what-is-commitlint
80+
```
81+
82+
```JSON
83+
[
84+
{
85+
"hash": "cb0f846f13b490c2fd17bd5ed0b6f65ba9b86c75",
86+
"message": "wrong message",
87+
"valid": false,
88+
"errors": ["subject may not be empty", "type may not be empty"],
89+
"warnings": [],
90+
},
91+
{
92+
"hash": "cb14483cbde23b61322ffb8d3fcdc87f514a3141",
93+
"message": "chore: my message\n\nsome context without leading blank line",
94+
"valid": true,
95+
"errors": [],
96+
"warnings": ["body must have leading blank line"],
97+
},
98+
]
99+
```
100+
57101
## About `extends` in your config file
58102

59103
This is a [`Docker` action](https://github.com/actions/toolkit/blob/e2adf403d6d14a9ca7474976ccaca20f72ff8209/docs/action-types.md#why-would-i-choose-a-docker-action), and was made like this so that you can run it with minimum setup, regardless of your repo's environment. It comes packed with the most famous shared configurations that you can use in your commitlint config's `extends` field:

action.js

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const lint = require('@commitlint/lint')
66
const { format } = require('@commitlint/format')
77
const load = require('@commitlint/load')
88
const gitCommits = require('./gitCommits')
9+
const generateOutputs = require('./generateOutputs')
910

1011
const pullRequestEvent = 'pull_request'
1112

@@ -134,6 +135,8 @@ const showLintResults = async ([from, to]) => {
134135
)
135136
const formattedResults = formatErrors(lintedCommits)
136137

138+
generateOutputs(lintedCommits)
139+
137140
if (hasOnlyWarnings(lintedCommits)) {
138141
handleOnlyWarnings(formattedResults)
139142
} else if (formattedResults) {

action.test.js

+59-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ const {
1111
updatePullRequestEnvVars,
1212
} = require('./testUtils')
1313

14+
const resultsOutputId = 'results'
15+
1416
const {
1517
matchers: { contains },
1618
} = td
@@ -43,6 +45,7 @@ describe('Commit Linter action', () => {
4345
core = require('@actions/core')
4446
td.replace(core, 'getInput')
4547
td.replace(core, 'setFailed')
48+
td.replace(core, 'setOutput')
4649
td.when(core.getInput('configFile')).thenReturn('./commitlint.config.js')
4750
td.when(core.getInput('firstParent')).thenReturn('true')
4851
td.when(core.getInput('failOnWarnings')).thenReturn('false')
@@ -317,6 +320,8 @@ describe('Commit Linter action', () => {
317320
})
318321

319322
describe('when all errors are just warnings', () => {
323+
let expectedResultsOutput
324+
320325
beforeEach(async () => {
321326
cwd = await git.bootstrap('fixtures/conventional')
322327
await gitEmptyCommit(
@@ -328,6 +333,17 @@ describe('Commit Linter action', () => {
328333
updatePushEnvVars(cwd, to)
329334
td.replace(process, 'cwd', () => cwd)
330335
td.replace(console, 'log')
336+
337+
expectedResultsOutput = [
338+
{
339+
hash: to,
340+
message:
341+
'chore: correct message\n\nsome context without leading blank line',
342+
valid: true,
343+
errors: [],
344+
warnings: ['body must have leading blank line'],
345+
},
346+
]
331347
})
332348

333349
it('should pass and show that warnings exist', async () => {
@@ -337,6 +353,12 @@ describe('Commit Linter action', () => {
337353
td.verify(console.log(contains('You have commit messages with warnings')))
338354
})
339355

356+
it('should show the results in an output', async () => {
357+
await runAction()
358+
359+
td.verify(core.setOutput(resultsOutputId, expectedResultsOutput))
360+
})
361+
340362
describe('and failOnWarnings is set to true', () => {
341363
beforeEach(() => {
342364
td.when(core.getInput('failOnWarnings')).thenReturn('true')
@@ -349,18 +371,30 @@ describe('Commit Linter action', () => {
349371
core.setFailed(contains('You have commit messages with errors')),
350372
)
351373
})
374+
375+
it('should show the results in an output', async () => {
376+
await runAction()
377+
378+
td.verify(core.setOutput(resultsOutputId, expectedResultsOutput))
379+
})
352380
})
353381
})
354382

355383
describe('when a subset of errors are just warnings', () => {
384+
let firstHash
385+
let secondHash
386+
356387
beforeEach(async () => {
357388
cwd = await git.bootstrap('fixtures/conventional')
389+
await gitEmptyCommit(cwd, 'message from before push')
358390
await gitEmptyCommit(
359391
cwd,
360392
'chore: correct message\nsome context without leading blank line',
361393
)
362394
await gitEmptyCommit(cwd, 'wrong message')
363-
const [before, to] = await getCommitHashes(cwd)
395+
const [before, firstCommit, to] = await getCommitHashes(cwd)
396+
firstHash = firstCommit
397+
secondHash = to
364398
await createPushEventPayload(cwd, { before, to })
365399
updatePushEnvVars(cwd, to)
366400
td.replace(process, 'cwd', () => cwd)
@@ -375,6 +409,30 @@ describe('Commit Linter action', () => {
375409
)
376410
})
377411

412+
it('should show the results in an output', async () => {
413+
await runAction()
414+
415+
const expectedResultsOutput = [
416+
{
417+
hash: secondHash,
418+
message: 'wrong message',
419+
valid: false,
420+
errors: ['subject may not be empty', 'type may not be empty'],
421+
warnings: [],
422+
},
423+
{
424+
hash: firstHash,
425+
message:
426+
'chore: correct message\n\nsome context without leading blank line',
427+
valid: true,
428+
errors: [],
429+
warnings: ['body must have leading blank line'],
430+
},
431+
]
432+
433+
td.verify(core.setOutput(resultsOutputId, expectedResultsOutput))
434+
})
435+
378436
describe('and failOnWarnings is set to true', () => {
379437
beforeEach(() => {
380438
td.when(core.getInput('failOnWarnings')).thenReturn('true')

action.yml

+3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ inputs:
1818
description: 'Link to a page explaining your commit message convention'
1919
default: 'https://github.com/conventional-changelog/commitlint/#what-is-commitlint'
2020
required: false
21+
outputs:
22+
results:
23+
description: The error and warning messages for each one of the analyzed commits
2124
runs:
2225
using: 'docker'
2326
image: 'docker://wagoid/commitlint-github-action:1.7.0'

generateOutputs.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const core = require('@actions/core')
2+
3+
const resultsOutputId = 'results'
4+
5+
const mapMessageValidation = item => item.message
6+
7+
const mapResultOutput = ({
8+
hash,
9+
lintResult: { valid, errors, warnings, input },
10+
}) => ({
11+
hash,
12+
message: input,
13+
valid,
14+
errors: errors.map(mapMessageValidation),
15+
warnings: warnings.map(mapMessageValidation),
16+
})
17+
18+
const generateOutputs = lintedCommits => {
19+
const resultsOutput = lintedCommits.map(mapResultOutput)
20+
21+
core.setOutput(resultsOutputId, resultsOutput)
22+
}
23+
24+
module.exports = generateOutputs

package-lock.json

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"license": "ISC",
1616
"homepage": "https://github.com/wagoid/commitlint-github-action",
1717
"dependencies": {
18-
"@actions/core": "^1.1.1",
18+
"@actions/core": "^1.2.4",
1919
"@actions/github": "^1.1.0",
2020
"@commitlint/config-angular": "^8.3.4",
2121
"@commitlint/config-conventional": "^8.3.4",

0 commit comments

Comments
 (0)