Skip to content

Commit 568afb8

Browse files
committed
ci: Commonize the way PrInfo is loaded from env
1 parent ff2cc0e commit 568afb8

File tree

1 file changed

+22
-10
lines changed

1 file changed

+22
-10
lines changed

ci/ci-util.py

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import subprocess as sp
1313
import sys
1414
from dataclasses import dataclass
15+
from functools import cache
1516
from glob import glob
1617
from inspect import cleandoc
1718
from os import getenv
@@ -62,7 +63,7 @@
6263

6364
# libm PR CI takes a long time and doesn't need to run unless relevant files have been
6465
# changed. Anything matching this regex pattern will trigger a run.
65-
TRIGGER_LIBM_PR_CI = ".*(libm|musl).*"
66+
TRIGGER_LIBM_CI_FILE_PAT = ".*(libm|musl).*"
6667

6768
TYPES = ["f16", "f32", "f64", "f128"]
6869

@@ -125,8 +126,18 @@ class PrInfo:
125126
cfg: PrCfg
126127

127128
@classmethod
128-
def load(cls, pr_number: int | str) -> Self:
129-
"""For a given PR number, query the body and commit list"""
129+
def from_env(cls) -> Self | None:
130+
"""Create a PR object from the PR_NUMBER environment if set, `None` otherwise."""
131+
pr_env = os.environ.get("PR_NUMBER")
132+
if pr_env is not None and len(pr_env) > 0:
133+
return cls.from_pr(pr_env)
134+
135+
return None
136+
137+
@classmethod
138+
@cache # Cache so we don't print info messages multiple times
139+
def from_pr(cls, pr_number: int | str) -> Self:
140+
"""For a given PR number, query the body and commit list."""
130141
pr_info = sp.check_output(
131142
[
132143
"gh",
@@ -238,22 +249,23 @@ def may_skip_libm_ci(self) -> bool:
238249
"""If this is a PR and no libm files were changed, allow skipping libm
239250
jobs."""
240251

241-
if self.is_pr():
242-
return all(not re.match(TRIGGER_LIBM_PR_CI, str(f)) for f in self.changed)
252+
# Always run on merge CI
253+
if not self.is_pr():
254+
return False
243255

244-
return False
256+
# By default, run if there are any changed files matching the pattern
257+
return all(not re.match(TRIGGER_LIBM_CI_FILE_PAT, str(f)) for f in self.changed)
245258

246259
def emit_workflow_output(self):
247260
"""Create a JSON object a list items for each type's changed files, if any
248261
did change, and the routines that were affected by the change.
249262
"""
250263

251-
pr_number = os.environ.get("PR_NUMBER")
252264
skip_tests = False
253265
error_on_many_tests = False
254266

255-
if pr_number is not None and len(pr_number) > 0:
256-
pr = PrInfo.load(pr_number)
267+
pr = PrInfo.from_env()
268+
if pr is not None:
257269
skip_tests = pr.cfg.skip_extensive
258270
error_on_many_tests = not pr.cfg.allow_many_extensive
259271

@@ -398,7 +410,7 @@ def handle_bench_regressions(args: list[str]):
398410
eprint(USAGE)
399411
exit(1)
400412

401-
pr = PrInfo.load(pr_number)
413+
pr = PrInfo.from_pr(pr_number)
402414
if pr.cfg.allow_regressions:
403415
eprint("PR allows regressions")
404416
return

0 commit comments

Comments
 (0)