Skip to content

Commit

Permalink
fix: use event name to set isPrerelease on pull requests (#1)
Browse files Browse the repository at this point in the history
Use the GITHUB_EVENT_NAME environment variable to determine if the triggering event of the workflow was a pull request.
  • Loading branch information
mikael-andersson91 authored Nov 9, 2022
1 parent dbd80d2 commit 98b7698
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 8 deletions.
12 changes: 9 additions & 3 deletions src/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 2 additions & 0 deletions tests/action.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
setCommitSha,
setInput,
setRepository,
setEventName,
} from './helper.test';

jest.spyOn(core, 'debug').mockImplementation(() => {});
Expand All @@ -33,6 +34,7 @@ describe('github-tag-action', () => {
jest.clearAllMocks();
setBranch('master');
setCommitSha('79e0ea271c26aa152beef77c3275ff7b8f8d8274');
setEventName('push');
loadDefaultInputs();
});

Expand Down
4 changes: 4 additions & 0 deletions tests/helper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
6 changes: 3 additions & 3 deletions tests/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 98b7698

Please sign in to comment.