|
| 1 | +"""A Dependabot minor or patch update of the dependency files merges without a |
| 2 | +review; every other change keeps the reviewed path (ADR-1126).""" |
| 3 | + |
| 4 | +from pathlib import Path |
| 5 | +import textwrap |
| 6 | +import unittest |
| 7 | + |
| 8 | +ROOT = Path(__file__).resolve().parents[2] |
| 9 | +REPO = "cdeust/Cortex" |
| 10 | +HEAD = "a" * 40 |
| 11 | +MINOR = "version-update:semver-minor" |
| 12 | +PATCH = "version-update:semver-patch" |
| 13 | +MAJOR = "version-update:semver-major" |
| 14 | + |
| 15 | + |
| 16 | +def resolver(): |
| 17 | + text = (ROOT / ".github/workflows/pipeline-post-merge.yml").read_text() |
| 18 | + source = text.split(" python3 - <<'PY'\n", 1)[1].split( |
| 19 | + "\n PY", 1 |
| 20 | + )[0] |
| 21 | + namespace = {"__name__": "resolver_test"} |
| 22 | + exec(compile(textwrap.dedent(source), "trusted_resolver", "exec"), namespace) |
| 23 | + return namespace |
| 24 | + |
| 25 | + |
| 26 | +def commit(updates=(PATCH,), author="dependabot[bot]", verified=True): |
| 27 | + lines = "".join(f"- dependency-name: x\n update-type: {u}\n" for u in updates) |
| 28 | + return { |
| 29 | + "author": {"login": author}, |
| 30 | + "commit": { |
| 31 | + "message": f"deps: bump x\n\n---\nupdated-dependencies:\n{lines}...\n", |
| 32 | + "verification": {"verified": verified}, |
| 33 | + }, |
| 34 | + } |
| 35 | + |
| 36 | + |
| 37 | +def pull(login="dependabot[bot]", kind="Bot"): |
| 38 | + return { |
| 39 | + "number": 7, |
| 40 | + "state": "open", |
| 41 | + "draft": False, |
| 42 | + "title": "deps: bump x", |
| 43 | + "labels": [], |
| 44 | + "user": {"login": login, "type": kind}, |
| 45 | + } |
| 46 | + |
| 47 | + |
| 48 | +class FakeApi: |
| 49 | + def __init__(self, commits, files): |
| 50 | + self.commits, self.files, self.puts = commits, files, [] |
| 51 | + |
| 52 | + def records(self, path, key=None): |
| 53 | + if path.endswith("/commits"): |
| 54 | + return self.commits |
| 55 | + if path.endswith("/files"): |
| 56 | + return [{"filename": name} for name in self.files] |
| 57 | + return [] # no review run, no comment: the reviewed path holds |
| 58 | + |
| 59 | + def request(self, method, path, body=None): |
| 60 | + self.puts.append((method, path, body)) |
| 61 | + return {"merged": True, "sha": "c" * 40} |
| 62 | + |
| 63 | + |
| 64 | +class LowRiskTierTests(unittest.TestCase): |
| 65 | + def low(self, commits=None, files=("uv.lock",), pr=None): |
| 66 | + api = FakeApi([commit()] if commits is None else commits, files) |
| 67 | + return resolver()["_is_low_risk"](pr or pull(), REPO, api) |
| 68 | + |
| 69 | + def test_a_dependabot_patch_of_the_lock_is_low_risk(self): |
| 70 | + self.assertTrue(self.low()) |
| 71 | + |
| 72 | + def test_a_grouped_minor_and_patch_update_is_low_risk(self): |
| 73 | + self.assertTrue(self.low([commit((MINOR, PATCH, PATCH))])) |
| 74 | + self.assertTrue(self.low(files=("uv.lock", "pyproject.toml"))) |
| 75 | + |
| 76 | + def test_a_major_update_is_not(self): |
| 77 | + self.assertFalse(self.low([commit((MAJOR,))])) |
| 78 | + self.assertFalse(self.low([commit((PATCH, MAJOR))])) |
| 79 | + |
| 80 | + def test_an_update_type_it_does_not_know_is_not(self): |
| 81 | + self.assertFalse(self.low([commit(("version-update:semver-minorx",))])) |
| 82 | + self.assertFalse(self.low([commit(())])) |
| 83 | + self.assertFalse(self.low([])) |
| 84 | + |
| 85 | + def test_a_workflow_or_any_other_file_is_not(self): |
| 86 | + self.assertFalse(self.low(files=(".github/workflows/scorecard.yml",))) |
| 87 | + self.assertFalse(self.low(files=("uv.lock", "mcp_server/server.py"))) |
| 88 | + self.assertFalse(self.low(files=())) |
| 89 | + |
| 90 | + def test_another_author_or_an_unsigned_commit_is_not(self): |
| 91 | + self.assertFalse(self.low(pr=pull(login="cdeust", kind="User"))) |
| 92 | + self.assertFalse(self.low(pr=pull(kind="User"))) |
| 93 | + self.assertFalse(self.low([commit(), commit(author="cdeust")])) |
| 94 | + self.assertFalse(self.low([commit(verified=False)])) |
| 95 | + |
| 96 | + def test_a_low_risk_pr_merges_without_a_review(self): |
| 97 | + api = FakeApi([commit()], ("uv.lock",)) |
| 98 | + merge = resolver()["_merge_open_pr"] |
| 99 | + self.assertEqual(merge(pull(), "/merge", HEAD, api, REPO), "c" * 40) |
| 100 | + self.assertEqual( |
| 101 | + api.puts, [("PUT", "/merge", {"sha": HEAD, "merge_method": "squash"})] |
| 102 | + ) |
| 103 | + |
| 104 | + def test_any_other_pr_still_needs_the_review(self): |
| 105 | + ns = resolver() |
| 106 | + for api in ( |
| 107 | + FakeApi([commit((MAJOR,))], ("uv.lock",)), |
| 108 | + FakeApi([commit()], (".github/workflows/pipeline.yml",)), |
| 109 | + ): |
| 110 | + with self.assertRaises(ns["PostMergeError"]) as caught: |
| 111 | + ns["_merge_open_pr"](pull(), "/merge", HEAD, api, REPO) |
| 112 | + self.assertIn("no Claude review ran", str(caught.exception)) |
| 113 | + self.assertEqual(api.puts, []) |
| 114 | + |
| 115 | + def test_a_hold_label_still_holds_a_low_risk_pr(self): |
| 116 | + ns = resolver() |
| 117 | + pr = {**pull(), "labels": [{"name": "hold"}]} |
| 118 | + api = FakeApi([commit()], ("uv.lock",)) |
| 119 | + with self.assertRaises(ns["PostMergeError"]): |
| 120 | + ns["_merge_open_pr"](pr, "/merge", HEAD, api, REPO) |
| 121 | + self.assertEqual(api.puts, []) |
| 122 | + |
| 123 | + |
| 124 | +if __name__ == "__main__": |
| 125 | + unittest.main() |
0 commit comments