This is an automated email from the ASF dual-hosted git repository.
amoghdesai pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/airflow.git
The following commit(s) were added to refs/heads/main by this push:
new fc3b015a217 Fetching dynamic target branches for execution API prek
hook (#63985)
fc3b015a217 is described below
commit fc3b015a21712ba5d894d497d66377c85e7642db
Author: Amogh Desai <[email protected]>
AuthorDate: Sat Mar 21 15:43:31 2026 +0530
Fetching dynamic target branches for execution API prek hook (#63985)
---
.pre-commit-config.yaml | 2 +-
scripts/ci/prek/check_execution_api_versions.py | 75 +++++--------------------
2 files changed, 15 insertions(+), 62 deletions(-)
diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml
index bc10b84beff..c94bd75e18a 100644
--- a/.pre-commit-config.yaml
+++ b/.pre-commit-config.yaml
@@ -1056,7 +1056,7 @@ repos:
name: Check execution API datamodel changes have corresponding version
updates
entry: ./scripts/ci/prek/check_execution_api_versions.py
language: python
- pass_filenames: false
+ pass_filenames: true
files:
^airflow-core/src/airflow/api_fastapi/execution_api/(datamodels|versions)/.*\.py$
require_serial: true
- id: generate-tasksdk-datamodels
diff --git a/scripts/ci/prek/check_execution_api_versions.py
b/scripts/ci/prek/check_execution_api_versions.py
index 872812b7ec9..833ce32bcf7 100755
--- a/scripts/ci/prek/check_execution_api_versions.py
+++ b/scripts/ci/prek/check_execution_api_versions.py
@@ -34,62 +34,17 @@ from common_prek_utils import console, get_remote_for_main
DATAMODELS_PREFIX =
"airflow-core/src/airflow/api_fastapi/execution_api/datamodels/"
VERSIONS_PREFIX =
"airflow-core/src/airflow/api_fastapi/execution_api/versions/"
-TARGET_BRANCH = "main"
-def get_changed_files_ci() -> list[str]:
- """Get changed files in CI by comparing against main."""
- remote = get_remote_for_main()
- ref = f"{remote}/{TARGET_BRANCH}"
-
- fetch_result = subprocess.run(
- ["git", "fetch", remote, TARGET_BRANCH],
- capture_output=True,
- text=True,
- check=False,
- )
- if fetch_result.returncode != 0:
- console.print(f"[yellow]WARNING: Failed to fetch {ref}:
{fetch_result.stderr.strip()}[/]")
-
- is_main = not os.environ.get("GITHUB_BASE_REF")
- diff_target = "HEAD~1" if is_main else f"{ref}...HEAD"
-
- result = subprocess.run(
- ["git", "diff", "--name-only", diff_target],
- capture_output=True,
- text=True,
- check=False,
- )
- if result.returncode != 0:
- console.print(
- f"[yellow]WARNING: git diff against {ref} failed (exit
{result.returncode}), "
- "retrying with deeper fetch...[/]"
- )
- subprocess.run(
- ["git", "fetch", "--deepen=50", remote, TARGET_BRANCH],
- capture_output=True,
- text=True,
- check=False,
- )
- try:
- result = subprocess.run(
- ["git", "diff", "--name-only", f"{ref}...HEAD"],
- capture_output=True,
- text=True,
- check=True,
- )
- except subprocess.CalledProcessError as e:
- console.print(f"[bold red]ERROR:[/] git diff failed (exit
{e.returncode})")
- if e.stdout:
- console.print(f"[dim]stdout:[/]\n{e.stdout.strip()}")
- if e.stderr:
- console.print(f"[dim]stderr:[/]\n{e.stderr.strip()}")
- raise
- return [f for f in result.stdout.strip().splitlines() if f]
+def get_target_branch() -> str:
+ """Branch to compare against. GITHUB_BASE_REF for PRs, DEFAULT_BRANCH in
CI, else main."""
+ return os.environ.get("GITHUB_BASE_REF") or
os.environ.get("DEFAULT_BRANCH") or "main"
-def get_changed_files_local() -> list[str]:
- """Get staged files in a local (non-CI) environment."""
+def get_changed_files(filenames: list[str]) -> list[str]:
+ """Get changed files. Uses filenames from prek when provided, else staged
files for local runs."""
+ if filenames:
+ return filenames
result = subprocess.run(
["git", "diff", "--cached", "--name-only"],
capture_output=True,
@@ -115,11 +70,12 @@ def generate_schema(cwd: Path) -> dict:
def generate_schema_from_main() -> dict:
- """Generate schema from main branch using worktree."""
+ """Generate schema from target branch using worktree."""
+ target_branch = get_target_branch()
remote = get_remote_for_main()
- ref = f"{remote}/{TARGET_BRANCH}"
+ ref = f"{remote}/{target_branch}"
worktree_path = Path(tempfile.mkdtemp()) / "airflow-main"
- subprocess.run(["git", "fetch", remote, TARGET_BRANCH],
capture_output=True, check=False)
+ subprocess.run(["git", "fetch", remote, target_branch],
capture_output=True, check=False)
subprocess.run(["git", "worktree", "add", str(worktree_path), ref],
capture_output=True, check=True)
try:
return generate_schema(worktree_path)
@@ -145,11 +101,7 @@ def schemas_equal(schema1: dict, schema2: dict) -> bool:
def main() -> int:
- is_ci = os.environ.get("CI")
- if is_ci:
- changed_files = get_changed_files_ci()
- else:
- changed_files = get_changed_files_local()
+ changed_files = get_changed_files(sys.argv[1:])
datamodel_files = [
f for f in changed_files if f.startswith(DATAMODELS_PREFIX) and not
f.endswith("__init__.py")
@@ -194,8 +146,9 @@ def main() -> int:
console.print(f" - [magenta]{f}[/]")
console.print("")
remote = get_remote_for_main()
+ target_branch = get_target_branch()
console.print(
- f"Schema diff against [cyan]{remote}/{TARGET_BRANCH}[/]
detected differences.\n"
+ f"Schema diff against [cyan]{remote}/{target_branch}[/]
detected differences.\n"
"\n"
"Please add or update a version file under:\n"
f" [cyan]{VERSIONS_PREFIX}[/]\n"