From 5687ff9bf5a9a69ee70d6c14c43e87d7602d137f Mon Sep 17 00:00:00 2001 From: Etienne Magnier Date: Tue, 30 Nov 2021 15:55:02 +0100 Subject: [PATCH] added option to skip commit after a bump --- README.md | 10 ++++++++++ action.yml | 4 ++++ index.js | 8 ++++++-- package-lock.json | 3 ++- tests/end-to-end/config.yaml | 21 +++++++++++++++++++++ tests/end-to-end/index.test.js | 32 +++++++++++++++++++++++++++++--- 6 files changed, 72 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 87608fc..e039920 100644 --- a/README.md +++ b/README.md @@ -54,6 +54,16 @@ Remove the 'actions/setup-node@v1' step from your action.yml file skip-tag: 'true' ``` +**skip-commit:** No commit is made after the version is bumped (optional). Example: +```yaml +- name: 'Automated Version Bump' + uses: 'phips28/gh-action-bump-version@master' + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + with: + skip-commit: 'true' +``` + **default:** Set a default version bump to use (optional - defaults to patch). Example: ```yaml - name: 'Automated Version Bump' diff --git a/action.yml b/action.yml index d28b671..0a65d44 100644 --- a/action.yml +++ b/action.yml @@ -30,6 +30,10 @@ inputs: description: 'Avoid to add a TAG to the version update commit' default: 'false' required: false + skip-commit: + description: 'Avoid to add a commit after the version is bumped' + default: 'false' + required: false PACKAGEJSON_DIR: description: 'Custom dir to the package' default: '' diff --git a/index.js b/index.js index a69eb09..e626e2c 100644 --- a/index.js +++ b/index.js @@ -146,7 +146,9 @@ const workspace = process.env.GITHUB_WORKSPACE; console.log('current:', current, '/', 'version:', version); let newVersion = execSync(`npm version --git-tag-version=false ${version}`).toString().trim().replace(/^v/, ''); newVersion = `${tagPrefix}${newVersion}`; - await runInWorkspace('git', ['commit', '-a', '-m', commitMessage.replace(/{{version}}/g, newVersion)]); + if (process.env['INPUT_SKIP-COMMIT'] !== 'true') { + await runInWorkspace('git', ['commit', '-a', '-m', commitMessage.replace(/{{version}}/g, newVersion)]); + } // now go to the actual branch to perform the same versioning if (isPullRequest) { @@ -161,7 +163,9 @@ const workspace = process.env.GITHUB_WORKSPACE; console.log(`::set-output name=newTag::${newVersion}`); try { // to support "actions/checkout@v1" - await runInWorkspace('git', ['commit', '-a', '-m', commitMessage.replace(/{{version}}/g, newVersion)]); + if (process.env['INPUT_SKIP-COMMIT'] !== 'true') { + await runInWorkspace('git', ['commit', '-a', '-m', commitMessage.replace(/{{version}}/g, newVersion)]); + } } catch (e) { console.warn( 'git commit failed because you are using "actions/checkout@v2"; ' + diff --git a/package-lock.json b/package-lock.json index 75569d7..c729475 100644 --- a/package-lock.json +++ b/package-lock.json @@ -5,7 +5,8 @@ "requires": true, "packages": { "": { - "version": "9.0.7", + "name": "gh-action-bump-version", + "version": "9.0.10", "license": "ISC", "devDependencies": { "@types/jest": "^27.0.1", diff --git a/tests/end-to-end/config.yaml b/tests/end-to-end/config.yaml index ce2d5f1..0ecb15e 100644 --- a/tests/end-to-end/config.yaml +++ b/tests/end-to-end/config.yaml @@ -155,6 +155,27 @@ suites: expected: version: 4.1.3 branch: other-branch + - name: skip-commit + yaml: + name: Bump Version (Skip Commit) + on: + push: + jobs: + bump-version: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - id: version-bump + uses: ./action + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + with: + skip-commit: true + tests: + - message: no keywords + expected: + version: 4.1.4 + message: 'ci: version bump to 4.1.3' actionFiles: - index.js diff --git a/tests/end-to-end/index.test.js b/tests/end-to-end/index.test.js index d03df21..37f099f 100644 --- a/tests/end-to-end/index.test.js +++ b/tests/end-to-end/index.test.js @@ -99,7 +99,12 @@ async function getMostRecentWorkflowRunDate() { return date; } -function generateExpectationText({ version: expectedVersion, tag: expectedTag, branch: expectedBranch }) { +function generateExpectationText({ + version: expectedVersion, + tag: expectedTag, + branch: expectedBranch, + message: expectedMessage, +}) { const results = [`- **Version:** ${expectedVersion}`]; if (expectedTag) { results.push(`- **Tag:** ${expectedTag}`); @@ -107,10 +112,18 @@ function generateExpectationText({ version: expectedVersion, tag: expectedTag, b if (expectedBranch) { results.push(`- **Branch:** ${expectedBranch}`); } + if (expectedMessage) { + results.push(`- **Message:** ${expectedMessage}`); + } return results.join('\n'); } -async function assertExpectation({ version: expectedVersion, tag: expectedTag, branch: expectedBranch }) { +async function assertExpectation({ + version: expectedVersion, + tag: expectedTag, + branch: expectedBranch, + message: expectedMessage, +}) { if (expectedTag === undefined) { expectedTag = expectedVersion; } @@ -119,9 +132,17 @@ async function assertExpectation({ version: expectedVersion, tag: expectedTag, b await git('checkout', expectedBranch); } await git('pull'); - const [packageVersion, latestTag] = await Promise.all([getPackageJsonVersion(), getLatestTag()]); + const [packageVersion, latestTag, latestMessage] = await Promise.all([ + getPackageJsonVersion(), + getLatestTag(), + getLatestCommitMessage(), + ]); + if (!expectedMessage) { + expectedMessage = latestMessage; + } expect(packageVersion).toBe(expectedVersion); expect(latestTag).toBe(expectedTag); + expect(latestMessage).toBe(expectedMessage); if (expectedBranch) { await git('checkout', 'main'); } @@ -138,3 +159,8 @@ async function getLatestTag() { const result = await git({ suppressOutput: true }, 'describe', '--tags', '--abbrev=0'); return result.stdout; } + +async function getLatestCommitMessage() { + const result = await git({ suppressOutput: true }, 'show', '--no-patch', '--format=%s'); + return result.stdout; +}