From 53c57d324476885e99bbec8a46f4919ccdc17979 Mon Sep 17 00:00:00 2001 From: Jongsun Suh Date: Mon, 11 Dec 2023 09:05:30 -0800 Subject: [PATCH] [update-changelog] Fix bug introduced in #158 - `updateChangelog` returns results even if `isReleaseCandidate` is false --- src/update-changelog.ts | 43 ++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 22 deletions(-) diff --git a/src/update-changelog.ts b/src/update-changelog.ts index 06f1d31..583c1fa 100644 --- a/src/update-changelog.ts +++ b/src/update-changelog.ts @@ -197,6 +197,11 @@ export async function updateChangelog({ tagPrefixes = ['v'], formatter = undefined, }: UpdateChangelogOptions): Promise { + if (isReleaseCandidate && !currentVersion) { + throw new Error( + `A version must be specified if 'isReleaseCandidate' is set.`, + ); + } const changelog = parseChangelog({ changelogContent, repoUrl, @@ -233,14 +238,8 @@ export async function updateChangelog({ return undefined; } - if (isReleaseCandidate) { - if (!currentVersion) { - throw new Error( - `A version must be specified if 'isReleaseCandidate' is set.`, - ); - } - - if (mostRecentTag === `${tagPrefixes[0]}${currentVersion ?? ''}`) { + if (isReleaseCandidate && currentVersion) { + if (mostRecentTag === `${tagPrefixes[0]}${currentVersion}`) { throw new Error( `Current version already has a tag ('${mostRecentTag}'), which is unexpected for a release candidate.`, ); @@ -258,22 +257,22 @@ export async function updateChangelog({ if (hasUnreleasedChanges) { changelog.migrateUnreleasedChangesToRelease(currentVersion); } + } - const newChangeEntries = newCommits.map(({ prNumber, description }) => { - if (prNumber) { - const suffix = `([#${prNumber}](${repoUrl}/pull/${prNumber}))`; - return `${description} ${suffix}`; - } - return description; - }); - - for (const description of newChangeEntries.reverse()) { - changelog.addChange({ - version: isReleaseCandidate ? currentVersion : undefined, - category: ChangeCategory.Uncategorized, - description, - }); + const newChangeEntries = newCommits.map(({ prNumber, description }) => { + if (prNumber) { + const suffix = `([#${prNumber}](${repoUrl}/pull/${prNumber}))`; + return `${description} ${suffix}`; } + return description; + }); + + for (const description of newChangeEntries.reverse()) { + changelog.addChange({ + version: isReleaseCandidate ? currentVersion : undefined, + category: ChangeCategory.Uncategorized, + description, + }); } return changelog.toString();