diff --git a/src/action.ts b/src/action.ts index 4f91232c..6dbfe331 100644 --- a/src/action.ts +++ b/src/action.ts @@ -53,9 +53,15 @@ export default async function main() { ? appendToPreReleaseTag : currentBranch; - const validTags = await getValidTags(); - const latestTag = getLatestTag(validTags); - const latestPrereleaseTag = getLatestPrereleaseTag(validTags, identifier); + const prefixRegex = new RegExp(`^${tagPrefix}`); + + const validTags = await getValidTags(prefixRegex); + const latestTag = getLatestTag(validTags, prefixRegex); + const latestPrereleaseTag = getLatestPrereleaseTag( + validTags, + identifier, + prefixRegex + ); const commits = await getCommits(latestTag.commit.sha); @@ -66,12 +72,15 @@ export default async function main() { } else { let previousTag: SemVer | null; if (!latestPrereleaseTag) { - previousTag = parse(latestTag.name); + previousTag = parse(latestTag.name.replace(prefixRegex, '')); } else { previousTag = parse( - gte(latestTag.name, latestPrereleaseTag.name) - ? latestTag.name - : latestPrereleaseTag.name + gte( + latestTag.name.replace(prefixRegex, ''), + latestPrereleaseTag.name.replace(prefixRegex, '') + ) + ? latestTag.name.replace(prefixRegex, '') + : latestPrereleaseTag.name.replace(prefixRegex, '') ); } diff --git a/src/utils.ts b/src/utils.ts index 1017e365..11c5605d 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -5,18 +5,20 @@ import DEFAULT_RELEASE_TYPES from '@semantic-release/commit-analyzer/lib/default import { compareCommits, listTags } from './github'; import { Await } from './ts'; -export async function getValidTags() { +export async function getValidTags(prefixRegex: RegExp) { const tags = await listTags(); - const invalidTags = tags - .map((tag) => tag.name) - .filter((name) => !valid(name)); + const invalidTags = tags.filter( + (tag) => !valid(tag.name.replace(prefixRegex, '')) + ); invalidTags.forEach((name) => core.debug(`Found Invalid Tag: ${name}.`)); const validTags = tags - .filter((tag) => valid(tag.name)) - .sort((a, b) => rcompare(a.name, b.name)); + .filter((tag) => valid(tag.name.replace(prefixRegex, ''))) + .sort((a, b) => + rcompare(a.name.replace(prefixRegex, ''), b.name.replace(prefixRegex, '')) + ); validTags.forEach((tag) => core.debug(`Found Valid Tag: ${tag.name}.`)); @@ -38,9 +40,12 @@ export function getBranchFromRef(ref: string) { return ref.replace('refs/heads/', ''); } -export function getLatestTag(tags: Await>) { +export function getLatestTag( + tags: Await>, + prefixRegex: RegExp +) { return ( - tags.find((tag) => !prerelease(tag.name)) || { + tags.find((tag) => !prerelease(tag.name.replace(prefixRegex, ''))) || { name: '0.0.0', commit: { sha: 'HEAD', @@ -51,11 +56,12 @@ export function getLatestTag(tags: Await>) { export function getLatestPrereleaseTag( tags: Await>, - identifier: string + identifier: string, + prefixRegex: RegExp ) { return tags - .filter((tag) => prerelease(tag.name)) - .find((tag) => tag.name.match(identifier)); + .filter((tag) => prerelease(tag.name.replace(prefixRegex, ''))) + .find((tag) => tag.name.replace(prefixRegex, '').match(identifier)); } export function mapCustomReleaseRules(customReleaseTypes: string) { diff --git a/tests/utils.test.ts b/tests/utils.test.ts index 2c558e9a..a2002852 100644 --- a/tests/utils.test.ts +++ b/tests/utils.test.ts @@ -6,6 +6,8 @@ import * as github from '../src/github'; jest.spyOn(core, 'debug').mockImplementation(() => {}); jest.spyOn(core, 'warning').mockImplementation(() => {}); +const regex = /^v/; + describe('utils', () => { it('extracts branch from ref', () => { /* @@ -51,7 +53,7 @@ describe('utils', () => { /* * When */ - const validTags = await getValidTags(); + const validTags = await getValidTags(regex); /* * Then @@ -101,7 +103,7 @@ describe('utils', () => { /* * When */ - const validTags = await getValidTags(); + const validTags = await getValidTags(regex); /* * Then