Add the project setting merge = "human", under which chunk-pr merge leaves the pull request ready and a do-epic chunk parks on your merge

This commit is contained in:
Lilleman
2026-11-19 15:51:44 +01:00
parent 017cf33611
commit e2f5661760
15 changed files with 240 additions and 58 deletions
+10 -2
View File
@@ -12,8 +12,10 @@ WAIT_SECONDS = 90 * 60
PARKS = "CHUNK_PR_PARKS"
def awaited(forge: Forge, wait_for_approval: bool) -> bool | Failure:
"""Whether merge waits for an approval: the project config's `wait-for-approval`. chunk-pr reads approvals on GitHub only, so a Gitea project that sets it fails, and merge never skips an approval the config requires."""
def awaited(forge: Forge, wait_for_approval: bool, by_human: bool = False) -> bool | Failure:
"""Whether merge waits for an approval: the project config's `wait-for-approval`, and never where a person merges the pull request (by_human), who sees its approval themselves. chunk-pr reads approvals on GitHub only, so a Gitea project that sets it fails, and merge never skips an approval the config requires."""
if by_human:
return False
if wait_for_approval and forge != "gh":
return Failure("the project's config sets wait-for-approval, but chunk-pr reads approvals on GitHub only, so it cannot merge this pull request; ask the human to merge it")
return wait_for_approval
@@ -52,6 +54,12 @@ def left_to_park(number: str, run: Run) -> Failure:
return unanswered(number, run) or Failure(f"pull request {number} passed its checks and awaits an approval; end your run now, since do-epic parks the chunk and resumes this session once it is approved or a reviewer comments")
def left_to_a_person(number: str, parks: bool) -> str:
"""What merge prints where the project's config leaves the merge of pull request number to a person (`merge = "human"`): what the worker does now, which under PARKS is to end its run, as in `left_to_park`."""
now = "end your run now, since do-epic parks the chunk and resumes this session once it is merged or a reviewer comments." if parks else "leave the merge to them, and report the chunk as ready to merge. Once it is merged, run chunk-pr clean in this worktree."
return f"Marked pull request #{number} ready. It passed its checks and awaits a person's merge, since the project's config sets `merge = \"human\"`: {now}"
def unanswered(number: str, run: Run) -> Failure | None:
"""A Failure naming the files where a reviewer's comment waits for an answer on pull request number, as `waiting_threads` finds them, or saying why they cannot be read. None where no thread waits."""
waiting = waiting_threads(number, run)
+24 -7
View File
@@ -47,6 +47,11 @@ merge merges the pull request in its turn: the chunks of one project on this
requires. It waits for that outside its turn, so other chunks merge meanwhile, and takes
a new turn once approved. In a chunk do-epic runs, which sets CHUNK_PR_PARKS=1, it
fails at once instead of waiting: do-epic parks the chunk until the approval comes.
Where the project's config sets `merge = "human"` (`project-config --help`), it does
steps 1 to 4, marks the pull request ready in place of step 5, and prints that the pull
request awaits a person's merge, exit 0. The plan items step 2 finished are commits on
the branch, so they land with that merge. It reads no approval then, whatever
`wait-for-approval` says. Once a person merged it, run clean.
It stops, without merging, and says what to do before running it again:
- on a merge conflict in step 1, naming the conflicted files, with the merge in progress;
- where `larv-review scoring-run-owed` exits 1 after step 3, because the chunk owes a
@@ -86,7 +91,7 @@ import sys
import time
import typing
from chunk_pr.approval import approval, approved, awaited, left_to_park, PARKS, unanswered
from chunk_pr.approval import approval, approved, awaited, left_to_a_person, left_to_park, PARKS, unanswered
from forge.pulls import checks_passed, merged_pull, open_pull, required_checks
from failure import Failure
from forge.commands import Forge, forge_of, gitea, Run, runner
@@ -306,7 +311,7 @@ def finishes_of(given: list[str]) -> list[list[str]] | None:
def merged(run: Run, config: Config, sleep: typing.Callable[[float], None], finishes: typing.Sequence[list[str]] = (), parks: bool = False) -> str | Failure:
"""Merge the branch's pull request in the project's merge queue. Each pass of the loop is one turn in the queue; a pull request that still wants its approval leaves the queue while it waits, so the next chunk merges meanwhile, and takes a new turn once approved. Where parks, it doesn't wait for the approval: it fails at once, as `left_to_park` says."""
"""Merge the branch's pull request in the project's merge queue. Each pass of the loop is one turn in the queue; a pull request that still wants its approval leaves the queue while it waits, so the next chunk merges meanwhile, and takes a new turn once approved. Where parks, it doesn't wait for the approval: it fails at once, as `left_to_park` says. Where the project's config leaves the merge to a person, the turn ends with the pull request ready, as `left_to_a_person` says."""
found = pull(run)
if isinstance(found, Failure):
return found
@@ -314,7 +319,8 @@ def merged(run: Run, config: Config, sleep: typing.Callable[[float], None], fini
loaded = config()
if isinstance(loaded, Failure):
return loaded
waits = awaited(forge, loaded.wait_for_approval)
by_human = loaded.merge == "human"
waits = awaited(forge, loaded.wait_for_approval, by_human)
if isinstance(waits, Failure):
return waits
method = merge_method(forge, run)
@@ -331,10 +337,12 @@ def merged(run: Run, config: Config, sleep: typing.Callable[[float], None], fini
if isinstance(absent, Failure):
return absent
file = pathlib.Path(queue[0]) / QUEUE
pull_request = Merging(forge, number, method, str(base), required, waits)
pull_request = Merging(forge, number, method, str(base), required, waits, by_human)
while True:
with turn(file, number, sleep) as untaken:
taken = untaken or merge_turn(pull_request, finishes, loaded, run, sleep)
if isinstance(taken, AwaitsMerge):
return left_to_a_person(number, parks) + absent
if not isinstance(taken, AwaitsApproval):
return taken if isinstance(taken, Failure) else taken + absent
if parks:
@@ -347,13 +355,14 @@ def merged(run: Run, config: Config, sleep: typing.Callable[[float], None], fini
@dataclasses.dataclass(frozen=True)
class Merging:
"""A pull request that merge takes turns for: its forge and number, the merge method, the default branch it merges into with the checks that branch requires, and whether it waits for an approval."""
"""A pull request that merge takes turns for: its forge and number, the merge method, the default branch it merges into with the checks that branch requires, whether it waits for an approval, and whether a person merges it."""
forge: Forge
number: str
method: str
base: str
required: set[str]
waits: bool
by_human: bool = False
@dataclasses.dataclass(frozen=True)
@@ -361,12 +370,20 @@ class AwaitsApproval:
"""The pull request passed its checks but awaits a human's approval, so merge leaves the queue until it has one."""
def merge_turn(pull_request: Merging, finishes: typing.Sequence[list[str]], config: ProjectConfig, run: Run, sleep: typing.Callable[[float], None]) -> str | AwaitsApproval | Failure:
"""One turn in the queue: level the branch and finish its items, wait for its checks, and merge it where nothing still waits. What merged it, or why it waits or fails."""
@dataclasses.dataclass(frozen=True)
class AwaitsMerge:
"""The pull request passed its checks and is marked ready, and the project's config leaves its merge to a person."""
def merge_turn(pull_request: Merging, finishes: typing.Sequence[list[str]], config: ProjectConfig, run: Run, sleep: typing.Callable[[float], None]) -> str | AwaitsApproval | AwaitsMerge | Failure:
"""One turn in the queue: level the branch and finish its items, wait for its checks, and merge it where nothing still waits. What merged it, or why it waits or fails. A pull request a person merges is marked ready instead: its finished items are commits on its branch, so they reach the default branch with that person's merge."""
head = levelled_and_finished(run, pull_request.base, finishes, config)
checked = head if isinstance(head, Failure) else checks_passed(pull_request.forge, pull_request.number, head, pull_request.required, run, sleep, NO_CHECKS_SECONDS)
if isinstance(checked, Failure):
return checked
if pull_request.by_human:
marked = marked_ready(run, None)
return marked if isinstance(marked, Failure) else AwaitsMerge()
ready = approval(pull_request.number, run) if pull_request.waits else True
if isinstance(ready, Failure):
return ready
+42 -1
View File
@@ -81,7 +81,7 @@ class Checkout:
return failed
texts = {name: str(answer).strip() for name, answer in named.items()}
paths = {name: pathlib.Path(texts[name]) for name in ("plan", "changelog", "comprehension-history") if texts.get(name, "").startswith("/")}
return ProjectConfig("proj", pathlib.Path("/config"), pathlib.Path("/checkout"), paths, wait_for_approval=texts.get("wait-for-approval") == "true", commit_title=texts.get("commit-title", "{summary}"), pull_request_title=texts.get("pull-request-title", "{summary}"), branch=texts.get("branch", "item-{id}"))
return ProjectConfig("proj", pathlib.Path("/config"), pathlib.Path("/checkout"), paths, wait_for_approval=texts.get("wait-for-approval") == "true", merge=texts.get("merge", "chunk"), commit_title=texts.get("commit-title", "{summary}"), pull_request_title=texts.get("pull-request-title", "{summary}"), branch=texts.get("branch", "item-{id}"))
def __call__(self, command: list[str]) -> str | Failure:
self.ran.append(command)
@@ -360,6 +360,47 @@ class Merge(unittest.TestCase):
checkout.decisions = list(decisions) or ["APPROVED"]
return checkout
def merged_by_a_human(self, origin: str = GITHUB) -> Checkout:
"""A Checkout whose project config sets `merge = "human"`."""
checkout = Checkout(origin, open=True)
checkout.answers[(PROJECT_CONFIG, "merge")] = "human\n"
return checkout
def test_under_a_humans_merge_it_waits_for_the_checks_marks_the_pull_request_ready_and_leaves_it_unmerged(self):
for origin, ready in ((GITHUB, ["gh", "pr", "ready", "12"]), (GITEA, ["tea", "pulls", "edit", "12", "--ready"])):
with self.subTest(origin):
checkout = self.merged_by_a_human(origin)
done = step(["merge"], checkout, checkout.config)
self.assertEqual(done, "Marked pull request #12 ready. It passed its checks and awaits a person's merge, since the project's config sets `merge = \"human\"`: leave the merge to them, and report the chunk as ready to merge. Once it is merged, run chunk-pr clean in this worktree.")
self.assertEqual(checkout.acted()[-1], ready)
self.assertFalse(any("merge" in command[1:4] for command in checkout.acted() if command[0] != "git"), checkout.acted())
def test_under_a_humans_merge_a_chunk_that_parks_is_told_to_end_its_run(self):
checkout = self.merged_by_a_human()
done = merged(checkout, checkout.config, self.fail, parks=True)
self.assertEqual(done, "Marked pull request #12 ready. It passed its checks and awaits a person's merge, since the project's config sets `merge = \"human\"`: end your run now, since do-epic parks the chunk and resumes this session once it is merged or a reviewer comments.")
def test_under_a_humans_merge_a_failed_check_leaves_the_pull_request_as_it_was(self):
checkout = self.merged_by_a_human()
checkout.answers[tuple(GH_CHECKS)] = Failure("gh pr checks 12: CI / tests\tfail")
self.assertEqual(step(["merge"], checkout, checkout.config), Failure("gh pr checks 12: CI / tests\tfail"))
self.assertEqual(changes(checkout), [])
def test_under_a_humans_merge_it_never_reads_the_approval_also_on_gitea(self):
for origin in (GITHUB, GITEA):
with self.subTest(origin):
checkout = self.merged_by_a_human(origin)
checkout.answers[tuple(WAITS)] = "true\n"
checkout.decisions = ["REVIEW_REQUIRED"]
self.assertIn("awaits a person's merge", str(step(["merge"], checkout, checkout.config)))
self.assertNotIn(GH_DECISION, checkout.ran)
def test_under_a_humans_merge_the_plan_items_are_finished_on_the_branch_so_they_land_with_the_merge(self):
checkout = self.merged_by_a_human()
done = step(["merge", "--finish", "7", "--no-changelog"], checkout, checkout.config)
self.assertIn("awaits a person's merge", str(done))
self.assertIn([MergeQueue.TODO_ITEM, "finish", "7", "--no-changelog"], checkout.ran)
def test_a_project_without_wait_for_approval_never_reads_the_review_decision(self):
checkout = Checkout(GITHUB, open=True)
checkout.decisions = ["REVIEW_REQUIRED"]