Skip to content

Commit cff1472

Browse files
committed
Read pull request labels and the merge base for fork pushes
A fork push carries no labels, so full tests needed, allow provider dependency bump and the other labels did nothing in fork CI and a PR that needed one stayed red. Read them from the open apache/airflow pull request of the pushed branch. The pyproject.toml and provider dependency bump checks compared the pushed commit with its parent, which after a merge of upstream main is the contributor's own previous tip, so a release manager's bump from main counted as theirs. Read the previous version at the compare's merge base instead. Generated-by: Claude Opus 5
1 parent 7281e4c commit cff1472

8 files changed

Lines changed: 248 additions & 39 deletions

File tree

‎dev/breeze/doc/ci/04_selective_checks.md‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -343,8 +343,16 @@ flowchart TD
343343
not from `git diff-tree`, with a renamed file counted under both paths, and the run is selected
344344
exactly like a pull request with those files. `is_committer_build` is always `False` because
345345
forks have no self-hosted runners. When the file list reaches the compare API's 300-file cap,
346-
or the compare fails, everything runs. The `pyproject.toml` and provider dependency checks still compare the pushed
347-
commit with its parent, so in a fork they see only the last commit's changes.
346+
or the compare fails, everything runs, including the UI E2E tests, which otherwise only follow
347+
matching files or the `full tests needed` label. Checks that compare a file with its previous version
348+
(`pyproject.toml` and the provider dependency bump check) read the previous version at the
349+
compare's merge base in `apache/airflow`, not at the pushed commit's parent, which after a
350+
merge of upstream `main` is the contributor's own previous tip. A file missing at the merge
351+
base counts as new; any other failure to read it fails the selective checks instead of
352+
skipping the check. The labels come from the open
353+
`apache/airflow` pull request of the pushed branch, so `full tests needed`,
354+
`allow provider dependency bump` and the other labels work as on a pull request; a label
355+
added later takes effect on the next fork run.
348356

349357
## Troubleshooting: my CI run does too much
350358

‎dev/breeze/doc/ci/08_fork_ci_for_external_contributors.md‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,18 @@ upstream yields no changed files, so such a run performs basic checks only.
359359
of the pushed code, breeze's default branch at that commit (`main`, or the release branch it
360360
was cut from), because a branch name such as `backport-<sha>-v3-1-test` cannot be trusted to
361361
tell. A renamed file counts under both paths. The API caps the file list at 300; at the cap,
362-
and when the compare fails, the run falls back to "run everything" rather than failing.
362+
and when the compare fails, the run falls back to "run everything", UI E2E tests included,
363+
rather than failing.
364+
- **Previous versions** of changed files, for the `pyproject.toml` and provider dependency
365+
bump checks, are read at the compare's merge base in `apache/airflow`
366+
(`GET /repos/apache/airflow/contents/<path>?ref=<merge-base>`). The pushed commit's parent
367+
would be the contributor's own previous tip after they merge upstream `main`, so a release
368+
manager's bump from `main` would count as theirs. A file missing at the merge base is new; any
369+
other read failure fails the run rather than letting the check pass unseen.
370+
- **Labels** come from the open `apache/airflow` pull request whose head is
371+
`<fork-owner>:<branch>`, since a push event carries none. `full tests needed`,
372+
`allow provider dependency bump`, `skip common compat check` and the other labels work as on
373+
a pull request; a label added later takes effect on the next fork run.
363374
- `_should_run_all_tests_and_versions` treats a fork push like a pull request. Today any
364375
`push` runs the full matrix, which would cost a contributor ~220 minutes per push.
365376
- `is_committer_build` is always false. Forks have no self-hosted runners; a committer pushing

‎dev/breeze/src/airflow_breeze/commands/ci_commands.py‎

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
fix_ownership_using_docker,
6161
perform_environment_checks,
6262
)
63-
from airflow_breeze.utils.fork_compare import get_fork_changes, is_fork_push
63+
from airflow_breeze.utils.fork_compare import get_fork_changes, get_fork_pull_request_labels, is_fork_push
6464
from airflow_breeze.utils.github import format_github_token_scope_guidance, retrieve_github_token
6565
from airflow_breeze.utils.path_utils import AIRFLOW_HOME_PATH, AIRFLOW_ROOT_PATH
6666
from airflow_breeze.utils.run_utils import run_command
@@ -326,14 +326,18 @@ def selective_check(
326326
changed_files: tuple[str, ...] = ()
327327
fork_push = is_fork_push(github_event, github_repository)
328328
fork_changes_truncated = False
329-
if fork_push and commit_ref is not None:
330-
fork_changes = get_fork_changes(
331-
fork_owner=github_repository.split("/")[0],
332-
sha=commit_ref,
333-
base=default_branch,
329+
fork_merge_base = None
330+
labels = tuple(ast.literal_eval(pr_labels)) if pr_labels else ()
331+
fork_owner = github_repository.split("/")[0]
332+
if fork_push and github_context_dict.get("ref_name"):
333+
labels = get_fork_pull_request_labels(
334+
fork_owner=fork_owner, branch=github_context_dict["ref_name"]
334335
)
336+
if fork_push and commit_ref is not None:
337+
fork_changes = get_fork_changes(fork_owner=fork_owner, sha=commit_ref, base=default_branch)
335338
changed_files = fork_changes.files
336339
fork_changes_truncated = fork_changes.truncated
340+
fork_merge_base = fork_changes.merge_base
337341
console_print(f"\n[info]Changed files against apache/airflow:[/]\n{changed_files}\n")
338342
elif commit_ref is not None and selective_tests_on_push and github_event == GithubEvents.PUSH:
339343
# The base is the last successful run of the branch, so this run also covers the commits
@@ -356,7 +360,7 @@ def selective_check(
356360
files=changed_files,
357361
default_branch=default_branch,
358362
default_constraints_branch=default_constraints_branch,
359-
pr_labels=tuple(ast.literal_eval(pr_labels)) if pr_labels else (),
363+
pr_labels=labels,
360364
github_event=github_event,
361365
github_repository=github_repository,
362366
github_actor=github_actor,
@@ -365,6 +369,7 @@ def selective_check(
365369
selective_tests_on_push=selective_tests_on_push,
366370
fork_push=fork_push,
367371
fork_changes_truncated=fork_changes_truncated,
372+
fork_merge_base=fork_merge_base,
368373
)
369374
print(str(sc), file=sys.stderr)
370375
except Exception:

‎dev/breeze/src/airflow_breeze/utils/fork_compare.py‎

Lines changed: 77 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
class ForkChanges:
3535
files: tuple[str, ...]
3636
truncated: bool
37+
merge_base: str | None = None
3738

3839

3940
def is_fork_push(github_event: GithubEvents, github_repository: str) -> bool:
@@ -50,14 +51,19 @@ def get_fork_changes(*, fork_owner: str, sha: str, base: str) -> ForkChanges:
5051
branch name cannot be trusted to tell. A renamed file counts under both paths, as in
5152
``git diff``. When the compare fails the change set is reported as truncated, so the run
5253
covers everything instead of failing.
54+
55+
``merge_base`` is the upstream commit the changes are measured from. Checks that compare a
56+
file with its previous version use it instead of the pushed commit's parent, which after a
57+
merge of upstream ``main`` into the branch is the contributor's own previous tip.
5358
"""
5459
result = run_gh_command(
5560
[
5661
"gh",
5762
"api",
5863
f"repos/{APACHE_AIRFLOW_GITHUB_REPOSITORY}/compare/{base}...{fork_owner}:{sha}",
5964
"--jq",
60-
"[.files[] | [.filename, (.previous_filename // empty)]]",
65+
"{files: [.files[] | [.filename, (.previous_filename // empty)]], "
66+
"merge_base: .merge_base_commit.sha}",
6167
],
6268
capture_output=True,
6369
text=True,
@@ -68,6 +74,74 @@ def get_fork_changes(*, fork_owner: str, sha: str, base: str) -> ForkChanges:
6874
f"so everything runs: {result.stderr.strip()}[/]"
6975
)
7076
return ForkChanges(files=(), truncated=True)
71-
entries = json.loads(result.stdout or "[]")
77+
compare = json.loads(result.stdout or "{}")
78+
entries = compare.get("files", [])
7279
files = tuple(path for entry in entries for path in entry)
73-
return ForkChanges(files=files, truncated=len(entries) >= COMPARE_FILE_CAP)
80+
return ForkChanges(
81+
files=files, truncated=len(entries) >= COMPARE_FILE_CAP, merge_base=compare.get("merge_base")
82+
)
83+
84+
85+
def get_fork_pull_request_labels(*, fork_owner: str, branch: str) -> tuple[str, ...]:
86+
"""
87+
Labels of the open apache/airflow pull request from ``fork_owner:branch``.
88+
89+
A fork push carries no pull request, so labels such as ``full tests needed`` or
90+
``allow provider dependency bump`` are read here. A label added later takes effect on the
91+
next fork run. No pull request, or a failed call, means no labels.
92+
"""
93+
result = run_gh_command(
94+
[
95+
"gh",
96+
"api",
97+
"--method",
98+
"GET",
99+
f"repos/{APACHE_AIRFLOW_GITHUB_REPOSITORY}/pulls",
100+
"-f",
101+
f"head={fork_owner}:{branch}",
102+
"-f",
103+
"state=open",
104+
"--jq",
105+
"[.[0].labels[]?.name]",
106+
],
107+
capture_output=True,
108+
text=True,
109+
)
110+
if result.returncode != 0:
111+
console_print(
112+
f"[warning]Could not read the labels of the pull request from {fork_owner}:{branch}: "
113+
f"{result.stderr.strip()}[/]"
114+
)
115+
return ()
116+
return tuple(json.loads(result.stdout or "[]"))
117+
118+
119+
class UpstreamFileError(RuntimeError):
120+
"""A file of apache/airflow could not be read for a reason other than its absence."""
121+
122+
123+
def get_upstream_file(*, path: str, ref: str) -> str | None:
124+
"""
125+
Content of ``path`` at ``ref`` in apache/airflow, or None when the file does not exist there.
126+
127+
Any other failure raises: the checks that compare a file with its previous version would
128+
otherwise pass silently for a change they never saw.
129+
"""
130+
result = run_gh_command(
131+
[
132+
"gh",
133+
"api",
134+
"-H",
135+
"Accept: application/vnd.github.raw",
136+
f"repos/{APACHE_AIRFLOW_GITHUB_REPOSITORY}/contents/{path}?ref={ref}",
137+
],
138+
capture_output=True,
139+
text=True,
140+
)
141+
if result.returncode == 0:
142+
return result.stdout
143+
if "404" in result.stderr or "Not Found" in result.stderr:
144+
return None
145+
raise UpstreamFileError(
146+
f"Could not read {path} at {ref} in {APACHE_AIRFLOW_GITHUB_REPOSITORY}: {result.stderr.strip()}"
147+
)

‎dev/breeze/src/airflow_breeze/utils/selective_checks.py‎

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
)
7272
from airflow_breeze.utils.console import console_print, get_console
7373
from airflow_breeze.utils.exclude_from_matrix import excluded_combos
74+
from airflow_breeze.utils.fork_compare import get_upstream_file
7475
from airflow_breeze.utils.functools_cache import clearable_cache
7576
from airflow_breeze.utils.kubernetes_utils import get_kubernetes_python_combos
7677
from airflow_breeze.utils.packages import get_available_distributions, get_suspended_provider_ids
@@ -901,6 +902,7 @@ class SelectiveChecks:
901902
"_github_event",
902903
"_fork_push",
903904
"_fork_changes_truncated",
905+
"_fork_merge_base",
904906
}
905907

906908
def __init__(
@@ -918,11 +920,14 @@ def __init__(
918920
selective_tests_on_push: bool = False,
919921
fork_push: bool = False,
920922
fork_changes_truncated: bool = False,
923+
fork_merge_base: str | None = None,
921924
):
922925
"""
923926
:param fork_push: the push happened in a contributor fork (AIP-120); ``files`` are the
924927
changes against apache/airflow and the run is selected like a pull request.
925928
:param fork_changes_truncated: the compare API capped the changed files of a fork push.
929+
:param fork_merge_base: the apache/airflow commit a fork push is compared with; previous
930+
versions of changed files are read there instead of from the pushed commit's parent.
926931
"""
927932
self._files = files
928933
# A changed test helper selects the tests that import it, as if those test files had changed;
@@ -952,6 +957,7 @@ def __init__(
952957
self._selective_tests_on_push = selective_tests_on_push
953958
self._fork_push = fork_push
954959
self._fork_changes_truncated = fork_changes_truncated
960+
self._fork_merge_base = fork_merge_base
955961
self._new_toml: dict[str, Any] = {}
956962
self._old_toml: dict[str, Any] = {}
957963

@@ -1012,6 +1018,18 @@ def _get_full_run_events(self) -> list[GithubEvents]:
10121018
return [GithubEvents.SCHEDULE, GithubEvents.WORKFLOW_DISPATCH]
10131019
return [GithubEvents.PUSH, GithubEvents.SCHEDULE, GithubEvents.WORKFLOW_DISPATCH]
10141020

1021+
def _get_previous_file_content(self, path: str) -> str | None:
1022+
if self._fork_merge_base:
1023+
return get_upstream_file(path=path, ref=self._fork_merge_base)
1024+
result = run_command(
1025+
["git", "show", f"{self._commit_ref}^:{path}"],
1026+
capture_output=True,
1027+
text=True,
1028+
cwd=AIRFLOW_ROOT_PATH,
1029+
check=False,
1030+
)
1031+
return result.stdout if result.returncode == 0 else None
1032+
10151033
def _is_pull_request_like(self) -> bool:
10161034
return self._github_event == GithubEvents.PULL_REQUEST or self._fork_push
10171035

@@ -1289,6 +1307,13 @@ def run_ui_e2e_tests(self) -> bool:
12891307
f"'{FULL_TESTS_NEEDED_LABEL}' label is set[/]"
12901308
)
12911309
return True
1310+
# The UI files of a truncated fork change set may be among the files the compare API
1311+
# did not list, so the file match below cannot rule them out.
1312+
if self._fork_changes_truncated:
1313+
console_print(
1314+
f"[warning]{FileGroupForCi.UI_FILES} e2e enabled because the fork change set is truncated[/]"
1315+
)
1316+
return True
12921317
matched_files = self._matching_files(FileGroupForCi.UI_FILES, CI_FILE_GROUP_MATCHES)
12931318
if matched_files:
12941319
console_print(
@@ -1871,21 +1896,14 @@ def pyproject_toml_changed(self) -> bool:
18711896
f"Could not get pyproject.toml from {self._commit_ref}[/]"
18721897
)
18731898
return False
1874-
old_result = run_command(
1875-
["git", "show", f"{self._commit_ref}^:pyproject.toml"],
1876-
capture_output=True,
1877-
text=True,
1878-
cwd=AIRFLOW_ROOT_PATH,
1879-
check=False,
1880-
)
1881-
if old_result.returncode != 0:
1899+
old_content = self._get_previous_file_content("pyproject.toml")
1900+
if old_content is None:
18821901
console_print(
1883-
f"[warning]Cannot determine pyproject.toml changes. "
1884-
f"Could not get pyproject.toml from {self._commit_ref}^[/]"
1902+
"[warning]Cannot determine pyproject.toml changes. Could not get its previous version[/]"
18851903
)
18861904
return False
18871905
self._new_toml = tomllib.loads(new_result.stdout)
1888-
self._old_toml = tomllib.loads(old_result.stdout)
1906+
self._old_toml = tomllib.loads(old_content)
18891907
return True
18901908

18911909
@cached_property
@@ -2357,20 +2375,13 @@ def provider_dependency_bump(self) -> bool:
23572375
if new_result.returncode != 0:
23582376
continue
23592377

2360-
# Get the old version of the file
2361-
old_result = run_command(
2362-
["git", "show", f"{self._commit_ref}^:{pyproject_file}"],
2363-
capture_output=True,
2364-
text=True,
2365-
cwd=AIRFLOW_ROOT_PATH,
2366-
check=False,
2367-
)
2368-
if old_result.returncode != 0:
2378+
old_content = self._get_previous_file_content(pyproject_file)
2379+
if old_content is None:
23692380
continue
23702381

23712382
try:
23722383
new_toml = tomllib.loads(new_result.stdout)
2373-
old_toml = tomllib.loads(old_result.stdout)
2384+
old_toml = tomllib.loads(old_content)
23742385
except Exception:
23752386
continue
23762387

‎dev/breeze/tests/test_fork_compare.py‎

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@
2626
from airflow_breeze.utils.fork_compare import (
2727
COMPARE_FILE_CAP,
2828
ForkChanges,
29+
UpstreamFileError,
2930
get_fork_changes,
31+
get_fork_pull_request_labels,
32+
get_upstream_file,
3033
is_fork_push,
3134
)
3235

@@ -62,7 +65,9 @@ def test_is_fork_push(event, repository, expected):
6265
],
6366
)
6467
def test_get_fork_changes(mock_gh, files, expected):
65-
mock_gh.return_value = subprocess.CompletedProcess([], 0, stdout=json.dumps(files), stderr="")
68+
mock_gh.return_value = subprocess.CompletedProcess(
69+
[], 0, stdout=json.dumps({"files": files, "merge_base": None}), stderr=""
70+
)
6671

6772
assert get_fork_changes(fork_owner="someone", sha="abc", base="main") == expected
6873

@@ -76,3 +81,53 @@ def test_get_fork_changes_runs_everything_when_the_compare_fails(mock_gh):
7681
assert get_fork_changes(fork_owner="someone", sha="abc", base="main") == ForkChanges(
7782
files=(), truncated=True
7883
)
84+
85+
86+
@mock.patch("airflow_breeze.utils.fork_compare.run_gh_command", autospec=True)
87+
def test_get_fork_changes_returns_the_merge_base(mock_gh):
88+
mock_gh.return_value = subprocess.CompletedProcess(
89+
[], 0, stdout=json.dumps({"files": [["a.py"]], "merge_base": "base-sha"}), stderr=""
90+
)
91+
92+
assert get_fork_changes(fork_owner="someone", sha="abc", base="main").merge_base == "base-sha"
93+
94+
95+
@mock.patch("airflow_breeze.utils.fork_compare.run_gh_command", autospec=True)
96+
@pytest.mark.parametrize(
97+
("returncode", "stdout", "expected"),
98+
[
99+
pytest.param(0, '["full tests needed"]', ("full tests needed",), id="pull-request"),
100+
pytest.param(0, "[]", (), id="no-pull-request"),
101+
pytest.param(1, "", (), id="call-fails"),
102+
],
103+
)
104+
def test_get_fork_pull_request_labels(mock_gh, returncode, stdout, expected):
105+
mock_gh.return_value = subprocess.CompletedProcess([], returncode, stdout=stdout, stderr="boom")
106+
107+
assert get_fork_pull_request_labels(fork_owner="someone", branch="fix") == expected
108+
109+
assert "head=someone:fix" in mock_gh.call_args.args[0]
110+
111+
112+
@mock.patch("airflow_breeze.utils.fork_compare.run_gh_command", autospec=True)
113+
@pytest.mark.parametrize(
114+
("returncode", "stderr", "expected"),
115+
[
116+
pytest.param(0, "", "content", id="found"),
117+
pytest.param(1, "gh: Not Found (HTTP 404)", None, id="missing"),
118+
],
119+
)
120+
def test_get_upstream_file(mock_gh, returncode, stderr, expected):
121+
mock_gh.return_value = subprocess.CompletedProcess([], returncode, stdout="content", stderr=stderr)
122+
123+
assert get_upstream_file(path="pyproject.toml", ref="base-sha") == expected
124+
125+
assert mock_gh.call_args.args[0][-1] == "repos/apache/airflow/contents/pyproject.toml?ref=base-sha"
126+
127+
128+
@mock.patch("airflow_breeze.utils.fork_compare.run_gh_command", autospec=True)
129+
def test_get_upstream_file_fails_on_errors_other_than_absence(mock_gh):
130+
mock_gh.return_value = subprocess.CompletedProcess([], 1, stdout="", stderr="HTTP 502: Bad Gateway")
131+
132+
with pytest.raises(UpstreamFileError, match="502"):
133+
get_upstream_file(path="pyproject.toml", ref="base-sha")

0 commit comments

Comments
 (0)