95 lines
4.9 KiB
Python
95 lines
4.9 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
|
|
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."""
|
|
|
|
|
|
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, and the chunk has news again once the pull request is no longer open.
|
|
"""
|
|
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 by_human:
|
|
return Gate(worktree, pull, MERGE)
|
|
return Open(f"pull request #{pull} passed its checks" + (" and is approved" if decision == APPROVED else ""))
|
|
|
|
|
|
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
|