77It reads job definitions from `src/ci/github-actions/jobs.yml`
88and filters them based on the event that happened on CI.
99"""
10+ import dataclasses
1011import enum
1112import json
1213import logging
@@ -46,28 +47,31 @@ class JobType(enum.Enum):
4647 Auto = enum .auto ()
4748
4849
49- def find_job_type (github_ctx : Dict [str , Any ]) -> Optional [JobType ]:
50- event_name = github_ctx ["event_name" ]
51- ref = github_ctx ["ref" ]
52- repository = github_ctx ["repository" ]
50+ @dataclasses .dataclass
51+ class GitHubCtx :
52+ event_name : str
53+ ref : str
54+ repository : str
5355
54- if event_name == "pull_request" :
56+
57+ def find_job_type (ctx : GitHubCtx ) -> Optional [JobType ]:
58+ if ctx .event_name == "pull_request" :
5559 return JobType .PR
56- elif event_name == "push" :
60+ elif ctx . event_name == "push" :
5761 old_bors_try_build = (
58- ref in ("refs/heads/try" , "refs/heads/try-perf" ) and
59- repository == "rust-lang-ci/rust"
62+ ctx . ref in ("refs/heads/try" , "refs/heads/try-perf" ) and
63+ ctx . repository == "rust-lang-ci/rust"
6064 )
6165 new_bors_try_build = (
62- ref == "refs/heads/automation/bors/try" and
63- repository == "rust-lang/rust"
66+ ctx . ref == "refs/heads/automation/bors/try" and
67+ ctx . repository == "rust-lang/rust"
6468 )
6569 try_build = old_bors_try_build or new_bors_try_build
6670
6771 if try_build :
6872 return JobType .Try
6973
70- if ref == "refs/heads/auto" and repository == "rust-lang-ci/rust" :
74+ if ctx . ref == "refs/heads/auto" and ctx . repository == "rust-lang-ci/rust" :
7175 return JobType .Auto
7276
7377 return None
@@ -84,13 +88,21 @@ def calculate_jobs(job_type: JobType, job_data: Dict[str, Any]) -> List[Dict[str
8488 return []
8589
8690
91+ def get_github_ctx () -> GitHubCtx :
92+ return GitHubCtx (
93+ event_name = os .environ ["GITHUB_EVENT_NAME" ],
94+ ref = os .environ ["GITHUB_REF" ],
95+ repository = os .environ ["GITHUB_REPOSITORY" ]
96+ )
97+
98+
8799if __name__ == "__main__" :
88100 logging .basicConfig (level = logging .INFO )
89101
90102 with open (JOBS_YAML_PATH ) as f :
91103 data = yaml .safe_load (f )
92104
93- github_ctx = json . loads ( os . environ [ "GITHUB_CTX" ] )
105+ github_ctx = get_github_ctx ( )
94106
95107 job_type = find_job_type (github_ctx )
96108 logging .info (f"Job type: { job_type } " )
0 commit comments