Add do-epic's gate read and parked-chunk state files
This commit is contained in:
@@ -0,0 +1,89 @@
|
||||
import json
|
||||
import pathlib
|
||||
import sys
|
||||
import unittest
|
||||
|
||||
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
|
||||
|
||||
from do_epic.gates import APPROVAL, CHECKS, gate, Gate, 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"
|
||||
HEAD = "abc" * 4
|
||||
|
||||
|
||||
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 __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:
|
||||
self.ran: list[str] = []
|
||||
self.fails = fails
|
||||
self.answers = {
|
||||
"git worktree": worktrees,
|
||||
"git remote": origin + "\n",
|
||||
"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 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}/branches/main": '{"name": "main"}',
|
||||
"tea api repos/{owner}/{repo}/commits/" + HEAD + "/status": status,
|
||||
}
|
||||
|
||||
def at(self, folder: str):
|
||||
def run(command: list[str]) -> str | Failure:
|
||||
self.ran.append(folder)
|
||||
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
|
||||
|
||||
def gate(self) -> Gate | Open | Failure:
|
||||
return gate("ABC-21", "/code/billing", self.at)
|
||||
|
||||
|
||||
class GateOfAChunk(unittest.TestCase):
|
||||
def test_a_pull_request_whose_checks_passed_awaits_the_approval_its_protection_requires(self):
|
||||
forge = Forge()
|
||||
self.assertEqual(forge.gate(), Gate("/code/billing-ABC-21", "7", APPROVAL))
|
||||
self.assertEqual(set(forge.ran[1:]), {"/code/billing-ABC-21"})
|
||||
|
||||
def test_a_pull_request_awaits_its_checks_while_one_runs_whatever_its_reviews_say(self):
|
||||
for decision in ("REVIEW_REQUIRED", "APPROVED", "CHANGES_REQUESTED", ""):
|
||||
with self.subTest(decision=decision):
|
||||
self.assertEqual(Forge(decision=decision, buckets=("pass", "pending")).gate(), Gate("/code/billing-ABC-21", "7", CHECKS))
|
||||
|
||||
def test_a_gitea_pull_request_awaits_only_its_checks(self):
|
||||
gitea = "https://gitea.larvit.se/o/billing.git"
|
||||
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_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"),
|
||||
"pull request #7 passed its checks": Forge(decision=""),
|
||||
"pull request #7 is a draft": Forge(draft=True, buckets=("pending",)),
|
||||
"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 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():
|
||||
with self.subTest(news=news):
|
||||
self.assertEqual(forge.gate(), Open(news))
|
||||
|
||||
def test_a_read_that_fails_is_a_failure_and_says_nothing_of_the_gate(self):
|
||||
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"))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user