Skip to content

Commit 3269c4d

Browse files
vincentkocsteipete
authored andcommitted
fix(release): validate changelog before atomic write
1 parent c1664a3 commit 3269c4d

2 files changed

Lines changed: 85 additions & 8 deletions

File tree

‎.agents/skills/openclaw-changelog-update/scripts/verify-release-notes.mjs‎

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
#!/usr/bin/env node
22

33
import { execFileSync, spawnSync } from "node:child_process";
4-
import { existsSync, mkdirSync, readFileSync, renameSync, rmSync, writeFileSync } from "node:fs";
4+
import {
5+
existsSync,
6+
mkdirSync,
7+
mkdtempSync,
8+
readFileSync,
9+
renameSync,
10+
rmSync,
11+
writeFileSync,
12+
} from "node:fs";
513
import path from "node:path";
614
import { pathToFileURL } from "node:url";
715
import {
@@ -2227,6 +2235,19 @@ function releaseChecks(changelog, version, releaseTags) {
22272235
return checks;
22282236
}
22292237

2238+
function writeFileAtomic(filePath, contents) {
2239+
const directory = path.dirname(filePath);
2240+
mkdirSync(directory, { recursive: true });
2241+
const tempDirectory = mkdtempSync(path.join(directory, `.${path.basename(filePath)}.tmp-`));
2242+
const tempPath = path.join(tempDirectory, path.basename(filePath));
2243+
try {
2244+
writeFileSync(tempPath, contents);
2245+
renameSync(tempPath, filePath);
2246+
} finally {
2247+
rmSync(tempDirectory, { force: true, recursive: true });
2248+
}
2249+
}
2250+
22302251
function main() {
22312252
const options = parseArgs(process.argv.slice(2));
22322253
if (options.help) {
@@ -2396,32 +2417,32 @@ function main() {
23962417
ledger,
23972418
relationships.directCommits,
23982419
);
2399-
24002420
if (options.manifestPath) {
2401-
writeFileSync(options.manifestPath, `${JSON.stringify(manifest, null, 2)}\n`);
2421+
writeFileAtomic(options.manifestPath, `${JSON.stringify(manifest, null, 2)}\n`);
24022422
}
24032423

2424+
let candidateChangelog = changelog;
2425+
let candidateSection = section;
24042426
if (options.writeLedger) {
2405-
changelog = replaceLedger(
2427+
candidateChangelog = replaceLedger(
24062428
changelog,
24072429
section,
24082430
ledger.ledger,
24092431
ledger.pullRequests,
24102432
relationships.directCommits,
24112433
);
2412-
writeFileSync("CHANGELOG.md", changelog);
2413-
section = sectionFor(changelog, options.version);
2434+
candidateSection = sectionFor(candidateChangelog, options.version);
24142435
}
24152436

24162437
const errors = ledgerChecks(
2417-
section,
2438+
candidateSection,
24182439
ledger.pullRequests,
24192440
nodes,
24202441
relationships.directCommits,
24212442
source.shippedBaselines,
24222443
);
24232444
const github = options.checkGithub
2424-
? releaseChecks(changelog, options.version, options.releaseTags)
2445+
? releaseChecks(candidateChangelog, options.version, options.releaseTags)
24252446
: [];
24262447
for (const check of github) {
24272448
if (!check.matches) {
@@ -2430,6 +2451,11 @@ function main() {
24302451
);
24312452
}
24322453
}
2454+
if (errors.length === 0) {
2455+
if (options.writeLedger) {
2456+
writeFileAtomic("CHANGELOG.md", candidateChangelog);
2457+
}
2458+
}
24332459

24342460
const result = {
24352461
base: options.base,

‎test/scripts/verify-release-notes.test.ts‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -720,6 +720,57 @@ describe("release-note verification", () => {
720720
}
721721
});
722722

723+
it("leaves CHANGELOG.md untouched when the rendered ledger fails validation", () => {
724+
const cwd = mkdtempSync(join(tmpdir(), "openclaw-release-notes-"));
725+
try {
726+
git(cwd, ["init", "-q"]);
727+
const changelog = [
728+
"# Changelog",
729+
"",
730+
"## 2026.7.1",
731+
"",
732+
"### Highlights",
733+
"",
734+
"- Only one highlight.",
735+
"",
736+
"### Changes",
737+
"",
738+
"### Fixes",
739+
"",
740+
].join("\n");
741+
writeFileSync(join(cwd, "CHANGELOG.md"), changelog);
742+
git(cwd, ["add", "CHANGELOG.md"]);
743+
git(cwd, ["commit", "-qm", "initial"]);
744+
const manifestPath = join(cwd, "release-manifest.json");
745+
746+
const result = spawnSync(
747+
process.execPath,
748+
[
749+
verifier,
750+
"--base",
751+
"HEAD",
752+
"--target",
753+
"HEAD",
754+
"--main-ref",
755+
"HEAD",
756+
"--manifest",
757+
manifestPath,
758+
"--version",
759+
"2026.7.1",
760+
"--write-ledger",
761+
],
762+
{ cwd, encoding: "utf8" },
763+
);
764+
765+
expect(result.status).toBe(1);
766+
expect(result.stdout).toContain("1 errors");
767+
expect(JSON.parse(readFileSync(manifestPath, "utf8")).version).toBe("2026.7.1");
768+
expect(readFileSync(join(cwd, "CHANGELOG.md"), "utf8")).toBe(changelog);
769+
} finally {
770+
rmSync(cwd, { recursive: true, force: true });
771+
}
772+
});
773+
723774
it("rejects a release base that is not an ancestor of the target", () => {
724775
const cwd = mkdtempSync(join(tmpdir(), "openclaw-release-notes-"));
725776
try {

0 commit comments

Comments
 (0)