-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Expand file tree
/
Copy pathgenerate_job_repos.py
More file actions
executable file
·86 lines (67 loc) · 2.84 KB
/
generate_job_repos.py
File metadata and controls
executable file
·86 lines (67 loc) · 2.84 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
#!/usr/bin/env python3
# Copyright The Kubernetes Authors.
#
# Licensed 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.
"""Generate job_repos.json mapping Prow job names to org/repo."""
import json
import os
import sys
import yaml
def generate_job_repos(jobs_dir):
"""Parse Prow job configs and return a dict mapping job name to org/repo."""
if not os.path.isdir(jobs_dir):
raise FileNotFoundError(f'jobs directory not found: {jobs_dir}')
mapping = {}
for root, _, files in os.walk(jobs_dir):
for fname in files:
if not fname.endswith(('.yaml', '.yml')):
continue
filepath = os.path.join(root, fname)
with open(filepath) as f:
try:
data = yaml.safe_load(f)
except Exception:
continue
if not data:
continue
# Presubmits: repo is the YAML key (already org/repo format)
for repo, jobs in (data.get('presubmits') or {}).items():
for job in (jobs or []):
if isinstance(job, dict) and 'name' in job:
mapping[job['name']] = repo
# Postsubmits: same structure as presubmits
for repo, jobs in (data.get('postsubmits') or {}).items():
for job in (jobs or []):
if isinstance(job, dict) and 'name' in job:
mapping[job['name']] = repo
# Periodics: repo from first extra_refs entry
for job in (data.get('periodics') or []):
if not isinstance(job, dict) or 'name' not in job:
continue
extra_refs = job.get('extra_refs', [])
if extra_refs:
ref = extra_refs[0]
org = ref.get('org', '')
repo = ref.get('repo', '')
if org and repo:
mapping[job['name']] = f'{org}/{repo}'
return mapping
def main():
jobs_dir = sys.argv[1] if len(sys.argv) > 1 else 'config/jobs'
output = sys.argv[2] if len(sys.argv) > 2 else 'job_repos.json'
mapping = generate_job_repos(jobs_dir)
with open(output, 'w') as f:
json.dump(mapping, f, sort_keys=True)
print(f'Generated {output} with {len(mapping)} job mappings')
if __name__ == '__main__':
main()