From 128614ad8b661f91e7038d97c93e89fbe6c26857 Mon Sep 17 00:00:00 2001 From: Mario Campos Date: Fri, 18 Sep 2026 12:24:19 -0500 Subject: [PATCH] Simplify `getHeader` to operate on `ChangelogSection`s --- pr-checks/changelog.test.ts | 24 +++++++++++++++--------- pr-checks/changelog.ts | 10 ++++++---- 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/pr-checks/changelog.test.ts b/pr-checks/changelog.test.ts index b88f9b0c0..2e8712ea8 100755 --- a/pr-checks/changelog.test.ts +++ b/pr-checks/changelog.test.ts @@ -26,20 +26,26 @@ import { CHANGELOG_FILE } from "./config"; const testDate = new Date(2026, 7, 14); describe("getHeader", async () => { + function Section(headerLine: string): ChangelogSection { + return { + headerLine, + bodyLines: [], + }; + } await it("returns non-headers unchanged", () => { - assert.equal("foo", getHeader("foo")); - assert.equal("- bar", getHeader("- bar")); + assert.equal("foo", getHeader(Section("foo"))); + assert.equal("- bar", getHeader(Section("- bar"))); }); await it("strips octothorpes", async () => { - assert.equal("foo", getHeader("# foo")); - assert.equal("foo", getHeader("## foo")); - assert.equal("foo", getHeader("### foo")); - assert.equal("foo", getHeader("#### foo")); - assert.equal("foo", getHeader("##### foo")); - assert.equal("foo", getHeader("###### foo")); + assert.equal("foo", getHeader(Section("# foo"))); + assert.equal("foo", getHeader(Section("## foo"))); + assert.equal("foo", getHeader(Section("### foo"))); + assert.equal("foo", getHeader(Section("#### foo"))); + assert.equal("foo", getHeader(Section("##### foo"))); + assert.equal("foo", getHeader(Section("###### foo"))); }); await it("strips whitespace", async () => { - assert.equal("foo", getHeader("# foo ")); + assert.equal("foo", getHeader(Section("# foo "))); }); }); diff --git a/pr-checks/changelog.ts b/pr-checks/changelog.ts index e738f2a4a..fa992d6ed 100644 --- a/pr-checks/changelog.ts +++ b/pr-checks/changelog.ts @@ -33,9 +33,11 @@ export interface Changelog { sections: ChangelogSection[]; } -/** Returns the text of a CHANGELOG.md header (without the '## ' prefix). */ -export function getHeader(headerLine: string): string { - return headerLine.replace(/^#+\s+/, "").trimEnd(); +/** + * Returns the text of the header (without the '## ' prefix) of the given section. + * */ +export function getHeader(section: ChangelogSection): string { + return section.headerLine.replace(/^#+\s+/, "").trimEnd(); } /** Returns `date` formatted as `DD Mon YYYY`. */ @@ -147,7 +149,7 @@ export function addBodyLinesToUnreleasedSection( lines: string[], ) { const unreleasedSection = changelog.sections[0]; - if (getHeader(unreleasedSection.headerLine) !== UNRELEASED_PLACEHOLDER) { + if (getHeader(unreleasedSection) !== UNRELEASED_PLACEHOLDER) { throw Error("'[UNRELEASED]' is not the first section of 'CHANGELOG.md'"); }