From ca50d45043368cb5e0cec8d602dfda97637f6b6e Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Sun, 21 Jul 2024 16:46:33 +0200 Subject: [PATCH] Add option to sort via semver before determin latest tag (#146) --- src/index.test.ts | 4 ++-- src/index.ts | 12 +++++++++--- src/utils/config.ts | 1 + src/utils/types.ts | 6 ++++++ 4 files changed, 18 insertions(+), 5 deletions(-) diff --git a/src/index.test.ts b/src/index.test.ts index 98e1f1d..03eace8 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -43,8 +43,8 @@ describe('index', () => { const git = getMockedGit(); const forge = getMockedForged(); - const latestTag = '2.0.1'; - const tags = ['1.0.3', latestTag]; + const latestTag = '1.0.4'; + const tags = ['1.0.3', '2.0.1', latestTag]; vi.spyOn(git, 'tags').mockImplementation(() => mockSimpleGitResponse({ all: tags, latest: latestTag })); const commits = [ diff --git a/src/index.ts b/src/index.ts index c5dfaa4..a196c5c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -78,13 +78,19 @@ export async function run({ git, forge, config }: { git: SimpleGit; forge: Forge } const tags = await git.tags(['--sort=-creatordate']); + let latestTag = tags.latest; - if (!tags.latest && tags.all.length > 0) { + if (tags.all.length > 0 && config.user.sortTags) { + const sortedTags = semver.rsort(tags.all.filter((tag) => semver.valid(tag))); + latestTag = sortedTags[0]; + } + + if (!latestTag && tags.all.length > 0) { console.log(c.yellow('# Latest tag not found, but tags exist, skipping.')); return; } - const latestTag = tags.latest || '0.0.0'; + latestTag = latestTag || '0.0.0'; if (tags.latest) { console.log('# Lastest tag is:', c.green(latestTag)); } else { @@ -101,7 +107,7 @@ export async function run({ git, forge, config }: { git: SimpleGit; forge: Forge }, ); - // if the lastest tag is an RC and the next version should be the actual release, + // if the latest tag is an RC and the next version should be the actual release, // we need to include all commits since the last non RC version and the release branch if (semver.prerelease(latestTag) !== null && !shouldBeRC) { const latestNonRCTags = tags.all diff --git a/src/utils/config.ts b/src/utils/config.ts index 7815c7f..3785fb2 100644 --- a/src/utils/config.ts +++ b/src/utils/config.ts @@ -65,6 +65,7 @@ export const defaultUserConfig: UserConfig = { skipLabels: ['skip-release', 'skip-changelog', 'regression'], skipCommitsWithoutPullRequest: true, commentOnReleasedPullRequests: true, + sortTags: true, }; export async function getConfig(): Promise { diff --git a/src/utils/types.ts b/src/utils/types.ts index 5bbd0e8..89e9997 100644 --- a/src/utils/types.ts +++ b/src/utils/types.ts @@ -108,6 +108,12 @@ export type UserConfig = Partial<{ * @default true */ commentOnReleasedPullRequests: boolean; + + /** + * Run semver sort over tags before detecting latest tag + * @default true + */ + sortTags: boolean; }>; export const defineConfig = (config: UserConfig) => config;