diff --git a/.github/workflows/closura-arc-ci.yml b/.github/workflows/closura-arc-ci.yml index 6d1dff8ee64cb..cb9f3d471eea9 100644 --- a/.github/workflows/closura-arc-ci.yml +++ b/.github/workflows/closura-arc-ci.yml @@ -111,7 +111,7 @@ jobs: if [ -f .git/shallow ]; then unshallow=(--unshallow); fi git fetch --no-tags --filter=blob:none "${unshallow[@]}" origin "${{ github.base_ref || github.ref_name }}:refs/remotes/origin/${{ github.base_ref || github.ref_name }}" git fetch --no-tags --filter=blob:none https://github.com/pingdotgg/t3code.git main:refs/remotes/upstream/main - node --test scripts/fork-drift-receipt.test.mjs + node --test scripts/fork-drift-receipt.test.mjs scripts/fork-sync-plan.test.mjs # Exit 1 means the release line is past its drift budget, which is # reported rather than failed. Exit 2 means the receipt itself broke. node scripts/fork-drift-receipt.mjs --base "origin/${{ github.base_ref || github.ref_name }}" || rc=$? diff --git a/.github/workflows/upstream-sync.yml b/.github/workflows/upstream-sync.yml new file mode 100644 index 0000000000000..a192a63d5ac81 --- /dev/null +++ b/.github/workflows/upstream-sync.yml @@ -0,0 +1,46 @@ +name: Upstream fork sync + +# Fetches pingdotgg/t3code main once a day. A clean merge becomes a pull +# request against release/native-0.1.10. Conflicts are filed for Platform / +# CTO. This workflow never merges that pull request and never force-pushes. +# It runs on a hosted runner because it only fetches and merges git history; +# it does not execute upstream code. The fork's ARC workflow still has to +# pass before anyone merges. +on: + schedule: + - cron: "15 8 * * *" + workflow_dispatch: + +permissions: + contents: write + pull-requests: write + issues: write + +concurrency: + group: t3-upstream-sync + cancel-in-progress: false + +jobs: + sync: + if: ${{ github.repository == 'nullStack65/t3code' }} + runs-on: ubuntu-latest + timeout-minutes: 30 + steps: + - name: Checkout the release line + uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 + with: + ref: release/native-0.1.10 + fetch-depth: 0 + persist-credentials: true + + - name: Sync upstream main + shell: bash + env: + UPSTREAM_URL: https://github.com/pingdotgg/t3code.git + BASE_BRANCH: release/native-0.1.10 + GH_REPO: nullStack65/t3code + # Empty when the secret is unset. gh then uses the default token, + # which can open the pull request but cannot start its checks. + T3_FORK_SYNC_TOKEN: ${{ secrets.T3_FORK_SYNC_TOKEN }} + GH_TOKEN: ${{ secrets.T3_FORK_SYNC_TOKEN || github.token }} + run: bash scripts/upstream-sync.sh diff --git a/docs/operations/fork-sync.md b/docs/operations/fork-sync.md index 35871b51796a9..3c823648cbe07 100644 --- a/docs/operations/fork-sync.md +++ b/docs/operations/fork-sync.md @@ -2,23 +2,39 @@ The fleet builds and deploys from `release/native-0.1.10` on `nullStack65/t3code`. Upstream is `pingdotgg/t3code` `main`, fetched as -`upstream/main`. This says how the two stay close. It does not perform a sync. +`upstream/main`. This says how the two stay close. The daily workflow performs +the fetch and opens the pull request. Merging that pull request is still a +separate step. ## Cadence -Once a week, Builder opens a pull request that merges `upstream/main` into a -branch cut from `release/native-0.1.10`, named `sync/upstream-`. -The pull request runs the fork's ARC CI. It merges only when that CI is green -and the one bot review round is done, like any other change. +`.github/workflows/upstream-sync.yml` runs once a day. It fetches +`upstream/main` and, when the merge into a branch cut from +`release/native-0.1.10` is clean, opens or updates +`sync/upstream-`. The pull request runs the fork's ARC CI. It +merges only when that CI is green and the one bot review round is done, like +any other change. The workflow does not merge it. + +A clean update of an existing sync branch is a merge, never a force-push. If +the push is not a fast-forward, the workflow stops and says so. + +The workflow opens the pull request with `T3_FORK_SYNC_TOKEN` when that +repository secret is set, so the pull request checks start. Without the +secret, the default token can open the pull request and cannot start the +other workflows. The body says that, and the pull request stays unmerged +until a `nullStack65` push starts CI. ## Conflict budget -A sync that conflicts in 25 files or fewer is resolved inside that pull -request. Fork files win where the fork deliberately diverges (the native -release line, the ARC workflow, the inbox and placement changes); upstream wins -everywhere else. A sync that conflicts in more than 25 files stops, and the -pull request says which files conflict and asks for a decision instead of -forcing the merge. +The daily job does not resolve conflicts. Any conflict updates one open issue, +titled `Upstream sync blocked`, naming Platform / CTO and listing the files. +It does not push a resolution. + +A person may resolve a sync of 25 files or fewer in that pull request. Fork +files win where the fork deliberately diverges (the native release line, the +ARC workflow, the inbox and placement changes); upstream wins everywhere else. +A sync that conflicts in more than 25 files still waits for a decision. Nobody +force-pushes it. ## Drift receipt @@ -34,11 +50,14 @@ node scripts/fork-drift-receipt.mjs --base origin/release/native-0.1.10 ``` The receipt measures drift. It does not count merge conflicts, because counting -those means running the merge. The 25-file conflict budget applies when the -weekly sync pull request is opened. +those means running the merge. `scripts/upstream-sync.sh` runs that merge. +`scripts/fork-sync-plan.mjs` decides whether the result may be pushed. A +conflict, a failed merge, or a rejected push is never pushed, and the decision +sets `allowForcePush` to false. ## Who resolves -Builder owns the sync pull request and resolves conflicts inside the budget. -A sync past the budget, or one whose CI fails for a reason other than -conflicts, goes back to the delegating thread with the receipt. +The daily job files the conflict issue. Platform / CTO owns a sync past the +25-file budget. A person may resolve 25 files or fewer. A sync whose CI fails +for a reason other than conflicts goes back to the delegating thread with the +receipt. diff --git a/knip.jsonc b/knip.jsonc index e3589f643c325..c096f2c2b5097 100644 --- a/knip.jsonc +++ b/knip.jsonc @@ -39,6 +39,9 @@ "setup-worktree.ts", "sign-macos.ts", "prepare-server-compile-cache.cjs", + // The daily upstream sync runs these by path. + "fork-drift-receipt.mjs", + "fork-sync-plan.mjs", "smoke-desktop-app.ts", "verify-windows-desktop-artifact.ts", ], diff --git a/scripts/fork-sync-plan.mjs b/scripts/fork-sync-plan.mjs new file mode 100644 index 0000000000000..f549753c48cac --- /dev/null +++ b/scripts/fork-sync-plan.mjs @@ -0,0 +1,128 @@ +#!/usr/bin/env node +// Decide what the daily upstream sync may do. It never resolves conflicts and +// never allows a force-push. The shell in scripts/upstream-sync.sh performs +// the fetch and the push this decision names. + +import { readFileSync } from "node:fs"; + +import { CONFLICT_BUDGET } from "./fork-drift-receipt.mjs"; + +export { CONFLICT_BUDGET }; + +const OWNER = "Platform / CTO"; + +export function decideSync({ + behind = 0, + conflictFiles = [], + pushRejected = false, + mergeFailed = false, +} = {}) { + const files = conflictFiles.filter((name) => name.length > 0); + if (pushRejected) { + return { + action: "flag-diverged", + allowForcePush: false, + owner: OWNER, + files, + }; + } + if (files.length > 0 || mergeFailed) { + return { + action: "flag-conflicts", + allowForcePush: false, + owner: OWNER, + overBudget: files.length > CONFLICT_BUDGET, + files, + }; + } + if (behind === 0) return { action: "up-to-date", allowForcePush: false, files: [] }; + return { action: "open-pr", allowForcePush: false, files: [] }; +} + +export function pullRequestBody({ behind, ahead, mergeBase, ciWillStart }) { + const checks = ciWillStart + ? "A repository secret opened this pull request, so the fork checks can start." + : "No T3_FORK_SYNC_TOKEN secret is set, so this pull request was opened with the default GitHub token. That token does not start other workflows. Platform / CTO: set T3_FORK_SYNC_TOKEN to a nullStack65 token, or push an empty commit as nullStack65, before treating CI as run. Do not merge while the required checks are missing."; + return [ + `Upstream main is ${behind} commits ahead of release/native-0.1.10. The fork has ${ahead} commits that are not upstream. Merge base \`${mergeBase}\`.`, + "", + "This merges upstream/main into a branch cut from the release line. The workflow does not merge the pull request.", + "", + checks, + "", + "Model: github-actions", + "Task: sync", + "", + "Deploy: fleet", + "", + ].join("\n"); +} + +export function issueBody({ decision, behind, ahead, mergeBase, date }) { + const files = decision.files ?? []; + const budget = + decision.overBudget === true + ? `Conflicts in ${files.length} files, past the ${CONFLICT_BUDGET}-file budget. This needs a decision before anyone resolves it.` + : `Conflicts in ${files.length} file${files.length === 1 ? "" : "s"}. The daily sync does not resolve conflicts.`; + const diverged = + decision.action === "flag-diverged" + ? "The sync branch already differs, so updating it would take a force-push. The push was not retried." + : budget; + return [ + `Platform / CTO: the upstream sync for ${date} stopped.`, + "", + diverged, + `The release line is ${behind} commits behind upstream and ${ahead} commits ahead. Merge base \`${mergeBase}\`.`, + "Do not force-push. Do not bypass CI.", + "", + "Files:", + ...(files.length > 0 + ? files.map((name) => `- ${name}`) + : ["- (push rejected before a file list was available)"]), + "", + ].join("\n"); +} + +function argument(args, name) { + const index = args.indexOf(name); + return index === -1 ? undefined : args[index + 1]; +} + +function main() { + const args = process.argv.slice(2); + const conflictsPath = argument(args, "--conflicts-file"); + const conflictFiles = + conflictsPath === undefined + ? [] + : readFileSync(conflictsPath, "utf8") + .split("\n") + .map((line) => line.trim()) + .filter((line) => line.length > 0); + const behind = Number(argument(args, "--behind") ?? "0"); + const ahead = Number(argument(args, "--ahead") ?? "0"); + const mergeBase = argument(args, "--merge-base") ?? ""; + const date = argument(args, "--date") ?? ""; + const ciWillStart = args.includes("--ci-will-start"); + const decision = decideSync({ + behind, + conflictFiles, + pushRejected: args.includes("--push-rejected"), + mergeFailed: args.includes("--merge-failed"), + }); + const body = + decision.action === "open-pr" + ? { pullRequest: pullRequestBody({ behind, ahead, mergeBase, ciWillStart }) } + : decision.action === "up-to-date" + ? {} + : { issue: issueBody({ decision, behind, ahead, mergeBase, date }) }; + process.stdout.write(`${JSON.stringify({ ...decision, ...body })}\n`); +} + +if (import.meta.url === `file://${process.argv[1]}`) { + try { + main(); + } catch (error) { + process.stderr.write(`fork-sync-plan: ${error.message}\n`); + process.exit(2); + } +} diff --git a/scripts/fork-sync-plan.test.mjs b/scripts/fork-sync-plan.test.mjs new file mode 100644 index 0000000000000..aae36616717ab --- /dev/null +++ b/scripts/fork-sync-plan.test.mjs @@ -0,0 +1,204 @@ +import assert from "node:assert/strict"; +import { execFileSync } from "node:child_process"; +import { + chmodSync, + cpSync, + mkdirSync, + mkdtempSync, + readFileSync, + rmSync, + writeFileSync, +} from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import test from "node:test"; + +import { CONFLICT_BUDGET, decideSync, issueBody, pullRequestBody } from "./fork-sync-plan.mjs"; + +const scriptDir = new URL(".", import.meta.url).pathname; + +test("a clean lag opens a pull request and forbids a force-push", () => { + const decision = decideSync({ behind: 4, conflictFiles: [] }); + assert.equal(decision.action, "open-pr"); + assert.equal(decision.allowForcePush, false); +}); + +test("any conflict flags Platform / CTO and does not resolve it", () => { + const one = decideSync({ behind: 4, conflictFiles: ["apps/server/src/server.ts"] }); + assert.equal(one.action, "flag-conflicts"); + assert.equal(one.owner, "Platform / CTO"); + assert.equal(one.overBudget, false); + assert.equal(one.allowForcePush, false); + + const files = Array.from({ length: CONFLICT_BUDGET + 1 }, (_, index) => `file-${index}.ts`); + const over = decideSync({ behind: 783, conflictFiles: files }); + assert.equal(over.overBudget, true); + assert.equal(over.allowForcePush, false); +}); + +test("a rejected push is not retried with force", () => { + const decision = decideSync({ behind: 2, pushRejected: true }); + assert.equal(decision.action, "flag-diverged"); + assert.equal(decision.allowForcePush, false); +}); + +test("an up-to-date line does nothing", () => { + assert.equal(decideSync({ behind: 0 }).action, "up-to-date"); +}); + +test("the sync pull request body has one Deploy line", () => { + const body = pullRequestBody({ + behind: 3, + ahead: 1, + mergeBase: "abc", + ciWillStart: false, + }); + const deploy = body.split("\n").filter((line) => line.startsWith("Deploy:")); + assert.deepEqual(deploy, ["Deploy: fleet"]); + assert.match(body, /Platform \/ CTO/); + assert.match(body, /Model: github-actions/); + assert.match(body, /Task: sync/); +}); + +test("the conflict issue names the owner and the files", () => { + const decision = decideSync({ + behind: 2, + conflictFiles: ["apps/web/src/components/Sidebar.tsx"], + }); + const body = issueBody({ + decision, + behind: 2, + ahead: 9, + mergeBase: "abc", + date: "2026-11-21", + }); + assert.match(body, /Platform \/ CTO/); + assert.match(body, /Sidebar\.tsx/); + assert.match(body, /Do not force-push/); +}); + +test("the workflow script never force-pushes or merges", () => { + const source = readFileSync(join(scriptDir, "upstream-sync.sh"), "utf8"); + assert.equal(source.includes("--force"), false); + assert.equal(source.includes("push -f"), false); + assert.equal(source.includes("pr merge"), false); +}); + +function git(cwd, args) { + return execFileSync("git", ["-C", cwd, ...args], { + encoding: "utf8", + env: { ...process.env, GIT_CONFIG_NOSYSTEM: "1", GIT_TERMINAL_PROMPT: "0" }, + }); +} + +function commit(cwd, file, contents, message) { + writeFileSync(join(cwd, file), contents); + git(cwd, ["add", file]); + git(cwd, ["commit", "-q", "-m", message]); +} + +function identity(cwd) { + git(cwd, ["config", "user.email", "sync@example.com"]); + git(cwd, ["config", "user.name", "sync"]); + git(cwd, ["config", "commit.gpgsign", "false"]); +} + +function fixture() { + const root = mkdtempSync(join(tmpdir(), "upstream-sync-")); + const origin = join(root, "origin.git"); + const work = join(root, "work"); + const upstream = join(root, "upstream"); + execFileSync("git", ["init", "-q", work]); + identity(work); + git(work, ["checkout", "-q", "-b", "release/native-0.1.10"]); + commit(work, "README.md", "base\n", "base"); + execFileSync("git", ["init", "--bare", "-q", origin]); + git(work, ["remote", "add", "origin", origin]); + git(work, ["push", "-q", "origin", "release/native-0.1.10"]); + execFileSync("git", ["clone", "-q", work, upstream]); + identity(upstream); + git(upstream, ["checkout", "-q", "-b", "main"]); + const bin = join(root, "bin"); + mkdirSync(bin); + writeFileSync( + join(bin, "gh"), + `#!/bin/sh +printf '%s\\n' "$*" >> "$GH_LOG" +case "$1 $2" in + "pr list"|"issue list") exit 0 ;; + "pr create") printf '%s\\n' "https://github.com/nullStack65/t3code/pull/1" ;; + "issue create") printf '%s\\n' "https://github.com/nullStack65/t3code/issues/1" ;; +esac +`, + ); + chmodSync(join(bin, "gh"), 0o755); + mkdirSync(join(work, "scripts")); + for (const name of ["fork-drift-receipt.mjs", "fork-sync-plan.mjs", "upstream-sync.sh"]) { + cpSync(join(scriptDir, name), join(work, "scripts", name)); + } + chmodSync(join(work, "scripts", "upstream-sync.sh"), 0o755); + return { root, origin, work, upstream, bin }; +} + +function decisionFrom(output) { + const line = output.split("\n").findLast((entry) => entry.startsWith("{")); + return JSON.parse(line); +} + +function runSync(ctx, extraEnv = {}) { + return execFileSync("bash", ["scripts/upstream-sync.sh"], { + cwd: ctx.work, + encoding: "utf8", + env: { + ...process.env, + PATH: `${ctx.bin}:${process.env.PATH}`, + GH_LOG: join(ctx.root, "gh.log"), + UPSTREAM_URL: ctx.upstream, + PUSH_REMOTE: "origin", + GH_REPO: "nullStack65/t3code", + SYNC_DATE: "2026-11-21", + GIT_CONFIG_NOSYSTEM: "1", + GIT_TERMINAL_PROMPT: "0", + ...extraEnv, + }, + }); +} + +test("a conflicting upstream flags and does not push", () => { + const ctx = fixture(); + try { + commit(ctx.work, "README.md", "fork\n", "fork"); + git(ctx.work, ["push", "-q", "origin", "release/native-0.1.10"]); + commit(ctx.upstream, "README.md", "upstream\n", "upstream"); + const release = git(ctx.origin, ["rev-parse", "release/native-0.1.10"]).trim(); + const output = runSync(ctx, { DRY_RUN: "1" }); + const decision = decisionFrom(output); + assert.equal(decision.action, "flag-conflicts"); + assert.equal(decision.allowForcePush, false); + assert.ok(decision.files.includes("README.md")); + assert.equal(git(ctx.origin, ["rev-parse", "release/native-0.1.10"]).trim(), release); + assert.throws(() => git(ctx.origin, ["rev-parse", "sync/upstream-2026-11-21"])); + } finally { + rmSync(ctx.root, { recursive: true, force: true }); + } +}); + +test("a clean upstream opens a sync branch without moving the release line", () => { + const ctx = fixture(); + try { + commit(ctx.upstream, "upstream.txt", "new\n", "upstream feature"); + const release = git(ctx.origin, ["rev-parse", "release/native-0.1.10"]).trim(); + const output = runSync(ctx); + const decision = decisionFrom(output); + assert.equal(decision.action, "open-pr"); + assert.equal(decision.allowForcePush, false); + assert.equal(git(ctx.origin, ["rev-parse", "release/native-0.1.10"]).trim(), release); + assert.equal(git(ctx.origin, ["rev-parse", "sync/upstream-2026-11-21"]).trim().length, 40); + const log = readFileSync(join(ctx.root, "gh.log"), "utf8"); + assert.match(log, /pr create/); + assert.equal(log.includes("pr merge"), false); + assert.equal(log.includes("--force"), false); + } finally { + rmSync(ctx.root, { recursive: true, force: true }); + } +}); diff --git a/scripts/upstream-sync.sh b/scripts/upstream-sync.sh new file mode 100755 index 0000000000000..788d3a98a49d5 --- /dev/null +++ b/scripts/upstream-sync.sh @@ -0,0 +1,172 @@ +#!/usr/bin/env bash +# Daily upstream sync for the nullStack65/t3code fork. +# +# Fetches upstream main. A clean merge into sync/upstream-, cut from the +# release line, is pushed and opened as a pull request. An existing sync branch +# is updated by merge, never by force-push. Conflicts, and a push that is not +# a fast-forward, are written to one issue for Platform / CTO. +# +# DRY_RUN=1 prints the decision and does not commit, push, or call gh. + +set -euo pipefail + +root="$(git rev-parse --show-toplevel)" +cd "$root" + +base="${BASE_BRANCH:-release/native-0.1.10}" +date="${SYNC_DATE:-$(date -u +%Y-%m-%d)}" +branch="sync/upstream-${date}" +remote="${PUSH_REMOTE:-origin}" +upstream_url="${UPSTREAM_URL:?UPSTREAM_URL is required}" +repo="${GH_REPO:-nullStack65/t3code}" +dry="${DRY_RUN:-0}" +ci_flag=() +if [ -n "${T3_FORK_SYNC_TOKEN:-}" ]; then + ci_flag=(--ci-will-start) +fi + +file_issue() { + local decision="$1" + local body + body="$(mktemp)" + node --input-type=module -e ' + const decision = JSON.parse(process.argv[1]); + process.stdout.write(decision.issue ?? ""); + ' "$decision" >"$body" + local existing + existing="$(gh issue list --repo "$repo" --state open --search "Upstream sync blocked in:title" --json number --jq '.[0].number // empty')" + if [ -z "$existing" ]; then + gh issue create --repo "$repo" --title "Upstream sync blocked" --body-file "$body" + else + gh issue comment "$existing" --repo "$repo" --body-file "$body" + fi + rm -f "$body" +} + +git fetch --no-tags "$upstream_url" main:refs/remotes/upstream/main +git fetch --no-tags "$remote" "$base:refs/remotes/origin/$base" +if git fetch --no-tags "$remote" "$branch:refs/remotes/origin/$branch" 2>/dev/null; then + have_branch=1 +else + have_branch=0 +fi + +set +e +receipt="$(node scripts/fork-drift-receipt.mjs --base "origin/$base")" +receipt_rc=$? +set -e +if [ "$receipt_rc" -eq 2 ]; then + printf '%s\n' "$receipt" >&2 + exit 2 +fi + +read -r behind ahead merge_base < <( + node --input-type=module -e ' + const drift = JSON.parse(process.argv[1]); + process.stdout.write(`${drift.behind} ${drift.ahead} ${drift.mergeBase}\n`); + ' "$receipt" +) + +plan() { + node scripts/fork-sync-plan.mjs \ + --behind "$behind" \ + --ahead "$ahead" \ + --merge-base "$merge_base" \ + --date "$date" \ + "$@" \ + "${ci_flag[@]}" +} + +stop_for() { + local decision extra=() + if [ ! -s "$conflicts_file" ] && [[ " $* " != *" --push-rejected "* ]]; then + extra+=(--merge-failed) + fi + decision="$(plan "$@" "${extra[@]}")" + printf '%s\n' "$decision" + git merge --abort >/dev/null 2>&1 || git reset --hard HEAD + if [ "$have_branch" = 1 ]; then + git checkout -B "$branch" "origin/$branch" >/dev/null 2>&1 || true + else + git checkout -B "$base" "origin/$base" >/dev/null 2>&1 || true + fi + if [ "$dry" = 1 ]; then + exit 0 + fi + file_issue "$decision" + exit 0 +} + +if [ "$behind" -eq 0 ]; then + plan + exit 0 +fi + +if [ "$have_branch" = 1 ]; then + git checkout -B "$branch" "origin/$branch" +else + git checkout -B "$branch" "origin/$base" +fi +git branch --unset-upstream "$branch" >/dev/null 2>&1 || true + +conflicts_file="$(mktemp)" +trap 'rm -f "$conflicts_file"' EXIT + +merge_one() { + local ref="$1" + : >"$conflicts_file" + set +e + git merge --no-ff --no-commit "$ref" + local merge_rc=$? + set -e + git diff --name-only --diff-filter=U >"$conflicts_file" || true + if [ -s "$conflicts_file" ] || [ "$merge_rc" -ne 0 ]; then + return 1 + fi + if git diff --cached --quiet; then + git merge --abort >/dev/null 2>&1 || true + else + git -c user.name="github-actions[bot]" \ + -c user.email="41898282+github-actions[bot]@users.noreply.github.com" \ + commit -m "Merge ${ref} into ${branch}" + fi +} + +if [ "$have_branch" = 1 ]; then + if ! merge_one "origin/$base"; then + stop_for --conflicts-file "$conflicts_file" + fi +fi + +if ! merge_one upstream/main; then + stop_for --conflicts-file "$conflicts_file" +fi + +if [ "$dry" = 1 ]; then + plan + exit 0 +fi + +set +e +git push "$remote" "HEAD:refs/heads/${branch}" +push_rc=$? +set -e +if [ "$push_rc" -ne 0 ]; then + stop_for --push-rejected +fi + +decision="$(plan)" +printf '%s\n' "$decision" +body="$(mktemp)" +node --input-type=module -e ' + const decision = JSON.parse(process.argv[1]); + process.stdout.write(decision.pullRequest ?? ""); +' "$decision" >"$body" +existing="$(gh pr list --repo "$repo" --head "$branch" --base "$base" --json number --jq '.[0].number // empty')" +if [ -z "$existing" ]; then + gh pr create --repo "$repo" --base "$base" --head "$branch" \ + --title "sync: upstream main ${date}" --body-file "$body" +else + gh pr edit "$existing" --repo "$repo" --body-file "$body" +fi +rm -f "$body"