Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion .github/scripts/merge-previous-release-changelog.mts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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}.`,
Expand Down
32 changes: 32 additions & 0 deletions .github/scripts/tests/merge-previous-releases.test.mts
Original file line number Diff line number Diff line change
Expand Up @@ -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`),
);
});
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down
Loading