Skip to content

Commit b2124af

Browse files
committed
fix(release): validate changelog before atomic write
1 parent 2571ae3 commit b2124af

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 {
@@ -2004,6 +2012,19 @@ function releaseChecks(changelog, version, releaseTags) {
20042012
return checks;
20052013
}
20062014

2015+
function writeFileAtomic(filePath, contents) {
2016+
const directory = path.dirname(filePath);
2017+
mkdirSync(directory, { recursive: true });
2018+
const tempDirectory = mkdtempSync(path.join(directory, `.${path.basename(filePath)}.tmp-`));
2019+
const tempPath = path.join(tempDirectory, path.basename(filePath));
2020+
try {
2021+
writeFileSync(tempPath, contents);
2022+
renameSync(tempPath, filePath);
2023+
} finally {
2024+
rmSync(tempDirectory, { force: true, recursive: true });
2025+
}
2026+
}
2027+
20072028
function main() {
20082029
const options = parseArgs(process.argv.slice(2));
20092030
if (options.help) {
@@ -2173,32 +2194,32 @@ function main() {
21732194
ledger,
21742195
relationships.directCommits,
21752196
);
2176-
21772197
if (options.manifestPath) {
2178-
writeFileSync(options.manifestPath, `${JSON.stringify(manifest, null, 2)}\n`);
2198+
writeFileAtomic(options.manifestPath, `${JSON.stringify(manifest, null, 2)}\n`);
21792199
}
21802200

2201+
let candidateChangelog = changelog;
2202+
let candidateSection = section;
21812203
if (options.writeLedger) {
2182-
changelog = replaceLedger(
2204+
candidateChangelog = replaceLedger(
21832205
changelog,
21842206
section,
21852207
ledger.ledger,
21862208
ledger.pullRequests,
21872209
relationships.directCommits,
21882210
);
2189-
writeFileSync("CHANGELOG.md", changelog);
2190-
section = sectionFor(changelog, options.version);
2211+
candidateSection = sectionFor(candidateChangelog, options.version);
21912212
}
21922213

21932214
const errors = ledgerChecks(
2194-
section,
2215+
candidateSection,
21952216
ledger.pullRequests,
21962217
nodes,
21972218
relationships.directCommits,
21982219
source.shippedBaselines,
21992220
);
22002221
const github = options.checkGithub
2201-
? releaseChecks(changelog, options.version, options.releaseTags)
2222+
? releaseChecks(candidateChangelog, options.version, options.releaseTags)
22022223
: [];
22032224
for (const check of github) {
22042225
if (!check.matches) {
@@ -2207,6 +2228,11 @@ function main() {
22072228
);
22082229
}
22092230
}
2231+
if (errors.length === 0) {
2232+
if (options.writeLedger) {
2233+
writeFileAtomic("CHANGELOG.md", candidateChangelog);
2234+
}
2235+
}
22102236

22112237
const result = {
22122238
base: options.base,

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

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,57 @@ describe("release-note verification", () => {
470470
}
471471
});
472472

473+
it("leaves CHANGELOG.md untouched when the rendered ledger fails validation", () => {
474+
const cwd = mkdtempSync(join(tmpdir(), "openclaw-release-notes-"));
475+
try {
476+
git(cwd, ["init", "-q"]);
477+
const changelog = [
478+
"# Changelog",
479+
"",
480+
"## 2026.7.1",
481+
"",
482+
"### Highlights",
483+
"",
484+
"- Only one highlight.",
485+
"",
486+
"### Changes",
487+
"",
488+
"### Fixes",
489+
"",
490+
].join("\n");
491+
writeFileSync(join(cwd, "CHANGELOG.md"), changelog);
492+
git(cwd, ["add", "CHANGELOG.md"]);
493+
git(cwd, ["commit", "-qm", "initial"]);
494+
const manifestPath = join(cwd, "release-manifest.json");
495+
496+
const result = spawnSync(
497+
process.execPath,
498+
[
499+
verifier,
500+
"--base",
501+
"HEAD",
502+
"--target",
503+
"HEAD",
504+
"--main-ref",
505+
"HEAD",
506+
"--manifest",
507+
manifestPath,
508+
"--version",
509+
"2026.7.1",
510+
"--write-ledger",
511+
],
512+
{ cwd, encoding: "utf8" },
513+
);
514+
515+
expect(result.status).toBe(1);
516+
expect(result.stdout).toContain("1 errors");
517+
expect(JSON.parse(readFileSync(manifestPath, "utf8")).version).toBe("2026.7.1");
518+
expect(readFileSync(join(cwd, "CHANGELOG.md"), "utf8")).toBe(changelog);
519+
} finally {
520+
rmSync(cwd, { recursive: true, force: true });
521+
}
522+
});
523+
473524
it("rejects a release base that is not an ancestor of the target", () => {
474525
const cwd = mkdtempSync(join(tmpdir(), "openclaw-release-notes-"));
475526
try {

0 commit comments

Comments
 (0)