Skip to content

Commit

Permalink
Parse try build CI job name from commit message
Browse files Browse the repository at this point in the history
  • Loading branch information
Kobzol committed May 2, 2024
1 parent a556d75 commit 334c6ef
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 5 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
43 changes: 38 additions & 5 deletions src/ci/github-actions/calculate-job-matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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: <job-name>
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]:
Expand All @@ -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()
Expand All @@ -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 []
Expand All @@ -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
)


Expand Down

0 comments on commit 334c6ef

Please sign in to comment.