#!/bin/sh set -eu cd "$(git rev-parse --show-toplevel)" if [ "$(git symbolic-ref --short -q HEAD)" != main ]; then echo "push-main: check out main first" >&2 exit 1 fi commit=$(git rev-parse HEAD) work=$(mktemp -d "${TMPDIR:-/tmp}/push-main.XXXXXX") trap 'rm -rf "$work"' EXIT trap 'exit 130' HUP INT TERM mkdir "$work/tree" git archive -o "$work/tree.tar" "$commit" tar -x -f "$work/tree.tar" -C "$work/tree" ( cd "$work/tree" npm test npm run --if-present typecheck npx --yes @kodycodes/cli@latest dev check --target mock ) pkg=$(cd "$work/tree" && node -p 'require("./package.json").name') git push origin "${commit}:refs/heads/main" if npx --yes @kodycodes/cli@latest package publish "$pkg" >"$work/out" 2>"$work/err"; then cli=0 else cli=$? fi node -e ' const fs = require("node:fs") const [outFile, errFile, commit, cli] = process.argv.slice(1) const fail = (line) => { console.error(`push-main: ${line}`) console.error("push-main: this run printed no valid Promote link. Do not Promote.") process.exit(1) } let body = null try { body = JSON.parse(fs.readFileSync(outFile, "utf8")) } catch {} const status = typeof body?.status === "string" ? body.status : null if (status === "checks_failed") { console.error("push-main: Kody refused the publish; its checks failed:") for (const check of body.failed_checks ?? []) { console.error(`- ${check.kind}: ${check.message ?? "(no message)"}`) } fail("fix the failed checks, commit, and rerun scripts/push-main.sh.") } if (status === "locked" || status === "approval_required") { if (cli !== "0" && cli !== "1") { fail(`Kody answered ${status}, but kody package publish exited ${cli}`) } let linked = null try { linked = new URL(body.approval_url).searchParams.get("commit") } catch {} if (linked !== commit) { fail(`Kody answered ${status} with an approval link for commit ${linked ?? "(none)"}, not the pushed ${commit}`) } if (typeof body.message === "string" && body.message.trim()) { console.error(`push-main: Kody says: ${body.message.trim()}`) } console.log(`push-main: Kody checks passed for ${commit}. This is the only valid Promote link:`) console.log(body.approval_url) process.exit(0) } if (status === "published" || status === "already_published") { if (cli !== "0") { fail(`Kody answered ${status}, but kody package publish exited ${cli}`) } if (body.published_commit !== commit) { fail(`Kody answered ${status} for commit ${body.published_commit}, not the pushed ${commit}`) } console.log(`push-main: Kody ${status.replace("_", " ")} ${commit}`) process.exit(0) } if (status === "dispatched") { const workflow = typeof body.workflow_id === "string" ? ` as workflow ${body.workflow_id}` : "" fail(`Kody runs the publish in the background${workflow}; it has not checked ${commit} yet`) } const lastErr = fs.readFileSync(errFile, "utf8").trim().split("\n").pop() fail(`kody package publish exited ${cli}${status ? ` with status ${status}` : ""}: ${body?.message ?? (lastErr || "(no output)")}`) ' "$work/out" "$work/err" "$commit" "$cli"