Skip to content

Commit a8f7536

Browse files
authored
Merge pull request #104 from durable-workflow/agent/cli/101-maintenance-sonar-wait
Isolate maintenance Sonar polling from PR analysis
2 parents 21e31db + c14c786 commit a8f7536

4 files changed

Lines changed: 174 additions & 1 deletion

File tree

‎.github/workflows/build.yml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ jobs:
9595
run: |
9696
python -m pip install --only-binary :all: PyYAML==6.0.2
9797
python scripts/ci/test-release-cache-policy.py
98+
python scripts/ci/test-sonar-wait-routing.py
9899
python scripts/ci/test-native-prerequisites.py
99100
node --test scripts/ci/test-release-workflow-metadata.js
100101
node --test scripts/ci/test-standalone-runtime.js
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
name: SonarQube maintenance
2+
3+
on:
4+
push:
5+
branches: [maintenance/2.3.6-base]
6+
7+
permissions:
8+
contents: read
9+
10+
concurrency:
11+
group: sonar-maintenance-${{ github.ref }}
12+
cancel-in-progress: false
13+
14+
jobs:
15+
analysis:
16+
name: SonarQube analysis
17+
if: github.event_name == 'push' && github.ref == 'refs/heads/maintenance/2.3.6-base'
18+
runs-on: ubuntu-latest
19+
timeout-minutes: 15
20+
steps:
21+
- uses: actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 # v6
22+
with:
23+
fetch-depth: 0
24+
persist-credentials: false
25+
- uses: SonarSource/sonarqube-scan-action@d209202bc7d53ff1cc128f7f907dac145c9d6ae9 # v8.3.0
26+
env:
27+
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
28+
with:
29+
args: -Dsonar.qualitygate.wait=false -Dsonar.qualitygate.timeout=300

‎.github/workflows/sonar.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: SonarQube
22

33
on:
44
push:
5-
branches: [main, maintenance/2.3.6-base]
5+
branches: [main]
66
pull_request:
77
branches: [main, maintenance/2.3.6-base]
88

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
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

Comments
 (0)