-
Notifications
You must be signed in to change notification settings - Fork 472
Convert update-release-branch.py to TypeScript
#4010
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
mbg
wants to merge
28
commits into
main
Choose a base branch
from
mbg/ts/update-release-branch
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
4c2bf01
Scaffold basic `update-release-branch.ts`
mbg 8505293
Add constants
mbg 1495237
Add `getGitHubToken`
mbg a10d7a7
Parse command line options
mbg d0b11ca
Make script executable
mbg 1b146d1
Validate target branch and extract major version
mbg 440cebc
Add `getCurrentVersion`
mbg 4ca9f53
Add `runGit` and use it to obtain the source branch SHA
mbg c3da0a9
Fetch commit info
mbg 5579217
Check whether the new branch exists
mbg 460cc0c
Add dry run support and push branch
mbg 212aa33
Add `runCommand`
mbg c8ed70e
Add `rebuildAction` function
mbg 639fc5d
Add changelog helpers
mbg 5e21203
Add `prepareNewBranch`
mbg 78d71fb
Create PR
mbg 5c030f4
Add summary comment
mbg 2c45c81
Add `DryRunOption` interface
mbg 4b861b8
Move changelog-related definitions into their own module
mbg e2472fc
Make `processChangelogForBackports` dry-run-aware
mbg fd0ae66
Restore some comments
mbg d4b3323
Use TS script in workflow
mbg f9a9f48
Run `npm ci` in `release-initialise` workflow.
mbg 583bf3e
Fix comment typos
mbg d694648
Make `changelog.ts` more testable and add tests
mbg 9b314f4
Make `replaceVersionInPackageJson` dry-run-aware
mbg a464bf1
Move `package.json` helpers into their own file
mbg ae48798
Make `versions.ts` testable and add basic tests
mbg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| #!/usr/bin/env npx tsx | ||
|
|
||
| /** | ||
| * Tests for `changelog.ts`. | ||
| */ | ||
|
|
||
| import * as assert from "node:assert/strict"; | ||
| import { describe, it } from "node:test"; | ||
|
|
||
| import { | ||
| EMPTY_CHANGELOG, | ||
| getReleaseDateString, | ||
| processChangelogForBackports, | ||
| setVersionAndDate, | ||
| } from "./changelog"; | ||
|
|
||
| const testDate = new Date(2026, 7, 14); | ||
|
|
||
| describe("getReleaseDateString", async () => { | ||
| await it("formats dates as expected", async () => { | ||
| assert.equal(getReleaseDateString(testDate), "14 Aug 2026"); | ||
| }); | ||
| }); | ||
|
|
||
| const emptyChangelogExpected = `# CodeQL Action Changelog | ||
|
|
||
| ## 9.99.9 - 14 Aug 2026 | ||
|
|
||
| No user facing changes. | ||
|
|
||
| `; | ||
|
|
||
| describe("setVersionAndDate", async () => { | ||
| await it("replaces the placeholder", async () => { | ||
| const result = setVersionAndDate("9.99.9", EMPTY_CHANGELOG, testDate); | ||
| assert.equal(result, emptyChangelogExpected); | ||
| }); | ||
| }); | ||
|
|
||
| const testChangelog = `# CodeQL Action Changelog | ||
|
|
||
| ## 4.12.3 - 14 Aug 2026 | ||
|
|
||
| No user facing changes. | ||
| `; | ||
|
|
||
| const testChangelogResult: string = `# CodeQL Action Changelog | ||
|
|
||
| ## 3.12.3 - 14 Aug 2026 | ||
|
|
||
| No user facing changes. | ||
| `; | ||
|
|
||
| describe("processChangelogForBackports", async () => { | ||
| await it("replaces major versions", async () => { | ||
| const result = processChangelogForBackports("4", "3", testChangelog); | ||
|
|
||
| assert.deepEqual(result.split("\n"), testChangelogResult.split("\n")); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| import * as fs from "node:fs"; | ||
|
|
||
| import { CHANGELOG_FILE, DryRunOption } from "./config"; | ||
|
|
||
| /** Placeholder changelog content for a new release. */ | ||
| export const EMPTY_CHANGELOG = `# CodeQL Action Changelog | ||
|
|
||
| ## [UNRELEASED] | ||
|
|
||
| No user facing changes. | ||
|
|
||
| `; | ||
|
|
||
| /** Returns `date` formatted as `DD Mon YYYY`. */ | ||
| export function getReleaseDateString(today: Date = new Date()): string { | ||
| return today.toLocaleDateString("en-GB", { | ||
| day: "2-digit", | ||
| month: "short", | ||
| year: "numeric", | ||
| }); | ||
| } | ||
|
|
||
| export interface OpenChangelogOptions { | ||
| initChangelog?: boolean; | ||
| } | ||
|
|
||
| export function withChangelog( | ||
| transformer: (contents: string) => string, | ||
| options: DryRunOption & OpenChangelogOptions, | ||
| ): void { | ||
| let content: string; | ||
|
|
||
| if (options.initChangelog && !fs.existsSync(CHANGELOG_FILE)) { | ||
| content = EMPTY_CHANGELOG; | ||
| } else { | ||
| content = fs.readFileSync(CHANGELOG_FILE, "utf8"); | ||
| } | ||
|
|
||
| if (!options.dryRun) { | ||
| fs.writeFileSync(CHANGELOG_FILE, transformer(content), "utf8"); | ||
| } else { | ||
| console.info(`[DRY RUN] Would have written updated changelog.`); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Updates the `[UNRELEASED]` marker in `CHANGELOG.md` with the given version | ||
| * and today's date. | ||
| */ | ||
| export function setVersionAndDate( | ||
| version: string, | ||
| content: string, | ||
| date: Date = new Date(), | ||
| ): string { | ||
| const versionAndDate = `${version} - ${getReleaseDateString(date)}`; | ||
| return content.replace("[UNRELEASED]", versionAndDate); | ||
| } | ||
|
|
||
| /** | ||
| * 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, | ||
| content: string, | ||
| ): string { | ||
| const lines = content.split("\n"); | ||
|
|
||
| // Changelog entries can use the following format to indicate | ||
| // that they only apply to newer versions | ||
| 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. | ||
| // `foundContent` tracks whether we hit two headings in a row | ||
| 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`; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return output; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.