103 lines
5.5 KiB
Python
103 lines
5.5 KiB
Python
"""What the pull request of a do-epic chunk waits on, read once from its forge (docs/decisions.md → `do-epic: drives one Jira epic …`, "Long gates").
|
|
|
|
A gate is something only time or a person passes: checks that still run, an approval a reviewer has yet to give, the new review of a reviewer whose requested changes the worker answered, or the merge itself, where the project's config leaves it to a person. While a chunk's pull request waits on one, its worker doesn't have anything to do, so do-epic parks the chunk (`parked`). Anything else is news the worker acts on."""
|
|
import dataclasses
|
|
import typing
|
|
|
|
from forge.reviews import ANSWERED, APPROVED, CHANGES_REQUESTED, review_decision, REVIEW_REQUIRED, waiting_threads
|
|
from forge.pulls import CheckFailed, checks_now, open_pull, Pending, required_checks, unmergeable
|
|
from failure import Failure
|
|
from forge.commands import forge_of, Run
|
|
|
|
# What a pull request waits on, as the words after "awaits".
|
|
CHECKS = "its checks"
|
|
APPROVAL = "an approval"
|
|
NEW_REVIEW = "a new review"
|
|
MERGE = "your merge"
|
|
|
|
|
|
@dataclasses.dataclass(frozen=True)
|
|
class Gate:
|
|
"""The gate a chunk's open pull request waits on."""
|
|
# The chunk's worktree, where the pull request's branch is checked out.
|
|
worktree: str
|
|
# The pull request's number.
|
|
pull: str
|
|
# CHECKS, APPROVAL, NEW_REVIEW or MERGE.
|
|
on: str
|
|
|
|
|
|
@dataclasses.dataclass(frozen=True)
|
|
class Open:
|
|
"""A chunk that waits on no gate, so its worker goes on."""
|
|
# Why, as a sentence without its full stop, for the worker of a chunk that was parked.
|
|
news: str
|
|
|
|
|
|
@dataclasses.dataclass(frozen=True)
|
|
class Unopened(Open):
|
|
"""An Open whose worktree stands though no pull request is open at its head: one that was merged or closed, whose chunk still has its worktree to remove."""
|
|
|
|
|
|
@dataclasses.dataclass(frozen=True)
|
|
class Unmergeable(Open):
|
|
"""An Open whose pull request awaits a person's merge and doesn't merge into its base as it stands: its worker levels the branch."""
|
|
|
|
|
|
def gate(branch: str, checkout: str, at: typing.Callable[[str], Run], by_human: bool) -> Gate | Open | Failure:
|
|
"""What the chunk on branch waits on, in a worktree of checkout; at gives the runner of commands in a folder, and by_human says the project's config leaves the merge to a person (`merge = "human"`). A Failure where git or the forge cannot be read, which doesn't say anything about the gate.
|
|
|
|
A draft never waits on a gate: its worker still works on it. On Gitea only the checks are a gate, since chunk-pr reads approvals on GitHub only (`chunk_pr.approval.awaited`). Changes a reviewer requested are news until the worker answered them (`forge.reviews.answered`); from then on the pull request awaits a new review. Where a person merges, a pull request with nothing else to wait on awaits that merge. The chunk has news again once the pull request is no longer open. It has news too once the pull request no longer merges into its base as it stands (`forge.pulls.unmergeable`): the worker then levels the branch for that person.
|
|
"""
|
|
worktree = worktree_of(branch, at(checkout))
|
|
if not isinstance(worktree, str):
|
|
return worktree or Open(f"no worktree of {checkout} has the branch {branch} checked out")
|
|
run = at(worktree)
|
|
read = [run(["git", *args]) for args in (["remote", "get-url", "origin"], ["rev-parse", "HEAD"], ["symbolic-ref", "--short", "refs/remotes/origin/HEAD"])]
|
|
failed = next((answer for answer in read if isinstance(answer, Failure)), None)
|
|
if failed is not None:
|
|
return failed
|
|
origin, head, base = (str(answer).strip() for answer in read)
|
|
forge = forge_of(origin)
|
|
pull = open_pull(forge, branch, head, run)
|
|
if not isinstance(pull, str):
|
|
return pull or Unopened(f"no pull request from origin's {branch} is open at {head[:12]}; it may be merged or closed already")
|
|
decision = review_decision(forge, pull, head, run)
|
|
if not isinstance(decision, str):
|
|
return decision or Open(f"pull request #{pull} is a draft")
|
|
required = required_checks(forge, base.removeprefix("origin/"), run)
|
|
checks = required if isinstance(required, Failure) else checks_now(forge, pull, head, required, run)
|
|
if isinstance(checks, CheckFailed):
|
|
return Open(f"{checks.reason}; fix it and push")
|
|
if isinstance(checks, Failure):
|
|
return checks
|
|
if isinstance(checks, Pending):
|
|
return Gate(worktree, pull, CHECKS)
|
|
if decision == CHANGES_REQUESTED:
|
|
return Open(f"a reviewer requested changes on pull request #{pull}")
|
|
waiting = waiting_threads(pull, run) if forge == "gh" else []
|
|
if isinstance(waiting, Failure):
|
|
return waiting
|
|
if waiting:
|
|
return Open(f"a reviewer's comment on pull request #{pull} waits for an answer, in {', '.join(waiting)}")
|
|
if decision in (ANSWERED, REVIEW_REQUIRED):
|
|
return Gate(worktree, pull, NEW_REVIEW if decision == ANSWERED else APPROVAL)
|
|
if not by_human:
|
|
return Open(f"pull request #{pull} passed its checks" + (" and is approved" if decision == APPROVED else ""))
|
|
blocked = unmergeable(forge, pull, run)
|
|
if isinstance(blocked, Failure):
|
|
return blocked
|
|
return Unmergeable(f"pull request #{pull} {blocked}; run chunk-pr merge again to level the branch") if blocked else Gate(worktree, pull, MERGE)
|
|
|
|
|
|
def worktree_of(branch: str, run: Run) -> str | None | Failure:
|
|
"""The worktree, of the checkout run works in, that has branch checked out; None where none has."""
|
|
listed = run(["git", "worktree", "list", "--porcelain"])
|
|
if isinstance(listed, Failure):
|
|
return listed
|
|
for block in listed.split("\n\n"):
|
|
lines = block.splitlines()
|
|
if f"branch refs/heads/{branch}" in lines and lines[0].startswith("worktree "):
|
|
return lines[0].removeprefix("worktree ")
|
|
return None
|