From a556d75a87d7fe89441f425323b9ecf187670db1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Ber=C3=A1nek?= Date: Thu, 2 May 2024 21:42:36 +0200 Subject: [PATCH 1/2] Use sum type for `WorkflowRunType` --- src/ci/github-actions/calculate-job-matrix.py | 40 ++++++++++++------- 1 file changed, 26 insertions(+), 14 deletions(-) diff --git a/src/ci/github-actions/calculate-job-matrix.py b/src/ci/github-actions/calculate-job-matrix.py index 68565f489c939..1a9d01969b860 100755 --- a/src/ci/github-actions/calculate-job-matrix.py +++ b/src/ci/github-actions/calculate-job-matrix.py @@ -8,10 +8,10 @@ and filters them based on the event that happened on CI. """ import dataclasses -import enum import json import logging import os +import typing from pathlib import Path from typing import List, Dict, Any, Optional @@ -44,10 +44,22 @@ def add_base_env(jobs: List[Job], environment: Dict[str, str]) -> List[Job]: return jobs -class WorkflowRunType(enum.Enum): - PR = enum.auto() - Try = enum.auto() - Auto = enum.auto() +@dataclasses.dataclass +class PRRunType: + pass + + +@dataclasses.dataclass +class TryRunType: + job: Optional[str] = None + + +@dataclasses.dataclass +class AutoRunType: + pass + + +WorkflowRunType = typing.Union[PRRunType, TryRunType, AutoRunType] @dataclasses.dataclass @@ -59,7 +71,7 @@ class GitHubCtx: def find_run_type(ctx: GitHubCtx) -> Optional[WorkflowRunType]: if ctx.event_name == "pull_request": - return WorkflowRunType.PR + return PRRunType() elif ctx.event_name == "push": old_bors_try_build = ( ctx.ref in ("refs/heads/try", "refs/heads/try-perf") and @@ -72,20 +84,20 @@ def find_run_type(ctx: GitHubCtx) -> Optional[WorkflowRunType]: try_build = old_bors_try_build or new_bors_try_build if try_build: - return WorkflowRunType.Try + return TryRunType() if ctx.ref == "refs/heads/auto" and ctx.repository == "rust-lang-ci/rust": - return WorkflowRunType.Auto + return AutoRunType() return None def calculate_jobs(run_type: WorkflowRunType, job_data: Dict[str, Any]) -> List[Job]: - if run_type == WorkflowRunType.PR: + if isinstance(run_type, PRRunType): return add_base_env(name_jobs(job_data["pr"], "PR"), job_data["envs"]["pr"]) - elif run_type == WorkflowRunType.Try: + elif isinstance(run_type, TryRunType): return add_base_env(name_jobs(job_data["try"], "try"), job_data["envs"]["try"]) - elif run_type == WorkflowRunType.Auto: + elif isinstance(run_type, AutoRunType): return add_base_env(name_jobs(job_data["auto"], "auto"), job_data["envs"]["auto"]) return [] @@ -107,11 +119,11 @@ def get_github_ctx() -> GitHubCtx: def format_run_type(run_type: WorkflowRunType) -> str: - if run_type == WorkflowRunType.PR: + if isinstance(run_type, PRRunType): return "pr" - elif run_type == WorkflowRunType.Auto: + elif isinstance(run_type, AutoRunType): return "auto" - elif run_type == WorkflowRunType.Try: + elif isinstance(run_type, TryRunType): return "try" else: raise AssertionError() From 334c6efb793ec80d49730fca9a9946076e8daede Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Ber=C3=A1nek?= Date: Thu, 2 May 2024 21:44:47 +0200 Subject: [PATCH 2/2] Parse try build CI job name from commit message --- .github/workflows/ci.yml | 2 + src/ci/github-actions/calculate-job-matrix.py | 43 ++++++++++++++++--- 2 files changed, 40 insertions(+), 5 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index a2769a2c2749d..1c5330ec25dd6 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -52,6 +52,8 @@ jobs: - name: Checkout the source code uses: actions/checkout@v4 - name: Calculate the CI job matrix + env: + COMMIT_MESSAGE: ${{ github.event.head_commit.message }} run: python3 src/ci/github-actions/calculate-job-matrix.py >> $GITHUB_OUTPUT id: jobs job: diff --git a/src/ci/github-actions/calculate-job-matrix.py b/src/ci/github-actions/calculate-job-matrix.py index 1a9d01969b860..cb45111eec4d1 100755 --- a/src/ci/github-actions/calculate-job-matrix.py +++ b/src/ci/github-actions/calculate-job-matrix.py @@ -11,6 +11,7 @@ import json import logging import os +import re import typing from pathlib import Path from typing import List, Dict, Any, Optional @@ -67,6 +68,23 @@ class GitHubCtx: event_name: str ref: str repository: str + commit_message: Optional[str] + + +def get_job_from_commit(ctx: GitHubCtx) -> Optional[str]: + """ + Tries to parse a name of a CI job that should be executed in the form of + ci-job: + from the commit message of the passed GitHub context. + """ + if ctx.commit_message is None: + return None + + regex = re.compile(r"ci-job: (.*)") + match = regex.search(ctx.commit_message) + if match is None: + return None + return match.group(1) def find_run_type(ctx: GitHubCtx) -> Optional[WorkflowRunType]: @@ -84,7 +102,8 @@ def find_run_type(ctx: GitHubCtx) -> Optional[WorkflowRunType]: try_build = old_bors_try_build or new_bors_try_build if try_build: - return TryRunType() + job_name = get_job_from_commit(ctx) + return TryRunType(job=job_name) if ctx.ref == "refs/heads/auto" and ctx.repository == "rust-lang-ci/rust": return AutoRunType() @@ -96,8 +115,16 @@ def calculate_jobs(run_type: WorkflowRunType, job_data: Dict[str, Any]) -> List[ if isinstance(run_type, PRRunType): return add_base_env(name_jobs(job_data["pr"], "PR"), job_data["envs"]["pr"]) elif isinstance(run_type, TryRunType): - return add_base_env(name_jobs(job_data["try"], "try"), job_data["envs"]["try"]) - elif isinstance(run_type, AutoRunType): + jobs = job_data["try"] + if run_type.job is not None: + jobs = [job for job in job_data["auto"] if job["image"] == run_type.job] + if not jobs: + raise Exception( + f"CI job `{run_type.job}` asked for in the try build does not exist" + ) + + return add_base_env(name_jobs(jobs, "try"), job_data["envs"]["try"]) + elif run_type is AutoRunType: return add_base_env(name_jobs(job_data["auto"], "auto"), job_data["envs"]["auto"]) return [] @@ -111,10 +138,16 @@ def skip_jobs(jobs: List[Dict[str, Any]], channel: str) -> List[Job]: def get_github_ctx() -> GitHubCtx: + event_name = os.environ["GITHUB_EVENT_NAME"] + + commit_message = None + if event_name == "push": + commit_message = os.environ["COMMIT_MESSAGE"] return GitHubCtx( - event_name=os.environ["GITHUB_EVENT_NAME"], + event_name=event_name, ref=os.environ["GITHUB_REF"], - repository=os.environ["GITHUB_REPOSITORY"] + repository=os.environ["GITHUB_REPOSITORY"], + commit_message=commit_message )