Skip to content

Commit 164a812

Browse files
authored
Run fork pushes with pull-request selective checks (#74820)
* Run fork pushes with pull-request selective checks A push in a contributor fork would otherwise be treated as a post-merge push and run the full matrix, about 220 minutes on the contributor's 20-runner cap, for every push. Computing the change set against apache/airflow gives the same selection a pull request would get, independent of how stale the fork's main is, and fork runs must never select self-hosted runners that forks do not have. Generated-by: Claude Opus 5 * 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 * Match the fork pull request by base when reading its labels One head branch can have open pull requests into several bases, so the label lookup now filters on the branch line the changes are compared against. The fork push selective-check tests move to test_ci_commands.py, next to the other selective_check command tests. Generated-by: Claude Opus 5
1 parent 3ce8be4 commit 164a812

8 files changed

Lines changed: 586 additions & 32 deletions

File tree

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

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,11 @@ high-blast-radius signals:
141141

142142
```mermaid
143143
flowchart TD
144-
A([_should_run_all_tests_and_versions]) --> B{event is<br/>PUSH / SCHEDULE / DISPATCH?}
144+
A([_should_run_all_tests_and_versions]) --> T{fork push with<br/>truncated change set?}
145+
T -->|yes| YES0[True<br/>run everything]
146+
T -->|no| FP{fork push?}
147+
FP -->|yes - treated as a pull request| E
148+
FP -->|no| B{event is<br/>PUSH / SCHEDULE / DISPATCH?}
145149
B -->|yes| C{PUSH and only<br/>.txt/.md files changed?}
146150
C -->|yes| NO1[False<br/>skip full tests]
147151
C -->|no| D{PUSH with<br/>--selective-tests-on-push?}
@@ -332,6 +336,23 @@ flowchart TD
332336
6. **Scheduled nightly canary** (SCHEDULE on `apache/airflow`). Full matrix on all versions, every
333337
canary-only job. The most thorough run; it is what gives us confidence that the PR-time shortcuts
334338
never let a regression through.
339+
7. **Push in a contributor fork** (AIP-120, see
340+
[Fork CI for external contributors](08_fork_ci_for_external_contributors.md)). The changed files
341+
come from the cross-repository compare API against `apache/airflow`'s branch line of the pushed
342+
code (breeze's default branch at that commit: `main`, or the release branch it was cut from),
343+
not from `git diff-tree`, with a renamed file counted under both paths, and the run is selected
344+
exactly like a pull request with those files. `is_committer_build` is always `False` because
345+
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, 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.
335356

336357
## Troubleshooting: my CI run does too much
337358

@@ -452,7 +473,11 @@ together using `pytest-xdist` (pytest-xdist distributes the tests among parallel
452473
checks are performed. See also [1] note below. Two exceptions narrow this: a PUSH that changed **only**
453474
`.txt`/`.md` files skips full tests, and a PUSH with `--selective-tests-on-push` (which `ci-amd.yml`
454475
passes for pushes to release branches) is treated like a pull request — selective tests on default
455-
versions, not a `canary` run. The high-level flow and worked examples for these cases are in
476+
versions, not a `canary` run. A PUSH in a contributor fork is not a PUSH for this rule either: it is
477+
selected like a pull request against `apache/airflow`, and runs everything only when its change set
478+
reaches the compare API's 300-file cap (a list of 300 may be missing files) or the compare fails. The
479+
incremental base of a release-branch push (the last successful run of the branch) is only looked up in
480+
`apache/airflow`. The high-level flow and worked examples for these cases are in
456481
[The decision pipeline](#the-decision-pipeline) and
457482
[Worked examples](#worked-examples-pull-requests-pushes-and-canary-runs) above.
458483
* Python, Kubernetes, Backend, Kind, Helm versions are limited to "defaults" only unless `Full tests` mode
@@ -683,6 +708,9 @@ Runner Controller` but most of the jobs, committer builds will use "Self-hosted"
683708
while non-committer builds will use "Public" runners. For committers, this can be overridden by setting the
684709
`use public runners` label in the PR.
685710

711+
Pushes in a contributor fork are never committer builds, whoever pushes, because forks have no
712+
self-hosted runners.
713+
686714
## Changing behaviours of the CI runs by setting labels
687715

688716
Also, currently for most of the jobs, committer builds by default use "Self-hosted" runners, while

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

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -358,9 +358,22 @@ upstream yields no changed files, so such a run performs basic checks only.
358358
- **Changed files** come from the cross-repository compare API,
359359
`GET /repos/apache/airflow/compare/<base>...<fork-owner>:<sha>`, three-dot semantics, so
360360
the set equals what a pull request against upstream would show. The fork's `main` is never
361-
used as a base, so a stale fork does not inflate the change set. `<base>` is `main` unless
362-
the pushed branch name starts with a canonical release branch prefix. The API caps the file
363-
list at 300; at the cap the run falls back to "run everything".
361+
used as a base, so a stale fork does not inflate the change set. `<base>` is the branch line
362+
of the pushed code, breeze's default branch at that commit (`main`, or the release branch it
363+
was cut from), because a branch name such as `backport-<sha>-v3-1-test` cannot be trusted to
364+
tell. A renamed file counts under both paths. The API caps the file list at 300; at the cap,
365+
and when the compare fails, the run falls back to "run everything", UI E2E tests included,
366+
rather than failing.
367+
- **Previous versions** of changed files, for the `pyproject.toml` and provider dependency
368+
bump checks, are read at the compare's merge base in `apache/airflow`
369+
(`GET /repos/apache/airflow/contents/<path>?ref=<merge-base>`). The pushed commit's parent
370+
would be the contributor's own previous tip after they merge upstream `main`, so a release
371+
manager's bump from `main` would count as theirs. A file missing at the merge base is new; any
372+
other read failure fails the run rather than letting the check pass unseen.
373+
- **Labels** come from the open `apache/airflow` pull request whose head is
374+
`<fork-owner>:<branch>`, since a push event carries none. `full tests needed`,
375+
`allow provider dependency bump`, `skip common compat check` and the other labels work as on
376+
a pull request; a label added later takes effect on the next fork run.
364377
- `_should_run_all_tests_and_versions` treats a fork push like a pull request. Today any
365378
`push` runs the full matrix, which would cost a contributor ~220 minutes per push.
366379
- `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: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
fix_ownership_using_docker,
6161
perform_environment_checks,
6262
)
63+
from airflow_breeze.utils.fork_compare import get_fork_changes, get_fork_pull_request_labels, is_fork_push
6364
from airflow_breeze.utils.github import format_github_token_scope_guidance, retrieve_github_token
6465
from airflow_breeze.utils.path_utils import AIRFLOW_HOME_PATH, AIRFLOW_ROOT_PATH
6566
from airflow_breeze.utils.run_utils import run_command
@@ -323,7 +324,22 @@ def selective_check(
323324
github_context_dict = json.loads(github_context) if github_context else {}
324325
github_event = GithubEvents(github_event_name)
325326
changed_files: tuple[str, ...] = ()
326-
if commit_ref is not None and selective_tests_on_push and github_event == GithubEvents.PUSH:
327+
fork_push = is_fork_push(github_event, github_repository)
328+
fork_changes_truncated = False
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"], base=default_branch
335+
)
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)
338+
changed_files = fork_changes.files
339+
fork_changes_truncated = fork_changes.truncated
340+
fork_merge_base = fork_changes.merge_base
341+
console_print(f"\n[info]Changed files against apache/airflow:[/]\n{changed_files}\n")
342+
elif commit_ref is not None and selective_tests_on_push and github_event == GithubEvents.PUSH:
327343
# The base is the last successful run of the branch, so this run also covers the commits
328344
# whose push runs failed or were cancelled by a newer push. Without one, nothing after the
329345
# branch point is known to pass, so everything runs.
@@ -344,13 +360,16 @@ def selective_check(
344360
files=changed_files,
345361
default_branch=default_branch,
346362
default_constraints_branch=default_constraints_branch,
347-
pr_labels=tuple(ast.literal_eval(pr_labels)) if pr_labels else (),
363+
pr_labels=labels,
348364
github_event=github_event,
349365
github_repository=github_repository,
350366
github_actor=github_actor,
351367
github_context_dict=github_context_dict,
352368
platform=ci_platform,
353369
selective_tests_on_push=selective_tests_on_push,
370+
fork_push=fork_push,
371+
fork_changes_truncated=fork_changes_truncated,
372+
fork_merge_base=fork_merge_base,
354373
)
355374
print(str(sc), file=sys.stderr)
356375
except Exception:
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
"""Changed files of a push in a contributor fork, computed against apache/airflow (AIP-120)."""
18+
19+
from __future__ import annotations
20+
21+
import json
22+
from dataclasses import dataclass
23+
24+
from airflow_breeze.global_constants import APACHE_AIRFLOW_GITHUB_REPOSITORY, GithubEvents
25+
from airflow_breeze.utils.console import console_print
26+
from airflow_breeze.utils.github import run_gh_command
27+
28+
# The compare API lists at most this many files: a list that reaches it may be missing some, so
29+
# the change set is treated as truncated.
30+
COMPARE_FILE_CAP = 300
31+
32+
33+
@dataclass(frozen=True)
34+
class ForkChanges:
35+
files: tuple[str, ...]
36+
truncated: bool
37+
merge_base: str | None = None
38+
39+
40+
def is_fork_push(github_event: GithubEvents, github_repository: str) -> bool:
41+
return github_event == GithubEvents.PUSH and github_repository != APACHE_AIRFLOW_GITHUB_REPOSITORY
42+
43+
44+
def get_fork_changes(*, fork_owner: str, sha: str, base: str) -> ForkChanges:
45+
"""
46+
Three-dot compare of the pushed commit against the upstream base branch.
47+
48+
Three-dot semantics give the same file list a pull request against upstream would show,
49+
so a fork whose ``main`` is behind upstream does not inflate the change set. ``base`` is the
50+
branch line the pushed code belongs to (breeze's default branch at that commit), which a
51+
branch name cannot be trusted to tell. A renamed file counts under both paths, as in
52+
``git diff``. When the compare fails the change set is reported as truncated, so the run
53+
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.
58+
"""
59+
result = run_gh_command(
60+
[
61+
"gh",
62+
"api",
63+
f"repos/{APACHE_AIRFLOW_GITHUB_REPOSITORY}/compare/{base}...{fork_owner}:{sha}",
64+
"--jq",
65+
"{files: [.files[] | [.filename, (.previous_filename // empty)]], "
66+
"merge_base: .merge_base_commit.sha}",
67+
],
68+
capture_output=True,
69+
text=True,
70+
)
71+
if result.returncode != 0:
72+
console_print(
73+
f"[warning]Could not compare {fork_owner}:{sha} with {APACHE_AIRFLOW_GITHUB_REPOSITORY}:{base}, "
74+
f"so everything runs: {result.stderr.strip()}[/]"
75+
)
76+
return ForkChanges(files=(), truncated=True)
77+
compare = json.loads(result.stdout or "{}")
78+
entries = compare.get("files", [])
79+
files = tuple(path for entry in entries for path in entry)
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, base: str) -> tuple[str, ...]:
86+
"""
87+
Labels of the open apache/airflow pull request from ``fork_owner:branch`` into ``base``.
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. One head branch can have open pull requests into several bases, so ``base`` is
92+
the same branch line the changes are compared against. No pull request, or a failed call,
93+
means no labels.
94+
"""
95+
result = run_gh_command(
96+
[
97+
"gh",
98+
"api",
99+
"--method",
100+
"GET",
101+
f"repos/{APACHE_AIRFLOW_GITHUB_REPOSITORY}/pulls",
102+
"-f",
103+
f"head={fork_owner}:{branch}",
104+
"-f",
105+
f"base={base}",
106+
"-f",
107+
"state=open",
108+
"--jq",
109+
"[.[0].labels[]?.name]",
110+
],
111+
capture_output=True,
112+
text=True,
113+
)
114+
if result.returncode != 0:
115+
console_print(
116+
f"[warning]Could not read the labels of the pull request from {fork_owner}:{branch}: "
117+
f"{result.stderr.strip()}[/]"
118+
)
119+
return ()
120+
return tuple(json.loads(result.stdout or "[]"))
121+
122+
123+
class UpstreamFileError(RuntimeError):
124+
"""A file of apache/airflow could not be read for a reason other than its absence."""
125+
126+
127+
def get_upstream_file(*, path: str, ref: str) -> str | None:
128+
"""
129+
Content of ``path`` at ``ref`` in apache/airflow, or None when the file does not exist there.
130+
131+
Any other failure raises: the checks that compare a file with its previous version would
132+
otherwise pass silently for a change they never saw.
133+
"""
134+
result = run_gh_command(
135+
[
136+
"gh",
137+
"api",
138+
"-H",
139+
"Accept: application/vnd.github.raw",
140+
f"repos/{APACHE_AIRFLOW_GITHUB_REPOSITORY}/contents/{path}?ref={ref}",
141+
],
142+
capture_output=True,
143+
text=True,
144+
)
145+
if result.returncode == 0:
146+
return result.stdout
147+
if "404" in result.stderr or "Not Found" in result.stderr:
148+
return None
149+
raise UpstreamFileError(
150+
f"Could not read {path} at {ref} in {APACHE_AIRFLOW_GITHUB_REPOSITORY}: {result.stderr.strip()}"
151+
)

0 commit comments

Comments
 (0)