Park on a new review once requested changes are answered, never park a Gitea draft, and count a slot only for a window that started
This commit is contained in:
@@ -5,7 +5,7 @@ import unittest
|
||||
|
||||
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
|
||||
|
||||
from do_epic.gates import APPROVAL, CHECKS, gate, Gate, Open
|
||||
from do_epic.gates import APPROVAL, CHECKS, gate, Gate, NEW_REVIEW, Open
|
||||
from failure import Failure
|
||||
|
||||
WORKTREES = "worktree /code/billing\nHEAD aaa\nbranch refs/heads/main\n\nworktree /code/billing-ABC-21\nHEAD abc\nbranch refs/heads/ABC-21\n"
|
||||
@@ -16,10 +16,20 @@ def thread(resolved: bool, last: str, path: str = "a.py") -> dict:
|
||||
return {"isResolved": resolved, "path": path, "comments": {"nodes": [{"author": {"login": last}}]}}
|
||||
|
||||
|
||||
class Forge:
|
||||
"""Stands in for git and the forge's CLI in the checkout and the chunk's worktree; `ran` holds each folder a command ran in."""
|
||||
def review(state: str, at: str, by: str = "reviewer", on: str = HEAD) -> dict:
|
||||
"""A review as `gh pr view --json reviews` prints it: made by `by` at `at`, on the commit `on`."""
|
||||
return {"author": {"login": by}, "state": state, "submittedAt": at, "commit": {"oid": on}}
|
||||
|
||||
def __init__(self, origin: str = "git@github.com:o/billing.git", pulls: str = f"7\t{HEAD}\tfalse\n", draft: bool = False, decision: str = "REVIEW_REQUIRED", buckets: tuple[str, ...] = ("pass",), threads: tuple[dict, ...] = (), status: str = '{"state": "success", "total_count": 1}', worktrees: str = WORKTREES, fails: str = "") -> None:
|
||||
|
||||
def comment(by: str, at: str) -> dict:
|
||||
return {"author": {"login": by}, "createdAt": at}
|
||||
|
||||
|
||||
class Forge:
|
||||
"""Stands in for git and the forge's CLI in the checkout and the chunk's worktree; `ran` holds each folder a command ran in. Only the CLI of origin's forge answers: `gh` fails on a Gitea origin, and `tea` on a GitHub one."""
|
||||
|
||||
def __init__(self, origin: str = "git@github.com:o/billing.git", pulls: str = f"7\t{HEAD}\tfalse\n", draft: bool = False, decision: str = "REVIEW_REQUIRED", buckets: tuple[str, ...] = ("pass",), threads: tuple[dict, ...] = (), status: str = '{"state": "success", "total_count": 1}', worktrees: str = WORKTREES, fails: str = "", reviews: tuple[dict, ...] = (), comments: tuple[dict, ...] = (), title: str = "Add x") -> None:
|
||||
self.cli = "gh" if "github.com" in origin else "tea"
|
||||
self.ran: list[str] = []
|
||||
self.commands: list[list[str]] = []
|
||||
self.fails = fails
|
||||
@@ -29,10 +39,11 @@ class Forge:
|
||||
"git rev-parse": HEAD + "\n",
|
||||
"git symbolic-ref": "origin/main\n",
|
||||
"gh pr list": pulls,
|
||||
"gh pr view": json.dumps({"isDraft": draft, "reviewDecision": decision}),
|
||||
"gh pr view": json.dumps({"isDraft": draft, "reviewDecision": decision, "author": {"login": "worker"}, "reviews": list(reviews), "comments": list(comments)}),
|
||||
"gh pr checks": json.dumps([{"bucket": bucket} for bucket in buckets]),
|
||||
"gh api graphql": json.dumps({"data": {"repository": {"pullRequest": {"author": {"login": "worker"}, "reviewThreads": {"nodes": list(threads)}}}}}),
|
||||
"tea api repos/{owner}/{repo}/pulls?state=open&limit=50": json.dumps([{"number": 7, "merged": False, "head": {"label": "ABC-21", "sha": HEAD, "repo": {"full_name": "o/billing"}}, "base": {"repo": {"full_name": "o/billing"}}}]),
|
||||
"tea api repos/{owner}/{repo}/pulls/7": json.dumps({"number": 7, "title": title}),
|
||||
"tea api repos/{owner}/{repo}/branches/main": '{"name": "main"}',
|
||||
"tea api repos/{owner}/{repo}/commits/" + HEAD + "/status": status,
|
||||
}
|
||||
@@ -41,6 +52,8 @@ class Forge:
|
||||
def run(command: list[str]) -> str | Failure:
|
||||
self.ran.append(folder)
|
||||
self.commands.append(command)
|
||||
if command[0] in ("gh", "tea") and command[0] != self.cli:
|
||||
return Failure(f"{command[0]} cannot read this origin")
|
||||
key = " ".join(command) if command[0] == "tea" else " ".join(command[:3] if command[0] == "gh" else command[:2])
|
||||
return Failure(f"{key} failed") if self.fails and key.startswith(self.fails) else self.answers[key]
|
||||
return run
|
||||
@@ -55,7 +68,7 @@ class GateOfAChunk(unittest.TestCase):
|
||||
self.assertEqual(forge.gate(), Gate("/code/billing-ABC-21", "7", APPROVAL))
|
||||
self.assertEqual((forge.ran[0], set(forge.ran[1:])), ("/code/billing", {"/code/billing-ABC-21"}))
|
||||
self.assertEqual(forge.commands[:4], [["git", "worktree", "list", "--porcelain"], ["git", "remote", "get-url", "origin"], ["git", "rev-parse", "HEAD"], ["git", "symbolic-ref", "--short", "refs/remotes/origin/HEAD"]])
|
||||
self.assertIn(["gh", "pr", "view", "7", "--json", "isDraft,reviewDecision"], forge.commands)
|
||||
self.assertIn(["gh", "pr", "view", "7", "--json", "isDraft,reviewDecision,author,reviews,comments"], forge.commands)
|
||||
self.assertIn(["gh", "pr", "checks", "7", "--json", "bucket"], forge.commands)
|
||||
self.assertTrue(any(command[:3] == ["gh", "api", "graphql"] and "number=7" in command for command in forge.commands), forge.commands)
|
||||
|
||||
@@ -74,6 +87,39 @@ class GateOfAChunk(unittest.TestCase):
|
||||
self.assertEqual(Forge(gitea, status='{"state": "pending", "total_count": 1}').gate(), Gate("/code/billing-ABC-21", "7", CHECKS))
|
||||
self.assertEqual(Forge(gitea).gate(), Open("pull request #7 passed its checks"))
|
||||
|
||||
def test_a_gitea_draft_awaits_nothing_though_its_checks_run(self):
|
||||
gitea = "https://gitea.larvit.se/o/billing.git"
|
||||
for title in ("WIP: Add x", "[wip] Add x"):
|
||||
with self.subTest(title=title):
|
||||
self.assertEqual(Forge(gitea, title=title, status='{"state": "pending", "total_count": 1}').gate(), Open("pull request #7 is a draft"))
|
||||
self.assertEqual(Forge(gitea, title="Add WIP: x", status='{"state": "pending", "total_count": 1}').gate(), Gate("/code/billing-ABC-21", "7", CHECKS))
|
||||
unread = Forge(gitea)
|
||||
unread.answers["tea api repos/{owner}/{repo}/pulls/7"] = "[1]"
|
||||
self.assertEqual(unread.gate(), Failure("tea api printed no title of pull request 7: [1]"))
|
||||
|
||||
def test_requested_changes_are_news_until_the_worker_pushed_or_replied_and_then_await_a_new_review(self):
|
||||
asked = review("CHANGES_REQUESTED", "2026-11-18T10:00:00Z")
|
||||
before, after, old = "2026-11-18T09:00:00Z", "2026-11-18T11:00:00Z", "fed" * 4
|
||||
news, waits = Open("a reviewer requested changes on pull request #7"), Gate("/code/billing-ABC-21", "7", NEW_REVIEW)
|
||||
cases = {
|
||||
"nobody answered": ((asked,), (), news),
|
||||
"the worker pushed": ((review("CHANGES_REQUESTED", "2026-11-18T10:00:00Z", on=old),), (), waits),
|
||||
"the worker replied in a thread": ((asked, review("COMMENTED", after, by="worker")), (), waits),
|
||||
"the worker commented": ((asked,), (comment("worker", after),), waits),
|
||||
"the worker spoke only before": ((review("COMMENTED", before, by="worker"), asked, review("PENDING", None, by="worker")), (comment("worker", before),), news), # type: ignore[arg-type]
|
||||
"only a reviewer spoke after": ((asked, review("COMMENTED", after)), (comment("reviewer", after),), news),
|
||||
"a reviewer asked again on the new head": ((asked, review("CHANGES_REQUESTED", before, on=old), review("COMMENTED", "2026-11-18T09:30:00Z", by="worker")), (), news),
|
||||
"GitHub lists no such review": ((review("APPROVED", before, on=old),), (comment("worker", after),), news),
|
||||
}
|
||||
for case, (reviews, comments, found) in cases.items():
|
||||
with self.subTest(case=case):
|
||||
self.assertEqual(Forge(decision="CHANGES_REQUESTED", reviews=reviews, comments=comments).gate(), found)
|
||||
self.assertEqual(NEW_REVIEW, "a new review")
|
||||
|
||||
def test_a_reviewers_unanswered_comment_is_news_though_the_worker_answered_the_requested_changes(self):
|
||||
forge = Forge(decision="CHANGES_REQUESTED", reviews=(review("CHANGES_REQUESTED", "2026-11-18T10:00:00Z", on="fed" * 4),), threads=(thread(False, "reviewer"),))
|
||||
self.assertEqual(forge.gate(), Open("a reviewer's comment on pull request #7 waits for an answer, in a.py"))
|
||||
|
||||
def test_the_worker_goes_on_where_nothing_but_the_worker_moves_the_pull_request(self):
|
||||
cases = {
|
||||
"pull request #7 passed its checks and is approved": Forge(decision="APPROVED"),
|
||||
@@ -82,7 +128,7 @@ class GateOfAChunk(unittest.TestCase):
|
||||
"a check on pull request 7 failed; fix it and push": Forge(buckets=("fail",)),
|
||||
"a reviewer requested changes on pull request #7": Forge(decision="CHANGES_REQUESTED"),
|
||||
"a reviewer's comment on pull request #7 waits for an answer, in a.py, b.py": Forge(threads=(thread(False, "reviewer", "b.py"), thread(False, "reviewer"), thread(True, "reviewer", "c.py"), thread(False, "worker", "d.py"))),
|
||||
"no pull request from origin's ABC-21 is open at abcabcabcabc": Forge(pulls=""),
|
||||
"no pull request from origin's ABC-21 is open at abcabcabcabc; it may be merged or closed already": Forge(pulls=""),
|
||||
"no worktree of /code/billing has the branch ABC-21 checked out": Forge(worktrees="worktree /code/billing\nHEAD aaa\nbranch refs/heads/main\n"),
|
||||
}
|
||||
for news, forge in cases.items():
|
||||
@@ -93,7 +139,9 @@ class GateOfAChunk(unittest.TestCase):
|
||||
for command in ("git worktree", "git remote", "gh pr list", "gh pr view", "gh pr checks", "gh api graphql"):
|
||||
with self.subTest(command=command):
|
||||
self.assertEqual(Forge(fails=command).gate(), Failure(f"{command} failed"))
|
||||
self.assertEqual(Forge("https://gitea.larvit.se/o/billing.git", fails="tea api repos/{owner}/{repo}/branches").gate(), Failure("tea api repos/{owner}/{repo}/branches/main failed"))
|
||||
for endpoint in ("pulls?state=open&limit=50", "pulls/7", "branches/main"):
|
||||
with self.subTest(endpoint=endpoint):
|
||||
self.assertEqual(Forge("https://gitea.larvit.se/o/billing.git", fails=f"tea api repos/{{owner}}/{{repo}}/{endpoint}").gate(), Failure(f"tea api repos/{{owner}}/{{repo}}/{endpoint} failed"))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in New Issue
Block a user