Answer stability round 1: 2 🔴, 6 🟡 (0 misread); merge by the first method the repository allows, sort the imports, keep 'the cut' out of open_pull's shared text, fake each forge apart and test the failed ready, listing and git calls; the default-branch refusal and help rewording, answering product-owner and prose, ride along

This commit is contained in:
2026-11-09 02:14:12 +01:00
parent 6820946f2b
commit 7bd0b9a446
3 changed files with 151 additions and 71 deletions
+49 -20
View File
@@ -4,31 +4,40 @@ usage: chunk-pr open <title> [<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.
Run it in the chunk's worktree, on the chunk's branch. ready and merge act on the branch's open
pull request only while its head is the commit checked out, so push before you run them.
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.
open pushes the branch, setting its upstream, and opens a draft pull request into origin's
default branch. <title> is one line, as ~/.claude/CLAUDE.md → "Git, commits, and PRs"
says, and <body> is the pull request's summary. Where the branch's pull request is open
already, it opens no second one. On origin's default branch it refuses, pushing nothing.
ready marks the pull request ready for review. On Gitea a draft is a pull request whose title
starts with `WIP: `, so there it drops that prefix.
merge merges the pull request, and only while its head is still the commit checked out, so a
push after the gate is never merged. It takes the first method the repository allows of:
a merge commit, fast-forward only, a rebase followed by a merge commit, a rebase, a squash.
GitHub offers only the merge commit, the rebase and the squash. --gate-passed says the
merge gate passed on that commit; without it, chunk-pr refuses.
It prints the pull request's number, exit 0. A failure prints git's or the forge's reason, exit 1.
It prints what it did, exit 0, or why it failed, exit 1.
"""
import json
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
from chunk_preflight.gathered import Forge
from chunk_preflight.choices import forge_of
from deploy_release.forge import gitea, open_pull, Run, runner
USAGE = "usage: chunk-pr open <title> [<body>] | chunk-pr ready | chunk-pr merge --gate-passed"
# Each forge's merge methods in the order merge prefers them, as the repository setting that allows the method and the method's name in the forge's merge call. A merge commit and a fast-forward keep the branch's commits as they are; a rebase or a squash rewrites them.
METHODS: dict[Forge, tuple[tuple[str, str], ...]] = {
"gh": (("mergeCommitAllowed", "--merge"), ("rebaseMergeAllowed", "--rebase"), ("squashMergeAllowed", "--squash")),
"tea": (("allow_merge_commits", "merge"), ("allow_fast_forward_only_merge", "fast-forward-only"), ("allow_rebase_explicit", "rebase-merge"), ("allow_rebase", "rebase"), ("allow_squash_merge", "squash")),
}
def main(argv: list[str]) -> int:
if argv[:1] in (["-h"], ["--help"]):
@@ -47,7 +56,7 @@ def step(argv: list[str], run: Run) -> str | Failure:
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 Failure("give a title of one line")
return opened(title, body[0] if body else "", run)
case ["ready"]:
return marked_ready(run)
@@ -89,6 +98,8 @@ def opened(title: str, body: str, run: Run) -> str | Failure:
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/")
if branch == base:
return Failure(f"{branch} is origin's default branch; open the pull request from the chunk's branch")
pushed = run(["git", "push", "--quiet", "--set-upstream", "origin", branch])
if isinstance(pushed, Failure):
return pushed
@@ -121,9 +132,27 @@ def merged(run: Run) -> str | Failure:
if isinstance(found, Failure):
return found
forge, number, head = found
# The forge refuses where the pull request's head has moved off head.
method = merge_method(forge, run)
if isinstance(method, Failure):
return method
if forge == "gh":
done = run(["gh", "pr", "merge", number, "--merge", "--match-head-commit", head])
done = run(["gh", "pr", "merge", number, method, "--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}."
done = gitea(run, f"repos/{{owner}}/{{repo}}/pulls/{number}/merge", f"Do={method}", f"head_commit_id={head}")
return done if isinstance(done, Failure) else f"Merged pull request #{number} ({method.removeprefix('--')})."
def merge_method(forge: Forge, run: Run) -> str | Failure:
"""The first of METHODS[forge] that the repository's settings allow."""
if forge == "gh":
viewed = run(["gh", "repo", "view", "--json", ",".join(setting for setting, _ in METHODS["gh"])])
try:
settings = viewed if isinstance(viewed, Failure) else json.loads(viewed)
except ValueError:
return Failure(f"gh repo view printed no JSON: {str(viewed).strip()[:200]}")
else:
settings = gitea(run, "repos/{owner}/{repo}")
if isinstance(settings, Failure):
return settings
allowed = next((method for setting, method in METHODS[forge] if isinstance(settings, dict) and settings.get(setting) is True), None)
return allowed if allowed is not None else Failure(f"the repository allows none of the merge methods chunk-pr knows: {', '.join(method for _, method in METHODS[forge])}")