-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Expand file tree
/
Copy pathcheck_asf_yaml_status_checks.py
More file actions
145 lines (120 loc) · 5.05 KB
/
Copy pathcheck_asf_yaml_status_checks.py
File metadata and controls
145 lines (120 loc) · 5.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/usr/bin/env python3
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
"""
Validate that every entry in .asf.yaml required_status_checks
matches an actual GitHub Actions job name, and that the workflow
is not filtered by paths/paths-ignore (which would prevent the
check from running on some PRs, blocking merges).
A typo or stale entry in required_status_checks will block all
merges for the project, so this check catches that early.
"""
import glob
import os
import sys
import yaml
def get_required_checks(asf_yaml_path):
"""Extract all required_status_checks contexts from .asf.yaml."""
with open(asf_yaml_path) as f:
config = yaml.safe_load(f)
checks = {} # context -> list of branches requiring it
branches = config.get("github", {}).get("protected_branches", {})
for branch, settings in branches.items():
contexts = (
settings.get("required_status_checks", {}).get("contexts", [])
)
for ctx in contexts:
checks.setdefault(ctx, []).append(branch)
return checks
def get_workflow_jobs(workflows_dir):
"""Collect all jobs with their metadata from GitHub Actions workflow files.
Returns a dict mapping job identifier (name or key) to a list of
(workflow_file, has_path_filters) tuples.
"""
jobs = {} # identifier -> [(workflow_file, has_path_filters)]
for workflow_file in sorted(glob.glob(os.path.join(workflows_dir, "*.yml"))):
with open(workflow_file) as f:
workflow = yaml.safe_load(f)
if not workflow or "jobs" not in workflow:
continue
# Check if pull_request trigger has path filters
on = workflow.get(True, workflow.get("on", {})) # yaml parses `on:` as True
pr_trigger = on.get("pull_request", {}) if isinstance(on, dict) else {}
has_path_filters = bool(
isinstance(pr_trigger, dict)
and (pr_trigger.get("paths") or pr_trigger.get("paths-ignore"))
)
basename = os.path.basename(workflow_file)
for job_key, job_config in workflow.get("jobs", {}).items():
if not isinstance(job_config, dict):
continue
job_name = job_config.get("name", job_key)
info = (basename, has_path_filters)
jobs.setdefault(job_name, []).append(info)
if job_key != job_name:
jobs.setdefault(job_key, []).append(info)
return jobs
def main():
repo_root = os.path.dirname(
os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
)
asf_yaml = os.path.join(repo_root, ".asf.yaml")
workflows_dir = os.path.join(repo_root, ".github", "workflows")
required_checks = get_required_checks(asf_yaml)
if not required_checks:
print("No required_status_checks found in .asf.yaml — nothing to validate.")
return
jobs = get_workflow_jobs(workflows_dir)
errors = []
for ctx in sorted(required_checks):
branches = ", ".join(sorted(required_checks[ctx]))
if ctx not in jobs:
errors.append(
f' - "{ctx}" (branch: {branches}): '
f"not found in any GitHub Actions workflow"
)
continue
# Check if ALL workflows providing this job have path filters
# (if at least one doesn't, the check will still run)
filtered_workflows = [
wf for wf, has_filter in jobs[ctx] if has_filter
]
unfiltered_workflows = [
wf for wf, has_filter in jobs[ctx] if not has_filter
]
if filtered_workflows and not unfiltered_workflows:
wf_list = ", ".join(filtered_workflows)
errors.append(
f' - "{ctx}" (branch: {branches}): '
f"workflow {wf_list} uses paths/paths-ignore filters on "
f"pull_request, so this check won't run for some PRs "
f"and will block merging"
)
if errors:
print("ERROR: Problems found with required_status_checks in .asf.yaml:\n")
print("\n".join(errors))
print()
print("Available job names across all workflows:")
for name in sorted(jobs):
print(f" - {name}")
sys.exit(1)
print(
f"OK: All {len(required_checks)} required_status_checks "
"match existing GitHub Actions jobs."
)
if __name__ == "__main__":
main()