Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use event name to set isPrerelease #1

Merged
merged 9 commits into from
Nov 9, 2022
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