Skip to content

Commit 545c12f

Browse files
committed
Refactor calculate-job-matrix.py
1 parent 7a90679 commit 545c12f

File tree

1 file changed

+48
-23
lines changed

1 file changed

+48
-23
lines changed

src/ci/github-actions/calculate-job-matrix.py

+48-23
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99
1010
Currently, it only supports PR and try builds.
1111
"""
12-
12+
import enum
1313
import json
14+
import logging
1415
import os
15-
import sys
1616
from pathlib import Path
17-
from typing import List, Dict
17+
from typing import List, Dict, Any, Optional
1818

1919
import yaml
2020

@@ -27,33 +27,58 @@ def name_jobs(jobs: List[Dict], prefix: str) -> List[Dict]:
2727
return jobs
2828

2929

30-
if __name__ == "__main__":
31-
github_ctx = json.loads(os.environ["GITHUB_CTX"])
30+
class JobType(enum.Enum):
31+
PR = enum.auto()
32+
Try = enum.auto()
3233

33-
with open(JOBS_YAML_PATH) as f:
34-
data = yaml.safe_load(f)
3534

35+
def find_job_type(github_ctx: Dict[str, Any]) -> Optional[JobType]:
3636
event_name = github_ctx["event_name"]
3737
ref = github_ctx["ref"]
3838
repository = github_ctx["repository"]
3939

40-
old_bors_try_build = (
41-
ref in ("refs/heads/try", "refs/heads/try-perf") and
42-
repository == "rust-lang-ci/rust"
43-
)
44-
new_bors_try_build = (
45-
ref == "refs/heads/automation/bors/try" and
46-
repository == "rust-lang/rust"
47-
)
48-
try_build = old_bors_try_build or new_bors_try_build
40+
if event_name == "pull_request":
41+
return JobType.PR
42+
elif event_name == "push":
43+
old_bors_try_build = (
44+
ref in ("refs/heads/try", "refs/heads/try-perf") and
45+
repository == "rust-lang-ci/rust"
46+
)
47+
new_bors_try_build = (
48+
ref == "refs/heads/automation/bors/try" and
49+
repository == "rust-lang/rust"
50+
)
51+
try_build = old_bors_try_build or new_bors_try_build
52+
53+
if try_build:
54+
return JobType.Try
55+
56+
return None
57+
58+
59+
def calculate_jobs(job_type: JobType, job_data: Dict[str, Any]) -> List[Dict[str, Any]]:
60+
if job_type == JobType.PR:
61+
return name_jobs(job_data["pr"], "PR")
62+
elif job_type == JobType.Try:
63+
return name_jobs(job_data["try"], "Try")
64+
65+
return []
66+
67+
68+
if __name__ == "__main__":
69+
logging.basicConfig(level=logging.INFO)
70+
71+
github_ctx = json.loads(os.environ["GITHUB_CTX"])
72+
73+
with open(JOBS_YAML_PATH) as f:
74+
data = yaml.safe_load(f)
75+
76+
job_type = find_job_type(github_ctx)
77+
logging.info(f"Job type: {job_type}")
4978

5079
jobs = []
51-
# Pull request CI jobs. Their name is 'PR - <image>'
52-
if event_name == "pull_request":
53-
jobs = name_jobs(data["pr"], "PR")
54-
# Try builds
55-
elif event_name == "push" and try_build:
56-
jobs = name_jobs(data["try"], "try")
80+
if job_type is not None:
81+
jobs = calculate_jobs(job_type, data)
5782

58-
print(f"Output:\n{json.dumps(jobs, indent=4)}", file=sys.stderr)
83+
logging.info(f"Output:\n{yaml.dump(jobs, indent=4)}")
5984
print(f"jobs={json.dumps(jobs)}")

0 commit comments

Comments
 (0)