Skip to content

Commit

Permalink
Merge pull request #89 from manics/branchRegex
Browse files Browse the repository at this point in the history
Add branchRegex for validating/excluding branch names
  • Loading branch information
consideRatio authored Jun 4, 2021
2 parents 3f32820 + ee80246 commit fcf518f
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 6 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ The GitHub action's only output is named `tags` and is a JSON formatted list. Se
- `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.
- `branchRegex`: If a branch name does not match this regex return `defaultTag` or empty instead.

## Output parameters

Expand Down Expand Up @@ -64,6 +65,7 @@ jobs:
with:
githubToken: ${{ secrets.GITHUB_TOKEN }}
prefix: "my-username/my-image-name:"
branchRegex: "^[^/]+$"

# https://github.com/docker/login-action
- name: Login to DockerHub
Expand Down
43 changes: 42 additions & 1 deletion __tests__/main.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ test("No other tags", async () => {
"repo",
"refs/tags/0.0.1",
"",
"",
""
);
expect(tags).toEqual(["0.0.1", "0.0", "0", "latest"]);
Expand All @@ -40,6 +41,7 @@ test("Is the latest tag", async () => {
"repo",
"refs/tags/2.0.0",
"",
"",
""
);
expect(tags).toEqual(["2.0.0", "2.0", "2", "latest"]);
Expand All @@ -66,6 +68,7 @@ test("Not the latest major tag", async () => {
"repo",
"refs/tags/1.1.0",
"",
"",
""
);
expect(tags).toEqual(["1.1.0", "1.1", "1"]);
Expand Down Expand Up @@ -95,6 +98,7 @@ test("Not the latest minor tag", async () => {
"repo",
"refs/tags/2.2.1",
"",
"",
""
);
expect(tags).toEqual(["2.2.1", "2.2"]);
Expand All @@ -121,6 +125,7 @@ test("Includes pre-releases", async () => {
"repo",
"refs/tags/1.1.0-2",
"",
"",
""
);
expect(tags).toEqual(["1.1.0-2", "1.1.0", "1.1", "1"]);
Expand All @@ -144,6 +149,7 @@ test("Includes pre-releases jump one", async () => {
"repo",
"refs/tags/1.1.0-2",
"",
"",
""
);
expect(tags).toEqual(["1.1.0-2", "1.1.0", "1.1", "1", "latest"]);
Expand All @@ -164,6 +170,7 @@ test("Includes pre-releases no jump", async () => {
"repo",
"refs/tags/1.1.0-1",
"",
"",
""
);
expect(tags).toEqual(["1.1.0-1", "1.1.0", "1.1", "1", "latest"]);
Expand All @@ -190,6 +197,7 @@ test("Handling of an outdated build number", async () => {
"repo",
"refs/tags/1.1.0-1",
"",
"",
""
);
expect(tags).toEqual(["1.1.0-1"]);
Expand All @@ -216,6 +224,7 @@ test("Handling build number comparisons numerically", async () => {
"repo",
"refs/tags/1.1.0-10",
"",
"",
""
);
expect(tags).toEqual(["1.1.0-10", "1.1.0", "1.1", "1"]);
Expand All @@ -229,6 +238,7 @@ test("Unsupported prerelease tag", async () => {
"repo",
"refs/tags/2.0.0-rc1",
"",
"",
""
);
expect(tags).toEqual(["2.0.0-rc1"]);
Expand Down Expand Up @@ -258,7 +268,8 @@ test("No ref use default", async () => {
"repo",
null,
"",
"default-tag"
"default-tag",
""
);
expect(tags).toEqual(["default-tag"]);
});
Expand All @@ -277,6 +288,7 @@ test("Prefix", async () => {
"repo",
"refs/tags/0.0.1",
"prefix:",
"",
""
);
expect(tags).toEqual([
Expand All @@ -295,11 +307,38 @@ test("Branch", async () => {
"repo",
"refs/heads/main",
"",
"",
""
);
expect(tags).toEqual(["main"]);
});

test("Branch doesn't match regex", async () => {
const tags = await calculateTags(
"TOKEN",
"owner",
"repo",
"refs/heads/branch/with/slash",
"",
"",
"^[^/]+$"
);
expect(tags).toEqual([]);
});

test("Branch doesn't match regex with default", async () => {
const tags = await calculateTags(
"TOKEN",
"owner",
"repo",
"refs/heads/branch/with/slash",
"prefix-ignored-by-defaultTag",
"default-tag",
"^[^/]+$"
);
expect(tags).toEqual(["default-tag"]);
});

test("Includes pre-releases one", async () => {
const scope = nock("https://api.github.com")
.get("/repos/owner/repo/tags")
Expand All @@ -320,6 +359,7 @@ test("Includes pre-releases one", async () => {
"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 @@ -349,6 +389,7 @@ test("Includes pre-releases one with new tag", async () => {
"repo",
"refs/tags/1.1.0-1",
"",
"",
""
);
expect(tags).toEqual(["1.1.0-1", "1.1.0", "1.1", "1"]);
Expand Down
5 changes: 5 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ inputs:
This can be useful for running a workflow in pull requests where no suitable git references are present.
`prefix` is _not_ automatically added.
default: ""
branchRegex:
required: false
description: |-
If a branch name does not match this regex return `defaultTag` or empty instead.
default: "^.+$"
outputs:
tags:
description: A JSON formatted list of calculated tags.
Expand Down
25 changes: 23 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.

25 changes: 23 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,20 @@ 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, defaultTag) {
function checkAgainstRegex(name, regexAllowed) {
const re = new RegExp(regexAllowed);
return re.test(name);
}

async function calculateTags(
token,
owner,
repo,
ref,
prefix,
defaultTag,
regexAllowed
) {
// 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 @@ -40,6 +53,12 @@ async function calculateTags(token, owner, repo, ref, prefix, defaultTag) {
if (ref.startsWith("refs/heads/")) {
const branch = ref.substring(11);
core.debug(`Branch: ${branch}`);
if (!checkAgainstRegex(branch, regexAllowed)) {
if (defaultTag) {
return [defaultTag];
}
return [];
}
return [`${prefix}${branch}`];
}
if (!ref.startsWith("refs/tags/")) {
Expand Down Expand Up @@ -125,6 +144,7 @@ async function run() {
const githubToken = core.getInput("githubToken");
const prefix = core.getInput("prefix");
const defaultTag = core.getInput("defaultTag");
const branchRegex = core.getInput("branchRegex");

core.debug(JSON.stringify(github.context));
const allTags = await calculateTags(
Expand All @@ -133,7 +153,8 @@ async function run() {
github.context.payload.repository.name,
github.context.payload.ref,
prefix,
defaultTag
defaultTag,
branchRegex
);

core.info(allTags);
Expand Down

0 comments on commit fcf518f

Please sign in to comment.