fix(ci): PR gate check turns green when its issue gets accepted later
- Label changes on an issue trigger the gate for open PRs that link it - Sweep and issue runs re-run the PR's last failed gate run once the PR passes, so the red check on the head commit clears - Workflow gets actions: write for the rerun Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@@ -47,6 +47,7 @@ LABEL_NO_VISUAL = "gate: no visual"
47
47
LABEL_SKIP = "gate: skip"
48
48
49
49
COMMENT_MARKER = "<!-- pr-gate -->"
50
GATE_WORKFLOW = "pr-gate.yml"
50
51
DEFAULT_REPOSITORY = "danieldaeschle/rusty-teams"
51
52
52
53
@@ -308,6 +309,16 @@ def run_gate(github: GitHub, number: int, enforce: bool) -> bool:
308
309
return is_draft or not missing
309
310
310
311
312
def rerun_failed_gate(github: GitHub, raw: dict) -> None:
313
"""A sweep or an issue label passes the PR, but the red check sits on the head commit's own run: run that again."""
314
sha = urllib.parse.quote(raw["head"]["sha"])
315
runs = github.request("GET", f"/actions/workflows/{GATE_WORKFLOW}/runs?head_sha={sha}&event=pull_request_target&per_page=20")
316
latest = max((runs or {}).get("workflow_runs", []), key=lambda run: run["created_at"], default=None)
317
if latest and latest.get("conclusion") == "failure":
318
github.request("POST", f"/actions/runs/{latest['id']}/rerun")
319
print(f"#{raw['number']}: re-ran the failed gate run {latest['id']}")
320
321
311
322
def dry_run(github: GitHub, number: int) -> int:
312
323
_, pull_request = load_pull_request(github, number)
313
324
missing = check(pull_request)
@@ -335,10 +346,14 @@ def main() -> int:
335
346
number = os.environ.get("PR_NUMBER", "").strip()
336
347
if number:
337
348
return 0 if run_gate(github, int(number), enforce=True) else 1
349
issue = os.environ.get("ISSUE_NUMBER", "").strip()
338
350
failed = []
339
351
for raw in github.paged("/pulls?state=open"):
352
if issue and int(issue) not in linked_issue_numbers(raw.get("body") or ""):
353
continue
340
354
try:
341
run_gate(github, raw["number"], enforce=False)
355
if run_gate(github, raw["number"], enforce=False):
356
rerun_failed_gate(github, raw)
342
357
except urllib.error.HTTPError as error:
343
358
print(f"::warning::#{raw['number']}: {error}")
344
359
failed.append(raw["number"])
@@ -47,6 +47,7 @@ LABEL_NO_VISUAL = "gate: no visual"47
LABEL_SKIP = "gate: skip"47
LABEL_SKIP = "gate: skip"48
48
49
COMMENT_MARKER = "<!-- pr-gate -->"49
COMMENT_MARKER = "<!-- pr-gate -->"50
GATE_WORKFLOW = "pr-gate.yml"50
DEFAULT_REPOSITORY = "danieldaeschle/rusty-teams"51
DEFAULT_REPOSITORY = "danieldaeschle/rusty-teams"51
52
52
53
@@ -308,6 +309,16 @@ def run_gate(github: GitHub, number: int, enforce: bool) -> bool:308
return is_draft or not missing309
return is_draft or not missing309
310
310
311
312
def rerun_failed_gate(github: GitHub, raw: dict) -> None:313
"""A sweep or an issue label passes the PR, but the red check sits on the head commit's own run: run that again."""314
sha = urllib.parse.quote(raw["head"]["sha"])315
runs = github.request("GET", f"/actions/workflows/{GATE_WORKFLOW}/runs?head_sha={sha}&event=pull_request_target&per_page=20")316
latest = max((runs or {}).get("workflow_runs", []), key=lambda run: run["created_at"], default=None)317
if latest and latest.get("conclusion") == "failure":318
github.request("POST", f"/actions/runs/{latest['id']}/rerun")319
print(f"#{raw['number']}: re-ran the failed gate run {latest['id']}")320
321
311
def dry_run(github: GitHub, number: int) -> int:322
def dry_run(github: GitHub, number: int) -> int:312
_, pull_request = load_pull_request(github, number)323
_, pull_request = load_pull_request(github, number)313
missing = check(pull_request)324
missing = check(pull_request)@@ -335,10 +346,14 @@ def main() -> int:335
number = os.environ.get("PR_NUMBER", "").strip()346
number = os.environ.get("PR_NUMBER", "").strip()336
if number:347
if number:337
return 0 if run_gate(github, int(number), enforce=True) else 1348
return 0 if run_gate(github, int(number), enforce=True) else 1349
issue = os.environ.get("ISSUE_NUMBER", "").strip()338
failed = []350
failed = []339
for raw in github.paged("/pulls?state=open"):351
for raw in github.paged("/pulls?state=open"):352
if issue and int(issue) not in linked_issue_numbers(raw.get("body") or ""):353
continue340
try:354
try:341
run_gate(github, raw["number"], enforce=False)355
if run_gate(github, raw["number"], enforce=False):356
rerun_failed_gate(github, raw)342
except urllib.error.HTTPError as error:357
except urllib.error.HTTPError as error:343
print(f"::warning::#{raw['number']}: {error}")358
print(f"::warning::#{raw['number']}: {error}")344
failed.append(raw["number"])359
failed.append(raw["number"])@@ -148,11 +148,13 @@ class DocumentationTests(unittest.TestCase):
148
148
149
149
150
150
class FakeGitHub:
151
def __init__(self):
151
def __init__(self, responses=None):
152
152
self.calls = []
153
self.responses = responses or {}
153
154
154
155
def request(self, method, path, payload=None):
155
156
self.calls.append((method, path, payload))
157
return self.responses.get(path)
156
158
157
159
158
160
class LabelTests(unittest.TestCase):
@@ -179,5 +181,29 @@ class LabelTests(unittest.TestCase):
179
181
)
180
182
181
183
184
class RerunTests(unittest.TestCase):
185
RUNS_PATH = "/actions/workflows/pr-gate.yml/runs?head_sha=abc&event=pull_request_target&per_page=20"
186
PULL = {"number": 5, "head": {"sha": "abc"}}
187
188
def runs(self, *runs):
189
return FakeGitHub({self.RUNS_PATH: {"workflow_runs": list(runs)}})
190
191
def test_the_latest_failed_run_is_run_again(self):
192
github = self.runs(
193
{"id": 1, "created_at": "2026-11-25T10:04:38Z", "conclusion": "failure"},
194
{"id": 2, "created_at": "2026-11-25T10:05:01Z", "conclusion": "failure"},
195
)
196
pr_gate.rerun_failed_gate(github, self.PULL)
197
self.assertEqual(github.calls[-1], ("POST", "/actions/runs/2/rerun", None))
198
199
def test_a_passing_latest_run_is_left_alone(self):
200
github = self.runs(
201
{"id": 1, "created_at": "2026-11-25T10:04:38Z", "conclusion": "failure"},
202
{"id": 2, "created_at": "2026-11-25T10:05:01Z", "conclusion": "success"},
203
)
204
pr_gate.rerun_failed_gate(github, self.PULL)
205
self.assertEqual(len(github.calls), 1)
206
207
182
208
if __name__ == "__main__":
183
209
unittest.main()
@@ -148,11 +148,13 @@ class DocumentationTests(unittest.TestCase):148
148
149
149
150
class FakeGitHub:150
class FakeGitHub:151
def __init__(self):151
def __init__(self, responses=None):152
self.calls = []152
self.calls = []153
self.responses = responses or {}153
154
154
def request(self, method, path, payload=None):155
def request(self, method, path, payload=None):155
self.calls.append((method, path, payload))156
self.calls.append((method, path, payload))157
return self.responses.get(path)156
158
157
159
158
class LabelTests(unittest.TestCase):160
class LabelTests(unittest.TestCase):@@ -179,5 +181,29 @@ class LabelTests(unittest.TestCase):179
)181
)180
182
181
183
184
class RerunTests(unittest.TestCase):185
RUNS_PATH = "/actions/workflows/pr-gate.yml/runs?head_sha=abc&event=pull_request_target&per_page=20"186
PULL = {"number": 5, "head": {"sha": "abc"}}187
188
def runs(self, *runs):189
return FakeGitHub({self.RUNS_PATH: {"workflow_runs": list(runs)}})190
191
def test_the_latest_failed_run_is_run_again(self):192
github = self.runs(193
{"id": 1, "created_at": "2026-11-25T10:04:38Z", "conclusion": "failure"},194
{"id": 2, "created_at": "2026-11-25T10:05:01Z", "conclusion": "failure"},195
)196
pr_gate.rerun_failed_gate(github, self.PULL)197
self.assertEqual(github.calls[-1], ("POST", "/actions/runs/2/rerun", None))198
199
def test_a_passing_latest_run_is_left_alone(self):200
github = self.runs(201
{"id": 1, "created_at": "2026-11-25T10:04:38Z", "conclusion": "failure"},202
{"id": 2, "created_at": "2026-11-25T10:05:01Z", "conclusion": "success"},203
)204
pr_gate.rerun_failed_gate(github, self.PULL)205
self.assertEqual(len(github.calls), 1)206
207
182
if __name__ == "__main__":208
if __name__ == "__main__":183
unittest.main()209
unittest.main()@@ -6,15 +6,18 @@ name: PR gate
6
6
on:
7
7
pull_request_target:
8
8
types: [opened, edited, synchronize, reopened, ready_for_review, labeled, unlabeled]
9
issues:
10
types: [labeled, unlabeled]
9
11
workflow_dispatch:
10
12
11
13
permissions:
14
actions: write
12
15
contents: read
13
16
issues: write
14
17
pull-requests: write
15
18
16
19
concurrency:
17
group: pr-gate-${{ github.event.pull_request.number || 'all' }}
20
group: pr-gate-${{ github.event.pull_request.number || github.event.issue.number || 'all' }}
18
21
cancel-in-progress: true
19
22
20
23
jobs:
@@ -22,6 +25,7 @@ jobs:
22
25
name: Ready for review
23
26
runs-on: ubuntu-latest
24
27
timeout-minutes: 10
28
if: github.event_name != 'issues' || github.event.label.name == 'status: accepted'
25
29
steps:
26
30
- uses: actions/checkout@v6
27
31
with:
@@ -33,5 +37,6 @@ jobs:
33
37
env:
34
38
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
35
39
PR_NUMBER: ${{ github.event.pull_request.number }}
40
ISSUE_NUMBER: ${{ github.event.issue.number }}
36
41
run: python3 -I .github/scripts/pr_gate.py
37
42
@@ -6,15 +6,18 @@ name: PR gate6
on:6
on:7
pull_request_target:7
pull_request_target:8
types: [opened, edited, synchronize, reopened, ready_for_review, labeled, unlabeled]8
types: [opened, edited, synchronize, reopened, ready_for_review, labeled, unlabeled]9
issues:10
types: [labeled, unlabeled]9
workflow_dispatch:11
workflow_dispatch:10
12
11
permissions:13
permissions:14
actions: write12
contents: read15
contents: read13
issues: write16
issues: write14
pull-requests: write17
pull-requests: write15
18
16
concurrency:19
concurrency:17
group: pr-gate-${{ github.event.pull_request.number || 'all' }}20
group: pr-gate-${{ github.event.pull_request.number || github.event.issue.number || 'all' }}18
cancel-in-progress: true21
cancel-in-progress: true19
22
20
jobs:23
jobs:@@ -22,6 +25,7 @@ jobs:22
name: Ready for review25
name: Ready for review23
runs-on: ubuntu-latest26
runs-on: ubuntu-latest24
timeout-minutes: 1027
timeout-minutes: 1028
if: github.event_name != 'issues' || github.event.label.name == 'status: accepted'25
steps:29
steps:26
- uses: actions/checkout@v630
- uses: actions/checkout@v627
with:31
with:@@ -33,5 +37,6 @@ jobs:33
env:37
env:34
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}38
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}35
PR_NUMBER: ${{ github.event.pull_request.number }}39
PR_NUMBER: ${{ github.event.pull_request.number }}40
ISSUE_NUMBER: ${{ github.event.issue.number }}36
run: python3 -I .github/scripts/pr_gate.py41
run: python3 -I .github/scripts/pr_gate.py37
42