Skip to content

Commit 5d653be

Browse files
committed
Add parseChangelog and renderChangelog
1 parent 48e76d0 commit 5d653be

2 files changed

Lines changed: 97 additions & 0 deletions

File tree

‎pr-checks/changelog.test.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,19 @@
44
* Tests for `changelog.ts`.
55
*/
66

7+
import * as fs from "node:fs";
78
import * as assert from "node:assert/strict";
89
import { describe, it } from "node:test";
910

1011
import {
1112
EMPTY_CHANGELOG,
1213
getReleaseDateString,
14+
parseChangelog,
1315
processChangelogForBackports,
16+
renderChangelog,
1417
setVersionAndDate,
1518
} from "./changelog";
19+
import { CHANGELOG_FILE } from "./config";
1620

1721
const testDate = new Date(2026, 7, 14);
1822

@@ -37,6 +41,14 @@ describe("setVersionAndDate", async () => {
3741
});
3842
});
3943

44+
describe("parseChangelog + renderChangelog", async () => {
45+
await it("renderChangelog(parseChangelog(c)) == c", async () => {
46+
const actualChangelog = fs.readFileSync(CHANGELOG_FILE, "utf-8");
47+
const roundtrip = renderChangelog(parseChangelog(actualChangelog));
48+
assert.deepEqual(roundtrip.split("\n"), actualChangelog.split("\n"));
49+
});
50+
});
51+
4052
const testChangelog = `# CodeQL Action Changelog
4153
4254
## 4.12.3 - 14 Aug 2026

‎pr-checks/changelog.ts‎

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,22 @@ export const EMPTY_CHANGELOG = `# CodeQL Action Changelog
1515
1616
${NO_CHANGES_STR}`;
1717

18+
/**
19+
* Represents sections in a changelog.
20+
*/
21+
export interface ChangelogSection {
22+
headerLine: string;
23+
bodyLines: string[];
24+
}
25+
26+
/**
27+
* Represents a changelog.
28+
*/
29+
export interface Changelog {
30+
preamble: string[];
31+
sections: ChangelogSection[];
32+
}
33+
1834
/** Returns `date` formatted as `DD Mon YYYY`. */
1935
export function getReleaseDateString(today: Date = new Date()): string {
2036
return today.toLocaleDateString("en-GB", {
@@ -60,6 +76,75 @@ export function setVersionAndDate(
6076
return content.replace(UNRELEASED_PLACEHOLDER, versionAndDate);
6177
}
6278

79+
/**
80+
* Parses `content` into a structured representation of a changelog.
81+
*
82+
* @param content The contents of the changelog file.
83+
*/
84+
export function parseChangelog(content: string): Changelog {
85+
const lines = content.split("\n");
86+
let i = 0;
87+
88+
const preamble: string[] = [];
89+
const sections: ChangelogSection[] = [];
90+
let currentSection: ChangelogSection | undefined = undefined;
91+
92+
// Process all lines of the input file.
93+
while (i < lines.length) {
94+
const line = lines[i];
95+
96+
// Sections of the changelog start with `## `.
97+
if (line.startsWith("## ")) {
98+
// We have discovered a new section. If `currentSection` is already defined,
99+
// then this marks the end of that section. Push it to the array of sections
100+
// in the changelog.
101+
if (currentSection !== undefined) {
102+
sections.push(currentSection);
103+
}
104+
105+
// Initialise the new section.
106+
currentSection = { headerLine: line, bodyLines: [] };
107+
} else if (currentSection !== undefined) {
108+
// Add lines between the section header and the next to the current section.
109+
currentSection.bodyLines.push(line);
110+
} else {
111+
// This is neither a section header nor are we in a section already,
112+
// so this line is part of the preamble.
113+
preamble.push(line);
114+
}
115+
116+
i++;
117+
}
118+
119+
// Push the current section to the array of completed sections, if there is
120+
// still one unfinished.
121+
if (currentSection !== undefined) {
122+
sections.push(currentSection);
123+
}
124+
125+
return { preamble, sections };
126+
}
127+
128+
/**
129+
* Combines an array of lines into a single string by adding line breaks.
130+
*/
131+
export function unlines(lines: string[]): string {
132+
return `${lines.join("\n")}`;
133+
}
134+
135+
/**
136+
* Renders a given changelog to a string.
137+
*/
138+
export function renderChangelog(changelog: Changelog): string {
139+
let result = unlines(changelog.preamble);
140+
141+
for (const section of changelog.sections) {
142+
result += `\n${section.headerLine}\n${unlines(section.bodyLines)}`;
143+
}
144+
145+
return result;
146+
}
147+
63148
/**
64149
* Processes changelog entries for a backport, converting version references
65150
* from the source major version to the target major version and filtering

0 commit comments

Comments
 (0)