Skip to content

Commit

Permalink
test for prerelease
Browse files Browse the repository at this point in the history
  • Loading branch information
smac89 committed Nov 22, 2023
1 parent ec2511f commit 5c1025f
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 50 deletions.
2 changes: 1 addition & 1 deletion scripts/releaser.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ fi

git stash
git fetch origin --tags
jq --arg version "$version" '.version = $version' package.json > package.json.tmp
jq --arg version "$version" '.version = $version' package.json >package.json.tmp
mv package.json.tmp package.json
yarn build
git add lib
Expand Down
40 changes: 15 additions & 25 deletions src/functions/public.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import SemVer from 'semver/classes/semver';
import semverGt from 'semver/functions/gt';
import major from 'semver/functions/major';
import valid from 'semver/functions/valid';

import { context } from '@actions/github';
import Private from '@actionstagger/functions/private';
import { preferences } from '@actionstagger/util';

import type { GitHub, LatestRef, TaggedRef } from './types';

Expand All @@ -15,7 +13,11 @@ namespace Functions {
* Check if this release is publically available and has been published
*/
export function isPublishedRelease(): boolean {
return Private.isPublicRelease() && context.payload.action === 'published';
return (
Private.isPublicRelease() &&
// TODO v3: Only check for context.payload.action === 'released'
(context.payload.action === 'published' || context.payload.action === 'released')
);
}

/**
Expand All @@ -26,42 +28,33 @@ namespace Functions {
}

/**
* Check if this event was a new tag push
* Check if this event was a new tag push without a prerelease
*/
export function isRefPush(): boolean {
return Private.isBranchPush() || Private.isTagPush();
export function isPublicRefPush(): boolean {
return Private.isRefPush() && !Private.isPreReleaseRef();
}

/**
* Get the tag version being published via push or release event
*
* @returns The tag being published
*/
export function getPublishRefVersion(): SemVer {
return Functions.isRefPush()
? Private.getPushRefVersion(Functions.getPreferredRef())
: Private.getReleaseTag();
export function getPublishRefVersion() {
return Private.isRefPush() ? Private.getPushRefVersion() : Private.getReleaseTag();
}

/**
* Checks if the tag version of the pushed tag/release has valid version
*/
export function isSemVersionedTag(): boolean {
export function isSemVersionedRef(): boolean {
return valid(Functions.getPublishRefVersion()) !== null;
}

/**
* Get the major number of the release tag
*/
export function majorVersion(): number {
return major(Functions.getPublishRefVersion());
}

/**
* Returns the appropriate ref depending on the input preferences
*/
export function getPreferredRef(): 'heads' | 'tags' {
return preferences.preferBranchRelease ? 'heads' : 'tags';
return major(Functions.getPublishRefVersion()!);
}

/**
Expand All @@ -79,14 +72,11 @@ namespace Functions {
* @param {GitHub} github The octokit client instance
*/
export async function findLatestRef(github: GitHub): Promise<LatestRef> {
let [majorLatest, majorSha] = [Functions.getPublishRefVersion(), process.env.GITHUB_SHA!];
let [majorLatest, majorSha] = [Functions.getPublishRefVersion()!, process.env.GITHUB_SHA!];
let [repoLatest, repoSha] = [majorLatest, majorSha];

const major = majorLatest.major;
for await (const [semverRef, shaId] of Private.listAllRefs(
github,
Functions.getPreferredRef()
)) {
for await (const [semverRef, shaId] of Private.listAllRefs(github)) {
if (semverRef.major === major && semverGt(semverRef, majorLatest)) {
[majorLatest, majorSha] = [semverRef, shaId];
}
Expand All @@ -113,7 +103,7 @@ namespace Functions {
): Promise<TaggedRef> {
const major = Functions.majorVersion();

const ref = `${Functions.getPreferredRef()}/v${major}`;
const ref = `${Private.getPreferredRef()}/v${major}`;
await Private.createRef(github, ref);

if (publishLatest) {
Expand Down
10 changes: 5 additions & 5 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,23 @@ import {
findLatestRef,
getPublishRefVersion,
isEditedRelease,
isPublicRefPush,
isPublishedRelease,
isRefPush,
isSemVersionedTag,
isSemVersionedRef,
} from '@actionstagger/functions';
import type { GitHub } from '@actionstagger/functions/types';
import { preferences } from '@actionstagger/util';

export default async function main(): Promise<void> {
if (!(isRefPush() || isPublishedRelease() || isEditedRelease())) {
if (!(isPublicRefPush() || isPublishedRelease() || isEditedRelease())) {
core.info(
'This action should only be used in a release context or when creating a new tag or branch'
);
ifErrorSubmitBug();
return;
}

if (!isSemVersionedTag()) {
if (!isSemVersionedRef()) {
core.info('This action can only operate on semantically versioned tags');
core.info('See: https://semver.org/');
ifErrorSubmitBug();
Expand All @@ -32,7 +32,7 @@ export default async function main(): Promise<void> {

const octokit = createOctoKit();
const { repoLatest, majorLatest } = await findLatestRef(octokit);
const releaseVer = getPublishRefVersion();
const releaseVer = getPublishRefVersion()!;

if (semverGte(releaseVer, majorLatest.name)) {
const publishLatest = preferences.publishLatest && semverGte(releaseVer, repoLatest.name);
Expand Down
59 changes: 40 additions & 19 deletions tests/functions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe('Functions', () => {
jest.replaceProperty(process, 'env', {
INPUT_PREFER_BRANCH_RELEASES: preferbranchReleases,
});
await import('@actionstagger/functions/public').then(({ default: { getPreferredRef } }) =>
await import('@actionstagger/functions/private').then(({ default: { getPreferredRef } }) =>
expect(getPreferredRef()).toBe(expected)
);
});
Expand Down Expand Up @@ -64,9 +64,8 @@ describe('Functions', () => {
});

test(`on ${eventName} when INPUT_PREFER_BRANCH_RELEASES=${preferbranchReleases}, pushed ref=${githubRef}, returns=${expected}`, async () => {
await import('@actionstagger/functions/public').then(
({ default: { getPublishRefVersion } }) =>
expect(getPublishRefVersion().version).toBe(expected)
await import('@actionstagger/functions').then(({ getPublishRefVersion }) =>
expect(getPublishRefVersion()!.version).toBe(expected)
);
});
});
Expand Down Expand Up @@ -109,13 +108,38 @@ describe('Functions', () => {
});

test(`on ${eventName}, release tag=${tagName}, returns=${expected}`, async () => {
await import('@actionstagger/functions/public').then(
({ default: { getPublishRefVersion } }) =>
expect(getPublishRefVersion().version).toBe(expected)
await import('@actionstagger/functions').then(({ getPublishRefVersion }) =>
expect(getPublishRefVersion()!.version).toBe(expected)
);
});
});

describe.each([
{ eventName: 'release', action: 'published', expected: true },
{ eventName: 'release', action: 'edited', expected: false },
])('#isPublishedRelease()', ({ eventName, action, expected }) => {
const dir = fs.mkdtempSync(os.tmpdir() + '/jest-release');

beforeEach(() => {
const releaseEvent = {
action,
release: {
tag_name: 'v3.0.0',
},
};
fs.writeFileSync(`${dir}/event.json`, JSON.stringify(releaseEvent));
jest.replaceProperty(process, 'env', {
GITHUB_EVENT_PATH: `${dir}/event.json`,
GITHUB_EVENT_NAME: eventName,
});
});

test('Should have been set to true', async () =>
await import('@actionstagger/functions').then(({ isPublishedRelease }) =>
expect(isPublishedRelease()).toBe(expected)
));
});

describe.each([
{
pushedRef: 'v3.2.2',
Expand Down Expand Up @@ -154,7 +178,7 @@ describe('Functions', () => {
const { default: semverParse } = await import('semver/functions/parse');
const semverTag = semverParse(pushedRef)!;

await import('@actionstagger/functions/public').then(functions => {
await import('@actionstagger/functions').then(functions => {
jest.spyOn(functions.default, 'getPublishRefVersion').mockReturnValue(semverTag);
});

Expand All @@ -174,9 +198,7 @@ describe('Functions', () => {
} exists and latest ref is ${repoLatest ?? 'unknown'}, returns [${expected.join(
', '
)}]`, async () => {
const {
default: { findLatestRef },
} = await import('@actionstagger/functions/public');
const { findLatestRef } = await import('@actionstagger/functions');
await findLatestRef(getOctokit('TEST_TOKEN')).then(({ repoLatest, majorLatest }) => {
expect([repoLatest.name, majorLatest.name]).toEqual(expected);
});
Expand All @@ -203,24 +225,23 @@ describe('Functions', () => {
jest.spyOn(functions.default, 'createRef').mockResolvedValue()
);

await import('@actionstagger/functions/public').then(functions =>
await import('@actionstagger/functions').then(functions =>
jest.spyOn(functions.default, 'getPublishRefVersion').mockReturnValue(semverTag)
);

await import('@actionstagger/functions/public').then(functions =>
await import('@actionstagger/functions/private').then(functions =>
jest.spyOn(functions.default, 'getPreferredRef').mockReturnValue('tags')
);
});

test(`when creating ref for ${refToCreate} and publishLatest=${publishLatest}, will create ${expectedRef} and${
publishLatest ? '' : ' not'
} publish latest tag`, async () => {
await import('@actionstagger/functions/public').then(
({ default: { createRequiredRefs } }) =>
createRequiredRefs(getOctokit('TEST_TOKEN'), publishLatest).then(({ ref, latest }) => {
expect(ref).toBe(expectedRef);
expect(latest).toBe(publishLatest);
})
await import('@actionstagger/functions').then(({ createRequiredRefs }) =>
createRequiredRefs(getOctokit('TEST_TOKEN'), publishLatest).then(({ ref, latest }) => {
expect(ref).toBe(expectedRef);
expect(latest).toBe(publishLatest);
})
);
});
}
Expand Down

0 comments on commit 5c1025f

Please sign in to comment.