Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add prefix match tag input option #129

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ jobs:
- **custom_tag** _(optional)_ - Custom tag name. If specified, it overrides bump settings.
- **create_annotated_tag** _(optional)_ - Boolean to create an annotated rather than a lightweight one (default: `false`).
- **tag_prefix** _(optional)_ - A prefix to the tag name (default: `v`).
- **prefix_match_tag** _(optional)_ - Enhance tag validity check: given tag mush MATCH with tag_prefix + SemVer notation. (default: `false`).
- **append_to_pre_release_tag** _(optional)_ - A suffix to the pre-release tag name (default: `<branch>`).

#### Customize the conventional commit messages & titles of changelog sections
Expand Down
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ inputs:
description: "A prefix to the tag name (default: `v`)."
required: false
default: "v"
prefix_match_tag:
description: "Enhance tag validity check: given tag mush MATCH with tag_prefix + SemVer notation."
required: false
default: "false"
append_to_pre_release_tag:
description: "A suffix to a pre-release tag name (default: `<branch>`)."
required: false
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "github-tag-action",
"version": "6.0.0",
"version": "6.1.0",
"private": true,
"description": "A GitHub Action to automatically bump and tag master, on merge, with the latest SemVer formatted version.",
"main": "lib/main.js",
Expand Down
4 changes: 3 additions & 1 deletion src/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export default async function main() {
| ReleaseType
| 'false';
const tagPrefix = core.getInput('tag_prefix');
const prefixMatchTag = core.getInput('prefix_match_tag');
const customTag = core.getInput('custom_tag');
const releaseBranches = core.getInput('release_branches');
const preReleaseBranches = core.getInput('pre_release_branches');
Expand Down Expand Up @@ -69,7 +70,8 @@ export default async function main() {

const validTags = await getValidTags(
prefixRegex,
/true/i.test(shouldFetchAllTags)
/true/i.test(shouldFetchAllTags),
/true/i.test(prefixMatchTag)
);
const latestTag = getLatestTag(validTags, prefixRegex, tagPrefix);
const latestPrereleaseTag = getLatestPrereleaseTag(
Expand Down
14 changes: 12 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ type Tags = Await<ReturnType<typeof listTags>>;

export async function getValidTags(
prefixRegex: RegExp,
shouldFetchAllTags: boolean
shouldFetchAllTags: boolean,
prefixMatchTag: boolean = false
) {
const tags = await listTags(shouldFetchAllTags);

Expand All @@ -20,8 +21,17 @@ export async function getValidTags(

invalidTags.forEach((name) => core.debug(`Found Invalid Tag: ${name}.`));

let prefixRegexMatchSemver: RegExp = RegExp(
prefixRegex.toString().slice(1, -1) + '[0-9]+',
'g'
);

const validTags = tags
.filter((tag) => valid(tag.name.replace(prefixRegex, '')))
.filter(
(tag) =>
valid(tag.name.replace(prefixRegex, '')) &&
(!prefixMatchTag || tag.name.match(prefixRegexMatchSemver))
)
.sort((a, b) =>
rcompare(a.name.replace(prefixRegex, ''), b.name.replace(prefixRegex, ''))
);
Expand Down
90 changes: 90 additions & 0 deletions tests/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,96 @@ describe('utils', () => {
expect(validTags).toHaveLength(1);
});

it('returns valid tags #2', async () => {
/*
* Given
*/
const testTags = [
{
name: 'v1.0.0',
commit: { sha: 'string', url: 'string' },
zipball_url: 'string',
tarball_url: 'string',
node_id: 'string',
},
{
name: '0.0.91',
commit: { sha: 'string', url: 'string' },
zipball_url: 'string',
tarball_url: 'string',
node_id: 'string',
},
];
const mockListTags = jest
.spyOn(github, 'listTags')
.mockImplementation(async () => testTags);

const regex_2 = /^/;

/*
* When
*/
const validTags = await getValidTags(regex_2, false, true);

/*
* Then
*/
expect(mockListTags).toHaveBeenCalled();
expect(validTags).toHaveLength(1);
expect(validTags[0]).toEqual({
name: '0.0.91',
commit: { sha: 'string', url: 'string' },
zipball_url: 'string',
tarball_url: 'string',
node_id: 'string',
});
});

it('returns valid tags #3', async () => {
/*
* Given
*/
const testTags = [
{
name: 'v1.0.0',
commit: { sha: 'string', url: 'string' },
zipball_url: 'string',
tarball_url: 'string',
node_id: 'string',
},
{
name: '1.0.91',
commit: { sha: 'string', url: 'string' },
zipball_url: 'string',
tarball_url: 'string',
node_id: 'string',
},
];
const mockListTags = jest
.spyOn(github, 'listTags')
.mockImplementation(async () => testTags);

const regex_2 = /^v/;

/*
* When
*/
const validTags = await getValidTags(regex_2, false, true);

/*
* Then
*/
expect(mockListTags).toHaveBeenCalled();
expect(validTags).toHaveLength(1);
expect(validTags[0]).toEqual({
name: 'v1.0.0',
commit: { sha: 'string', url: 'string' },
zipball_url: 'string',
tarball_url: 'string',
node_id: 'string',
});
});

it('returns sorted tags', async () => {
/*
* Given
Expand Down