Skip to content

Commit

Permalink
fix(release): ensure that invalid semver tags are skipped
Browse files Browse the repository at this point in the history
  • Loading branch information
fahslaj committed Oct 23, 2023
1 parent c379aca commit 3a24229
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
1 change: 1 addition & 0 deletions packages/nx/src/command-line/release/utils/git.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ jest.mock('./exec-command', () => ({
execCommand: jest.fn(() =>
Promise.resolve(`
x5.0.0
release/4.😐2.2
release/4.2.1
release/[email protected]
v4.0.1
Expand Down
15 changes: 12 additions & 3 deletions packages/nx/src/command-line/release/utils/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ function escapeRegExp(string) {
return string.replace(/[/\-\\^$*+?.()|[\]{}]/g, '\\$&');
}

// https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string
const SEMVER_REGEX =
/^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$/g;

export async function getLatestGitTagForPattern(
releaseTagPattern: string,
additionalInterpolationData = {}
Expand All @@ -61,12 +65,17 @@ export async function getLatestGitTagForPattern(
' ',
'(.+)'
)}`;
const matchingTags = tags.filter((tag) => !!tag.match(tagRegexp));
const matchingSemverTags = tags.filter(
(tag) =>
// Do the match against SEMVER_REGEX to ensure that we skip tags that aren't valid semver versions
!!tag.match(tagRegexp) && tag.match(tagRegexp)[1]?.match(SEMVER_REGEX)
);

if (!matchingTags.length) {
if (!matchingSemverTags.length) {
return null;
}
const [latestMatchingTag, version] = matchingTags[0].match(tagRegexp);

const [latestMatchingTag, version] = matchingSemverTags[0].match(tagRegexp);

return {
tag: latestMatchingTag,
Expand Down

0 comments on commit 3a24229

Please sign in to comment.