diff --git a/README.md b/README.md index d00bfa1..399319f 100644 --- a/README.md +++ b/README.md @@ -100,6 +100,25 @@ You can perform the automated bump in the merge build by using: bumpr bump ``` +If you have some other CI script you want to run only in a PR build, you can check by using + + ``` + bumpr is-pr + ``` + +This command will exit with a 0 exit code if the current build is a PR build, and a 1 if it is not. So you can have +a CI script like this: + + ``` + bumpr is-pr && echo "Do PR stuff" + ``` + +or + + ``` + bumpr is-pr || echo "Do merge stuff" + ``` + If you have the `logging` feature enabled, you can output a specific key from the log file using: ``` diff --git a/bin/cli.js b/bin/cli.js index a9fb2c3..ef96422 100755 --- a/bin/cli.js +++ b/bin/cli.js @@ -35,6 +35,17 @@ program withBumpr(bumpr => bumpr.bump()) }) +program + .command('is-pr') + .description('check if current build is a PR build') + .action(() => { + // Use exit code 0 if the current build is a PR build, 1 if it is not + // this will allow CI scripts to do things like: + // $ bumpr is-pr && echo "A PR build" + // $ bumpr is-pr || echo "A merge build" + withBumpr(bumpr => process.exit(bumpr.isPr() ? 0 : 1)) + }) + program .command('log ') .description(`Output the given key from the ${name} log file`) diff --git a/src/bumpr.js b/src/bumpr.js index 391e501..dab518d 100644 --- a/src/bumpr.js +++ b/src/bumpr.js @@ -103,6 +103,13 @@ class Bumpr { }) } + /** + * Check if a build is happening in a PR + */ + isPr() { + return this.config.computed.ci.isPr + } + /** * Read the bumpr log and output the given key from it * @returns {Promise} a promise resolved with the value or rejected on error diff --git a/src/tests/bumpr.test.js b/src/tests/bumpr.test.js index 354ce04..dffc54c 100644 --- a/src/tests/bumpr.test.js +++ b/src/tests/bumpr.test.js @@ -278,6 +278,31 @@ describe('Bumpr', () => { }) }) + describe('.isPr()', () => { + let ret + describe('when not a PR build', () => { + beforeEach(() => { + set(bumpr.config, 'computed.ci.isPr', false) + ret = bumpr.isPr() + }) + + it('should return false', () => { + expect(ret).toEqual(false) + }) + }) + + describe('when it is a PR build', () => { + beforeEach(() => { + set(bumpr.config, 'computed.ci.isPr', true) + ret = bumpr.isPr() + }) + + it('should return true', () => { + expect(ret).toEqual(true) + }) + }) + }) + describe('.log()', () => { beforeEach(() => { set(bumpr.config, 'features.logging.file', 'the-log-file')