Visitar URL original
Convert `update-release-branch.py` to TypeScript by mbg · Pull Request #4010 · github/codeql-action · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
4c2bf01
Scaffold basic `update-release-branch.ts`
mbg Jul 14, 2026
8505293
Add constants
mbg Jul 14, 2026
1495237
Add `getGitHubToken`
mbg Jul 14, 2026
a10d7a7
Parse command line options
mbg Jul 14, 2026
d0b11ca
Make script executable
mbg Jul 14, 2026
1b146d1
Validate target branch and extract major version
mbg Jul 14, 2026
440cebc
Add `getCurrentVersion`
mbg Jul 14, 2026
4ca9f53
Add `runGit` and use it to obtain the source branch SHA
mbg Jul 14, 2026
c3da0a9
Fetch commit info
mbg Jul 14, 2026
5579217
Check whether the new branch exists
mbg Jul 14, 2026
460cc0c
Add dry run support and push branch
mbg Jul 14, 2026
212aa33
Add `runCommand`
mbg Jul 14, 2026
c8ed70e
Add `rebuildAction` function
mbg Jul 14, 2026
639fc5d
Add changelog helpers
mbg Jul 14, 2026
5e21203
Add `prepareNewBranch`
mbg Jul 14, 2026
78d71fb
Create PR
mbg Jul 14, 2026
5c030f4
Add summary comment
mbg Jul 14, 2026
2c45c81
Add `DryRunOption` interface
mbg Jul 14, 2026
4b861b8
Move changelog-related definitions into their own module
mbg Jul 14, 2026
e2472fc
Make `processChangelogForBackports` dry-run-aware
mbg Jul 14, 2026
fd0ae66
Restore some comments
mbg Jul 14, 2026
d4b3323
Use TS script in workflow
mbg Jul 14, 2026
f9a9f48
Run `npm ci` in `release-initialise` workflow.
mbg Jul 14, 2026
583bf3e
Fix comment typos
mbg Jul 14, 2026
d694648
Make `changelog.ts` more testable and add tests
mbg Jul 14, 2026
9b314f4
Make `replaceVersionInPackageJson` dry-run-aware
mbg Jul 14, 2026
a464bf1
Move `package.json` helpers into their own file
mbg Jul 14, 2026
ae48798
Make `versions.ts` testable and add basic tests
mbg Jul 14, 2026
41c7af7
Use `paginate`
mbg Jul 15, 2026
423e341
Delete python version
mbg Jul 15, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add changelog helpers
  • Loading branch information
mbg committed Jul 14, 2026
commit 639fc5d7eabd0bdfd0d598125e6580c7b07db17c
3 changes: 3 additions & 0 deletions pr-checks/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ export const PR_CHECK_EXCLUDED_FILE = path.join(PR_CHECKS_DIR, "excluded.yml");
/** The path of the main `package.json`. */
export const PACKAGE_JSON = path.join(REPO_ROOT, "package.json");

/** THe path of the changelog. */
Comment thread
mbg marked this conversation as resolved.
Resolved
export const CHANGELOG_FILE = path.join(REPO_ROOT, "CHANGELOG.md");

/** The path to the esbuild metadata file. */
export const BUNDLE_METADATA_FILE = path.join(REPO_ROOT, "meta.json");

Expand Down
107 changes: 106 additions & 1 deletion pr-checks/update-release-branch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as fs from "node:fs";
import { parseArgs } from "node:util";

import { type ApiClient, getApiClient } from "./api-client";
import { PACKAGE_JSON, REPO_ROOT } from "./config";
import { CHANGELOG_FILE, PACKAGE_JSON, REPO_ROOT } from "./config";

/** Placeholder changelog content for a new release. */
const EMPTY_CHANGELOG = `# CodeQL Action Changelog
Expand Down Expand Up @@ -148,6 +148,111 @@ export function getCurrentVersion(): string | undefined {
return pkg.version;
}

/** Returns today's date formatted as `DD Mon YYYY`. */
export function getTodayString(): string {
const today = new Date();
return today.toLocaleDateString("en-GB", {
day: "2-digit",
month: "short",
year: "numeric",
});
}

/**
* Updates the `[UNRELEASED]` marker in `CHANGELOG.md` with the given version
* and today's date.
*/
export function updateChangelog(version: string): void {
let content: string;

if (fs.existsSync(CHANGELOG_FILE)) {
content = fs.readFileSync(CHANGELOG_FILE, "utf8");
} else {
content = EMPTY_CHANGELOG;
}

const newContent = content.replace(
"[UNRELEASED]",
`${version} - ${getTodayString()}`,
);

fs.writeFileSync(CHANGELOG_FILE, newContent, "utf8");
}

/**
* Processes changelog entries for a backport, converting version references
* from the source major version to the target major version and filtering
* entries that only apply to newer versions.
*/
export function processChangelogForBackports(
sourceBranchMajorVersion: string,
targetBranchMajorVersion: string,
): void {
const content = fs.readFileSync(CHANGELOG_FILE, "utf8");
const lines = content.split("\n");
const someVersionsOnlyRegex = /\[v(\d+)\+ only\]/;

let output = "";
let i = 0;

// Copy lines until we find the first section heading.
let foundFirstSection = false;
while (!foundFirstSection && i < lines.length) {
let line = lines[i];
if (line.startsWith("## ")) {
line = line.replace(
`## ${sourceBranchMajorVersion}`,
`## ${targetBranchMajorVersion}`,
);
foundFirstSection = true;
}
output += `${line}\n`;
i++;
}

if (!foundFirstSection) {
throw new Error("Could not find any change sections in CHANGELOG.md");
}

// Process remaining lines.
let foundContent = false;
output += "\n";

while (i < lines.length) {
let line = lines[i];
i++;

// Filter out changelog entries that only apply to newer versions.
const match = someVersionsOnlyRegex.exec(line);
if (match) {
if (
Number.parseInt(targetBranchMajorVersion) < Number.parseInt(match[1])
) {
continue;
}
}

if (line.startsWith("## ")) {
line = line.replace(
`## ${sourceBranchMajorVersion}`,
`## ${targetBranchMajorVersion}`,
);
if (!foundContent) {
output += "No user facing changes.\n";
}
foundContent = false;
output += `\n${line}\n\n`;
} else {
if (line.trim() !== "") {
foundContent = true;
output += `${line}\n`;
}
}
}

fs.writeFileSync(CHANGELOG_FILE, output, "utf8");
}

/** Represents commits returned by the GitHub API (relevant fields only). */
export interface GitHubCommit {
sha: string;
Expand Down