"""Open the chunk's pull request as a draft, mark it ready, or merge it, with gh where origin is on GitHub and tea elsewhere. usage: chunk-pr open [<body>] chunk-pr ready chunk-pr merge --gate-passed Run it in the chunk's worktree, on the chunk's branch. Each step acts on the open pull request from origin's own branch of that name whose head is the commit checked out, so push before ready and merge. open pushes the branch, setting its upstream, then opens a draft pull request into origin's default branch, titled <title>: one line, as ~/.claude/CLAUDE.md → "Git, commits, and PRs" says. Where that pull request is open already, it opens no other. ready marks the pull request ready for review. Gitea has no draft state, so there it drops the title's `WIP: ` prefix, which Gitea reads as a draft. merge merges the pull request with a merge commit, and only while its head is still the commit checked out, so a push after the gate is never merged. --gate-passed says the merge gate passed on that commit; without it, chunk-pr refuses. A repo whose AGENTS.md names another merge method merges through its forge instead. It prints the pull request's number, exit 0. A failure prints git's or the forge's reason, exit 1. """ import os import sys from chunk_preflight.choices import forge_of from chunk_preflight.gathered import Forge from deploy_release.forge import gitea, open_pull, Run, runner from failure import Failure USAGE = "usage: chunk-pr open <title> [<body>] | chunk-pr ready | chunk-pr merge --gate-passed" def main(argv: list[str]) -> int: if argv[:1] in (["-h"], ["--help"]): print(__doc__.strip()) return 0 done = step(argv, runner(os.getcwd())) if isinstance(done, Failure): print(f"chunk-pr: {done.reason}", file=sys.stderr) return 1 print(done) return 0 def step(argv: list[str], run: Run) -> str | Failure: """Run the step argv names: the line to print, or a Failure. A malformed argv runs nothing.""" match argv: case ["open", title, *body] if len(body) <= 1: if not title.strip() or "\n" in title: return Failure("the title is one line of text") return opened(title, body[0] if body else "", run) case ["ready"]: return marked_ready(run) case ["merge", "--gate-passed"]: return merged(run) case ["merge"]: return Failure("merge only once the gate has passed, and say so with --gate-passed") return Failure(USAGE) def checkout(run: Run) -> tuple[Forge, str, str] | Failure: """The forge, the branch checked out and its head commit.""" answers = [run(["git", *args]) for args in (["remote", "get-url", "origin"], ["symbolic-ref", "--short", "HEAD"], ["rev-parse", "HEAD"])] failed = next((answer for answer in answers if isinstance(answer, Failure)), None) if failed is not None: return failed origin, branch, head = (str(answer).strip() for answer in answers) return forge_of(origin), branch, head def pull(run: Run) -> tuple[Forge, str, str] | Failure: """The forge, the number of the branch's open pull request at its head, and that head.""" found = checkout(run) if isinstance(found, Failure): return found forge, branch, head = found number = open_pull(forge, branch, head, run) if number is None: return Failure(f"no pull request from origin's {branch} is open at {head[:12]}; push the branch, or open one with chunk-pr open") return number if isinstance(number, Failure) else (forge, number, head) def opened(title: str, body: str, run: Run) -> str | Failure: found = checkout(run) if isinstance(found, Failure): return found forge, branch, head = found default = run(["git", "symbolic-ref", "--short", "refs/remotes/origin/HEAD"]) if isinstance(default, Failure): return Failure(f"origin records no default branch; set it with `git remote set-head origin --auto`: {default.reason}") base = default.strip().removeprefix("origin/") pushed = run(["git", "push", "--quiet", "--set-upstream", "origin", branch]) if isinstance(pushed, Failure): return pushed number = open_pull(forge, branch, head, run) if isinstance(number, str): return f"Pull request #{number} is open already." if isinstance(number, Failure): return number command = ["gh", "pr", "create"] if forge == "gh" else ["tea", "pulls", "create"] created = run([*command, "--draft", "--head", branch, "--base", base, "--title", title, "--body" if forge == "gh" else "--description", body]) if isinstance(created, Failure): return created number = open_pull(forge, branch, head, run) if number is None: return Failure(f"{command[0]} created the pull request, but no pull request from origin's {branch} is listed open at {head[:12]}") return number if isinstance(number, Failure) else f"Opened draft pull request #{number}." def marked_ready(run: Run) -> str | Failure: found = pull(run) if isinstance(found, Failure): return found forge, number, _ = found ready = run(["gh", "pr", "ready", number] if forge == "gh" else ["tea", "pulls", "edit", number, "--ready"]) return ready if isinstance(ready, Failure) else f"Marked pull request #{number} ready." def merged(run: Run) -> str | Failure: found = pull(run) if isinstance(found, Failure): return found forge, number, head = found # The forge refuses where the pull request's head has moved off head. if forge == "gh": done = run(["gh", "pr", "merge", number, "--merge", "--match-head-commit", head]) else: done = gitea(run, f"repos/{{owner}}/{{repo}}/pulls/{number}/merge", "Do=merge", f"head_commit_id={head}") return done if isinstance(done, Failure) else f"Merged pull request #{number}."