From d3cec0e99af7c4543ed2fc5a0843fa4bf309dcce Mon Sep 17 00:00:00 2001 From: Simon Li Date: Wed, 10 Feb 2021 15:58:18 +0000 Subject: [PATCH] Handle empty ref --- __tests__/main.test.js | 7 ++++++- src/index.js | 8 ++++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/__tests__/main.test.js b/__tests__/main.test.js index 1943626..4b46154 100644 --- a/__tests__/main.test.js +++ b/__tests__/main.test.js @@ -136,7 +136,7 @@ test("Unsupported prerelease tag", async () => { test("Not a tag", async () => { await expect( calculateTags("TOKEN", "owner", "repo", "something/else", "") - ).rejects.toEqual(new Error("Not a tag: something/else")); + ).rejects.toEqual(new Error("Not a tag or branch: something/else")); }); test("Invalid semver tag", async () => { @@ -145,6 +145,11 @@ test("Invalid semver tag", async () => { ).rejects.toEqual(new Error("Invalid semver tag: v1")); }); +test("No ref", async () => { + const tags = await calculateTags("TOKEN", "owner", "repo", null, ""); + expect(tags).toEqual([]); +}); + test("Prefix", async () => { const scope = nock("https://api.github.com") .get("/repos/owner/repo/tags") diff --git a/src/index.js b/src/index.js index d5f9735..f0d6010 100644 --- a/src/index.js +++ b/src/index.js @@ -14,13 +14,17 @@ function supportedPrerelease(pre) { async function calculateTags(token, owner, repo, ref, prefix) { core.debug(`ref: ${ref}`); + if (!ref) { + core.debug("No ref"); + return []; + } if (ref.startsWith("refs/heads/")) { const branch = ref.substring(11); - core.debug(`Branch: branch`); + core.debug(`Branch: ${branch}`); return [`${prefix}${branch}`]; } if (!ref.startsWith("refs/tags/")) { - throw new Error(`Not a tag: ${ref}`); + throw new Error(`Not a tag or branch: ${ref}`); } const currentTag = ref.substring(10);