Have chunk-pr merge wait for the required checks, and name sync, ready's title and wait-for in the docs
This commit is contained in:
+66
-13
@@ -3,7 +3,7 @@
|
||||
usage: chunk-pr open <title> [<body>]
|
||||
chunk-pr sync
|
||||
chunk-pr ready [<title>]
|
||||
chunk-pr merge --gate-passed
|
||||
chunk-pr merge
|
||||
chunk-pr clean
|
||||
|
||||
Run it in the chunk's worktree, on the chunk's branch. ready and merge act on the branch's open
|
||||
@@ -21,13 +21,16 @@ sync fetches origin and merges its default branch into the branch checked ou
|
||||
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. Given <title>, one line, it sets that
|
||||
as the pull request's title first; where that fails, the pull request stays a draft.
|
||||
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 also refuses where
|
||||
`larv-review scoring-run-owed` exits 1, because the chunk owes a scoring run it has not
|
||||
recorded; no flag waives that.
|
||||
merge waits for the checks origin's default branch requires to pass on the head, then 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 refuses where a required check fails. On Gitea it also refuses
|
||||
where one still runs after 9 minutes, so a foreground call of the Bash tool can hold the
|
||||
wait; run it again then. On GitHub, gh waits as long as the checks run.
|
||||
It takes the first merge 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. It also refuses where `larv-review
|
||||
scoring-run-owed` exits 1, because the chunk owes a scoring run it has not recorded; no
|
||||
flag waives that.
|
||||
clean acts once the forge reports the branch's pull request merged at the commit checked out,
|
||||
by any merge method. It fetches origin, fast-forwards the main checkout to origin's
|
||||
default branch, removes the chunk's worktree, then deletes the local branch; origin's
|
||||
@@ -39,10 +42,12 @@ clean acts once the forge reports the branch's pull request merged at the com
|
||||
It prints what it did, exit 0, or why it failed, exit 1.
|
||||
"""
|
||||
import dataclasses
|
||||
import fnmatch
|
||||
import json
|
||||
import os
|
||||
import pathlib
|
||||
import sys
|
||||
import time
|
||||
import typing
|
||||
|
||||
from failure import Failure
|
||||
@@ -51,7 +56,9 @@ from chunk_preflight.choices import forge_of
|
||||
from deploy_release.forge import gitea, merged_pull, open_pull, Run, runner
|
||||
|
||||
BIN = pathlib.Path(__file__).resolve().parents[1]
|
||||
USAGE = "usage: chunk-pr open <title> [<body>] | chunk-pr sync | chunk-pr ready [<title>] | chunk-pr merge --gate-passed | chunk-pr clean"
|
||||
POLL_SECONDS = 10
|
||||
CHECKS_TIMEOUT_SECONDS = 540
|
||||
USAGE = "usage: chunk-pr open <title> [<body>] | chunk-pr sync | chunk-pr ready [<title>] | chunk-pr merge | chunk-pr clean"
|
||||
|
||||
# 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], ...]] = {
|
||||
@@ -81,10 +88,8 @@ def step(argv: list[str], run: Run) -> str | Failure:
|
||||
return synced(run)
|
||||
case ["ready", *title] if len(title) <= 1:
|
||||
return Failure("give a title of one line") if title and not one_line(title[0]) else marked_ready(run, title[0] if title else None)
|
||||
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 merged(run)
|
||||
case ["clean"]:
|
||||
return cleaned(run)
|
||||
return Failure(USAGE)
|
||||
@@ -211,7 +216,12 @@ def marked_ready(run: Run, title: str | None) -> str | Failure:
|
||||
return f"Marked pull request #{number} ready, titled “{title}”." if title is not None else f"Marked pull request #{number} ready."
|
||||
|
||||
|
||||
def merged(run: Run) -> str | Failure:
|
||||
class Clock(typing.Protocol):
|
||||
def monotonic(self) -> float: ...
|
||||
def sleep(self, seconds: float) -> None: ...
|
||||
|
||||
|
||||
def merged(run: Run, clock: Clock = time) -> str | Failure:
|
||||
found = pull(run)
|
||||
if isinstance(found, Failure):
|
||||
return found
|
||||
@@ -222,6 +232,9 @@ def merged(run: Run) -> str | Failure:
|
||||
method = merge_method(forge, run)
|
||||
if isinstance(method, Failure):
|
||||
return method
|
||||
checked = checks_passed(forge, number, head, run, clock)
|
||||
if isinstance(checked, Failure):
|
||||
return checked
|
||||
if forge == "gh":
|
||||
done = run(["gh", "pr", "merge", number, method, "--match-head-commit", head])
|
||||
else:
|
||||
@@ -229,6 +242,46 @@ def merged(run: Run) -> str | Failure:
|
||||
return done if isinstance(done, Failure) else f"Merged pull request #{number} ({method.removeprefix('--')})."
|
||||
|
||||
|
||||
def checks_passed(forge: Forge, number: str, head: str, run: Run, clock: Clock) -> None | Failure:
|
||||
"""None once every check origin's default branch requires has passed at head, or a Failure where one failed or still runs at CHECKS_TIMEOUT_SECONDS."""
|
||||
if forge == "gh":
|
||||
watched = run(["gh", "pr", "checks", number, "--required", "--watch", "--interval", str(POLL_SECONDS)])
|
||||
# gh fails where the branch requires no checks; then nothing is to wait for.
|
||||
return None if not isinstance(watched, Failure) or "no required checks" in watched.reason else watched
|
||||
required = required_contexts(run)
|
||||
if isinstance(required, Failure) or not required:
|
||||
return required or None
|
||||
deadline = clock.monotonic() + CHECKS_TIMEOUT_SECONDS
|
||||
while True:
|
||||
combined = gitea(run, f"repos/{{owner}}/{{repo}}/commits/{head}/status")
|
||||
if isinstance(combined, Failure):
|
||||
return combined
|
||||
statuses = combined.get("statuses") or [] if isinstance(combined, dict) else []
|
||||
matching = {pattern: [status for status in statuses if fnmatch.fnmatchcase(str(status.get("context")), pattern)] for pattern in required}
|
||||
failed = next((status for found in matching.values() for status in found if status.get("status") in ("failure", "error")), None)
|
||||
if failed is not None:
|
||||
return Failure(f"the required check {failed.get('context')} failed at {head[:12]}: {failed.get('target_url')}")
|
||||
waiting = [pattern for pattern, found in matching.items() if not found or any(status.get("status") != "success" for status in found)]
|
||||
if not waiting:
|
||||
return None
|
||||
left = deadline - clock.monotonic()
|
||||
if left <= 0:
|
||||
return Failure(f"the required checks {', '.join(waiting)} still run at {head[:12]} after {CHECKS_TIMEOUT_SECONDS} s; run chunk-pr merge again")
|
||||
clock.sleep(min(POLL_SECONDS, left))
|
||||
|
||||
|
||||
def required_contexts(run: Run) -> list[str] | Failure:
|
||||
"""The status contexts, as Gitea's glob patterns, that the protection rule for origin's default branch requires; [] where none is required."""
|
||||
base = default_branch(run)
|
||||
if isinstance(base, Failure):
|
||||
return base
|
||||
rules = gitea(run, "repos/{owner}/{repo}/branch_protections")
|
||||
if isinstance(rules, Failure):
|
||||
return rules
|
||||
rule = next((rule for rule in rules if isinstance(rule, dict) and fnmatch.fnmatchcase(base, str(rule.get("rule_name") or rule.get("branch_name")))), None) if isinstance(rules, list) else None
|
||||
return list(rule.get("status_check_contexts") or []) if rule and rule.get("enable_status_check") else []
|
||||
|
||||
|
||||
def merge_method(forge: Forge, run: Run) -> str | Failure:
|
||||
"""The first of METHODS[forge] that the repository's settings allow."""
|
||||
if forge == "gh":
|
||||
|
||||
Reference in New Issue
Block a user