diff --git a/.github/workflows/node-github-release.yml b/.github/workflows/node-github-release.yml index 840d35db6..d7c89121c 100644 --- a/.github/workflows/node-github-release.yml +++ b/.github/workflows/node-github-release.yml @@ -431,248 +431,15 @@ jobs: MAKE_LATEST: ${{ steps.history.outputs.make-latest }} run: | set -euo pipefail - release_workspace="$(mktemp -d)" trap 'rm -rf "$release_workspace"' EXIT - verify_release_tag() { - local live_ref live_type live_object live_commit - if ! live_ref="$( - gh api \ - "repos/$GITHUB_REPOSITORY/git/ref/tags/$RELEASE_TAG" \ - --jq '[.object.type, .object.sha] | @tsv' - )"; then - echo "GitHub release tag must still point to the verified commit." >&2 - return 1 - fi - IFS=$'\t' read -r live_type live_object <<< "$live_ref" - if [[ ! "$live_object" =~ ^[0-9a-fA-F]{40}$ ]]; then - echo "GitHub release tag must still point to the verified commit." >&2 - return 1 - fi - - case "$live_type" in - commit) - live_commit="$live_object" - ;; - tag) - if ! live_commit="$( - gh api \ - "repos/$GITHUB_REPOSITORY/git/tags/$live_object" \ - --jq 'if .object.type == "commit" then .object.sha else empty end' - )"; then - echo "GitHub release tag must still point to the verified commit." >&2 - return 1 - fi - ;; - *) - echo "GitHub release tag must still point to the verified commit." >&2 - return 1 - ;; - esac - - if [[ ! "$live_commit" =~ ^[0-9a-fA-F]{40}$ || - "$live_commit" != "$RELEASE_SHA" ]]; then - echo "GitHub release tag must still point to the verified commit." >&2 - return 1 - fi - } - - github_release_or_missing() { - local endpoint="$1" label="$2" query_status=0 response - response="$(gh api --include "$endpoint" 2>/dev/null)" || - query_status=$? - printf '%s\n' "$response" | - CODEX_SECURITY_RELEASE_QUERY_STATUS="$query_status" \ - CODEX_SECURITY_RELEASE_LABEL="$label" \ - node --input-type=module --eval ' - import { readFileSync } from "node:fs"; - const fail = () => { - throw new Error( - "Unable to resolve the " + - process.env.CODEX_SECURITY_RELEASE_LABEL + - ".", - ); - }; - const raw = readFileSync(0, "utf8"); - const separator = raw.search(/\r?\n\r?\n/u); - const status = /^HTTP\/\d+(?:\.\d+)?\s+(\d{3})(?:\s|$)/u.exec( - raw.slice(0, separator), - ); - if (separator < 0 || status == null) fail(); - let response; - try { - response = JSON.parse( - raw.slice(separator + (raw[separator] === "\r" ? 4 : 2)), - ); - } catch { - fail(); - } - if (response == null || typeof response !== "object") fail(); - if (process.env.CODEX_SECURITY_RELEASE_QUERY_STATUS === "0") { - if (Number(status[1]) < 200 || Number(status[1]) >= 300) fail(); - process.stdout.write(JSON.stringify(response)); - } else if (status[1] !== "404") { - fail(); - } - ' - } - - existing_release="$( - github_release_or_missing \ - "repos/$GITHUB_REPOSITORY/releases/tags/$RELEASE_TAG" \ - "existing GitHub Release" - )" - if [[ -n "$existing_release" ]]; then - asset_name="$(basename "$RELEASE_ARCHIVE")" - downloaded_assets="$release_workspace/downloaded-assets" - mkdir "$downloaded_assets" - gh release download "$RELEASE_TAG" \ - --repo "$GITHUB_REPOSITORY" \ - --pattern "$asset_name" \ - --dir "$downloaded_assets" - - downloaded_archive="$downloaded_assets/$asset_name" - if [[ ! -f "$downloaded_archive" || -L "$downloaded_archive" ]]; then - echo "Expected the exact existing GitHub release asset." >&2 - exit 1 - fi - - printf '%s\n' "$existing_release" | - node sdk/typescript/scripts/release-automation.mjs \ - verify-github-release "$RELEASE_ARCHIVE" "$RELEASE_TAG" \ - "$downloaded_archive" - fi - - existing_notes_file="$release_workspace/existing-notes.md" - : > "$existing_notes_file" - if [[ -n "$existing_release" ]]; then - printf '%s\n' "$existing_release" | - node --input-type=module --eval ' - import { readFileSync, writeFileSync } from "node:fs"; - const release = JSON.parse(readFileSync(0, "utf8")); - if (release.body != null && typeof release.body !== "string") { - throw new Error("Existing release notes must be a string."); - } - writeFileSync( - process.argv[1], - (release.body ?? "").replace(/(?:\r?\n)+$/u, ""), - ); - ' "$existing_notes_file" - fi - - notes_args=( - --method POST - "repos/$GITHUB_REPOSITORY/releases/generate-notes" - -f "tag_name=$RELEASE_TAG" - -f "target_commitish=$RELEASE_SHA" - ) - if git cat-file -e "$RELEASE_SHA:.github/release.yml" 2>/dev/null; then - notes_args+=(-f "configuration_file_path=.github/release.yml") - fi - if [[ -n "$PREVIOUS_TAG" ]]; then - notes_args+=(-f "previous_tag_name=$PREVIOUS_TAG") - fi - generated_notes_file="$release_workspace/generated-notes.md" - gh api "${notes_args[@]}" | - node --input-type=module --eval ' - import { readFileSync, writeFileSync } from "node:fs"; - const response = JSON.parse(readFileSync(0, "utf8")); - if (typeof response.body !== "string") { - throw new Error("Generated GitHub release notes must be a string."); - } - writeFileSync(process.argv[1], response.body); - ' "$generated_notes_file" - release_note_args=() - - tagged_notes_file="$release_workspace/tagged-notes.md" - if git show "$RELEASE_SHA:.github/release-notes.md" \ - > "$tagged_notes_file" 2>/dev/null; then - release_note_args+=( - --tagged-notes-file "$tagged_notes_file" - ) - fi - - if [[ -s "$existing_notes_file" ]]; then - release_note_args+=( - --existing-notes-file "$existing_notes_file" - ) - fi - - published_notes_file="$release_workspace/published-notes.md" - node sdk/typescript/scripts/release-automation.mjs \ - compose-release-notes \ - "$RELEASE_VERSION" \ - "$generated_notes_file" \ - ${release_note_args[@]+"${release_note_args[@]}"} \ - > "$published_notes_file" - - if [[ -n "$existing_release" ]]; then - latest_response="$( - github_release_or_missing \ - "repos/$GITHUB_REPOSITORY/releases/latest" \ - "current Latest GitHub Release" - )" - current_latest_tag="" - if [[ -n "$latest_response" ]]; then - current_latest_tag="$( - printf '%s\n' "$latest_response" | - node --input-type=module --eval ' - import { readFileSync } from "node:fs"; - const response = JSON.parse(readFileSync(0, "utf8")); - if ( - typeof response.tag_name !== "string" || - response.tag_name.length === 0 - ) { - throw new Error( - "Unable to resolve the current Latest GitHub Release.", - ); - } - process.stdout.write(response.tag_name); - ' - )" - fi - currently_latest=false - if [[ "$current_latest_tag" == "$RELEASE_TAG" ]]; then - currently_latest=true - fi - - notes_changed=false - if ! cmp -s "$existing_notes_file" "$published_notes_file"; then - notes_changed=true - fi - if [[ "$notes_changed" == true || - "$currently_latest" != "$MAKE_LATEST" ]]; then - verify_release_tag - if [[ "$notes_changed" == true ]]; then - gh release edit "$RELEASE_TAG" \ - --repo "$GITHUB_REPOSITORY" \ - --latest="$MAKE_LATEST" \ - --notes-file "$published_notes_file" - echo "Updated existing GitHub Release with reviewed and generated notes." - else - gh release edit "$RELEASE_TAG" \ - --repo "$GITHUB_REPOSITORY" \ - --latest="$MAKE_LATEST" - fi - if [[ "$currently_latest" != "$MAKE_LATEST" ]]; then - echo "Updated existing GitHub Release Latest status." - fi - else - echo "The verified GitHub Release already exists." - fi - exit 0 - fi - - release_args=( - "$RELEASE_TAG" - "$RELEASE_ARCHIVE" - --repo "$GITHUB_REPOSITORY" - --title "Codex Security $RELEASE_VERSION" - --verify-tag - --notes-file "$published_notes_file" - --latest="$MAKE_LATEST" - ) - - verify_release_tag - gh release create "${release_args[@]}" + node --input-type=module --eval ' + import { publishGitHubRelease } from "./sdk/typescript/scripts/publish-github-release.mjs"; + try { + publishGitHubRelease(process.env, process.argv[1]); + } catch (error) { + if (error.status === undefined) throw error; + process.exit(error.status); + } + ' "$release_workspace" diff --git a/sdk/typescript/scripts/publish-github-release.mjs b/sdk/typescript/scripts/publish-github-release.mjs new file mode 100644 index 000000000..de3c0f9ca --- /dev/null +++ b/sdk/typescript/scripts/publish-github-release.mjs @@ -0,0 +1,212 @@ +import { spawnSync } from "node:child_process"; +import { lstatSync, mkdirSync, readFileSync, writeFileSync } from "node:fs"; +import { constants } from "node:os"; +import { basename, join } from "node:path"; +import { + composeReleaseNotes, + resolveReleaseSummary, + verifyGitHubRelease, +} from "./release-automation.mjs"; + +function runCommand(program, args, stderr = "inherit", stdout = "pipe") { + return spawnSync(program, args, { + encoding: "utf8", + maxBuffer: Infinity, + stdio: ["inherit", stdout, stderr], + }); +} +function output(result) { + if (result.error) { + console.error(result.error.message); + throw { status: result.error.code === "ENOENT" ? 127 : 126 }; + } + if (result.status !== 0) { + throw { status: result.status ?? 128 + constants.signals[result.signal] }; + } + return result.stdout; +} +function githubReleaseOrMissing(run, endpoint, label) { + const result = run("gh", ["api", "--include", endpoint], "ignore"); + const fail = () => { + throw new Error(`Unable to resolve the ${label}.`); + }; + const raw = result.stdout ?? ""; + const separator = raw.search(/\r?\n\r?\n/u); + const status = /^HTTP\/\d+(?:\.\d+)?\s+(\d{3})(?:\s|$)/u.exec( + raw.slice(0, separator), + ); + if (separator < 0 || status == null) fail(); + let response; + try { + response = JSON.parse( + raw.slice(separator + (raw[separator] === "\r" ? 4 : 2)), + ); + } catch { + fail(); + } + if (response == null || typeof response !== "object") fail(); + if (result.status === 0) { + if (Number(status[1]) < 200 || Number(status[1]) >= 300) fail(); + return response; + } + if (status[1] !== "404") fail(); +} + +/** @param {Function} run */ +export function publishGitHubRelease(env, workspace, run = runCommand) { + const { + GITHUB_REPOSITORY: repository, + RELEASE_TAG: tag, + RELEASE_VERSION: version, + RELEASE_SHA: sha, + RELEASE_ARCHIVE: archive, + PREVIOUS_TAG: previousTag, + MAKE_LATEST: makeLatest, + } = env; + const base = `repos/${repository}`; + const release = (action, args, assets = []) => { + const result = run( + "gh", + ["release", action, tag, ...assets, "--repo", repository, ...args], + "inherit", + "inherit", + ); + output(result); + }; + const verifyTag = () => { + const fail = () => { + console.error( + "GitHub release tag must still point to the verified commit.", + ); + throw { status: 1 }; + }; + const ref = run("gh", [ + "api", + `${base}/git/ref/tags/${tag}`, + "--jq", + "[.object.type, .object.sha] | @tsv", + ]); + if (ref.status !== 0) fail(); + const [type, ...objectFields] = ref.stdout.replace(/\n+$/u, "").split("\t"); + const object = objectFields.join("\t"); + if (!/^[0-9a-fA-F]{40}$/u.test(object)) fail(); + let commit = object; + if (type === "tag") { + const peeled = run("gh", [ + "api", + `${base}/git/tags/${object}`, + "--jq", + 'if .object.type == "commit" then .object.sha else empty end', + ]); + if (peeled.status !== 0) fail(); + commit = peeled.stdout.replace(/\n+$/u, ""); + } else if (type !== "commit") fail(); + if (!/^[0-9a-fA-F]{40}$/u.test(commit) || commit !== sha) fail(); + }; + const existing = githubReleaseOrMissing( + run, + `${base}/releases/tags/${tag}`, + "existing GitHub Release", + ); + if (existing !== undefined) { + const assetName = basename(archive); + const destination = join(workspace, "downloaded-assets"); + mkdirSync(destination); + release("download", ["--pattern", assetName, "--dir", destination]); + const downloaded = join(destination, assetName); + if (!lstatSync(downloaded, { throwIfNoEntry: false })?.isFile()) { + console.error("Expected the exact existing GitHub release asset."); + throw { status: 1 }; + } + const verified = verifyGitHubRelease( + existing, + readFileSync(archive), + tag, + assetName, + readFileSync(downloaded), + ); + console.log(JSON.stringify(verified)); + } + if (existing?.body != null && typeof existing.body !== "string") + throw new Error("Existing release notes must be a string."); + const existingNotes = (existing?.body ?? "").replace(/(?:\r?\n)+$/u, ""); + const notesArgs = [ + "api", + "--method", + "POST", + `${base}/releases/generate-notes`, + "-f", + `tag_name=${tag}`, + "-f", + `target_commitish=${sha}`, + ]; + if ( + run("git", ["cat-file", "-e", `${sha}:.github/release.yml`], "ignore") + .status === 0 + ) + notesArgs.push("-f", "configuration_file_path=.github/release.yml"); + if (previousTag) notesArgs.push("-f", `previous_tag_name=${previousTag}`); + const generatedResult = run("gh", notesArgs); + const generated = JSON.parse(generatedResult.stdout); + if (typeof generated.body !== "string") + throw new Error("Generated GitHub release notes must be a string."); + output(generatedResult); + const tagged = run( + "git", + ["show", `${sha}:.github/release-notes.md`], + "ignore", + ); + const generatedNotes = generated.body.replace(/(?:\r?\n)+$/u, ""); + if (generatedNotes.length === 0) + throw new Error("Generated GitHub release notes must not be empty."); + const notes = composeReleaseNotes( + generatedNotes, + resolveReleaseSummary( + version, + tagged.status === 0 ? tagged.stdout : undefined, + existingNotes, + ), + ); + const notesFile = join(workspace, "published-notes.md"); + writeFileSync(notesFile, notes); + if (existing !== undefined) { + const latest = githubReleaseOrMissing( + run, + `${base}/releases/latest`, + "current Latest GitHub Release", + ); + if ( + latest !== undefined && + (typeof latest.tag_name !== "string" || latest.tag_name.length === 0) + ) + throw new Error("Unable to resolve the current Latest GitHub Release."); + const currentlyLatest = String(latest?.tag_name === tag); + const notesChanged = !Buffer.from(existingNotes).equals(Buffer.from(notes)); + if (notesChanged || currentlyLatest !== makeLatest) { + const args = [`--latest=${makeLatest}`]; + if (notesChanged) args.push("--notes-file", notesFile); + verifyTag(); + release("edit", args); + if (notesChanged) + console.log( + "Updated existing GitHub Release with reviewed and generated notes.", + ); + if (currentlyLatest !== makeLatest) + console.log("Updated existing GitHub Release Latest status."); + } else console.log("The verified GitHub Release already exists."); + return; + } + verifyTag(); + release( + "create", + [ + "--title", + `Codex Security ${version}`, + "--verify-tag", + "--notes-file", + notesFile, + `--latest=${makeLatest}`, + ], + [archive], + ); +} diff --git a/sdk/typescript/tests-ts/release-automation.test.ts b/sdk/typescript/tests-ts/release-automation.test.ts index b44502db7..45abb90a8 100644 --- a/sdk/typescript/tests-ts/release-automation.test.ts +++ b/sdk/typescript/tests-ts/release-automation.test.ts @@ -1,6 +1,7 @@ import { spawnSync } from "node:child_process"; import { createHash } from "node:crypto"; import { + existsSync, mkdirSync, mkdtempSync, readFileSync, @@ -10,7 +11,8 @@ import { import { tmpdir } from "node:os"; import { join, resolve } from "node:path"; import { fileURLToPath, pathToFileURL } from "node:url"; -import { afterEach, describe, expect, test } from "bun:test"; +import { afterEach, describe, expect, spyOn, test } from "bun:test"; +import { publishGitHubRelease } from "../scripts/publish-github-release.mjs"; import { createTemporaryDirectoriesSync } from "./support/temporary-directories.js"; import { bashCommand, runCommand } from "./support/shell.js"; @@ -61,6 +63,147 @@ function runShell( }); } +function runPublisher({ + existingLookup = "existing", + existingNotes = "Generated release notes", + generatedNotes = "Generated release notes\n", + + latestTag = "npm-v0.1.3", + makeLatest = false, + releaseConfiguration = true, + releaseSummary = "__missing__", + tagType = "commit", + tagObject = releaseCommit, + peeledCommit = "", + downloadedArchive = archive, +} = {}) { + const workspace = mkdtempSync(join(tmpdir(), "github release ")); + const archivePath = join(workspace, "verified archive.tgz"); + writeFileSync(archivePath, archive); + const calls: string[][] = []; + let stdout = "", + stderr = "", + status = 0; + const logs = spyOn(console, "log").mockImplementation((text) => { + stdout += `${text}\n`; + }); + const errors = spyOn(console, "error").mockImplementation((text) => { + stderr += `${text}\n`; + }); + const response = (code: number, body: unknown) => ({ + status: code === 200 ? 0 : 1, + stdout: `HTTP/2.0 ${code} Response\r\ncontent-type: application/json\r\n\r\n${JSON.stringify(body)}\n`, + }); + const lookup = (kind: string) => { + if (kind === "network" || kind === "__network__") + return { status: 1, stdout: "" }; + if (kind === "malformed" || kind === "__malformed_missing__") + return { status: 1, stdout: "HTTP/2.0 404 Not Found\n\nnot-json" }; + return response( + kind === "unauthorized" + ? 401 + : kind === "unavailable" || kind === "__error__" + ? 500 + : 404, + { message: "Unavailable" }, + ); + }; + try { + publishGitHubRelease( + { + GITHUB_REPOSITORY: "test/codex-security", + RELEASE_TAG: "npm-v0.1.2", + RELEASE_VERSION: "0.1.2", + RELEASE_SHA: releaseCommit, + RELEASE_ARCHIVE: archivePath, + PREVIOUS_TAG: "npm-v0.1.1", + MAKE_LATEST: String(makeLatest), + }, + workspace, + (program: string, args: string[]) => { + calls.push([program, ...args]); + const endpoint = args.find((argument) => argument.startsWith("repos/")); + if (program === "gh" && args[0] === "api") { + switch (endpoint) { + case "repos/test/codex-security/releases/tags/npm-v0.1.2": + return existingLookup !== "existing" + ? lookup(existingLookup) + : response(200, { + tag_name: "npm-v0.1.2", + draft: false, + prerelease: false, + body: existingNotes, + assets: [{ name: "verified archive.tgz", digest }], + }); + case "repos/test/codex-security/releases/latest": + return latestTag.startsWith("__") + ? lookup(latestTag) + : response(200, { tag_name: latestTag }); + case "repos/test/codex-security/git/ref/tags/npm-v0.1.2": + return { + status: tagType === "missing" ? 1 : 0, + stdout: `${tagType}\t${tagObject}\n`, + }; + case `repos/test/codex-security/git/tags/${tagObject}`: + return { status: 0, stdout: `${peeledCommit}\n` }; + case "repos/test/codex-security/releases/generate-notes": + expect(args).toContain("previous_tag_name=npm-v0.1.1"); + expect( + args.includes("configuration_file_path=.github/release.yml"), + ).toBe(releaseConfiguration); + return { + status: 0, + stdout: JSON.stringify({ + body: generatedNotes, + }), + }; + } + } + if (program === "git" && args[0] === "cat-file") + return { status: releaseConfiguration ? 0 : 1, stdout: "" }; + if (program === "git" && args[0] === "show") + return { + status: releaseSummary === "__missing__" ? 1 : 0, + stdout: `${releaseSummary}\n`, + }; + if (program === "gh" && args[0] === "release") { + const value = (flag: string) => args[args.indexOf(flag) + 1]!; + if (args[1] === "download") + writeFileSync( + join(value("--dir"), value("--pattern")), + downloadedArchive, + ); + else if (args[1] === "edit" || args[1] === "create") { + expect(args).toContain(`--latest=${makeLatest}`); + expect(args.includes("--verify-tag")).toBe(args[1] === "create"); + if (args[1] === "create") + console.log("created verified GitHub release"); + else { + console.log(`updated latest: ${makeLatest}`); + if (args.includes("--notes-file")) + console.log( + `updated release notes: ${readFileSync(value("--notes-file"), "utf8")}`, + ); + } + } else throw new Error(`Unexpected release command: ${args}`); + return { status: 0, stdout: "" }; + } + throw new Error(`Unexpected publication command: ${program} ${args}`); + }, + ); + } catch (error) { + if (error instanceof Error) { + status = 1; + stderr += error.message; + } else status = (error as { status: number }).status; + } finally { + logs.mockRestore(); + errors.mockRestore(); + rmSync(workspace, { recursive: true, force: true }); + } + return { status, stdout, stderr, calls }; +} + function releaseLabelGhMock(handlers: readonly string[], shiftEndpoint = true) { return [ "gh() {", @@ -2830,75 +2973,19 @@ describe("GitHub release workflow safeguards", () => { peeledCommit = "", status, }) => { - const script = workflowStepShell( - githubReleaseWorkflow, - "Publish GitHub Release and generated notes", - ); - const mocks = [ - "gh() {", - ' if [[ "$1" == "api" ]]; then', - " shift", - ' if [[ "${1:-}" == "--include" ]]; then shift; fi', - ' if [[ "${1:-}" == "--method" ]]; then shift 2; fi', - ' case "$1" in', - ' "repos/test/codex-security/releases/tags/npm-v0.1.2")', - ' case "$MOCK_EXISTING_LOOKUP" in', - " missing)", - " printf '%s\\n' 'HTTP/2.0 404 Not Found' 'content-type: application/json' '' '{\"message\":\"Not Found\"}'", - " ;;", - " unavailable)", - " printf '%s\\n' 'HTTP/2.0 500 Internal Server Error' 'content-type: application/json' '' '{\"message\":\"Unavailable\"}'", - " ;;", - " unauthorized)", - " printf '%s\\n' 'HTTP/2.0 401 Unauthorized' 'content-type: application/json' '' '{\"message\":\"Bad credentials\"}'", - " ;;", - " network) ;;", - " malformed)", - " printf '%s\\n' 'HTTP/2.0 404 Not Found' 'content-type: application/json' '' 'not-json'", - " ;;", - " esac", - " return 1", - " ;;", - ' "repos/test/codex-security/git/ref/tags/npm-v0.1.2")', - ' if [[ "$MOCK_TAG_TYPE" == "missing" ]]; then return 1; fi', - ' printf \'%s\\t%s\\n\' "$MOCK_TAG_TYPE" "$MOCK_TAG_OBJECT"', - " ;;", - ' "repos/test/codex-security/git/tags/"*)', - " printf '%s\\n' \"$MOCK_PEELED_COMMIT\"", - " ;;", - ' "repos/test/codex-security/releases/generate-notes")', - " printf '%s\\n' '{\"body\":\"Generated release notes\"}'", - " ;;", - " *) return 65 ;;", - " esac", - ' elif [[ "$1" == "release" && "$2" == "create" ]]; then', - " printf '%s\\n' 'created verified GitHub release'", - " else", - " return 64", - " fi", - "}", - ].join("\n"); - const result = await runCommand(bash, [], { - input: `${mocks}\n${script}`, - cwd: fileURLToPath(new URL("../../../", import.meta.url)), - env: { - ...process.env, - GITHUB_REPOSITORY: "test/codex-security", - MAKE_LATEST: "false", - MOCK_EXISTING_LOOKUP: existingLookup, - MOCK_PEELED_COMMIT: peeledCommit, - MOCK_TAG_OBJECT: tagObject, - MOCK_TAG_TYPE: tagType, - PREVIOUS_TAG: "npm-v0.1.1", - RELEASE_ARCHIVE: "/tmp/openai-codex-security-0.1.2.tgz", - RELEASE_SHA: releaseCommit, - RELEASE_TAG: "npm-v0.1.2", - RELEASE_VERSION: "0.1.2", - }, - timeout: 10_000, + const result = runPublisher({ + existingLookup, + tagType, + tagObject, + peeledCommit, }); expect(result.status).toBe(status); + expect( + result.calls.some( + (call) => call[1] === "release" && call[2] === "create", + ), + ).toBe(status === 0); if (status === 0) { expect(result.stdout).toContain("created verified GitHub release"); } else { @@ -2912,14 +2999,75 @@ describe("GitHub release workflow safeguards", () => { }, ); + test.each([0, 17, 143])( + "runs publication and cleans up after exit %p", + async (exit) => { + const workspace = mkdtempSync(join(tmpdir(), "publish boundary ")); + const preload = join(workspace, "commands.mjs"); + const receipt = join(workspace, "notes-path"); + writeFileSync( + preload, + ` + import childProcess from "node:child_process"; + import { syncBuiltinESMExports } from "node:module"; + import { readFileSync, writeFileSync } from "node:fs"; + childProcess.spawnSync = (program, args, options) => { + if (program === "git") return { status: 1, stdout: "" }; + if (program !== "gh") throw new Error("Unexpected command: " + program); + if (args.includes("repos/test/codex-security/releases/tags/npm-v0.1.2")) { + return { status: 1, stdout: 'HTTP/2.0 404 Not Found\\n\\n{"message":"Not Found"}' }; + } + if (args.includes("repos/test/codex-security/releases/generate-notes")) { + return { status: 0, stdout: '{"body":"Generated notes"}' }; + } + if (args.includes("repos/test/codex-security/git/ref/tags/npm-v0.1.2")) { + return { status: 0, stdout: "commit\\t${releaseCommit}\\n" }; + } + if (args[0] !== "release" || args[1] !== "create" || options.stdio[1] !== "inherit") { + throw new Error("Unexpected command: " + args); + } + const notes = args[args.indexOf("--notes-file") + 1]; + if (readFileSync(notes, "utf8") !== "Generated notes") throw new Error("Wrong notes"); + writeFileSync(${JSON.stringify(receipt)}, notes); + process.stdout.write("release output\\n"); + return { status: ${exit === 143 ? "null" : exit}, signal: ${JSON.stringify(exit === 143 ? "SIGTERM" : null)} }; + }; + syncBuiltinESMExports(); + `, + ); + try { + const result = await runShell( + workflowStepShell( + githubReleaseWorkflow, + "Publish GitHub Release and generated notes", + ), + { + NODE_OPTIONS: `--import=${pathToFileURL(preload).href}`, + GH_TOKEN: "synthetic-unused-token", + GITHUB_REPOSITORY: "test/codex-security", + RELEASE_TAG: "npm-v0.1.2", + RELEASE_VERSION: "0.1.2", + RELEASE_SHA: releaseCommit, + RELEASE_ARCHIVE: join(workspace, "verified archive.tgz"), + PREVIOUS_TAG: "", + MAKE_LATEST: "false", + }, + { cwd: fileURLToPath(new URL("../../../", import.meta.url)) }, + ); + expect(result.status).toBe(exit); + expect(result.stdout).toContain("release output"); + expect(result.stderr).toBe(""); + expect(existsSync(readFileSync(receipt, "utf8"))).toBe(false); + } finally { + rmSync(workspace, { recursive: true, force: true }); + } + }, + ); + test("explicitly prevents historical releases becoming latest", () => { - expect(githubReleaseWorkflow).toContain( - '"repos/$GITHUB_REPOSITORY/releases/generate-notes"', - ); - expect(githubReleaseWorkflow).toContain( - '--notes-file "$published_notes_file"', - ); - expect(githubReleaseWorkflow).toContain('--latest="$MAKE_LATEST"'); + const result = runPublisher({ existingLookup: "missing" }); + expect(result.status).toBe(0); + expect(result.calls.at(-1)).toContain("--latest=false"); expect(githubReleaseWorkflow).toContain("release-history"); }); @@ -2930,9 +3078,12 @@ describe("GitHub release workflow safeguards", () => { expect(githubReleaseWorkflow).toContain( "steps.history.outputs.previous-tag", ); - expect(githubReleaseWorkflow).toContain( - 'notes_args+=(-f "previous_tag_name=$PREVIOUS_TAG")', - ); + const result = runPublisher({ existingLookup: "missing" }); + expect( + result.calls.find((call) => + call.includes("repos/test/codex-security/releases/generate-notes"), + ), + ).toContain("previous_tag_name=npm-v0.1.1"); }); test("recovers a public npm tarball after release artifacts expire", () => { @@ -2944,12 +3095,17 @@ describe("GitHub release workflow safeguards", () => { }); test("downloads and verifies existing GitHub release asset bytes", () => { - expect(githubReleaseWorkflow).toContain( - 'gh release download "$RELEASE_TAG"', - ); - expect(githubReleaseWorkflow).toContain( - 'verify-github-release "$RELEASE_ARCHIVE" "$RELEASE_TAG"', + const result = runPublisher({ + downloadedArchive: Buffer.from("wrong archive"), + }); + expect(result.status).toBe(1); + expect(result.stderr).toContain( + "Existing GitHub Release asset must match the verified npm artifact.", ); + expect(result.calls.map((call) => call.slice(0, 3))).toEqual([ + ["gh", "api", "--include"], + ["gh", "release", "download"], + ]); }); test.each([ @@ -3092,7 +3248,7 @@ describe("GitHub release workflow safeguards", () => { { description: "stale generated notes with a marked historical summary", existingNotes: - "\\n## Highlights\\nPreserved historical summary\\n\\n\\nStale generated notes", + "\n## Highlights\nPreserved historical summary\n\n\nStale generated notes", expectedSummary: "Preserved historical summary", updated: true, latestUpdated: false, @@ -3101,7 +3257,7 @@ describe("GitHub release workflow safeguards", () => { { description: "CRLF historical summary marker block", existingNotes: - "\\r\\nPreserved historical summary\\r\\n\\r\\n\\r\\nStale generated notes", + "\r\nPreserved historical summary\r\n\r\n\r\nStale generated notes", expectedSummary: "Preserved historical summary", updated: true, latestUpdated: false, @@ -3110,7 +3266,7 @@ describe("GitHub release workflow safeguards", () => { { description: "CRLF marker-only body with a terminal line ending", existingNotes: - "\\r\\nPreserved historical summary\\r\\n\\r\\n", + "\r\nPreserved historical summary\r\n\r\n", expectedSummary: "Preserved historical summary", updated: true, latestUpdated: false, @@ -3128,7 +3284,7 @@ describe("GitHub release workflow safeguards", () => { { description: "standalone marker pair outside the canonical prefix", existingNotes: - "* fix: generated title\\n\\nUnreviewed injected highlight\\n", + "* fix: generated title\n\nUnreviewed injected highlight\n", excludedSummary: "Unreviewed injected highlight", updated: true, latestUpdated: false, @@ -3137,7 +3293,7 @@ describe("GitHub release workflow safeguards", () => { { description: "duplicate standalone marker pair after a blank line", existingNotes: - "\\nFirst\\n\\n\\n\\nSecond\\n", + "\nFirst\n\n\n\nSecond\n", expectedError: "Existing release summary markers are malformed.", updated: false, latestUpdated: false, @@ -3146,7 +3302,7 @@ describe("GitHub release workflow safeguards", () => { { description: "NUL-bearing historical summary", existingNotes: - "\\nReviewed\\u0000summary\\n", + "\nReviewed\u0000summary\n", expectedError: "Existing release summary is empty.", updated: false, latestUpdated: false, @@ -3164,7 +3320,7 @@ describe("GitHub release workflow safeguards", () => { { description: "malformed historical summary markers", existingNotes: - "\\nIncomplete summary", + "\nIncomplete summary", expectedError: "Existing release summary markers are malformed.", updated: false, latestUpdated: false, @@ -3178,14 +3334,14 @@ describe("GitHub release workflow safeguards", () => { }, { description: "already current with trailing line feeds", - existingNotes: "Generated release notes\\n\\n", + existingNotes: "Generated release notes\n\n", updated: false, latestUpdated: false, status: 0, }, { description: "already current with CRLF-equivalent generated notes", - existingNotes: "Generated release notes\\r\\n", + existingNotes: "Generated release notes\r\n", generatedNotes: "Generated release notes\r\n", updated: false, latestUpdated: false, @@ -3193,8 +3349,16 @@ describe("GitHub release workflow safeguards", () => { }, { description: "already current with NUL-bearing generated notes", - existingNotes: "Generated release notes\\u0000", - generatedNotesBase64: "R2VuZXJhdGVkIHJlbGVhc2Ugbm90ZXMA", + existingNotes: "Generated release notes\u0000", + generatedNotes: "Generated release notes\0", + updated: false, + latestUpdated: false, + status: 0, + }, + { + description: "different strings encoding to the same UTF-8 bytes", + existingNotes: "\ud800", + generatedNotes: "\ufffd", updated: false, latestUpdated: false, status: 0, @@ -3293,7 +3457,7 @@ describe("GitHub release workflow safeguards", () => { async ({ existingNotes = "Generated release notes", generatedNotes = "Generated release notes\n", - generatedNotesBase64, + updated, latestTag = "npm-v0.1.3", makeLatest = false, @@ -3308,175 +3472,33 @@ describe("GitHub release workflow safeguards", () => { peeledCommit = "", status, }) => { - const script = workflowStepShell( - githubReleaseWorkflow, - "Publish GitHub Release and generated notes", - ); - const mocks = [ - "gh() {", - ' if [[ "$1" == "api" ]]; then', - " shift", - " local method=GET", - " local include=false", - ' if [[ "${1:-}" == "--include" ]]; then', - " include=true", - " shift", - " fi", - ' if [[ "${1:-}" == "--method" ]]; then', - ' method="$2"', - " shift 2", - " fi", - ' case "$method $1" in', - ' "GET repos/test/codex-security/releases/tags/npm-v0.1.2")', - ' if [[ "$include" != "true" ]]; then return 71; fi', - ' printf \'HTTP/2.0 200 OK\\ncontent-type: application/json\\n\\n{"tag_name":"npm-v0.1.2","draft":false,"prerelease":false,"body":"%s","assets":[]}\\n\' "$MOCK_EXISTING_NOTES"', - " ;;", - ' "GET repos/test/codex-security/releases/latest")', - ' if [[ "$include" != "true" ]]; then return 71; fi', - ' if [[ "$MOCK_LATEST_TAG" == "__missing__" ]]; then', - " printf '%s\\n' 'HTTP/2.0 404 Not Found' 'content-type: application/json' '' '{\"message\":\"Not Found\"}'", - " return 1", - " fi", - ' if [[ "$MOCK_LATEST_TAG" == "__error__" ]]; then', - " printf '%s\\n' 'HTTP/2.0 500 Internal Server Error' 'content-type: application/json' '' '{\"message\":\"Unavailable\"}'", - " return 1", - " fi", - ' if [[ "$MOCK_LATEST_TAG" == "__network__" ]]; then return 1; fi', - ' if [[ "$MOCK_LATEST_TAG" == "__malformed_missing__" ]]; then', - " printf '%s\\n' 'HTTP/2.0 404 Not Found' 'content-type: application/json' '' 'not-json'", - " return 1", - " fi", - ' printf \'HTTP/2.0 200 OK\\ncontent-type: application/json\\n\\n{"tag_name":"%s"}\\n\' "$MOCK_LATEST_TAG"', - " ;;", - ' "GET repos/test/codex-security/git/ref/tags/npm-v0.1.2")', - ' printf \'%s\\t%s\\n\' "$MOCK_TAG_TYPE" "$MOCK_TAG_OBJECT"', - " ;;", - ' "GET repos/test/codex-security/git/tags/"*)', - " printf '%s\\n' \"$MOCK_PEELED_COMMIT\"", - " ;;", - ' "POST repos/test/codex-security/releases/generate-notes")', - ' if [[ "$MOCK_RELEASE_CONFIGURATION" == "missing" &&', - ' "$*" == *"configuration_file_path=.github/release.yml"* ]]; then', - " printf '%s\\n' 'Could not find a configuration file at .github/release.yml' >&2", - " return 1", - " fi", - " printf '%s' \"$MOCK_GENERATED_NOTES_RESPONSE\"", - " ;;", - " *) return 65 ;;", - " esac", - ' elif [[ "$1" == "release" ]]; then', - " shift", - ' case "$1" in', - " download)", - " shift", - " local destination= pattern=", - " while (( $# > 0 )); do", - ' case "$1" in', - ' --dir) destination="$2"; shift 2 ;;', - ' --pattern) pattern="$2"; shift 2 ;;', - " --repo) shift 2 ;;", - " *) shift ;;", - " esac", - " done", - " printf '%s\\n' 'verified archive' > \"$destination/$pattern\"", - " ;;", - " edit)", - " shift", - " local edit_latest= notes_file= next_is_notes_file=false", - ' for argument in "$@"; do', - ' if [[ "$next_is_notes_file" == true ]]; then', - ' notes_file="$argument"', - " next_is_notes_file=false", - " continue", - " fi", - ' if [[ "$argument" == "--verify-tag" ]]; then', - " printf '%s\\n' 'unknown flag: --verify-tag' >&2", - " return 67", - " fi", - ' if [[ "$argument" == --latest=* ]]; then', - ' edit_latest="${argument#--latest=}"', - " fi", - ' if [[ "$argument" == "--notes-file" ]]; then', - " next_is_notes_file=true", - " fi", - " done", - ' if [[ "$edit_latest" != "$MOCK_MAKE_LATEST" ]]; then return 68; fi', - " printf 'updated latest: %s\\n' \"$edit_latest\"", - ' if [[ -n "$notes_file" ]]; then', - " printf '%s: ' 'updated release notes'", - ' cat "$notes_file"', - " fi", - " ;;", - " create) return 70 ;;", - " *) return 66 ;;", - " esac", - " else", - " return 64", - " fi", - "}", - "git() {", - ' if [[ "$1" == "cat-file" && "$2" == "-e" &&', - ' "$3" == "$RELEASE_SHA:.github/release.yml" ]]; then', - ' [[ "$MOCK_RELEASE_CONFIGURATION" == "present" ]]', - " return", - " fi", - ' if [[ "$1" == "cat-file" && "$2" == "-e" &&', - ' "$3" == "$RELEASE_SHA:.github/release-notes.md" ]]; then', - ' [[ "$MOCK_RELEASE_SUMMARY" != "__missing__" ]]', - " return", - " fi", - ' if [[ "$1" == "show" &&', - ' "$2" == "$RELEASE_SHA:.github/release-notes.md" ]]; then', - ' if [[ "$MOCK_RELEASE_SUMMARY" == "__missing__" ]]; then return 1; fi', - " printf '%s\\n' \"$MOCK_RELEASE_SUMMARY\"", - " return", - " fi", - ' command git "$@"', - "}", - "node() {", - ' if [[ "${1:-}" == "sdk/typescript/scripts/release-automation.mjs" &&', - ' "${2:-}" == "verify-github-release" ]]; then', - " cat >/dev/null", - " printf '%s\\n' 'verified existing GitHub release asset'", - " return 0", - " fi", - ' command node "$@"', - "}", - ].join("\n"); - const result = await runCommand(bash, [], { - input: `${mocks}\n${script}`, - cwd: fileURLToPath(new URL("../../../", import.meta.url)), - env: { - ...process.env, - GITHUB_REPOSITORY: "test/codex-security", - MAKE_LATEST: String(makeLatest), - MOCK_GENERATED_NOTES_RESPONSE: JSON.stringify({ - body: - generatedNotesBase64 === undefined - ? generatedNotes - : Buffer.from(generatedNotesBase64, "base64").toString("utf8"), - }), - MOCK_EXISTING_NOTES: existingNotes, - MOCK_LATEST_TAG: latestTag, - MOCK_MAKE_LATEST: String(makeLatest), - MOCK_PEELED_COMMIT: peeledCommit, - MOCK_RELEASE_CONFIGURATION: releaseConfiguration - ? "present" - : "missing", - MOCK_RELEASE_SUMMARY: releaseSummary, - MOCK_TAG_OBJECT: tagObject, - MOCK_TAG_TYPE: tagType, - PREVIOUS_TAG: "npm-v0.1.1", - RELEASE_ARCHIVE: "/tmp/openai-codex-security-0.1.2.tgz", - RELEASE_SHA: releaseCommit, - RELEASE_TAG: "npm-v0.1.2", - RELEASE_VERSION: "0.1.2", - }, - timeout: releaseWorkflowTimeout, + const result = runPublisher({ + existingNotes, + generatedNotes, + + latestTag, + makeLatest, + releaseConfiguration, + releaseSummary, + tagType, + tagObject, + peeledCommit, }); + expect( + result.calls.some( + (call) => call[1] === "release" && call[2] === "edit", + ), + ).toBe(updated || latestUpdated); + if (updated || latestUpdated) { + expect(result.calls.at(-2)).toContain( + tagType === "tag" + ? `repos/test/codex-security/git/tags/${tagObject}` + : "repos/test/codex-security/git/ref/tags/npm-v0.1.2", + ); + } expect(result.status).toBe(status); - expect(result.stdout).toContain("verified existing GitHub release asset"); + expect(result.stdout).toContain(`"digest":"${digest}"`); if (status !== 0) { if (expectedError !== undefined) { expect(result.stderr).toContain(expectedError); @@ -3527,69 +3549,41 @@ describe("GitHub release workflow safeguards", () => { test("routes release-note validation through the shared helper", () => { expect(protectedReleaseWorkflow).toContain("validate-release-notes"); expect(releaseCutWorkflow).toContain("validate-release-notes"); - expect(githubReleaseWorkflow).toContain("compose-release-notes"); + expect(githubReleaseWorkflow).toContain("publishGitHubRelease"); expect(protectedReleaseWorkflow).not.toContain("summary_header="); expect(releaseCutWorkflow).not.toContain("summary_header="); expect(githubReleaseWorkflow).not.toContain("CODEX_SECURITY_SUMMARY_START"); }); - test.each([ - { - description: "no optional note files", - arraySetup: "release_note_args=()", - optionalArguments: [], - }, - { - description: "quoted note-file paths", - arraySetup: [ - 'tagged_notes_file="/tmp/tagged notes.md"', - 'existing_notes_file="/tmp/existing notes.md"', - "release_note_args=(", - ' --tagged-notes-file "$tagged_notes_file"', - ' --existing-notes-file "$existing_notes_file"', - ")", - ].join("\n"), - optionalArguments: [ - "--tagged-notes-file", - "/tmp/tagged notes.md", - "--existing-notes-file", - "/tmp/existing notes.md", - ], - }, - ])( - "passes $description to release-note composition under nounset", - async ({ arraySetup, optionalArguments }) => { - const publishStep = workflowStepShell( - githubReleaseWorkflow, - "Publish GitHub Release and generated notes", - ); - const composeCommand = publishStep.match( - /(?node sdk\/typescript\/scripts\/release-automation\.mjs \\\n\s+compose-release-notes \\\n[\s\S]*?) \\\n\s*> "\$published_notes_file"/u, - )?.groups?.["command"]; - expect(composeCommand).toBeDefined(); - - const result = await runCommand(bash, [], { - input: [ - "set -u", - arraySetup, - 'generated_notes_file="/tmp/generated notes.md"', - "node() { printf '<%s>\\n' \"$@\"; }", - composeCommand ?? "exit 70", - ].join("\n"), - env: { ...process.env, RELEASE_VERSION: "0.1.2" }, - timeout: 10_000, - }); - - expect(result.status).toBe(0); - expect(result.stdout.trim().split("\n")).toEqual( - [ - "sdk/typescript/scripts/release-automation.mjs", + test.each([false, true])( + "composes notes with optional files: %p", + async (optional) => { + const workspace = mkdtempSync(join(tmpdir(), "release notes ")); + const generated = join(workspace, "generated notes.md"); + const tagged = join(workspace, "tagged notes.md"); + const existing = join(workspace, "existing notes.md"); + try { + writeFileSync(generated, "Generated notes\n"); + writeFileSync( + tagged, + "\nReviewed summary", + ); + writeFileSync(existing, "Old generated notes"); + const result = await runCommand("node", [ + fileURLToPath(automationScript), "compose-release-notes", "0.1.2", - "/tmp/generated notes.md", - ...optionalArguments, - ].map((argument) => `<${argument}>`), - ); + generated, + ...(optional + ? ["--tagged-notes-file", tagged, "--existing-notes-file", existing] + : []), + ]); + expect(result.status).toBe(0); + expect(result.stdout).toContain("Generated notes"); + expect(result.stdout.includes("Reviewed summary")).toBe(optional); + } finally { + rmSync(workspace, { recursive: true, force: true }); + } }, ); @@ -3883,27 +3877,15 @@ describe("GitHub release workflow safeguards", () => { "fix: preserve a trailing line feed\n", "fix: preserve a trailing carriage return\r", ])("rejects nonconventional pull request title %s", async (title) => { - const script = workflowStepShell( - releaseLabelsWorkflow, - "Categorize pull request without checking out its code", - ); - const mock = [ - "gh() {", - ' if [[ "$1" != "api" ]]; then return 64; fi', - " shift", - ' if [[ "$1" == "repos/test/codex-security/issues/17" ]]; then', - " printf '%s' \"$MOCK_PR_TITLE\" | base64", - " return 0", - " fi", - " printf '%s\\n' 'unexpected label mutation'", - " return 70", - "}", - ].join("\n"); - const result = await runShell(`${mock}\n${script}`, { - GITHUB_REPOSITORY: "test/codex-security", - MOCK_PR_TITLE: title, - PR_NUMBER: "17", - }); + const result = await runReleaseLabels(title, [ + ' "GET repos/test/codex-security/issues/17")', + " printf '%s' \"$MOCK_PR_TITLE\" | base64", + " ;;", + " *)", + " printf '%s\\n' 'unexpected label mutation'", + " return 70", + " ;;", + ]); expect(result.status).toBe(1); expect(result.stderr).toContain(