Files
SnapOtter/scripts/manage-release-notes.mjs
T
SnapOtterandGitHub d10d0f544f fix: release QA hardening across processing, media, security, and CI gates (#649)
A release-readiness QA pass over the whole product. The commits split into
defects a user would hit and gates that were reporting green while measuring
nothing.

## Fixes that change behaviour

Rate limiting was bypassable on every install: TRUST_PROXY defaulted to true, so
request.ip came from a client-set header and a forged X-Forwarded-For got past
the login limiter. The default is now a private-network trust list.

A transient Postgres outage stranded in-flight jobs, leaving finished output on
disk with no row pointing at it. A reconciler now resolves those rows and adopts
the bytes rather than dropping the work.

A Redis connection that moved to a new address wedged every read-blocked
consumer, so completions stopped signalling while health still answered 200.
Socket timeouts plus subscriber pings recover it.

Installing more than one AI bundle left the shared venv multi-versioned and
silently broke three tools. The installer now reconciles distributions to one
version each.

Converting an image to JXL at quality 1 through 4 returned a 500, because
libjxl 0.7 rejects the distance those values compute. The quality is floored at
what the encoder honours. A missing ffmpeg was also reported to the user as a
corrupt upload; it now says the engine is unavailable.

RAW uploads reached an unpatched LibRaw on arm64, so it is built from source at
0.22.2, and the release scan was split so it can fail on an unfixed critical
instead of hiding it behind ignore-unfixed.

## Gates that could not fail

Two mutation lanes ran zero mutants because Stryker crawled the gitignored docs
build; coverage discarded its whole report on any failing test; the lint gate
skipped root tests, scripts, and two workspaces; and several generated matrices
counted a host missing ffmpeg as a passing tool. Each now measures what it
claims.

Full evidence and the outstanding release items are tracked locally and are not
part of this branch.
2026-07-27 15:37:30 +08:00

135 lines
4.7 KiB
JavaScript

#!/usr/bin/env node
import {
copyFileSync,
existsSync,
mkdirSync,
readFileSync,
renameSync,
rmSync,
writeFileSync,
} from "node:fs";
import path from "node:path";
import { pathToFileURL } from "node:url";
const SEMVER =
/^(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)(-[0-9A-Za-z-]+(\.[0-9A-Za-z-]+)*)?(\+[0-9A-Za-z-]+(\.[0-9A-Za-z-]+)*)?$/;
function assertVersion(version) {
if (!SEMVER.test(version)) throw new Error(`Invalid semantic version: ${version}`);
}
export function archiveCustomReleaseNotes(root, version) {
assertVersion(version);
const source = path.join(root, ".release-notes.md");
if (!existsSync(source)) return false;
const archiveDirectory = path.join(root, ".release-notes");
const archived = path.join(archiveDirectory, `v${version}.md`);
mkdirSync(archiveDirectory, { recursive: true });
if (existsSync(archived)) {
if (!readFileSync(source).equals(readFileSync(archived))) {
throw new Error(`Archived release notes differ for v${version}`);
}
rmSync(source);
} else {
renameSync(source, archived);
}
return true;
}
export function materializeReleaseNotes(root, version, output) {
assertVersion(version);
const archived = path.join(root, ".release-notes", `v${version}.md`);
if (existsSync(archived)) {
copyFileSync(archived, output);
return true;
}
const changelog = readFileSync(path.join(root, "CHANGELOG.md"), "utf8");
const escapedVersion = version.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
const heading = new RegExp(`^# \\[${escapedVersion}\\](?:\\(|\\s|$)`, "m");
const match = heading.exec(changelog);
if (!match) throw new Error(`CHANGELOG.md has no generated notes for v${version}`);
const tail = changelog.slice(match.index);
const nextHeading = /\n# \[[0-9]/.exec(tail);
const notes = tail.slice(0, nextHeading?.index ?? tail.length).trimEnd();
writeFileSync(output, `${notes}\n`);
return false;
}
function docsChangelogBody(notes) {
let body = notes.replaceAll("\r\n", "\n");
body = body.replace(/^# SnapOtter [^\n]+[ \t]*\n(?:[ \t]*\n)?/, "");
body = body.replace(/^## Highlights[ \t]*\n/, "");
const upgrade = /^## Upgrade[ \t]*$/m.exec(body);
if (upgrade) {
const tail = body.slice(upgrade.index);
const divider = /^---[ \t]*$/m.exec(tail);
body = `${body.slice(0, upgrade.index)}${divider ? tail.slice(divider.index + divider[0].length) : ""}`;
}
return body.replace(/^---[ \t]*$/gm, "").trim();
}
export function syncDocsChangelog(
root,
version,
previousVersion,
relativePath = "apps/docs/changelog.md",
) {
assertVersion(version);
assertVersion(previousVersion);
const archived = path.join(root, ".release-notes", `v${version}.md`);
if (!existsSync(archived)) return false;
const body = docsChangelogBody(readFileSync(archived, "utf8"));
const entry = [
`## v${version}`,
"",
body,
"",
`[Full diff on GitHub](https://github.com/snapotter-hq/SnapOtter/compare/v${previousVersion}...v${version})`,
"",
"---",
].join("\n");
const changelogPath = path.join(root, relativePath);
const changelog = readFileSync(changelogPath, "utf8");
if (changelog.includes(`\n\n${entry}\n`)) return false;
const escapedVersion = version.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
if (new RegExp(`^## v${escapedVersion}(?:[ \\t]+\\{#[^}]+\\})?[ \\t]*$`, "m").test(changelog)) {
throw new Error(`Published changelog entry differs for v${version}`);
}
const heading = /^# Changelog(?:[ \t]+\{#[^}]+\})?[ \t]*$/m.exec(changelog);
if (!heading) throw new Error("Published changelog heading is missing");
const insertion = heading.index + heading[0].length;
const next = `${changelog.slice(0, insertion)}\n\n${entry}${changelog.slice(insertion)}`;
writeFileSync(changelogPath, next);
return true;
}
function main() {
const [command, version, argument] = process.argv.slice(2);
const rootFlag = process.argv.indexOf("--root");
const root = rootFlag === -1 ? process.cwd() : path.resolve(process.argv[rootFlag + 1] ?? "");
if (command === "archive" && version) {
process.stdout.write(`custom=${archiveCustomReleaseNotes(root, version)}\n`);
return;
}
if (command === "materialize" && version && argument) {
process.stdout.write(`custom=${materializeReleaseNotes(root, version, argument)}\n`);
return;
}
if (command === "sync-docs" && version && argument) {
process.stdout.write(`changed=${syncDocsChangelog(root, version, argument)}\n`);
return;
}
throw new Error(
"Usage: manage-release-notes.mjs <archive VERSION | materialize VERSION OUTPUT | sync-docs VERSION PREVIOUS_VERSION> [--root PATH]",
);
}
if (process.argv[1] && pathToFileURL(path.resolve(process.argv[1])).href === import.meta.url)
main();