Skip to content

Commit

Permalink
Handle empty ref
Browse files Browse the repository at this point in the history
  • Loading branch information
manics committed Feb 10, 2021
1 parent 98300ea commit d3cec0e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
7 changes: 6 additions & 1 deletion __tests__/main.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand All @@ -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")
Expand Down
8 changes: 6 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit d3cec0e

Please sign in to comment.