|
| 1 | +"""The review's completion wakes the resolver too, so the merge does not depend on |
| 2 | +which of the review and the Pipeline finishes first (ADR-1127).""" |
| 3 | + |
| 4 | +from pathlib import Path |
| 5 | +import textwrap |
| 6 | +import unittest |
| 7 | + |
| 8 | +ROOT = Path(__file__).resolve().parents[2] |
| 9 | +WORKFLOWS = ROOT / ".github/workflows" |
| 10 | +REPO = "cdeust/Cortex" |
| 11 | +HEAD = "a" * 40 |
| 12 | + |
| 13 | + |
| 14 | +def resolver(): |
| 15 | + text = (WORKFLOWS / "pipeline-post-merge.yml").read_text() |
| 16 | + source = text.split(" python3 - <<'PY'\n", 1)[1].split( |
| 17 | + "\n PY", 1 |
| 18 | + )[0] |
| 19 | + namespace = {"__name__": "resolver_test"} |
| 20 | + exec(compile(textwrap.dedent(source), "trusted_resolver", "exec"), namespace) |
| 21 | + return namespace |
| 22 | + |
| 23 | + |
| 24 | +def review(**changes): |
| 25 | + run = { |
| 26 | + "name": "PR Review", |
| 27 | + "path": ".github/workflows/claude-review.yml", |
| 28 | + "event": "pull_request", |
| 29 | + "status": "completed", |
| 30 | + "conclusion": "success", |
| 31 | + "head_repository": {"full_name": REPO}, |
| 32 | + "head_sha": HEAD, |
| 33 | + } |
| 34 | + return {**run, **changes} |
| 35 | + |
| 36 | + |
| 37 | +class FakeApi: |
| 38 | + def __init__(self, pipelines): |
| 39 | + self.pipelines, self.paths = pipelines, [] |
| 40 | + |
| 41 | + def records(self, path, key=None): |
| 42 | + self.paths.append(path) |
| 43 | + return self.pipelines |
| 44 | + |
| 45 | + |
| 46 | +class ReviewWakesResolverTests(unittest.TestCase): |
| 47 | + def pipeline_of(self, source, pipelines): |
| 48 | + api = FakeApi(pipelines) |
| 49 | + return resolver()["_pipeline_of_review"](source, REPO, api), api |
| 50 | + |
| 51 | + def held(self, source, pipelines, message): |
| 52 | + ns = resolver() |
| 53 | + with self.assertRaises(ns["PostMergeError"]) as caught: |
| 54 | + ns["_pipeline_of_review"](source, REPO, FakeApi(pipelines)) |
| 55 | + self.assertIn(message, str(caught.exception)) |
| 56 | + |
| 57 | + def test_the_newest_successful_pipeline_of_the_head_is_the_source(self): |
| 58 | + runs = [{"id": 5, "head_sha": HEAD}, {"id": 9, "head_sha": HEAD}] |
| 59 | + found, api = self.pipeline_of(review(), runs) |
| 60 | + self.assertEqual(found["id"], 9) |
| 61 | + self.assertEqual( |
| 62 | + api.paths, |
| 63 | + [ |
| 64 | + f"/repos/{REPO}/actions/workflows/pipeline.yml/runs" |
| 65 | + f"?head_sha={HEAD}&event=pull_request&status=success" |
| 66 | + ], |
| 67 | + ) |
| 68 | + |
| 69 | + def test_a_pipeline_of_another_head_does_not_count(self): |
| 70 | + self.held(review(), [{"id": 9, "head_sha": "b" * 40}], "has not succeeded") |
| 71 | + |
| 72 | + def test_no_successful_pipeline_holds_the_merge(self): |
| 73 | + self.held(review(), [], "Pipeline has not succeeded on this head") |
| 74 | + |
| 75 | + def test_a_review_that_failed_or_ran_elsewhere_is_refused(self): |
| 76 | + for change in ( |
| 77 | + {"conclusion": "failure"}, |
| 78 | + {"status": "in_progress"}, |
| 79 | + {"event": "pull_request_target"}, |
| 80 | + {"path": ".github/workflows/other.yml"}, |
| 81 | + ): |
| 82 | + self.held(review(**change), [], "did not complete successfully") |
| 83 | + fork = review(head_repository={"full_name": "someone/Cortex"}) |
| 84 | + self.held(fork, [], "head repository differs") |
| 85 | + |
| 86 | + def test_a_pr_merged_before_the_review_ended_is_not_tested_twice(self): |
| 87 | + text = (WORKFLOWS / "pipeline-post-merge.yml").read_text() |
| 88 | + body = text.split("def resolve(event", 1)[1].split("def main", 1)[0] |
| 89 | + held = body.split('run.get("woken_by_review")', 1)[1].split("return {", 2)[1] |
| 90 | + self.assertIn('"run_tests": "false"', held) |
| 91 | + self.assertLess( |
| 92 | + body.index('run.get("woken_by_review")'), |
| 93 | + body.index('"run_tests": "true"'), |
| 94 | + ) |
| 95 | + |
| 96 | + def test_both_workflows_wake_the_resolver(self): |
| 97 | + caller = (WORKFLOWS / "pipeline-after-merge.yml").read_text() |
| 98 | + self.assertIn("workflows: [Pipeline, PR Review]", caller) |
| 99 | + review_name = (WORKFLOWS / "claude-review.yml").read_text().splitlines()[0] |
| 100 | + self.assertEqual(review_name, "name: PR Review") |
| 101 | + |
| 102 | + |
| 103 | +if __name__ == "__main__": |
| 104 | + unittest.main() |
0 commit comments