From be19bb72fb968b8effdc11d69a1ee797becb85cb Mon Sep 17 00:00:00 2001 From: Mikael Andersson Date: Wed, 9 Nov 2022 16:57:44 +0100 Subject: [PATCH 1/9] fix: use event name to set isPrerelease Use GITHUB_EVENT_NAME environment variable to determine if the triggering event was a pull request. --- src/action.ts | 9 +++++++-- src/utils.ts | 4 ++-- tests/utils.test.ts | 4 ++-- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/action.ts b/src/action.ts index 2a073169e..49692d63a 100644 --- a/src/action.ts +++ b/src/action.ts @@ -38,13 +38,18 @@ export default async function main() { mappedReleaseRules = mapCustomReleaseRules(customReleaseRules); } - const { GITHUB_REF, GITHUB_SHA } = process.env; + const { GITHUB_REF, GITHUB_SHA, GITHUB_EVENT_NAME } = process.env; if (!GITHUB_REF) { core.setFailed('Missing GITHUB_REF.'); return; } + if(!GITHUB_EVENT_NAME) { + core.setFailed('Missing GITHUB_EVENT_NAME'); + return; + } + const commitRef = commitSha || GITHUB_SHA; if (!commitRef) { core.setFailed('Missing commit_sha or GITHUB_SHA.'); @@ -58,7 +63,7 @@ export default async function main() { const isPreReleaseBranch = preReleaseBranches .split(',') .some((branch) => currentBranch.match(branch)); - const isPullRequest = isPr(GITHUB_REF); + const isPullRequest = isPr(GITHUB_EVENT_NAME); const isPrerelease = !isReleaseBranch && !isPullRequest && isPreReleaseBranch; // Sanitize identifier according to diff --git a/src/utils.ts b/src/utils.ts index 05b1247f3..be77901f0 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -49,8 +49,8 @@ export function getBranchFromRef(ref: string) { return ref.replace('refs/heads/', ''); } -export function isPr(ref: string) { - return ref.includes('refs/pull/'); +export function isPr(eventName: string) { + return eventName.includes('pull_request'); } export function getLatestTag( diff --git a/tests/utils.test.ts b/tests/utils.test.ts index 417d8791e..257282361 100644 --- a/tests/utils.test.ts +++ b/tests/utils.test.ts @@ -31,12 +31,12 @@ describe('utils', () => { /* * Given */ - const remoteRef = 'refs/pull/123/merge'; + const eventName = 'pull_request'; /* * When */ - const isPullRequest = utils.isPr(remoteRef); + const isPullRequest = utils.isPr(eventName); /* * Then From a0612652c30cfb51de8118d0a956e45daad5791b Mon Sep 17 00:00:00 2001 From: Mikael Andersson Date: Wed, 9 Nov 2022 17:02:44 +0100 Subject: [PATCH 2/9] test: description of triggering event test Add description to indicate that event is looked for in test. --- tests/utils.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/utils.test.ts b/tests/utils.test.ts index 257282361..eb04d9385 100644 --- a/tests/utils.test.ts +++ b/tests/utils.test.ts @@ -27,7 +27,7 @@ describe('utils', () => { expect(branch).toEqual('master'); }); - it('test if ref is PR', () => { + it('test if triggering event is PR', () => { /* * Given */ From 5306fa23960fea49fbe71e3782d7b5ad95c4eee0 Mon Sep 17 00:00:00 2001 From: Mikael Andersson Date: Wed, 9 Nov 2022 17:16:39 +0100 Subject: [PATCH 3/9] test: add getEventName helper function --- tests/helper.test.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/helper.test.ts b/tests/helper.test.ts index d56b50be5..4b23a5f67 100644 --- a/tests/helper.test.ts +++ b/tests/helper.test.ts @@ -18,6 +18,10 @@ export function setCommitSha(sha: string) { process.env['GITHUB_SHA'] = sha; } +export function getEventName(eventName: string) { + process.env['GITHUB_EVENT_NAME'] = eventName; +} + export function setInput(key: string, value: string) { process.env[`INPUT_${key.toUpperCase()}`] = value; } From a2cc5b12eb5a8b2e841accb8e34ae6a742a2955d Mon Sep 17 00:00:00 2001 From: Mikael Andersson Date: Wed, 9 Nov 2022 17:27:53 +0100 Subject: [PATCH 4/9] fix: logic for determining prerelease Logic to determine prerelease based on if the triggering event was a pull request or if run on prerelease branch. --- src/action.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/action.ts b/src/action.ts index 49692d63a..ca014216a 100644 --- a/src/action.ts +++ b/src/action.ts @@ -64,7 +64,7 @@ export default async function main() { .split(',') .some((branch) => currentBranch.match(branch)); const isPullRequest = isPr(GITHUB_EVENT_NAME); - const isPrerelease = !isReleaseBranch && !isPullRequest && isPreReleaseBranch; + const isPrerelease = !isReleaseBranch && (isPullRequest || isPreReleaseBranch); // Sanitize identifier according to // https://semver.org/#backusnaur-form-grammar-for-valid-semver-versions From 4e0d5db8d3841f5eb81a4afcaaf7f9f98c5f5305 Mon Sep 17 00:00:00 2001 From: Mikael Andersson Date: Wed, 9 Nov 2022 17:36:23 +0100 Subject: [PATCH 5/9] test: get event name for creating tags Use jest to mock implementation of tag creation for push events. --- tests/action.test.ts | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/tests/action.test.ts b/tests/action.test.ts index 413e7bfae..117ed2d9a 100644 --- a/tests/action.test.ts +++ b/tests/action.test.ts @@ -769,6 +769,10 @@ describe('github-tag-action', () => { .spyOn(utils, 'getCommits') .mockImplementation(async (sha) => commits); + jest + .spyOn(utils,'getEventName') + .mockImplementation('push'); + const validTags = [ { name: 'v1.2.3', @@ -805,7 +809,9 @@ describe('github-tag-action', () => { jest .spyOn(utils, 'getCommits') .mockImplementation(async (sha) => commits); - + jest + .spyOn(utils,'getEventName') + .mockImplementation('push'); const validTags = [ { name: 'v1.2.3', @@ -846,7 +852,10 @@ describe('github-tag-action', () => { jest .spyOn(utils, 'getCommits') .mockImplementation(async (sha) => commits); - + jest + .spyOn(utils,'getEventName') + .mockImplementation('push'); + const validTags = [ { name: 'v1.2.3', From 23b9f1df973d9f8afe1d974c2c98d32918f629b2 Mon Sep 17 00:00:00 2001 From: Mikael Andersson Date: Wed, 9 Nov 2022 17:38:38 +0100 Subject: [PATCH 6/9] test: fix input for mock implementation of getEventName --- tests/action.test.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/action.test.ts b/tests/action.test.ts index 117ed2d9a..d3523e41a 100644 --- a/tests/action.test.ts +++ b/tests/action.test.ts @@ -771,7 +771,7 @@ describe('github-tag-action', () => { jest .spyOn(utils,'getEventName') - .mockImplementation('push'); + .mockImplementation((eventName) => 'push'); const validTags = [ { @@ -811,7 +811,7 @@ describe('github-tag-action', () => { .mockImplementation(async (sha) => commits); jest .spyOn(utils,'getEventName') - .mockImplementation('push'); + .mockImplementation((eventName) => 'push'); const validTags = [ { name: 'v1.2.3', @@ -854,8 +854,8 @@ describe('github-tag-action', () => { .mockImplementation(async (sha) => commits); jest .spyOn(utils,'getEventName') - .mockImplementation('push'); - + .mockImplementation((eventName) => 'push'); + const validTags = [ { name: 'v1.2.3', From 32a30c0ad5cabb50337f4e3937d780bf5c96399b Mon Sep 17 00:00:00 2001 From: Mikael Andersson Date: Wed, 9 Nov 2022 17:47:42 +0100 Subject: [PATCH 7/9] test: use setEventName in beforeAll --- tests/action.test.ts | 13 +++---------- tests/helper.test.ts | 2 +- 2 files changed, 4 insertions(+), 11 deletions(-) diff --git a/tests/action.test.ts b/tests/action.test.ts index d3523e41a..3646a84d0 100644 --- a/tests/action.test.ts +++ b/tests/action.test.ts @@ -8,6 +8,7 @@ import { setCommitSha, setInput, setRepository, + setEventName } from './helper.test'; jest.spyOn(core, 'debug').mockImplementation(() => {}); @@ -33,6 +34,7 @@ describe('github-tag-action', () => { jest.clearAllMocks(); setBranch('master'); setCommitSha('79e0ea271c26aa152beef77c3275ff7b8f8d8274'); + setEventName('push'); loadDefaultInputs(); }); @@ -769,10 +771,6 @@ describe('github-tag-action', () => { .spyOn(utils, 'getCommits') .mockImplementation(async (sha) => commits); - jest - .spyOn(utils,'getEventName') - .mockImplementation((eventName) => 'push'); - const validTags = [ { name: 'v1.2.3', @@ -809,9 +807,7 @@ describe('github-tag-action', () => { jest .spyOn(utils, 'getCommits') .mockImplementation(async (sha) => commits); - jest - .spyOn(utils,'getEventName') - .mockImplementation((eventName) => 'push'); + const validTags = [ { name: 'v1.2.3', @@ -852,9 +848,6 @@ describe('github-tag-action', () => { jest .spyOn(utils, 'getCommits') .mockImplementation(async (sha) => commits); - jest - .spyOn(utils,'getEventName') - .mockImplementation((eventName) => 'push'); const validTags = [ { diff --git a/tests/helper.test.ts b/tests/helper.test.ts index 4b23a5f67..99cf5feb1 100644 --- a/tests/helper.test.ts +++ b/tests/helper.test.ts @@ -18,7 +18,7 @@ export function setCommitSha(sha: string) { process.env['GITHUB_SHA'] = sha; } -export function getEventName(eventName: string) { +export function setEventName(eventName: string) { process.env['GITHUB_EVENT_NAME'] = eventName; } From 88f5cdc2cc2a1a51be89e5eae14828118a8adf29 Mon Sep 17 00:00:00 2001 From: Mikael Andersson Date: Wed, 9 Nov 2022 17:58:20 +0100 Subject: [PATCH 8/9] fix: prettier action.ts --- src/action.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/action.ts b/src/action.ts index ca014216a..776f5cb9d 100644 --- a/src/action.ts +++ b/src/action.ts @@ -64,7 +64,9 @@ export default async function main() { .split(',') .some((branch) => currentBranch.match(branch)); const isPullRequest = isPr(GITHUB_EVENT_NAME); - const isPrerelease = !isReleaseBranch && (isPullRequest || isPreReleaseBranch); + const isPrerelease = !isReleaseBranch && ( + isPullRequest || isPreReleaseBranch + ); // Sanitize identifier according to // https://semver.org/#backusnaur-form-grammar-for-valid-semver-versions From c5a664233bc50ce79451aeae1babfc57d1d135f4 Mon Sep 17 00:00:00 2001 From: Mikael Andersson Date: Wed, 9 Nov 2022 18:49:36 +0100 Subject: [PATCH 9/9] fix: prettier on action and action test --- src/action.ts | 7 +++---- tests/action.test.ts | 2 +- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/action.ts b/src/action.ts index 776f5cb9d..6af5c1c27 100644 --- a/src/action.ts +++ b/src/action.ts @@ -45,7 +45,7 @@ export default async function main() { return; } - if(!GITHUB_EVENT_NAME) { + if (!GITHUB_EVENT_NAME) { core.setFailed('Missing GITHUB_EVENT_NAME'); return; } @@ -64,9 +64,8 @@ export default async function main() { .split(',') .some((branch) => currentBranch.match(branch)); const isPullRequest = isPr(GITHUB_EVENT_NAME); - const isPrerelease = !isReleaseBranch && ( - isPullRequest || isPreReleaseBranch - ); + const isPrerelease = + !isReleaseBranch && (isPullRequest || isPreReleaseBranch); // Sanitize identifier according to // https://semver.org/#backusnaur-form-grammar-for-valid-semver-versions diff --git a/tests/action.test.ts b/tests/action.test.ts index 3646a84d0..26709341b 100644 --- a/tests/action.test.ts +++ b/tests/action.test.ts @@ -8,7 +8,7 @@ import { setCommitSha, setInput, setRepository, - setEventName + setEventName, } from './helper.test'; jest.spyOn(core, 'debug').mockImplementation(() => {});