From 98b7698a6bd3d4824d251f85dedcd18c410178e0 Mon Sep 17 00:00:00 2001 From: Mikael Andersson Date: Wed, 9 Nov 2022 18:56:15 +0100 Subject: [PATCH] fix: use event name to set isPrerelease on pull requests (#1) Use the GITHUB_EVENT_NAME environment variable to determine if the triggering event of the workflow was a pull request. --- src/action.ts | 12 +++++++++--- src/utils.ts | 4 ++-- tests/action.test.ts | 2 ++ tests/helper.test.ts | 4 ++++ tests/utils.test.ts | 6 +++--- 5 files changed, 20 insertions(+), 8 deletions(-) diff --git a/src/action.ts b/src/action.ts index 2a073169e..6af5c1c27 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,8 +63,9 @@ export default async function main() { const isPreReleaseBranch = preReleaseBranches .split(',') .some((branch) => currentBranch.match(branch)); - const isPullRequest = isPr(GITHUB_REF); - const isPrerelease = !isReleaseBranch && !isPullRequest && isPreReleaseBranch; + const isPullRequest = isPr(GITHUB_EVENT_NAME); + const isPrerelease = + !isReleaseBranch && (isPullRequest || isPreReleaseBranch); // Sanitize identifier according to // https://semver.org/#backusnaur-form-grammar-for-valid-semver-versions 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/action.test.ts b/tests/action.test.ts index 413e7bfae..26709341b 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(); }); diff --git a/tests/helper.test.ts b/tests/helper.test.ts index d56b50be5..99cf5feb1 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 setEventName(eventName: string) { + process.env['GITHUB_EVENT_NAME'] = eventName; +} + export function setInput(key: string, value: string) { process.env[`INPUT_${key.toUpperCase()}`] = value; } diff --git a/tests/utils.test.ts b/tests/utils.test.ts index 417d8791e..eb04d9385 100644 --- a/tests/utils.test.ts +++ b/tests/utils.test.ts @@ -27,16 +27,16 @@ describe('utils', () => { expect(branch).toEqual('master'); }); - it('test if ref is PR', () => { + it('test if triggering event is PR', () => { /* * Given */ - const remoteRef = 'refs/pull/123/merge'; + const eventName = 'pull_request'; /* * When */ - const isPullRequest = utils.isPr(remoteRef); + const isPullRequest = utils.isPr(eventName); /* * Then