diff --git a/README.md b/README.md index 2d60d97..a1f983a 100644 --- a/README.md +++ b/README.md @@ -8,3 +8,17 @@ from `@commitlint/travis-cli`. Only supports `git` projects, assumes a single repository (i.e. does not explicitly support cross- repo pull requests), and doesn't support renaming the remote to something other than `origin`. We welcome [pull requests](https://github.com/mixmaxhq/commitlint-jenkins/pulls)! + +## Flags + +### `--if-ci` + +If `commitlint-jenkins` is run outside of a CI context, it will fail. This flag simply ignores the +failure, for use-cases where `commitlint-jenkins` should be run from a script shared with a non-CI +workflow. + +### `--pr-only` + +If `commitlint-jenkins` is run in CI in a build that isn't a pull request build, silently exit. +This flag is particularly handy for use with +[`@mixmaxhq/semantic-commitlint`](https://github.com/mixmaxhq/semantic-commitlint). diff --git a/package.json b/package.json index 169f264..de7b379 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,8 @@ ], "scripts": { "ci": "npm run lint", - "lint": "eslint . && node . --if-ci", + "ci:commitlint": "node . --pr-only", + "lint": "eslint .", "prepublishOnly": "if [ \"$CI\" = '' ]; then node -p 'JSON.parse(process.env.npm_package_config_manualPublishMessage)'; exit 1; fi", "test": "echo \"Error: no test specified\" && exit 1", "semantic-release": "semantic-release" diff --git a/src/index.js b/src/index.js index e0f00d5..d9db84a 100644 --- a/src/index.js +++ b/src/index.js @@ -4,9 +4,9 @@ const argv = require('yargs').argv; const commitlint = require('@commitlint/cli'); const IF_CI = !!argv.ifCi; +const PR_ONLY = !!argv.prOnly; -// Allow to override used bins for testing purposes -const GIT = process.env.JENKINS_COMMITLINT_GIT_BIN || 'git'; +// Allow override of used bins for testing purposes const COMMITLINT = process.env.JENKINS_COMMITLINT_BIN; const REQUIRED = ['GIT_COMMIT']; @@ -40,14 +40,16 @@ async function main() { // We could lint since the last successful commit, but that would require a bunch of extra logic // to detect changes to commitlint.config.js or related modules. await lint(['--from', start, '--to', COMMIT]); - } else { + } else if (!PR_ONLY) { + // The --pr-only flag can be useful to use semantic-commitlint on the release branch instead of + // just linting one commit. const input = await rawCommit(COMMIT); await lint([], { input }); } } async function getBase({ branch, tip = 'HEAD' }) { - const result = await execa(GIT, ['merge-base', branch, tip]); + const result = await execa('git', ['merge-base', branch, tip]); return result.stdout; } @@ -59,7 +61,7 @@ async function lint(args, options) { } async function rawCommit(hash) { - const result = await execa(GIT, ['show', '--pretty=format:%B', hash]); + const result = await execa('git', ['show', '-s', '--pretty=format:%B', hash]); return result.stdout; }