Skip to content

Commit a00b914

Browse files
authored
Merge pull request #5 from mixmaxhq/eli/add-pr-only-flag
feat: add --pr-only flag
2 parents 44ebd17 + 0831609 commit a00b914

File tree

3 files changed

+23
-6
lines changed

3 files changed

+23
-6
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,17 @@ from `@commitlint/travis-cli`.
88
Only supports `git` projects, assumes a single repository (i.e. does not explicitly support cross-
99
repo pull requests), and doesn't support renaming the remote to something other than `origin`. We
1010
welcome [pull requests](https://github.com/mixmaxhq/commitlint-jenkins/pulls)!
11+
12+
## Flags
13+
14+
### `--if-ci`
15+
16+
If `commitlint-jenkins` is run outside of a CI context, it will fail. This flag simply ignores the
17+
failure, for use-cases where `commitlint-jenkins` should be run from a script shared with a non-CI
18+
workflow.
19+
20+
### `--pr-only`
21+
22+
If `commitlint-jenkins` is run in CI in a build that isn't a pull request build, silently exit.
23+
This flag is particularly handy for use with
24+
[`@mixmaxhq/semantic-commitlint`](https://github.com/mixmaxhq/semantic-commitlint).

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
],
1212
"scripts": {
1313
"ci": "npm run lint",
14-
"lint": "eslint . && node . --if-ci",
14+
"ci:commitlint": "node . --pr-only",
15+
"lint": "eslint .",
1516
"prepublishOnly": "if [ \"$CI\" = '' ]; then node -p 'JSON.parse(process.env.npm_package_config_manualPublishMessage)'; exit 1; fi",
1617
"test": "echo \"Error: no test specified\" && exit 1",
1718
"semantic-release": "semantic-release"

src/index.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ const argv = require('yargs').argv;
44
const commitlint = require('@commitlint/cli');
55

66
const IF_CI = !!argv.ifCi;
7+
const PR_ONLY = !!argv.prOnly;
78

8-
// Allow to override used bins for testing purposes
9-
const GIT = process.env.JENKINS_COMMITLINT_GIT_BIN || 'git';
9+
// Allow override of used bins for testing purposes
1010
const COMMITLINT = process.env.JENKINS_COMMITLINT_BIN;
1111

1212
const REQUIRED = ['GIT_COMMIT'];
@@ -40,14 +40,16 @@ async function main() {
4040
// We could lint since the last successful commit, but that would require a bunch of extra logic
4141
// to detect changes to commitlint.config.js or related modules.
4242
await lint(['--from', start, '--to', COMMIT]);
43-
} else {
43+
} else if (!PR_ONLY) {
44+
// The --pr-only flag can be useful to use semantic-commitlint on the release branch instead of
45+
// just linting one commit.
4446
const input = await rawCommit(COMMIT);
4547
await lint([], { input });
4648
}
4749
}
4850

4951
async function getBase({ branch, tip = 'HEAD' }) {
50-
const result = await execa(GIT, ['merge-base', branch, tip]);
52+
const result = await execa('git', ['merge-base', branch, tip]);
5153
return result.stdout;
5254
}
5355

@@ -59,7 +61,7 @@ async function lint(args, options) {
5961
}
6062

6163
async function rawCommit(hash) {
62-
const result = await execa(GIT, ['show', '--pretty=format:%B', hash]);
64+
const result = await execa('git', ['show', '-s', '--pretty=format:%B', hash]);
6365
return result.stdout;
6466
}
6567

0 commit comments

Comments
 (0)