Skip to content

Commit d3dd097

Browse files
committed
Update processChangelogForBackports to use parseChangelog
1 parent 5d653be commit d3dd097

1 file changed

Lines changed: 34 additions & 46 deletions

File tree

‎pr-checks/changelog.ts‎

Lines changed: 34 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -155,70 +155,58 @@ export function processChangelogForBackports(
155155
targetBranchMajorVersion: string,
156156
content: string,
157157
): string {
158-
const lines = content.split("\n");
159-
160158
// Changelog entries can use the following format to indicate
161159
// that they only apply to newer versions
162160
const someVersionsOnlyRegex = /\[v(\d+)\+ only\]/;
163161

164-
let output = "";
165-
let i = 0;
162+
// Parse the changelog.
163+
const changelog = parseChangelog(content);
166164

167-
// Copy lines until we find the first section heading.
168-
let foundFirstSection = false;
169-
while (!foundFirstSection && i < lines.length) {
170-
let line = lines[i];
171-
if (line.startsWith("## ")) {
172-
line = line.replace(
173-
`## ${sourceBranchMajorVersion}`,
174-
`## ${targetBranchMajorVersion}`,
175-
);
176-
foundFirstSection = true;
177-
}
178-
output += `${line}\n`;
179-
i++;
180-
}
181-
182-
if (!foundFirstSection) {
165+
if (changelog.sections.length === 0) {
183166
throw new Error("Could not find any change sections in CHANGELOG.md");
184167
}
185168

186-
// Process remaining lines.
187-
// `foundContent` tracks whether we hit two headings in a row
188-
let foundContent = false;
189-
output += "\n";
190-
191-
while (i < lines.length) {
192-
let line = lines[i];
193-
i++;
194-
195-
// Filter out changelog entries that only apply to newer versions.
196-
const match = someVersionsOnlyRegex.exec(line);
197-
if (match) {
169+
// Filter out changelog entries that only apply to newer versions and
170+
// update the section headings with the backport major version for
171+
// sections we keep.
172+
for (const section of changelog.sections) {
173+
// Update the section headings with the backport major version.
174+
section.headerLine = section.headerLine.replace(
175+
`## ${sourceBranchMajorVersion}`,
176+
`## ${targetBranchMajorVersion}`,
177+
);
178+
179+
const filteredEntries: string[] = [];
180+
let foundContent = false;
181+
182+
for (const line of section.bodyLines) {
183+
// Skip the entry if `someVersionsOnlyRegex` matches and the major version
184+
// of the target branch is smaller than the required version.
185+
const match = someVersionsOnlyRegex.exec(line);
198186
if (
187+
match &&
199188
Number.parseInt(targetBranchMajorVersion) < Number.parseInt(match[1])
200189
) {
201190
continue;
202191
}
203-
}
204192

205-
if (line.startsWith("## ")) {
206-
line = line.replace(
207-
`## ${sourceBranchMajorVersion}`,
208-
`## ${targetBranchMajorVersion}`,
209-
);
210-
if (!foundContent) {
211-
output += "No user facing changes.\n";
212-
}
213-
foundContent = false;
214-
output += `\n${line}\n\n`;
215-
} else {
193+
// Keep the line.
194+
filteredEntries.push(line);
195+
196+
// Set `foundContent` to `true` if the line is not empty.
216197
if (line.trim() !== "") {
217198
foundContent = true;
218-
output += `${line}\n`;
219199
}
220200
}
201+
202+
// Update the section with the retained entries.
203+
section.bodyLines = filteredEntries;
204+
205+
// Add an entry if we didn't keep any.
206+
if (!foundContent) {
207+
section.bodyLines.push(NO_CHANGES_STR.trim());
208+
}
221209
}
222210

223-
return output;
211+
return renderChangelog(changelog);
224212
}

0 commit comments

Comments
 (0)