From 550792f0ca7bb2cb7e9b15afee32ffead2b237e5 Mon Sep 17 00:00:00 2001 From: Wagner Santos Date: Mon, 20 Jul 2020 08:45:05 -0300 Subject: [PATCH] feat: add `results` output Resolves #39 --- README.md | 44 ++++++++++++++++++++++++++++++++++ action.js | 3 +++ action.test.js | 60 +++++++++++++++++++++++++++++++++++++++++++++- action.yml | 3 +++ generateOutputs.js | 24 +++++++++++++++++++ package-lock.json | 6 ++--- package.json | 2 +- 7 files changed, 137 insertions(+), 5 deletions(-) create mode 100644 generateOutputs.js diff --git a/README.md b/README.md index 35d902e..2aace07 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/action.js b/action.js index 89351bb..d103d49 100644 --- a/action.js +++ b/action.js @@ -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' @@ -134,6 +135,8 @@ const showLintResults = async ([from, to]) => { ) const formattedResults = formatErrors(lintedCommits) + generateOutputs(lintedCommits) + if (hasOnlyWarnings(lintedCommits)) { handleOnlyWarnings(formattedResults) } else if (formattedResults) { diff --git a/action.test.js b/action.test.js index e9fceae..fae0e1a 100644 --- a/action.test.js +++ b/action.test.js @@ -11,6 +11,8 @@ const { updatePullRequestEnvVars, } = require('./testUtils') +const resultsOutputId = 'results' + const { matchers: { contains }, } = td @@ -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') @@ -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( @@ -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 () => { @@ -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') @@ -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) @@ -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') diff --git a/action.yml b/action.yml index e717ac6..1db01b5 100644 --- a/action.yml +++ b/action.yml @@ -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' diff --git a/generateOutputs.js b/generateOutputs.js new file mode 100644 index 0000000..9fc8f0a --- /dev/null +++ b/generateOutputs.js @@ -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 diff --git a/package-lock.json b/package-lock.json index 7fd8a6a..51a897d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -5,9 +5,9 @@ "requires": true, "dependencies": { "@actions/core": { - "version": "1.1.1", - "resolved": "https://registry.npmjs.org/@actions/core/-/core-1.1.1.tgz", - "integrity": "sha512-O5G6EmlzTVsng7VSpNtszIoQq6kOgMGNTFB/hmwKNNA4V71JyxImCIrL27vVHCt2Cb3ImkaCr6o27C2MV9Ylwg==" + "version": "1.2.4", + "resolved": "https://registry.npmjs.org/@actions/core/-/core-1.2.4.tgz", + "integrity": "sha512-YJCEq8BE3CdN8+7HPZ/4DxJjk/OkZV2FFIf+DlZTC/4iBlzYCD5yjRR6eiOS5llO11zbRltIRuKAjMKaWTE6cg==" }, "@actions/github": { "version": "1.1.0", diff --git a/package.json b/package.json index dbaba1c..2be406c 100644 --- a/package.json +++ b/package.json @@ -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",