Skip to content

Commit dc2794b

Browse files
committed
Move changelog-related definitions into their own module
1 parent 2c45c81 commit dc2794b

3 files changed

Lines changed: 137 additions & 126 deletions

File tree

‎.vscode/settings.json‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,13 @@
2222
"[typescript]": {
2323
"editor.defaultFormatter": "esbenp.prettier-vscode"
2424
},
25+
"chat.agentSkillsLocations": {
26+
".agents/skills": true,
27+
".github/skills": true,
28+
".claude/skills": true,
29+
"~/.agents/skills": true,
30+
"~/.copilot/skills": true,
31+
"~/.claude/skills": true,
32+
"~/.vscode/extensions/synapsevscode.synapse-1.25.0/copilot/skills": true
33+
},
2534
}

‎pr-checks/changelog.ts‎

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
import * as fs from "node:fs";
2+
3+
import { CHANGELOG_FILE, DryRunOption } from "./config";
4+
5+
/** Placeholder changelog content for a new release. */
6+
export const EMPTY_CHANGELOG = `# CodeQL Action Changelog
7+
8+
## [UNRELEASED]
9+
10+
No user facing changes.
11+
12+
`;
13+
14+
/** Returns today's date formatted as `DD Mon YYYY`. */
15+
export function getTodayString(): string {
16+
const today = new Date();
17+
return today.toLocaleDateString("en-GB", {
18+
day: "2-digit",
19+
month: "short",
20+
year: "numeric",
21+
});
22+
}
23+
24+
/**
25+
* Updates the `[UNRELEASED]` marker in `CHANGELOG.md` with the given version
26+
* and today's date.
27+
*/
28+
export function setVersionAndDate(
29+
options: DryRunOption,
30+
version: string,
31+
): void {
32+
let content: string;
33+
34+
if (fs.existsSync(CHANGELOG_FILE)) {
35+
content = fs.readFileSync(CHANGELOG_FILE, "utf8");
36+
} else {
37+
content = EMPTY_CHANGELOG;
38+
}
39+
40+
const versionAndDate = `${version} - ${getTodayString()}`;
41+
const newContent = content.replace("[UNRELEASED]", versionAndDate);
42+
43+
if (!options.dryRun) {
44+
fs.writeFileSync(CHANGELOG_FILE, newContent, "utf8");
45+
} else {
46+
console.info(
47+
`[DRY RUN] Would have replaced '[UNRELEASED]' in '${CHANGELOG_FILE}' with '${versionAndDate}'.`,
48+
);
49+
}
50+
}
51+
52+
/**
53+
* Processes changelog entries for a backport, converting version references
54+
* from the source major version to the target major version and filtering
55+
* entries that only apply to newer versions.
56+
*/
57+
export function processChangelogForBackports(
58+
sourceBranchMajorVersion: string,
59+
targetBranchMajorVersion: string,
60+
): void {
61+
const content = fs.readFileSync(CHANGELOG_FILE, "utf8");
62+
const lines = content.split("\n");
63+
const someVersionsOnlyRegex = /\[v(\d+)\+ only\]/;
64+
65+
let output = "";
66+
let i = 0;
67+
68+
// Copy lines until we find the first section heading.
69+
let foundFirstSection = false;
70+
while (!foundFirstSection && i < lines.length) {
71+
let line = lines[i];
72+
if (line.startsWith("## ")) {
73+
line = line.replace(
74+
`## ${sourceBranchMajorVersion}`,
75+
`## ${targetBranchMajorVersion}`,
76+
);
77+
foundFirstSection = true;
78+
}
79+
output += `${line}\n`;
80+
i++;
81+
}
82+
83+
if (!foundFirstSection) {
84+
throw new Error("Could not find any change sections in CHANGELOG.md");
85+
}
86+
87+
// Process remaining lines.
88+
let foundContent = false;
89+
output += "\n";
90+
91+
while (i < lines.length) {
92+
let line = lines[i];
93+
i++;
94+
95+
// Filter out changelog entries that only apply to newer versions.
96+
const match = someVersionsOnlyRegex.exec(line);
97+
if (match) {
98+
if (
99+
Number.parseInt(targetBranchMajorVersion) < Number.parseInt(match[1])
100+
) {
101+
continue;
102+
}
103+
}
104+
105+
if (line.startsWith("## ")) {
106+
line = line.replace(
107+
`## ${sourceBranchMajorVersion}`,
108+
`## ${targetBranchMajorVersion}`,
109+
);
110+
if (!foundContent) {
111+
output += "No user facing changes.\n";
112+
}
113+
foundContent = false;
114+
output += `\n${line}\n\n`;
115+
} else {
116+
if (line.trim() !== "") {
117+
foundContent = true;
118+
output += `${line}\n`;
119+
}
120+
}
121+
}
122+
123+
fs.writeFileSync(CHANGELOG_FILE, output, "utf8");
124+
}

‎pr-checks/update-release-branch.ts‎

Lines changed: 4 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,8 @@ import * as fs from "node:fs";
2323
import { parseArgs } from "node:util";
2424

2525
import { type ApiClient, getApiClient } from "./api-client";
26-
import {
27-
CHANGELOG_FILE,
28-
DryRunOption,
29-
PACKAGE_JSON,
30-
REPO_ROOT,
31-
} from "./config";
32-
33-
/** Placeholder changelog content for a new release. */
34-
const EMPTY_CHANGELOG = `# CodeQL Action Changelog
35-
36-
## [UNRELEASED]
37-
38-
No user facing changes.
39-
40-
`;
26+
import * as changelog from "./changelog";
27+
import { DryRunOption, PACKAGE_JSON, REPO_ROOT } from "./config";
4128

4229
/**
4330
* NB: This exact commit message is used to find commits for reverting during backports.
@@ -190,115 +177,6 @@ export function replaceVersionInPackageJson(
190177
fs.writeFileSync(PACKAGE_JSON, `${output.join("\n")}\n`, "utf8");
191178
}
192179

193-
/** Returns today's date formatted as `DD Mon YYYY`. */
194-
export function getTodayString(): string {
195-
const today = new Date();
196-
return today.toLocaleDateString("en-GB", {
197-
day: "2-digit",
198-
month: "short",
199-
year: "numeric",
200-
});
201-
}
202-
203-
/**
204-
* Updates the `[UNRELEASED]` marker in `CHANGELOG.md` with the given version
205-
* and today's date.
206-
*/
207-
export function updateChangelog(options: MainOptions, version: string): void {
208-
let content: string;
209-
210-
if (fs.existsSync(CHANGELOG_FILE)) {
211-
content = fs.readFileSync(CHANGELOG_FILE, "utf8");
212-
} else {
213-
content = EMPTY_CHANGELOG;
214-
}
215-
216-
const versionAndDate = `${version} - ${getTodayString()}`;
217-
const newContent = content.replace("[UNRELEASED]", versionAndDate);
218-
219-
if (!options.dryRun) {
220-
fs.writeFileSync(CHANGELOG_FILE, newContent, "utf8");
221-
} else {
222-
console.info(
223-
`[DRY RUN] Would have replaced '[UNRELEASED]' in '${CHANGELOG_FILE}' with '${versionAndDate}'.`,
224-
);
225-
}
226-
}
227-
228-
/**
229-
* Processes changelog entries for a backport, converting version references
230-
* from the source major version to the target major version and filtering
231-
* entries that only apply to newer versions.
232-
*/
233-
export function processChangelogForBackports(
234-
sourceBranchMajorVersion: string,
235-
targetBranchMajorVersion: string,
236-
): void {
237-
const content = fs.readFileSync(CHANGELOG_FILE, "utf8");
238-
const lines = content.split("\n");
239-
const someVersionsOnlyRegex = /\[v(\d+)\+ only\]/;
240-
241-
let output = "";
242-
let i = 0;
243-
244-
// Copy lines until we find the first section heading.
245-
let foundFirstSection = false;
246-
while (!foundFirstSection && i < lines.length) {
247-
let line = lines[i];
248-
if (line.startsWith("## ")) {
249-
line = line.replace(
250-
`## ${sourceBranchMajorVersion}`,
251-
`## ${targetBranchMajorVersion}`,
252-
);
253-
foundFirstSection = true;
254-
}
255-
output += `${line}\n`;
256-
i++;
257-
}
258-
259-
if (!foundFirstSection) {
260-
throw new Error("Could not find any change sections in CHANGELOG.md");
261-
}
262-
263-
// Process remaining lines.
264-
let foundContent = false;
265-
output += "\n";
266-
267-
while (i < lines.length) {
268-
let line = lines[i];
269-
i++;
270-
271-
// Filter out changelog entries that only apply to newer versions.
272-
const match = someVersionsOnlyRegex.exec(line);
273-
if (match) {
274-
if (
275-
Number.parseInt(targetBranchMajorVersion) < Number.parseInt(match[1])
276-
) {
277-
continue;
278-
}
279-
}
280-
281-
if (line.startsWith("## ")) {
282-
line = line.replace(
283-
`## ${sourceBranchMajorVersion}`,
284-
`## ${targetBranchMajorVersion}`,
285-
);
286-
if (!foundContent) {
287-
output += "No user facing changes.\n";
288-
}
289-
foundContent = false;
290-
output += `\n${line}\n\n`;
291-
} else {
292-
if (line.trim() !== "") {
293-
foundContent = true;
294-
output += `${line}\n`;
295-
}
296-
}
297-
}
298-
299-
fs.writeFileSync(CHANGELOG_FILE, output, "utf8");
300-
}
301-
302180
/** Represents commits returned by the GitHub API (relevant fields only). */
303181
export interface GitHubCommit {
304182
sha: string;
@@ -795,7 +673,7 @@ export async function prepareNewBranch(
795673
console.log(
796674
`Migrating changelog notes from v${sourceBranchMajorVersion} to v${targetBranchMajorVersion}`,
797675
);
798-
processChangelogForBackports(
676+
changelog.processChangelogForBackports(
799677
sourceBranchMajorVersion,
800678
targetBranchMajorVersion,
801679
);
@@ -818,7 +696,7 @@ export async function prepareNewBranch(
818696
);
819697

820698
console.log("Updating changelog");
821-
updateChangelog(options, version);
699+
changelog.setVersionAndDate(options, version);
822700

823701
runGit(["add", "CHANGELOG.md"], {
824702
dryRun: options.dryRun,

0 commit comments

Comments
 (0)