Skip to content

Commit

Permalink
feat: add prefix match tag input option
Browse files Browse the repository at this point in the history
  • Loading branch information
Guillaume MOCQUET committed May 10, 2022
1 parent 92dc56e commit a304100
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 3 deletions.
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
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

0 comments on commit a304100

Please sign in to comment.