Reuse deploy-release's checks wait in chunk-pr merge
This commit is contained in:
@@ -7,7 +7,7 @@ import unittest
|
||||
|
||||
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
|
||||
|
||||
from chunk_pr.main import CHECKS_TIMEOUT_SECONDS, POLL_SECONDS, cleaned, merged, step
|
||||
from chunk_pr.main import cleaned, merged, step
|
||||
from failure import Failure
|
||||
from test_env import isolated_env
|
||||
from deploy_release.forge import Run
|
||||
@@ -21,25 +21,12 @@ GH_LIST = ["gh", "pr", "list", "--head", "item-7", "--json", "number,headRefOid,
|
||||
TEA_LIST = ["tea", "api", "repos/{owner}/{repo}/pulls?state=open&limit=50"]
|
||||
GH_SETTINGS = ["gh", "repo", "view", "--json", "mergeCommitAllowed,rebaseMergeAllowed,squashMergeAllowed"]
|
||||
TEA_SETTINGS = ["tea", "api", "repos/{owner}/{repo}"]
|
||||
GH_CHECKS = ["gh", "pr", "checks", "12", "--required", "--watch", "--interval", "10"]
|
||||
TEA_PROTECTIONS = ["tea", "api", "repos/{owner}/{repo}/branch_protections"]
|
||||
GH_CHECKS = ["gh", "pr", "checks", "12", "--watch", "--fail-fast"]
|
||||
TEA_BRANCH = ["tea", "api", "repos/{owner}/{repo}/branches/main"]
|
||||
TEA_STATUS = ["tea", "api", f"repos/{{owner}}/{{repo}}/commits/{HEAD}/status"]
|
||||
OWED = [str(pathlib.Path(__file__).resolve().parents[1] / "larv-review"), "scoring-run-owed"]
|
||||
|
||||
|
||||
class Clock:
|
||||
"""A fake clock whose sleep only advances its time."""
|
||||
|
||||
def __init__(self):
|
||||
self.now = 0.0
|
||||
|
||||
def monotonic(self) -> float:
|
||||
return self.now
|
||||
|
||||
def sleep(self, seconds: float):
|
||||
self.now += seconds
|
||||
|
||||
|
||||
class Checkout:
|
||||
"""A fake run in a checkout of branch item-7 at commit HEAD, cloned from the GitHub or Gitea origin it is given, with main as origin's default branch.
|
||||
|
||||
@@ -70,15 +57,17 @@ class Checkout:
|
||||
return f"12\t{HEAD}\tfalse\n" if self.open else ""
|
||||
if command == TEA_LIST:
|
||||
return json.dumps([{"number": 12, "head": {"ref": "item-7", "label": "item-7", "sha": HEAD, "repo": {"full_name": "me/project"}}, "base": {"repo": {"full_name": "me/project"}}}] if self.open else [])
|
||||
if command == TEA_STATUS and self.statuses:
|
||||
return self.statuses.pop(0)
|
||||
if command == TEA_STATUS:
|
||||
return self.statuses.pop(0) if self.statuses else json.dumps({"total_count": 0, "state": "pending", "statuses": []})
|
||||
if command == TEA_BRANCH:
|
||||
return json.dumps({"enable_status_check": False})
|
||||
if command in (GH_SETTINGS, TEA_SETTINGS):
|
||||
return json.dumps({setting: True for setting in self.allowed})
|
||||
return ""
|
||||
|
||||
def acted(self) -> list[list[str]]:
|
||||
"""What it ran beyond reading the checkout, the pull requests and the repository's settings."""
|
||||
reads = [GH_LIST, TEA_LIST, GH_SETTINGS, TEA_SETTINGS, OWED, GH_CHECKS, TEA_PROTECTIONS, TEA_STATUS]
|
||||
reads = [GH_LIST, TEA_LIST, GH_SETTINGS, TEA_SETTINGS, OWED, GH_CHECKS, TEA_BRANCH, TEA_STATUS]
|
||||
return [command for command in self.ran if tuple(command) not in self.answers and command not in reads]
|
||||
|
||||
|
||||
@@ -245,56 +234,47 @@ class Merge(unittest.TestCase):
|
||||
self.assertIsInstance(step(["merge", "--gate-passed"], checkout), Failure)
|
||||
self.assertEqual(checkout.ran, [])
|
||||
|
||||
def test_github_waits_for_the_required_checks_before_merging(self):
|
||||
def test_github_waits_for_the_checks_before_merging(self):
|
||||
checkout = Checkout(GITHUB, open=True)
|
||||
self.assertEqual(step(["merge"], checkout), "Merged pull request #12 (merge).")
|
||||
self.assertEqual(checkout.ran[-2:], [GH_CHECKS, ["gh", "pr", "merge", "12", "--merge", "--match-head-commit", HEAD]])
|
||||
|
||||
def test_a_failed_required_check_on_github_leaves_it_unmerged(self):
|
||||
def test_a_failed_check_on_github_leaves_it_unmerged(self):
|
||||
checkout = Checkout(GITHUB, open=True)
|
||||
checkout.answers[tuple(GH_CHECKS)] = Failure("gh pr checks 12: CI / tests\tfail")
|
||||
self.assertEqual(step(["merge"], checkout), Failure("gh pr checks 12: CI / tests\tfail"))
|
||||
self.assertEqual(checkout.acted(), [])
|
||||
|
||||
def test_a_github_repository_requiring_no_checks_merges(self):
|
||||
checkout = Checkout(GITHUB, open=True)
|
||||
checkout.answers[tuple(GH_CHECKS)] = Failure("gh pr checks 12: no required checks reported on the 'item-7' branch")
|
||||
self.assertEqual(step(["merge"], checkout), "Merged pull request #12 (merge).")
|
||||
|
||||
def protected(self, *statuses: list[tuple[str, str]], rule: str = "main") -> Checkout:
|
||||
"""A Gitea Checkout whose rule for branch rule requires CI / tests, its status at HEAD reading each of statuses in turn."""
|
||||
def protected(self, *reads: list[tuple[str, str]]) -> Checkout:
|
||||
"""A Gitea Checkout whose main requires a check matching CI / tests*, its status at HEAD holding each of reads in turn."""
|
||||
checkout = Checkout(GITEA, open=True)
|
||||
checkout.answers[tuple(TEA_PROTECTIONS)] = json.dumps([{"rule_name": rule, "enable_status_check": True, "status_check_contexts": ["CI / tests*"]}])
|
||||
checkout.statuses = [json.dumps({"statuses": [{"context": context, "status": state, "target_url": "https://ci/1"} for context, state in read]}) for read in statuses]
|
||||
checkout.answers[tuple(TEA_BRANCH)] = json.dumps({"enable_status_check": True, "status_check_contexts": ["CI / tests*"]})
|
||||
def combined(read: list[tuple[str, str]]) -> str:
|
||||
states = {state for _, state in read}
|
||||
state = "failure" if "failure" in states else "pending" if "pending" in states or not read else "success"
|
||||
return json.dumps({"total_count": len(read), "state": state, "statuses": [{"context": context, "status": status} for context, status in read]})
|
||||
checkout.statuses = [combined(read) for read in reads]
|
||||
return checkout
|
||||
|
||||
def test_gitea_waits_until_every_required_check_passes_then_merges(self):
|
||||
checkout = self.protected([], [("CI / tests (pull_request)", "pending"), ("lint", "failure")], [("CI / tests (pull_request)", "success"), ("lint", "failure")])
|
||||
clock = Clock()
|
||||
self.assertEqual(merged(checkout, clock), "Merged pull request #12 (merge).")
|
||||
self.assertEqual(clock.now, 2 * POLL_SECONDS)
|
||||
def test_gitea_waits_until_the_required_check_reports_and_passes_then_merges(self):
|
||||
checkout = self.protected([], [("CI / tests (pull_request)", "pending")], [("CI / tests (pull_request)", "success")])
|
||||
slept: list[float] = []
|
||||
self.assertEqual(merged(checkout, slept.append), "Merged pull request #12 (merge).")
|
||||
self.assertEqual(len(slept), 2)
|
||||
self.assertEqual(checkout.ran.count(TEA_STATUS), 3)
|
||||
|
||||
def test_a_failed_required_check_on_gitea_leaves_it_unmerged_naming_it(self):
|
||||
def test_a_failed_check_on_gitea_leaves_it_unmerged(self):
|
||||
checkout = self.protected([("CI / tests (pull_request)", "pending")], [("CI / tests (pull_request)", "failure")])
|
||||
self.assertEqual(merged(checkout, Clock()), Failure(f"the required check CI / tests (pull_request) failed at {HEAD}: https://ci/1"))
|
||||
self.assertIsInstance(merged(checkout, lambda _: None), Failure)
|
||||
self.assertEqual(checkout.acted(), [])
|
||||
|
||||
def test_gitea_checks_still_running_at_the_timeout_leave_it_unmerged(self):
|
||||
def test_gitea_checks_that_never_pass_leave_it_unmerged(self):
|
||||
checkout = self.protected(*[[("CI / tests (pull_request)", "pending")]] * 1000)
|
||||
clock = Clock()
|
||||
failed = merged(checkout, clock)
|
||||
failed = merged(checkout, lambda _: None)
|
||||
self.assertIsInstance(failed, Failure)
|
||||
self.assertIn("CI / tests*", failed.reason)
|
||||
self.assertIn("run chunk-pr merge again", failed.reason)
|
||||
self.assertLessEqual(clock.now, CHECKS_TIMEOUT_SECONDS)
|
||||
self.assertIn("gave up on pull request 12", failed.reason)
|
||||
self.assertEqual(checkout.acted(), [])
|
||||
|
||||
def test_gitea_without_a_rule_for_the_default_branch_merges_without_reading_the_status(self):
|
||||
checkout = self.protected(rule="release/*")
|
||||
self.assertEqual(step(["merge"], checkout), "Merged pull request #12 (merge).")
|
||||
self.assertNotIn(TEA_STATUS, checkout.ran)
|
||||
|
||||
def test_a_scoring_run_the_chunk_owes_and_has_not_recorded_refuses_the_merge(self):
|
||||
for origin in (GITHUB, GITEA):
|
||||
with self.subTest(origin=origin):
|
||||
|
||||
Reference in New Issue
Block a user