diff --git a/.github/scripts/merge-previous-release-changelog.mts b/.github/scripts/merge-previous-release-changelog.mts index b7113d57..48a6692a 100644 --- a/.github/scripts/merge-previous-release-changelog.mts +++ b/.github/scripts/merge-previous-release-changelog.mts @@ -135,6 +135,30 @@ function insertReleaseReference( return `${changelog.slice(0, index)}${reference}${lineEnding}${changelog.slice(index)}`; } +/** + * Points the `[Unreleased]` compare link at a newly restored release when the + * destination's link still starts from an older one. + * @param changelog - The destination changelog contents. + * @param version - The release that was restored. + * @returns The updated changelog, unchanged when the link is missing, + * ambiguous, or not a `compare/vX.Y.Z...HEAD` link. + */ +function updateUnreleasedReference(changelog: string, version: string): string { + const pattern = + /^(\[Unreleased\]: .*\/compare\/v?)(\d+\.\d+\.\d+)(\.\.\.HEAD)$/gmu; + const matches = [...changelog.matchAll(pattern)]; + const [match] = matches; + const base = match?.[2]; + if (matches.length !== 1 || !base || compareSemver(version, base) <= 0) { + return changelog; + } + return changelog.replace( + pattern, + (_whole, prefix: string, _base: string, suffix: string) => + `${prefix}${version}${suffix}`, + ); +} + /** * Copies a finalized release section from a source branch's changelog into * the destination changelog, preserving the destination on any ambiguity. @@ -182,12 +206,13 @@ function main(): void { version, lineEnding, ); - const merged = insertReleaseReference( + const withReference = insertReleaseReference( withSection, getReleaseReference(source, version), version, lineEnding, ); + const merged = updateUnreleasedReference(withReference, version); writeFileSync('CHANGELOG.md', merged); console.log( `Inserted finalized ${version} changelog history from ${sourceRef}.`, diff --git a/.github/scripts/tests/merge-previous-releases.test.mts b/.github/scripts/tests/merge-previous-releases.test.mts index 75a81525..dbe69fb4 100644 --- a/.github/scripts/tests/merge-previous-releases.test.mts +++ b/.github/scripts/tests/merge-previous-releases.test.mts @@ -173,3 +173,35 @@ test('preserves destination changelog when it already has the release section', assert.equal(fixture.readChangelog(), destinationChangelog); }); + +test('moves the Unreleased link to the restored release', () => { + const compare = 'https://example.com/compare/'; + const fixture = createReleaseMergeFixture({ + sourceChangelog: `# Changelog\n\n## [Unreleased]\n\n## [13.54.0]\n\n- Released entry (#2)\n\n## [13.53.0]\n\n- Existing entry (#1)\n\n[Unreleased]: ${compare}v13.54.0...HEAD\n[13.54.0]: ${compare}v13.53.0...v13.54.0\n[13.53.0]: ${compare}v13.52.0...v13.53.0\n`, + destinationChangelog: `# Changelog\n\n## [Unreleased]\n\n## [13.53.0]\n\n- Existing entry (#1)\n\n[Unreleased]: ${compare}v13.53.0...HEAD\n[13.53.0]: ${compare}v13.52.0...v13.53.0\n`, + }); + + fixture.run(); + + assert.equal( + fixture.readChangelog(), + `# Changelog\n\n## [Unreleased]\n\n## [13.54.0]\n\n- Released entry (#2)\n\n## [13.53.0]\n\n- Existing entry (#1)\n\n[Unreleased]: ${compare}v13.54.0...HEAD\n[13.54.0]: ${compare}v13.53.0...v13.54.0\n[13.53.0]: ${compare}v13.52.0...v13.53.0\n`, + ); +}); + +test('keeps an Unreleased link that already starts from a newer release', () => { + const compare = 'https://example.com/compare/'; + const destinationLinks = `[Unreleased]: ${compare}v13.55.0...HEAD\n[13.55.0]: ${compare}v13.53.0...v13.55.0\n[13.53.0]: ${compare}v13.52.0...v13.53.0\n`; + const fixture = createReleaseMergeFixture({ + sourceChangelog: `# Changelog\n\n## [Unreleased]\n\n## [13.54.0]\n\n- Released entry (#2)\n\n[Unreleased]: ${compare}v13.54.0...HEAD\n[13.54.0]: ${compare}v13.53.0...v13.54.0\n`, + destinationChangelog: `# Changelog\n\n## [Unreleased]\n\n## [13.55.0]\n\n- New entry (#4)\n\n## [13.53.0]\n\n- Existing entry (#1)\n\n${destinationLinks}`, + }); + + fixture.run(); + + assert.ok( + fixture + .readChangelog() + .includes(`[Unreleased]: ${compare}v13.55.0...HEAD\n`), + ); +}); diff --git a/CHANGELOG.md b/CHANGELOG.md index 825684f4..327df879 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed - Fall back to the `team-bots` label in `add-team-label` for bot authors missing from `topology.json` ([#327](https://github.com/MetaMask/github-tools/pull/327)) +- Point the `[Unreleased]` changelog link at a restored release in `merge-previous-releases` ([#326](https://github.com/MetaMask/github-tools/pull/326)) + - When the merge restores a release newer than the one the `[Unreleased]` compare link starts from, the link now starts from the restored release. ## [1.24.0]