diff --git a/pr-checks/changelog.test.ts b/pr-checks/changelog.test.ts index 2e8712ea8..8132e6576 100755 --- a/pr-checks/changelog.test.ts +++ b/pr-checks/changelog.test.ts @@ -108,25 +108,20 @@ describe("addBodyLinesToUnreleasedSection", async () => { }; } - await it("throws error if '[UNRELEASED]' section does not exist", async () => { - const emptyChangelog = newChangelogWithSections([]); - assert.throws(() => addBodyLinesToUnreleasedSection(emptyChangelog, [])); - - const releasedChangelog = newChangelogWithSections([ + await it("throws error if '[UNRELEASED]' section is not first", async () => { + const invalidChangelog = newChangelogWithSections([ { headerLine: "## Release 1.0.0", bodyLines: [], }, { - headerLine: "## Release 2.0.0", - bodyLines: [], - }, - { - headerLine: "## Release 3.0.0", + headerLine: `## ${UNRELEASED_PLACEHOLDER}`, bodyLines: [], }, ]); - assert.throws(() => addBodyLinesToUnreleasedSection(releasedChangelog, [])); + assert.throws(() => + addBodyLinesToUnreleasedSection(invalidChangelog, ["foo"]), + ); }); await it("overwrites 'No user facing changes.'", async () => { diff --git a/pr-checks/changelog.ts b/pr-checks/changelog.ts index fa992d6ed..e159a06a9 100644 --- a/pr-checks/changelog.ts +++ b/pr-checks/changelog.ts @@ -148,32 +148,21 @@ export function addBodyLinesToUnreleasedSection( changelog: Changelog, lines: string[], ) { + // Do nothing if there is nothing to insert. + if (lines.length === 0) return; + const unreleasedSection = changelog.sections[0]; if (getHeader(unreleasedSection) !== UNRELEASED_PLACEHOLDER) { throw Error("'[UNRELEASED]' is not the first section of 'CHANGELOG.md'"); } - let insertAtIndex = 0; - let deleteCount = 0; - - // If the section contains an empty line, preserve it -- insert afterward. - if ( - unreleasedSection.bodyLines.length > 0 && - unreleasedSection.bodyLines[0] === "" - ) { - insertAtIndex++; + if (unreleasedSection.bodyLines.includes(NO_CHANGES_STR)) { + unreleasedSection.bodyLines = ["", ...lines, ""]; + return; } - // If the section contains the stock message 'No user facing changes.' - if ( - lines.length > 0 && - unreleasedSection.bodyLines.length > insertAtIndex && - unreleasedSection.bodyLines[insertAtIndex].trim() === NO_CHANGES_STR - ) { - deleteCount++; // Delete the line by incrementing the delete marker. - } - - unreleasedSection.bodyLines.splice(insertAtIndex, deleteCount, ...lines); + // Insert `lines` after the first blank line. + unreleasedSection.bodyLines.splice(1, 0, ...lines); } /**