|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Keep PR scanning isolated and route only maintenance pushes without polling.""" |
| 3 | + |
| 4 | +from copy import deepcopy |
| 5 | +from pathlib import Path |
| 6 | +import unittest |
| 7 | + |
| 8 | +import yaml |
| 9 | + |
| 10 | +ROOT = Path(__file__).resolve().parents[2] |
| 11 | +BRANCH = "maintenance/2.3.6-base" |
| 12 | +REFERENCE = "refs/heads/" + BRANCH |
| 13 | +MAIN_CONDITION = "github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository" |
| 14 | +MAINTENANCE_CONDITION = "github.event_name == 'push' && github.ref == '" + REFERENCE + "'" |
| 15 | +ARGS = "-Dsonar.qualitygate.wait={} -Dsonar.qualitygate.timeout=300" |
| 16 | + |
| 17 | + |
| 18 | +def triggers(workflow): |
| 19 | + return workflow.get("on", workflow.get(True)) |
| 20 | + |
| 21 | + |
| 22 | +def scanner(workflow): |
| 23 | + [scan] = [step for step in workflow["jobs"]["analysis"]["steps"] |
| 24 | + if step.get("uses", "").startswith("SonarSource/sonarqube-scan-action@")] |
| 25 | + return scan |
| 26 | + |
| 27 | + |
| 28 | +def require_maintenance_boundary(workflow): |
| 29 | + job = workflow["jobs"]["analysis"] |
| 30 | + if (triggers(workflow) != {"push": {"branches": [BRANCH]}} |
| 31 | + or job.get("if") != MAINTENANCE_CONDITION |
| 32 | + or scanner(workflow).get("with") != {"args": ARGS.format("false")}): |
| 33 | + raise ValueError("Only the exact maintenance push may disable polling") |
| 34 | + |
| 35 | + |
| 36 | +class SonarWaitRoutingTest(unittest.TestCase): |
| 37 | + def setUp(self): |
| 38 | + self.main = yaml.safe_load((ROOT / ".github/workflows/sonar.yml").read_text()) |
| 39 | + self.maintenance = yaml.safe_load( |
| 40 | + (ROOT / ".github/workflows/sonar-maintenance.yml").read_text()) |
| 41 | + |
| 42 | + def assert_main_profile(self, workflow): |
| 43 | + self.assertEqual({"push": {"branches": ["main"]}, |
| 44 | + "pull_request": {"branches": ["main", BRANCH]}}, triggers(workflow)) |
| 45 | + self.assertEqual(MAIN_CONDITION, workflow["jobs"]["analysis"]["if"]) |
| 46 | + self.assertEqual({"args": ARGS.format("true")}, scanner(workflow)["with"]) |
| 47 | + |
| 48 | + def test_main_and_pr_scanner_keeps_its_existing_isolated_profile(self): |
| 49 | + self.assert_main_profile(self.main) |
| 50 | + |
| 51 | + def test_maintenance_has_only_the_exact_push_boundary(self): |
| 52 | + require_maintenance_boundary(self.maintenance) |
| 53 | + self.assertEqual({"analysis"}, set(self.maintenance["jobs"])) |
| 54 | + |
| 55 | + def assert_scanner_isolation(self, workflow): |
| 56 | + self.assertEqual({"contents": "read"}, workflow["permissions"]) |
| 57 | + job = workflow["jobs"]["analysis"] |
| 58 | + self.assertEqual({"name", "if", "runs-on", "timeout-minutes", "steps"}, set(job)) |
| 59 | + self.assertEqual("ubuntu-latest", job["runs-on"]) |
| 60 | + self.assertEqual(15, job["timeout-minutes"]) |
| 61 | + self.assertEqual([ |
| 62 | + {"uses": "actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803", |
| 63 | + "with": {"fetch-depth": 0, "persist-credentials": False}}, |
| 64 | + {"uses": "SonarSource/sonarqube-scan-action@d209202bc7d53ff1cc128f7f907dac145c9d6ae9", |
| 65 | + "env": {"SONAR_TOKEN": "${{ secrets.SONAR_TOKEN }}"}, |
| 66 | + "with": scanner(workflow)["with"]}, |
| 67 | + ], job["steps"]) |
| 68 | + |
| 69 | + def test_both_scanners_keep_upload_and_credential_isolation(self): |
| 70 | + for workflow in (self.main, self.maintenance): |
| 71 | + with self.subTest(workflow=workflow["name"]): |
| 72 | + self.assert_scanner_isolation(workflow) |
| 73 | + |
| 74 | + def test_changed_checkout_token_or_execution_is_refused(self): |
| 75 | + for source in (self.main, self.maintenance): |
| 76 | + for mutation in ("checkout", "token", "execution", "continue"): |
| 77 | + workflow = deepcopy(source) |
| 78 | + job = workflow["jobs"]["analysis"] |
| 79 | + if mutation == "checkout": |
| 80 | + job["steps"][0]["with"]["persist-credentials"] = True |
| 81 | + elif mutation == "token": |
| 82 | + scanner(workflow)["env"]["SONAR_TOKEN"] = "${{ secrets.OTHER_TOKEN }}" |
| 83 | + elif mutation == "execution": |
| 84 | + job["steps"].insert(1, {"run": "node scripts/candidate.js"}) |
| 85 | + else: |
| 86 | + job["continue-on-error"] = True |
| 87 | + with self.subTest(workflow=source["name"], mutation=mutation), self.assertRaises(AssertionError): |
| 88 | + self.assert_scanner_isolation(workflow) |
| 89 | + |
| 90 | + def test_overlapping_maintenance_push_route_is_refused(self): |
| 91 | + workflow = deepcopy(self.main) |
| 92 | + triggers(workflow)["push"]["branches"].append(BRANCH) |
| 93 | + with self.assertRaises(AssertionError): |
| 94 | + self.assert_main_profile(workflow) |
| 95 | + |
| 96 | + def test_event_routing_produces_one_analysis_with_required_wait(self): |
| 97 | + cases = [ |
| 98 | + ("push", REFERENCE, "", True, [False]), |
| 99 | + ("push", "refs/heads/main", "", True, [True]), |
| 100 | + ("pull_request", "refs/pull/1/merge", BRANCH, True, [True]), |
| 101 | + ("pull_request", "refs/pull/1/merge", "main", True, [True]), |
| 102 | + ("pull_request", "refs/pull/1/merge", BRANCH, False, []), |
| 103 | + ("push", REFERENCE + "-extra", "", True, []), |
| 104 | + ("push", "refs/heads/maintenance/other", "", True, []), |
| 105 | + ("push", "refs/tags/2.3.7", "", True, []), |
| 106 | + ("workflow_dispatch", REFERENCE, "", True, []), |
| 107 | + ("schedule", REFERENCE, "", True, []), |
| 108 | + ] |
| 109 | + for event, reference, base, same_repository, expected in cases: |
| 110 | + with self.subTest(event=event, reference=reference, base=base): |
| 111 | + actual = [] |
| 112 | + for workflow in (self.main, self.maintenance): |
| 113 | + route = triggers(workflow).get(event) |
| 114 | + branch = base if event == "pull_request" else reference.removeprefix("refs/heads/") |
| 115 | + if route is None or branch not in route["branches"]: |
| 116 | + continue |
| 117 | + if workflow is self.main and event == "pull_request" and not same_repository: |
| 118 | + continue |
| 119 | + actual.append(scanner(workflow)["with"]["args"] == ARGS.format("true")) |
| 120 | + self.assertEqual(expected, actual) |
| 121 | + |
| 122 | + def test_broader_triggers_or_conditions_are_refused(self): |
| 123 | + for mutation in ("pr", "main", "wildcard", "condition", "wait", "retarget"): |
| 124 | + workflow = deepcopy(self.maintenance) |
| 125 | + route = triggers(workflow) |
| 126 | + if mutation == "pr": |
| 127 | + route["pull_request"] = {"branches": [BRANCH]} |
| 128 | + elif mutation == "main": |
| 129 | + route["push"]["branches"].append("main") |
| 130 | + elif mutation == "wildcard": |
| 131 | + route["push"]["branches"] = ["maintenance/**"] |
| 132 | + elif mutation == "condition": |
| 133 | + workflow["jobs"]["analysis"]["if"] = "github.event_name == 'push'" |
| 134 | + elif mutation == "wait": |
| 135 | + scanner(workflow)["with"]["args"] = ARGS.format("true") |
| 136 | + else: |
| 137 | + scanner(workflow)["with"]["args"] += " -Dsonar.branch.name=main" |
| 138 | + with self.subTest(mutation=mutation), self.assertRaises(ValueError): |
| 139 | + require_maintenance_boundary(workflow) |
| 140 | + |
| 141 | + |
| 142 | +if __name__ == "__main__": |
| 143 | + unittest.main() |
0 commit comments