diff --git a/README.md b/README.md index 09774b4aa..03c2be21e 100644 --- a/README.md +++ b/README.md @@ -61,6 +61,9 @@ jobs: 1. `hotfix:patch,pre-feat:preminor`, 2. `bug:patch:Bug Fixes,chore:patch:Chores` +#### Create a tag without pushing it +- **push_tag** _(optional)_ - Push the tag to the remote. If false, tag is created but not pushed. (default: `true`) + #### Debugging - **dry_run** _(optional)_ - Do not perform tagging, just calculate next version and changelog, then exit diff --git a/action.yml b/action.yml index eae936ed1..485033c8d 100644 --- a/action.yml +++ b/action.yml @@ -59,6 +59,10 @@ inputs: description: "Do not perform tagging, just calculate next version and changelog, then exit." required: false default: "false" + push_tag: + description: "Push the created tag to remote ref. If false, tag is created, but not pushed." + required: false + default: "true" runs: using: "node16" diff --git a/src/action.ts b/src/action.ts index 2a073169e..4cbc0972b 100644 --- a/src/action.ts +++ b/src/action.ts @@ -32,6 +32,7 @@ export default async function main() { const customReleaseRules = core.getInput('custom_release_rules'); const shouldFetchAllTags = core.getInput('fetch_all_tags'); const commitSha = core.getInput('commit_sha'); + const pushTag = core.getBooleanInput('push_tag'); let mappedReleaseRules; if (customReleaseRules) { @@ -228,5 +229,5 @@ export default async function main() { return; } - await createTag(newTag, createAnnotatedTag, commitRef); + await createTag(newTag, createAnnotatedTag, commitRef, pushTag); } diff --git a/src/github.ts b/src/github.ts index 908a6996e..fbe664f49 100644 --- a/src/github.ts +++ b/src/github.ts @@ -68,7 +68,8 @@ export async function compareCommits(baseRef: string, headRef: string) { export async function createTag( newTag: string, createAnnotatedTag: boolean, - GITHUB_SHA: string + GITHUB_SHA: string, + pushTag: boolean = true ) { const octokit = getOctokitSingleton(); let annotatedTag: @@ -85,10 +86,14 @@ export async function createTag( }); } - core.debug(`Pushing new tag to the repo.`); - await octokit.git.createRef({ - ...context.repo, - ref: `refs/tags/${newTag}`, - sha: annotatedTag ? annotatedTag.data.sha : GITHUB_SHA, - }); + if (pushTag) { + core.debug(`Pushing new tag to the repo.`); + await octokit.git.createRef({ + ...context.repo, + ref: `refs/tags/${newTag}`, + sha: annotatedTag ? annotatedTag.data.sha : GITHUB_SHA, + }); + } else { + core.debug(`Tag was not pushed to remote`); + } } diff --git a/tests/action.test.ts b/tests/action.test.ts index 413e7bfae..1e0475529 100644 --- a/tests/action.test.ts +++ b/tests/action.test.ts @@ -62,7 +62,8 @@ describe('github-tag-action', () => { expect(mockCreateTag).toHaveBeenCalledWith( 'v0.0.1', expect.any(Boolean), - expect.any(String) + expect.any(String), + true ); expect(mockSetFailed).not.toBeCalled(); }); @@ -92,7 +93,8 @@ describe('github-tag-action', () => { expect(mockCreateTag).toHaveBeenCalledWith( 'v0.0.1', expect.any(Boolean), - expect.any(String) + expect.any(String), + true ); expect(mockSetFailed).not.toBeCalled(); }); @@ -169,7 +171,8 @@ describe('github-tag-action', () => { expect(mockCreateTag).toHaveBeenCalledWith( 'v2.0.0', expect.any(Boolean), - expect.any(String) + expect.any(String), + true ); expect(mockSetFailed).not.toBeCalled(); }); @@ -211,7 +214,38 @@ describe('github-tag-action', () => { expect(mockCreateTag).toHaveBeenCalledWith( 'v1.3.0', expect.any(Boolean), - expect.any(String) + expect.any(String), + true + ); + expect(mockSetFailed).not.toBeCalled(); + }); + + it('does create a tag, but does not push the tag', async () => { + /* + * Given + */ + setInput('push_tag', 'false'); + const commits: any[] = []; + jest + .spyOn(utils, 'getCommits') + .mockImplementation(async (sha) => commits); + + const validTags: any[] = []; + jest + .spyOn(utils, 'getValidTags') + .mockImplementation(async () => validTags); + /* + * When + */ + await action(); + /* + * Then + */ + expect(mockCreateTag).toHaveBeenCalledWith( + 'v0.0.1', + expect.any(Boolean), + expect.any(String), + false ); expect(mockSetFailed).not.toBeCalled(); }); @@ -257,7 +291,8 @@ describe('github-tag-action', () => { expect(mockCreateTag).toHaveBeenCalledWith( 'v1.2.4', expect.any(Boolean), - expect.any(String) + expect.any(String), + true ); expect(mockSetFailed).not.toBeCalled(); }); @@ -297,7 +332,8 @@ describe('github-tag-action', () => { expect(mockCreateTag).toHaveBeenCalledWith( 'v1.3.0', expect.any(Boolean), - expect.any(String) + expect.any(String), + true ); expect(mockSetFailed).not.toBeCalled(); }); @@ -341,7 +377,8 @@ describe('github-tag-action', () => { expect(mockCreateTag).toHaveBeenCalledWith( 'v2.0.0', expect.any(Boolean), - expect.any(String) + expect.any(String), + true ); expect(mockSetFailed).not.toBeCalled(); }); @@ -395,7 +432,8 @@ describe('github-tag-action', () => { expect(mockCreateTag).toHaveBeenCalledWith( 'v2.2.0', expect.any(Boolean), - expect.any(String) + expect.any(String), + true ); expect(mockSetFailed).not.toBeCalled(); }); @@ -440,7 +478,8 @@ describe('github-tag-action', () => { expect(mockCreateTag).toHaveBeenCalledWith( 'v1.3.0', expect.any(Boolean), - expect.any(String) + expect.any(String), + true ); expect(mockSetFailed).not.toBeCalled(); }); @@ -522,7 +561,8 @@ describe('github-tag-action', () => { expect(mockCreateTag).toHaveBeenCalledWith( 'v1.2.4-prerelease.0', expect.any(Boolean), - expect.any(String) + expect.any(String), + true ); expect(mockSetFailed).not.toBeCalled(); }); @@ -560,7 +600,8 @@ describe('github-tag-action', () => { expect(mockCreateTag).toHaveBeenCalledWith( 'v1.2.4-prerelease.0', expect.any(Boolean), - expect.any(String) + expect.any(String), + true ); expect(mockSetFailed).not.toBeCalled(); }); @@ -600,7 +641,8 @@ describe('github-tag-action', () => { expect(mockCreateTag).toHaveBeenCalledWith( 'v1.3.0-prerelease.0', expect.any(Boolean), - expect.any(String) + expect.any(String), + true ); expect(mockSetFailed).not.toBeCalled(); }); @@ -644,7 +686,8 @@ describe('github-tag-action', () => { expect(mockCreateTag).toHaveBeenCalledWith( 'v2.0.0-prerelease.0', expect.any(Boolean), - expect.any(String) + expect.any(String), + true ); expect(mockSetFailed).not.toBeCalled(); }); @@ -701,7 +744,8 @@ describe('github-tag-action', () => { expect(mockCreateTag).toHaveBeenCalledWith( 'v2.2.0-prerelease.0', expect.any(Boolean), - expect.any(String) + expect.any(String), + true ); expect(mockSetFailed).not.toBeCalled(); }); @@ -746,7 +790,8 @@ describe('github-tag-action', () => { expect(mockCreateTag).toHaveBeenCalledWith( 'v1.3.0-prerelease.0', expect.any(Boolean), - expect.any(String) + expect.any(String), + true ); expect(mockSetFailed).not.toBeCalled(); });