Skip to content

Commit bb1dc54

Browse files
committed
Add CHANGELOG function addBodyLinesToUnreleasedSection
This will be used by the `pr-checks/changenotes.mts` script to "compile" the latest release entry of CHANGELOG.md.
1 parent 47d607e commit bb1dc54

2 files changed

Lines changed: 132 additions & 0 deletions

File tree

‎pr-checks/changelog.test.ts‎

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,17 @@ import * as fs from "node:fs";
99
import { describe, it } from "node:test";
1010

1111
import {
12+
addBodyLinesToUnreleasedSection,
13+
ChangelogSection,
1214
EMPTY_CHANGELOG,
1315
getHeader,
1416
getReleaseDateString,
17+
NO_CHANGES_STR,
1518
parseChangelog,
1619
processChangelogForBackports,
1720
renderChangelog,
1821
setVersionAndDate,
22+
UNRELEASED_PLACEHOLDER,
1923
} from "./changelog";
2024
import { CHANGELOG_FILE } from "./config";
2125

@@ -89,3 +93,78 @@ describe("processChangelogForBackports", async () => {
8993
assert.deepEqual(result.split("\n"), testChangelogResult.split("\n"));
9094
});
9195
});
96+
97+
describe("addBodyLinesToUnreleasedSection", async () => {
98+
function newChangelogWithSections(sections: ChangelogSection[]) {
99+
return {
100+
preamble: [],
101+
sections,
102+
};
103+
}
104+
105+
await it("throws error if '[UNRELEASED]' section does not exist", async () => {
106+
const emptyChangelog = newChangelogWithSections([]);
107+
assert.throws(() => addBodyLinesToUnreleasedSection(emptyChangelog, []));
108+
109+
const releasedChangelog = newChangelogWithSections([
110+
{
111+
headerLine: "## Release 1.0.0",
112+
bodyLines: [],
113+
},
114+
{
115+
headerLine: "## Release 2.0.0",
116+
bodyLines: [],
117+
},
118+
{
119+
headerLine: "## Release 3.0.0",
120+
bodyLines: [],
121+
},
122+
]);
123+
assert.throws(() => addBodyLinesToUnreleasedSection(releasedChangelog, []));
124+
});
125+
126+
await it("overwrites 'No user facing changes.'", async () => {
127+
const changelog = newChangelogWithSections([
128+
{
129+
headerLine: `## ${UNRELEASED_PLACEHOLDER}`,
130+
bodyLines: ["", NO_CHANGES_STR, ""],
131+
},
132+
]);
133+
134+
addBodyLinesToUnreleasedSection(changelog, ["- foo"]);
135+
136+
assert.equal(changelog.sections[0].bodyLines.length, 3);
137+
assert.deepEqual(changelog.sections[0].bodyLines, ["", "- foo", ""]);
138+
});
139+
140+
await it("does nothing if lines is empty", async () => {
141+
const changelog = newChangelogWithSections([
142+
{
143+
headerLine: `## ${UNRELEASED_PLACEHOLDER}`,
144+
bodyLines: ["", NO_CHANGES_STR, ""],
145+
},
146+
]);
147+
const changelogClone = structuredClone(changelog);
148+
149+
addBodyLinesToUnreleasedSection(changelog, []);
150+
151+
assert.deepEqual(changelog, changelogClone);
152+
});
153+
154+
await it("inserts a line", async () => {
155+
const changelog = newChangelogWithSections([
156+
{
157+
headerLine: `## ${UNRELEASED_PLACEHOLDER}`,
158+
bodyLines: ["", "- Added a new dependency.", ""],
159+
},
160+
]);
161+
const lineToInsert = "- foo";
162+
163+
addBodyLinesToUnreleasedSection(changelog, [lineToInsert]);
164+
165+
assert.equal(changelog.sections[0].bodyLines.length, 4);
166+
assert.ok(
167+
changelog.sections[0].bodyLines.some((line) => line === lineToInsert),
168+
);
169+
});
170+
});

‎pr-checks/changelog.ts‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,59 @@ export function parseChangelog(content: string): Changelog {
132132
return { preamble, sections };
133133
}
134134

135+
/**
136+
* Inserts the changenotes `notes` under the `[UNRELEASED]` section of `changelog`.
137+
* If the section contains the stock message {@link NO_CHANGES_STR}, then
138+
* `notes` will be inserted in place and the stock message will be deleted.
139+
*
140+
* This function will throw an exception if `[UNRELEASED]` does not exist.
141+
*
142+
* @param changelog The CHANGELOG object to modify.
143+
* @param lines The changenotes to insert.
144+
*/
145+
export function addBodyLinesToUnreleasedSection(
146+
changelog: Changelog,
147+
lines: string[],
148+
) {
149+
// Find the '[UNRELEASED]' section.
150+
let unreleasedSection: ChangelogSection | undefined;
151+
for (const section of changelog.sections) {
152+
if (getHeader(section.headerLine) === UNRELEASED_PLACEHOLDER) {
153+
unreleasedSection = section;
154+
break;
155+
}
156+
}
157+
158+
// Ensure that the '[UNRELEASED]' section exists first.
159+
if (unreleasedSection === undefined) {
160+
throw Error(
161+
"Cannot put changenotes into CHANGELOG.md's '[UNRELEASED]' section because it does not exist",
162+
);
163+
}
164+
165+
let insertAtIndex = 0;
166+
let deleteCount = 0;
167+
168+
// If the section contains an empty line, preserve it -- insert afterward.
169+
if (
170+
unreleasedSection.bodyLines.length > 0 &&
171+
unreleasedSection.bodyLines[0] === ""
172+
) {
173+
insertAtIndex++;
174+
}
175+
176+
// If the section contains the stock message 'No user facing changes.'
177+
if (
178+
lines.length > 0 &&
179+
unreleasedSection.bodyLines.length > insertAtIndex &&
180+
unreleasedSection.bodyLines[insertAtIndex].trim() === NO_CHANGES_STR
181+
) {
182+
deleteCount++; // Delete the line by incrementing the delete marker.
183+
}
184+
185+
unreleasedSection.bodyLines.splice(insertAtIndex, deleteCount, ...lines);
186+
}
187+
135188
/**
136189
* Combines an array of lines into a single string by adding line breaks.
137190
*/

0 commit comments

Comments
 (0)