From e77aa0b7b57e0d1f9130e13f010ae7cd7a3e9022 Mon Sep 17 00:00:00 2001 From: Mark Stacey Date: Thu, 8 Apr 2021 17:53:00 -0230 Subject: [PATCH] Migrate unreleased changes in changelog (#10853) When updating the changelog for a release candidate, any unreleased changes are now migrated to the release header. Generally we don't make a habit of adding changes to the changelog prior to creating a release candidate, but if any are there we certainly don't want them duplicated. --- development/lib/changelog/changelog.js | 30 ++++++++++++++++++++ development/lib/changelog/updateChangelog.js | 10 ++++++- 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/development/lib/changelog/changelog.js b/development/lib/changelog/changelog.js index 07c0cd01c..b16dca200 100644 --- a/development/lib/changelog/changelog.js +++ b/development/lib/changelog/changelog.js @@ -233,6 +233,36 @@ class Changelog { } } + /** + * Migrate all unreleased changes to a release section. + * + * Changes are migrated in their existing categories, and placed above any + * pre-existing changes in that category. + * + * @param {Version} version - The release version to migrate unreleased + * changes to. + */ + migrateUnreleasedChangesToRelease(version) { + const releaseChanges = this._changes[version]; + if (!releaseChanges) { + throw new Error(`Specified release version does not exist: '${version}'`); + } + + const unreleasedChanges = this._changes[unreleased]; + + for (const category of Object.keys(unreleasedChanges)) { + if (releaseChanges[category]) { + releaseChanges[category] = [ + ...unreleasedChanges[category], + ...releaseChanges[category], + ]; + } else { + releaseChanges[category] = unreleasedChanges[category]; + } + } + this._changes[unreleased] = {}; + } + /** * Gets the metadata for all releases. * @returns {Array} The metadata for each release. diff --git a/development/lib/changelog/updateChangelog.js b/development/lib/changelog/updateChangelog.js index 2198b8f0a..ddc762fd6 100644 --- a/development/lib/changelog/updateChangelog.js +++ b/development/lib/changelog/updateChangelog.js @@ -127,7 +127,11 @@ async function updateChangelog({ ({ prNumber }) => !loggedPrNumbers.includes(prNumber), ); - if (newCommits.length === 0) { + const hasUnreleasedChanges = changelog.getUnreleasedChanges().length !== 0; + if ( + newCommits.length === 0 && + (!isReleaseCandidate || hasUnreleasedChanges) + ) { return undefined; } @@ -141,6 +145,10 @@ async function updateChangelog({ changelog.addRelease({ currentVersion }); } + if (isReleaseCandidate && hasUnreleasedChanges) { + changelog.migrateUnreleasedChangesToRelease(currentVersion); + } + const newChangeEntries = newCommits.map(({ prNumber, description }) => { if (prNumber) { const prefix = `[#${prNumber}](${repoUrl}/pull/${prNumber})`;