@@ -23,21 +23,8 @@ import * as fs from "node:fs";
2323import { parseArgs } from "node:util" ;
2424
2525import { 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 + ) \+ o n l y \] / ;
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). */
303181export 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