diff --git a/pr-checks/changenotes.ts b/pr-checks/changenotes.ts index 933d255cd5..d05f4ea892 100755 --- a/pr-checks/changenotes.ts +++ b/pr-checks/changenotes.ts @@ -50,6 +50,28 @@ function getChangenotes(): ChangenoteFile[] { }); } +/** + * Populates the '[UNRELEASED]' section of CHANGELOG.md with the contents + * of the changenotes in {@link CHANGENOTES_DIR} and then deletes those changenotes. + * @param contents The contents of CHANGELOG.md + * @returns The updated contents of CHANGELOG.md + */ +export function transferChangenotesToChangelog(contents: string): string { + const changenotes = getChangenotes(); + const changenoteBodies = changenotes.map((c) => c.content.trim()); + const changenotePaths = changenotes.map((c) => c.absolutePath); + + const changelog = parseChangelog(contents); + addBodyLinesToUnreleasedSection(changelog, changenoteBodies); + const updatedChangelog = renderChangelog(changelog); + + for (const p of changenotePaths) { + fs.unlinkSync(p); + } + + return updatedChangelog; +} + const entryPoint = process.argv[1]; if (entryPoint && import.meta.url === pathToFileURL(entryPoint).href) { try { @@ -91,26 +113,13 @@ function usage(): ExitCode { function assemble(): ExitCode { try { - const changenotes = getChangenotes(); - const changenoteBodies = changenotes.map((c) => c.content); - const changenotePaths = changenotes.map((c) => c.absolutePath); - withChangelog((contents) => { - const changelog = parseChangelog(contents); - addBodyLinesToUnreleasedSection(changelog, changenoteBodies); - return renderChangelog(changelog); + return transferChangenotesToChangelog(contents); }, {}); - - // Delete changenotes only after successful processing. - for (const p of changenotePaths) { - fs.unlinkSync(p); - } - return ExitCode.Success; } catch (e) { console.error("Failed to assemble changenotes to 'CHANGELOG.md'", e); } - return ExitCode.Failure; } diff --git a/pr-checks/update-release-branch.ts b/pr-checks/update-release-branch.ts index 93e50f7ad2..9d46fd3ecd 100755 --- a/pr-checks/update-release-branch.ts +++ b/pr-checks/update-release-branch.ts @@ -23,7 +23,13 @@ import { parseArgs } from "node:util"; import { type ApiClient, getApiClient } from "./api-client"; import * as changelog from "./changelog"; -import { DryRunOption, REPO_ROOT } from "./config"; +import { transferChangenotesToChangelog } from "./changenotes"; +import { + CHANGELOG_FILE, + CHANGENOTES_DIR, + DryRunOption, + REPO_ROOT, +} from "./config"; import { getCurrentVersion, replaceVersionInPackageJson, @@ -693,11 +699,14 @@ export async function prepareNewBranch( console.log("Updating changelog"); changelog.withChangelog( - (contents) => changelog.setVersionAndDate(version, contents), + (contents) => { + const populatedContents = transferChangenotesToChangelog(contents); + return changelog.setVersionAndDate(version, populatedContents); + }, { ...options, initChangelog: true }, ); - runGit(["add", "CHANGELOG.md"], { + runGit(["add", CHANGELOG_FILE, CHANGENOTES_DIR], { dryRun: options.dryRun, }); runGit(["commit", "-m", `Update changelog for v${version}`], {