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: enable creation of versions with commit sha postfix #156

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- run: npm ci
- run: npm run test
- run: npm run check
Expand Down
34 changes: 25 additions & 9 deletions src/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import { generateNotes } from '@semantic-release/release-notes-generator';
import {
getBranchFromRef,
isPr,
isPrereleaseBranch,
getCommits,
getLatestPrereleaseTag,
getLatestTag,
getValidTags,
mapCustomReleaseRules,
mergeWithDefaultChangelogRules,
getIdentifier,
} from './utils';
import { createTag } from './github';
import { Await } from './ts';
Expand Down Expand Up @@ -38,13 +40,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 @@ -55,17 +62,22 @@ export default async function main() {
const isReleaseBranch = releaseBranches
.split(',')
.some((branch) => currentBranch.match(branch));
const isPreReleaseBranch = preReleaseBranches
.split(',')
.some((branch) => currentBranch.match(branch));
const isPullRequest = isPr(GITHUB_REF);
const isPreReleaseBranch = isPrereleaseBranch(
preReleaseBranches,
currentBranch
);
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
const identifier = (
appendToPreReleaseTag ? appendToPreReleaseTag : currentBranch
).replace(/[^a-zA-Z0-9-]/g, '-');
const identifier = getIdentifier(
appendToPreReleaseTag,
currentBranch,
isPullRequest,
isPrerelease,
commitRef
);

const prefixRegex = new RegExp(`^${tagPrefix}`);

Expand Down Expand Up @@ -181,7 +193,11 @@ export default async function main() {
return;
}

newVersion = incrementedVersion;
if (isPullRequest) {
newVersion = `${incrementedVersion}-${identifier}`;
} else {
newVersion = incrementedVersion;
}
}

core.info(`New version is ${newVersion}.`);
Expand Down
36 changes: 34 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,20 @@ 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 isPrereleaseBranch(
preReleaseBranches: string,
currentBranch: string
) {
if (preReleaseBranches) {
return preReleaseBranches
.split(',')
.some((branch) => currentBranch.match(branch));
}
return false;
}

export function getLatestTag(
Expand Down Expand Up @@ -139,3 +151,23 @@ export function mergeWithDefaultChangelogRules(

return Object.values(mergedRules).filter((rule) => !!rule.section);
}

export function getIdentifier(
appendToPreReleaseTag: string,
currentBranch: string,
isPullRequest: boolean,
isPrerelease: boolean,
commitRef: string
): string {
// On prerelease: Sanitize identifier according to
// https://semver.org/#backusnaur-form-grammar-for-valid-semver-versions
let identifier: string;
if (isPullRequest) {
// On pull request, use commit SHA for identifier
return commitRef.slice(0, 7).replace(/[^a-zA-Z0-9-]/g, '-');
}
identifier = (
appendToPreReleaseTag ? appendToPreReleaseTag : currentBranch
).replace(/[^a-zA-Z0-9-]/g, '-');
return identifier;
}
53 changes: 52 additions & 1 deletion 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 @@ -27,12 +28,14 @@ const mockSetOutput = jest
.mockImplementation(() => {});

const mockSetFailed = jest.spyOn(core, 'setFailed');
const commitSha = '79e0ea271c26aa152beef77c3275ff7b8f8d8274';

describe('github-tag-action', () => {
beforeEach(() => {
jest.clearAllMocks();
setBranch('master');
setCommitSha('79e0ea271c26aa152beef77c3275ff7b8f8d8274');
setCommitSha(commitSha);
setEventName('push');
loadDefaultInputs();
});

Expand Down Expand Up @@ -222,6 +225,7 @@ describe('github-tag-action', () => {
jest.clearAllMocks();
setBranch('release');
setInput('release_branches', 'release');
setEventName('push');
});

it('does create patch tag', async () => {
Expand Down Expand Up @@ -484,6 +488,7 @@ describe('github-tag-action', () => {
/*
* Then
*/

expect(mockCreateTag).not.toBeCalled();
expect(mockSetFailed).not.toBeCalled();
});
Expand Down Expand Up @@ -873,4 +878,50 @@ describe('github-tag-action', () => {
expect(mockSetFailed).not.toBeCalled();
});
});

describe('pull requests', () => {
beforeEach(() => {
jest.clearAllMocks();
setBranch('branch-with-my-first-fix');
setEventName('pull_request');
});

it('does create new version with commit sha suffix on pull request', async () => {
/*
* Given
*/
const commits = [{ message: 'fix: this is my first fix', hash: null }];
jest
.spyOn(utils, 'getCommits')
.mockImplementation(async (sha) => commits);

const validTags = [
{
name: 'v1.2.3',
commit: { sha: '012345', url: '' },
zipball_url: '',
tarball_url: 'string',
node_id: 'string',
},
];
jest
.spyOn(utils, 'getValidTags')
.mockImplementation(async () => validTags);

/*
* When
*/
await action();

/*
* Then
*/
expect(mockSetOutput).toHaveBeenCalledWith(
'new_version',
`1.2.4-${commitSha.slice(0, 7)}`
);
expect(mockCreateTag).not.toHaveBeenCalledWith();
expect(mockSetFailed).not.toBeCalled();
});
});
});
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