Skip to content

Commit

Permalink
Add option to sort via semver before determin latest tag (#146)
Browse files Browse the repository at this point in the history
  • Loading branch information
6543 authored Jul 21, 2024
1 parent d3edd51 commit ca50d45
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
12 changes: 9 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/utils/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Config> {
Expand Down
6 changes: 6 additions & 0 deletions src/utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

0 comments on commit ca50d45

Please sign in to comment.