Skip to content

Commit

Permalink
feat: add results output
Browse files Browse the repository at this point in the history
Resolves #39
  • Loading branch information
wagoid committed Aug 2, 2020
1 parent 4b08865 commit 550792f
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 5 deletions.
44 changes: 44 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,50 @@ Link to a page explaining your commit message convention.

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

## Outputs

### `results`

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.

Below you can see an example text output together with its corresponding JSON output:

```
You have commit messages with errors
⧗ input: wrong message
✖ subject may not be empty [subject-empty]
✖ type may not be empty [type-empty]
✖ found 2 problems, 0 warnings
ⓘ Get help: https://github.com/conventional-changelog/commitlint/#what-is-commitlint
⧗ input: chore: my message
⚠ body must have leading blank line [body-leading-blank]
⚠ found 0 problems, 1 warnings
ⓘ Get help: https://github.com/conventional-changelog/commitlint/#what-is-commitlint
```

```JSON
[
{
"hash": "cb0f846f13b490c2fd17bd5ed0b6f65ba9b86c75",
"message": "wrong message",
"valid": false,
"errors": ["subject may not be empty", "type may not be empty"],
"warnings": [],
},
{
"hash": "cb14483cbde23b61322ffb8d3fcdc87f514a3141",
"message": "chore: my message\n\nsome context without leading blank line",
"valid": true,
"errors": [],
"warnings": ["body must have leading blank line"],
},
]
```

## About `extends` in your config file

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:
Expand Down
3 changes: 3 additions & 0 deletions action.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const lint = require('@commitlint/lint')
const { format } = require('@commitlint/format')
const load = require('@commitlint/load')
const gitCommits = require('./gitCommits')
const generateOutputs = require('./generateOutputs')

const pullRequestEvent = 'pull_request'

Expand Down Expand Up @@ -134,6 +135,8 @@ const showLintResults = async ([from, to]) => {
)
const formattedResults = formatErrors(lintedCommits)

generateOutputs(lintedCommits)

if (hasOnlyWarnings(lintedCommits)) {
handleOnlyWarnings(formattedResults)
} else if (formattedResults) {
Expand Down
60 changes: 59 additions & 1 deletion action.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ const {
updatePullRequestEnvVars,
} = require('./testUtils')

const resultsOutputId = 'results'

const {
matchers: { contains },
} = td
Expand Down Expand Up @@ -43,6 +45,7 @@ describe('Commit Linter action', () => {
core = require('@actions/core')
td.replace(core, 'getInput')
td.replace(core, 'setFailed')
td.replace(core, 'setOutput')
td.when(core.getInput('configFile')).thenReturn('./commitlint.config.js')
td.when(core.getInput('firstParent')).thenReturn('true')
td.when(core.getInput('failOnWarnings')).thenReturn('false')
Expand Down Expand Up @@ -317,6 +320,8 @@ describe('Commit Linter action', () => {
})

describe('when all errors are just warnings', () => {
let expectedResultsOutput

beforeEach(async () => {
cwd = await git.bootstrap('fixtures/conventional')
await gitEmptyCommit(
Expand All @@ -328,6 +333,17 @@ describe('Commit Linter action', () => {
updatePushEnvVars(cwd, to)
td.replace(process, 'cwd', () => cwd)
td.replace(console, 'log')

expectedResultsOutput = [
{
hash: to,
message:
'chore: correct message\n\nsome context without leading blank line',
valid: true,
errors: [],
warnings: ['body must have leading blank line'],
},
]
})

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

it('should show the results in an output', async () => {
await runAction()

td.verify(core.setOutput(resultsOutputId, expectedResultsOutput))
})

describe('and failOnWarnings is set to true', () => {
beforeEach(() => {
td.when(core.getInput('failOnWarnings')).thenReturn('true')
Expand All @@ -349,18 +371,30 @@ describe('Commit Linter action', () => {
core.setFailed(contains('You have commit messages with errors')),
)
})

it('should show the results in an output', async () => {
await runAction()

td.verify(core.setOutput(resultsOutputId, expectedResultsOutput))
})
})
})

describe('when a subset of errors are just warnings', () => {
let firstHash
let secondHash

beforeEach(async () => {
cwd = await git.bootstrap('fixtures/conventional')
await gitEmptyCommit(cwd, 'message from before push')
await gitEmptyCommit(
cwd,
'chore: correct message\nsome context without leading blank line',
)
await gitEmptyCommit(cwd, 'wrong message')
const [before, to] = await getCommitHashes(cwd)
const [before, firstCommit, to] = await getCommitHashes(cwd)
firstHash = firstCommit
secondHash = to
await createPushEventPayload(cwd, { before, to })
updatePushEnvVars(cwd, to)
td.replace(process, 'cwd', () => cwd)
Expand All @@ -375,6 +409,30 @@ describe('Commit Linter action', () => {
)
})

it('should show the results in an output', async () => {
await runAction()

const expectedResultsOutput = [
{
hash: secondHash,
message: 'wrong message',
valid: false,
errors: ['subject may not be empty', 'type may not be empty'],
warnings: [],
},
{
hash: firstHash,
message:
'chore: correct message\n\nsome context without leading blank line',
valid: true,
errors: [],
warnings: ['body must have leading blank line'],
},
]

td.verify(core.setOutput(resultsOutputId, expectedResultsOutput))
})

describe('and failOnWarnings is set to true', () => {
beforeEach(() => {
td.when(core.getInput('failOnWarnings')).thenReturn('true')
Expand Down
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ inputs:
description: 'Link to a page explaining your commit message convention'
default: 'https://github.com/conventional-changelog/commitlint/#what-is-commitlint'
required: false
outputs:
results:
description: The error and warning messages for each one of the analyzed commits
runs:
using: 'docker'
image: 'docker://wagoid/commitlint-github-action:1.7.0'
Expand Down
24 changes: 24 additions & 0 deletions generateOutputs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const core = require('@actions/core')

const resultsOutputId = 'results'

const mapMessageValidation = item => item.message

const mapResultOutput = ({
hash,
lintResult: { valid, errors, warnings, input },
}) => ({
hash,
message: input,
valid,
errors: errors.map(mapMessageValidation),
warnings: warnings.map(mapMessageValidation),
})

const generateOutputs = lintedCommits => {
const resultsOutput = lintedCommits.map(mapResultOutput)

core.setOutput(resultsOutputId, resultsOutput)
}

module.exports = generateOutputs
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"license": "ISC",
"homepage": "https://github.com/wagoid/commitlint-github-action",
"dependencies": {
"@actions/core": "^1.1.1",
"@actions/core": "^1.2.4",
"@actions/github": "^1.1.0",
"@commitlint/config-angular": "^8.3.4",
"@commitlint/config-conventional": "^8.3.4",
Expand Down

0 comments on commit 550792f

Please sign in to comment.