Skip to content

Commit

Permalink
fix(semantic-release): only output last release when there is one
Browse files Browse the repository at this point in the history
This fails on the first release of a repo
  • Loading branch information
tagoro9 committed Oct 27, 2023
1 parent 883fda5 commit 44c3646
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 8 deletions.
12 changes: 7 additions & 5 deletions semantic-release/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,13 @@ export async function main() {
);
if (result) {
const { lastRelease, nextRelease } = result;
setOutput(OUTPUTS.last_release_version, lastRelease.version);
setOutput(
OUTPUTS.last_release_major_version,
semver.major(lastRelease.version),
);
if (lastRelease.version) {
setOutput(OUTPUTS.last_release_version, lastRelease.version);
setOutput(
OUTPUTS.last_release_major_version,
semver.major(lastRelease.version),
);
}
if (nextRelease) {
setOutput(OUTPUTS.new_release_published, "true");
setOutput(OUTPUTS.new_release_type, nextRelease.type);
Expand Down
44 changes: 41 additions & 3 deletions semantic-release/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ jest.unstable_mockModule("@actions/core", () => ({
setOutput: setOutputMock,
}));

const mockRelease = (
overrides: Partial<Awaited<ReturnType<typeof semanticRelease>>> = {},
) => {
type SemanticRelease = Awaited<ReturnType<typeof semanticRelease>>;

const mockRelease = (overrides: Partial<SemanticRelease> = {}) => {
const lastRelease = {
channels: [],
gitHead: "test",
Expand Down Expand Up @@ -147,6 +147,44 @@ describe("semantic-release", () => {
`);
});

test("doesn't set last release outputs if there is no last release", async () => {
mockNpmInstall();
mockRelease({ lastRelease: false } as unknown as SemanticRelease);
await callAction();
expect(setOutputMock.mock.calls).toMatchInlineSnapshot(`
[
[
"new-release-published",
"true",
],
[
"new-release-type",
"prerelease",
],
[
"new-release-notes",
"New Release notes",
],
[
"new-release-version",
"1.2.3-test",
],
[
"new-release-major-version",
1,
],
[
"new-release-minor-version",
2,
],
[
"new-release-patch-version",
3,
],
]
`);
});

test("runs semantic release with the extra options", async () => {
process.env.SEMANTIC_ACTION_BRANCHES = "test";
process.env.SEMANTIC_ACTION_CI = "true";
Expand Down

0 comments on commit 44c3646

Please sign in to comment.