Skip to content

Commit

Permalink
Merge pull request #66 from manics/noref
Browse files Browse the repository at this point in the history
If no github ref is found optionally return a default tag
  • Loading branch information
consideRatio authored Apr 15, 2021
2 parents ee432d6 + 4bba581 commit 81e9059
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 10 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ The GitHub action's only output is named `tags` and is a JSON formatted list. Se
## Optional input parameters

- `prefix`: A string that each returned tag should be prefixed with, for example to tag a Docker container set this to `user/repository:`.
- `defaultTag`: If the tag output would be empty return this tag instead.
This can be useful for running a workflow in pull requests where no suitable git references are present.
`prefix` is _not_ automatically added.

## Example workflow

Expand Down
34 changes: 30 additions & 4 deletions __tests__/main.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ test("No other tags", async () => {
"owner",
"repo",
"refs/tags/0.0.1",
"",
""
);
expect(tags).toEqual(["0.0.1", "0.0", "0", "latest"]);
Expand All @@ -38,6 +39,7 @@ test("Is the latest tag", async () => {
"owner",
"repo",
"refs/tags/2.0.0",
"",
""
);
expect(tags).toEqual(["2.0.0", "2.0", "2", "latest"]);
Expand All @@ -63,6 +65,7 @@ test("Not the latest major tag", async () => {
"owner",
"repo",
"refs/tags/1.1.0",
"",
""
);
expect(tags).toEqual(["1.1.0", "1.1", "1"]);
Expand Down Expand Up @@ -91,6 +94,7 @@ test("Not the latest minor tag", async () => {
"owner",
"repo",
"refs/tags/2.2.1",
"",
""
);
expect(tags).toEqual(["2.2.1", "2.2"]);
Expand All @@ -116,6 +120,7 @@ test("Includes pre-releases", async () => {
"owner",
"repo",
"refs/tags/1.1.0-2",
"",
""
);
expect(tags).toEqual(["1.1.0-2", "1.1.0", "1.1", "1"]);
Expand All @@ -138,6 +143,7 @@ test("Includes pre-releases jump one", async () => {
"owner",
"repo",
"refs/tags/1.1.0-2",
"",
""
);
expect(tags).toEqual(["1.1.0-2", "1.1.0", "1.1", "1", "latest"]);
Expand All @@ -157,6 +163,7 @@ test("Includes pre-releases no jump", async () => {
"owner",
"repo",
"refs/tags/1.1.0-1",
"",
""
);
expect(tags).toEqual(["1.1.0-1", "1.1.0", "1.1", "1", "latest"]);
Expand All @@ -182,6 +189,7 @@ test("Handling of an outdated build number", async () => {
"owner",
"repo",
"refs/tags/1.1.0-1",
"",
""
);
expect(tags).toEqual(["1.1.0-1"]);
Expand All @@ -207,6 +215,7 @@ test("Handling build number comparisons numerically", async () => {
"owner",
"repo",
"refs/tags/1.1.0-10",
"",
""
);
expect(tags).toEqual(["1.1.0-10", "1.1.0", "1.1", "1"]);
Expand All @@ -219,28 +228,41 @@ test("Unsupported prerelease tag", async () => {
"owner",
"repo",
"refs/tags/2.0.0-rc1",
"",
""
);
expect(tags).toEqual(["2.0.0-rc1"]);
});

test("Not a tag", async () => {
await expect(
calculateTags("TOKEN", "owner", "repo", "something/else", "")
calculateTags("TOKEN", "owner", "repo", "something/else", "", "")
).rejects.toEqual(new Error("Not a tag or branch: something/else"));
});

test("Invalid semver tag", async () => {
await expect(
calculateTags("TOKEN", "owner", "repo", "refs/tags/v1", "")
calculateTags("TOKEN", "owner", "repo", "refs/tags/v1", "", "")
).rejects.toEqual(new Error("Invalid semver tag: v1"));
});

test("No ref", async () => {
const tags = await calculateTags("TOKEN", "owner", "repo", null, "");
const tags = await calculateTags("TOKEN", "owner", "repo", null, "", "");
expect(tags).toEqual([]);
});

test("No ref use default", async () => {
const tags = await calculateTags(
"TOKEN",
"owner",
"repo",
null,
"",
"default-tag"
);
expect(tags).toEqual(["default-tag"]);
});

test("Prefix", async () => {
const scope = nock("https://api.github.com")
.get("/repos/owner/repo/tags")
Expand All @@ -254,7 +276,8 @@ test("Prefix", async () => {
"owner",
"repo",
"refs/tags/0.0.1",
"prefix:"
"prefix:",
""
);
expect(tags).toEqual([
"prefix:0.0.1",
Expand All @@ -271,6 +294,7 @@ test("Branch", async () => {
"owner",
"repo",
"refs/heads/main",
"",
""
);
expect(tags).toEqual(["main"]);
Expand All @@ -295,6 +319,7 @@ test("Includes pre-releases one", async () => {
"owner",
"repo",
"refs/tags/1.1.0-1",
"",
""
);
expect(tags).toEqual(["1.1.0-1", "1.1.0", "1.1", "1", "latest"]);
Expand Down Expand Up @@ -323,6 +348,7 @@ test("Includes pre-releases one with new tag", async () => {
"owner",
"repo",
"refs/tags/1.1.0-1",
"",
""
);
expect(tags).toEqual(["1.1.0-1", "1.1.0", "1.1", "1"]);
Expand Down
7 changes: 6 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@ description: Calculate major and minor semver tags, e.g. for tagging containers
author: Simon Li
inputs:
githubToken:
required: true
required: false
description: GitHub token
default: ""
prefix:
required: false
description: Prefix each tag with this string, e.g. "docker/repository:"
default: ""
defaultTag:
required: false
description: If the tag output would be empty return this tag instead
default: ""
outputs:
tags:
description: List of calculated tags
Expand Down
9 changes: 7 additions & 2 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

9 changes: 7 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ function equalMajorMinorPatch(a, b) {
return a.major == b.major && a.minor == b.minor && a.patch == b.patch;
}

async function calculateTags(token, owner, repo, ref, prefix) {
async function calculateTags(token, owner, repo, ref, prefix, defaultTag) {
// About the parameters:
// - token is used to authenticate against the GitHub API that in turn is used
// to list tags for github.com/<owner>/<repo>.
Expand All @@ -32,6 +32,9 @@ async function calculateTags(token, owner, repo, ref, prefix) {

if (!ref) {
core.debug("No ref");
if (defaultTag) {
return [defaultTag];
}
return [];
}
if (ref.startsWith("refs/heads/")) {
Expand Down Expand Up @@ -121,14 +124,16 @@ async function run() {
// githubToken: ${{ secrets.GITHUB_TOKEN }}
const githubToken = core.getInput("githubToken");
const prefix = core.getInput("prefix");
const defaultTag = core.getInput("defaultTag");

core.debug(JSON.stringify(github.context));
const allTags = await calculateTags(
githubToken,
github.context.payload.repository.owner.login,
github.context.payload.repository.name,
github.context.payload.ref,
prefix
prefix,
defaultTag
);

core.info(allTags);
Expand Down

0 comments on commit 81e9059

Please sign in to comment.