Skip to content

Commit d83cdbd

Browse files
author
Guillaume MOCQUET
committed
feat: add prefix match tag input option
1 parent 92dc56e commit d83cdbd

File tree

3 files changed

+105
-3
lines changed

3 files changed

+105
-3
lines changed

src/action.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export default async function main() {
2121
| ReleaseType
2222
| 'false';
2323
const tagPrefix = core.getInput('tag_prefix');
24+
const prefixMatchTag = core.getInput('prefix_match_tag');
2425
const customTag = core.getInput('custom_tag');
2526
const releaseBranches = core.getInput('release_branches');
2627
const preReleaseBranches = core.getInput('pre_release_branches');
@@ -69,7 +70,8 @@ export default async function main() {
6970

7071
const validTags = await getValidTags(
7172
prefixRegex,
72-
/true/i.test(shouldFetchAllTags)
73+
/true/i.test(shouldFetchAllTags),
74+
/true/i.test(prefixMatchTag)
7375
);
7476
const latestTag = getLatestTag(validTags, prefixRegex, tagPrefix);
7577
const latestPrereleaseTag = getLatestPrereleaseTag(

src/utils.ts

+12-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ type Tags = Await<ReturnType<typeof listTags>>;
1010

1111
export async function getValidTags(
1212
prefixRegex: RegExp,
13-
shouldFetchAllTags: boolean
13+
shouldFetchAllTags: boolean,
14+
prefixMatchTag: boolean = false
1415
) {
1516
const tags = await listTags(shouldFetchAllTags);
1617

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

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

24+
let prefixRegexMatchSemver: RegExp = RegExp(
25+
prefixRegex.toString().slice(1, -1) + '[0-9]+',
26+
'g'
27+
);
28+
2329
const validTags = tags
24-
.filter((tag) => valid(tag.name.replace(prefixRegex, '')))
30+
.filter(
31+
(tag) =>
32+
valid(tag.name.replace(prefixRegex, '')) &&
33+
(!prefixMatchTag || tag.name.match(prefixRegexMatchSemver))
34+
)
2535
.sort((a, b) =>
2636
rcompare(a.name.replace(prefixRegex, ''), b.name.replace(prefixRegex, ''))
2737
);

tests/utils.test.ts

+90
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,96 @@ describe('utils', () => {
8080
expect(validTags).toHaveLength(1);
8181
});
8282

83+
it('returns valid tags #2', async () => {
84+
/*
85+
* Given
86+
*/
87+
const testTags = [
88+
{
89+
name: 'v1.0.0',
90+
commit: { sha: 'string', url: 'string' },
91+
zipball_url: 'string',
92+
tarball_url: 'string',
93+
node_id: 'string',
94+
},
95+
{
96+
name: '0.0.91',
97+
commit: { sha: 'string', url: 'string' },
98+
zipball_url: 'string',
99+
tarball_url: 'string',
100+
node_id: 'string',
101+
},
102+
];
103+
const mockListTags = jest
104+
.spyOn(github, 'listTags')
105+
.mockImplementation(async () => testTags);
106+
107+
const regex_2 = /^/;
108+
109+
/*
110+
* When
111+
*/
112+
const validTags = await getValidTags(regex_2, false, true);
113+
114+
/*
115+
* Then
116+
*/
117+
expect(mockListTags).toHaveBeenCalled();
118+
expect(validTags).toHaveLength(1);
119+
expect(validTags[0]).toEqual({
120+
name: '0.0.91',
121+
commit: { sha: 'string', url: 'string' },
122+
zipball_url: 'string',
123+
tarball_url: 'string',
124+
node_id: 'string',
125+
});
126+
});
127+
128+
it('returns valid tags #3', async () => {
129+
/*
130+
* Given
131+
*/
132+
const testTags = [
133+
{
134+
name: 'v1.0.0',
135+
commit: { sha: 'string', url: 'string' },
136+
zipball_url: 'string',
137+
tarball_url: 'string',
138+
node_id: 'string',
139+
},
140+
{
141+
name: '1.0.91',
142+
commit: { sha: 'string', url: 'string' },
143+
zipball_url: 'string',
144+
tarball_url: 'string',
145+
node_id: 'string',
146+
},
147+
];
148+
const mockListTags = jest
149+
.spyOn(github, 'listTags')
150+
.mockImplementation(async () => testTags);
151+
152+
const regex_2 = /^v/;
153+
154+
/*
155+
* When
156+
*/
157+
const validTags = await getValidTags(regex_2, false, true);
158+
159+
/*
160+
* Then
161+
*/
162+
expect(mockListTags).toHaveBeenCalled();
163+
expect(validTags).toHaveLength(1);
164+
expect(validTags[0]).toEqual({
165+
name: 'v1.0.0',
166+
commit: { sha: 'string', url: 'string' },
167+
zipball_url: 'string',
168+
tarball_url: 'string',
169+
node_id: 'string',
170+
});
171+
});
172+
83173
it('returns sorted tags', async () => {
84174
/*
85175
* Given

0 commit comments

Comments
 (0)