Skip to content

Commit

Permalink
Move auto jobs to calculate-job-matrix.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Kobzol committed Apr 20, 2024
1 parent 6f0ff0b commit dc3c544
Show file tree
Hide file tree
Showing 3 changed files with 415 additions and 403 deletions.
29 changes: 25 additions & 4 deletions src/ci/github-actions/calculate-job-matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,30 @@


def name_jobs(jobs: List[Dict], prefix: str) -> List[Dict]:
"""
Add a `name` attribute to each job, based on its image and the given `prefix`.
"""
for job in jobs:
job["name"] = f"{prefix} - {job['image']}"
return jobs


def add_base_env(jobs: List[Dict], environment: Dict[str, str]) -> List[Dict]:
"""
Prepends `environment` to the `env` attribute of each job.
The `env` of each job has higher precedence than `environment`.
"""
for job in jobs:
env = environment.copy()
env.update(job.get("env", {}))
job["env"] = env
return jobs


class JobType(enum.Enum):
PR = enum.auto()
Try = enum.auto()
Auto = enum.auto()


def find_job_type(github_ctx: Dict[str, Any]) -> Optional[JobType]:
Expand All @@ -53,26 +69,31 @@ def find_job_type(github_ctx: Dict[str, Any]) -> Optional[JobType]:
if try_build:
return JobType.Try

if ref == "refs/heads/auto" and repository == "rust-lang-ci/rust":
return JobType.Auto

return None


def calculate_jobs(job_type: JobType, job_data: Dict[str, Any]) -> List[Dict[str, Any]]:
if job_type == JobType.PR:
return name_jobs(job_data["pr"], "PR")
return add_base_env(name_jobs(job_data["pr"], "PR"), job_data["env"]["pr"])
elif job_type == JobType.Try:
return name_jobs(job_data["try"], "try")
return add_base_env(name_jobs(job_data["try"], "try"), job_data["env"]["production"])
elif job_type == JobType.Auto:
return add_base_env(name_jobs(job_data["auto"], "auto"), job_data["env"]["production"])

return []


if __name__ == "__main__":
logging.basicConfig(level=logging.INFO)

github_ctx = json.loads(os.environ["GITHUB_CTX"])

with open(JOBS_YAML_PATH) as f:
data = yaml.safe_load(f)

github_ctx = json.loads(os.environ["GITHUB_CTX"])

job_type = find_job_type(github_ctx)
logging.info(f"Job type: {job_type}")

Expand Down
Loading

0 comments on commit dc3c544

Please sign in to comment.