This is an automated email from the ASF dual-hosted git repository.

potiuk pushed a commit to branch v3-4-test
in repository https://gitbox.apache.org/repos/asf/airflow.git


The following commit(s) were added to refs/heads/v3-4-test by this push:
     new 6b3c99c19fb [v3-4-test] Add a prek hook to keep scheduled workflows 
skipping in forks (#76267) (#77104)
6b3c99c19fb is described below

commit 6b3c99c19fb6e95892dddc27f5f91bab37ff4c0b
Author: github-actions[bot] 
<41898282+github-actions[bot]@users.noreply.github.com>
AuthorDate: Sun Nov 15 06:52:44 2026 +0100

    [v3-4-test] Add a prek hook to keep scheduled workflows skipping in forks 
(#76267) (#77104)
    
    * Add a prek hook to keep scheduled workflows skipping in forks
    
    * Accept a scheduled job that requires a guarded job to succeed
    
    A job whose condition uses a status function such as `!cancelled()` still
    skips in forks when the condition also requires a guarded job it needs to
    have succeeded, as `trigger-upgrade-check` in
    mirror-hardened-base-images.yml does, so the check no longer reports it.
    The docstring also no longer says ci-arm.yml runs in forks on push.
    
    Generated-by: Claude Opus 5
    
    ---------
    (cherry picked from commit 71f6e9b2484ea469fe5be724eedc23a0f85e78f6)
    
    Co-authored-by: Justin Pakzad 
<[email protected]>
    Co-authored-by: Jarek Potiuk <[email protected]>
---
 .pre-commit-config.yaml                            |  10 ++
 .../doc/ci/08_fork_ci_for_external_contributors.md |   3 +-
 .../prek/check_scheduled_workflows_skip_forks.py   | 104 ++++++++++++++++
 .../test_check_scheduled_workflows_skip_forks.py   | 138 +++++++++++++++++++++
 4 files changed, 254 insertions(+), 1 deletion(-)

diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml
index c3ea15b7c4c..473eacf275c 100644
--- a/.pre-commit-config.yaml
+++ b/.pre-commit-config.yaml
@@ -1725,6 +1725,16 @@ repos:
         language: python
         pass_filenames: true
         files: ^\.github/workflows/.*\.yml$
+      - id: check-scheduled-workflows-skip-forks
+        name: Check scheduled workflows skip their cron runs in forks
+        env:
+          PYTHONPYCACHEPREFIX: .build/pycache
+        description: Forks that enable Actions get every cron, so each 
scheduled job must skip outside apache/airflow
+        entry: ./scripts/ci/prek/check_scheduled_workflows_skip_forks.py
+        language: python
+        pass_filenames: true
+        require_serial: true
+        files: ^\.github/workflows/.*\.yml$
       - id: check-docstring-param-types
         name: Check that docstrings do not specify param types
         env:
diff --git a/dev/breeze/doc/ci/08_fork_ci_for_external_contributors.md 
b/dev/breeze/doc/ci/08_fork_ci_for_external_contributors.md
index 0e1ed2cb926..38beb990567 100644
--- a/dev/breeze/doc/ci/08_fork_ci_for_external_contributors.md
+++ b/dev/breeze/doc/ci/08_fork_ci_for_external_contributors.md
@@ -168,7 +168,8 @@ The same applies to every other workflow with a `schedule` 
trigger: a fork that
 Actions gets all of their crons too. Their jobs carry a job-level
 `if: github.repository == 'apache/airflow'` guard, combined with
 `github.event_name != 'schedule'` when the workflow has other triggers that 
should still run
-in a fork. A new scheduled workflow needs the same guard. The guard skips the 
jobs but not
+in a fork. A new scheduled workflow needs the same guard, which the
+`check-scheduled-workflows-skip-forks` prek hook enforces. The guard skips the 
jobs but not
 the run itself, so the frequent crons still leave an empty run in every 
enabled fork. Check 8 of
 `breeze ci audit` offers to disable these workflows in the fork.
 
diff --git a/scripts/ci/prek/check_scheduled_workflows_skip_forks.py 
b/scripts/ci/prek/check_scheduled_workflows_skip_forks.py
new file mode 100755
index 00000000000..aed634c146a
--- /dev/null
+++ b/scripts/ci/prek/check_scheduled_workflows_skip_forks.py
@@ -0,0 +1,104 @@
+#!/usr/bin/env python
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+# /// script
+# requires-python = ">=3.11,<3.12"
+# dependencies = [
+#   "pyyaml>=6.0.3",
+#   "rich>=13.6.0",
+# ]
+# ///
+"""Check that every job in a scheduled workflow skips its cron runs in forks.
+
+Forks that enable Actions for fork CI also get every cron. A job passes when 
its ``if:`` has the
+``apache/airflow`` guard, or when it needs a job that passes and does not 
override the skip with
+``always()``, ``cancelled()`` or ``failure()`` - unless its ``if:`` still 
requires such a job to
+succeed with ``needs.<job>.result == 'success'``. ``ci-amd.yml`` and 
``ci-arm.yml`` are exempt: their
+``check-run-needed`` job skips scheduled runs in forks, and ``ci-amd.yml`` 
must still run there on push.
+"""
+
+from __future__ import annotations
+
+import re
+import sys
+from pathlib import Path
+from typing import Any
+
+import yaml
+from rich.console import Console
+
+console = Console(color_system="standard", width=200)
+
+EXEMPTIONS = frozenset({"ci-amd.yml", "ci-arm.yml"})
+GUARD = "github.repository == 'apache/airflow'"
+STATUS_CHECK_FUNCTIONS = frozenset({"always()", "cancelled()", "failure()"})
+REQUIRED_SUCCESS = re.compile(r"needs\.([\w-]+)\.result\s*==\s*'success'")
+
+
+def is_covered(jobs: dict[str, Any], job_name: str) -> bool:
+    """Whether the job skips scheduled runs in forks, through its own guard or 
a job it needs."""
+    job = jobs[job_name]
+    if_condition = job.get("if") or ""
+    if GUARD in if_condition:
+        return True
+    needs = job.get("needs") or []
+    if isinstance(needs, str):
+        needs = [needs]
+    if any(status_check in if_condition for status_check in 
STATUS_CHECK_FUNCTIONS):
+        # A status function runs the job even when its needs were skipped, 
unless the condition
+        # still requires one of them to have succeeded.
+        required = set(REQUIRED_SUCCESS.findall(if_condition))
+        return any(is_covered(jobs, parent) for parent in needs if parent in 
required)
+    return any(is_covered(jobs, parent) for parent in needs)
+
+
+def check_file(file_path: Path) -> list[str]:
+    """Return the jobs of a scheduled workflow that would still run on 
schedule in forks."""
+    if file_path.name in EXEMPTIONS:
+        return []
+    workflow = yaml.safe_load(file_path.read_text())
+    # PyYAML reads the bare `on:` key as boolean True.
+    triggers = workflow.get("on", workflow.get(True))
+    if not (isinstance(triggers, dict) and "schedule" in triggers):
+        return []
+    jobs = workflow["jobs"]
+    return [job_name for job_name in jobs if not is_covered(jobs, job_name)]
+
+
+def main(file_paths: list[str]) -> int:
+    errors = []
+    for file_path in file_paths:
+        path = Path(file_path)
+        for job_name in check_file(path):
+            errors.append(
+                f"[red]{path}[/]: job [yellow]{job_name}[/] runs on schedule 
in forks. "
+                f"Add `if: {GUARD}` to it, or make it need a job that has it."
+            )
+    if not errors:
+        return 0
+    for error in errors:
+        console.print(error)
+    console.print(
+        "\nWorkflows with other triggers should use "
+        f"`if: github.event_name != 'schedule' || {GUARD}`\n"
+        "so manual, push and pull request runs still work in forks."
+    )
+    return 1
+
+
+if __name__ == "__main__":
+    sys.exit(main(sys.argv[1:]))
diff --git a/scripts/tests/ci/prek/test_check_scheduled_workflows_skip_forks.py 
b/scripts/tests/ci/prek/test_check_scheduled_workflows_skip_forks.py
new file mode 100644
index 00000000000..131bd2b6950
--- /dev/null
+++ b/scripts/tests/ci/prek/test_check_scheduled_workflows_skip_forks.py
@@ -0,0 +1,138 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+from __future__ import annotations
+
+import pytest
+import yaml
+from check_scheduled_workflows_skip_forks import GUARD, check_file, is_covered
+
+SCHEDULE_ONLY = {"schedule": [{"cron": "0 0 * * *"}]}
+
+
[email protected](
+    ("jobs", "job_name", "expected"),
+    [
+        pytest.param({"job": {"if": GUARD}}, "job", True, id="own-guard"),
+        pytest.param({"job": {}}, "job", False, id="no-guard-no-needs"),
+        pytest.param(
+            {"parent": {"if": GUARD}, "job": {"needs": "parent"}}, "job", 
True, id="needs-as-string"
+        ),
+        pytest.param(
+            {"root": {"if": GUARD}, "middle": {"needs": "root"}, "job": 
{"needs": "middle"}},
+            "job",
+            True,
+            id="chain",
+        ),
+        pytest.param(
+            {"first": {}, "second": {"if": GUARD}, "job": {"needs": ["first", 
"second"]}},
+            "job",
+            True,
+            id="one-of-several-parents-covered",
+        ),
+        pytest.param(
+            {"first": {}, "second": {}, "job": {"needs": ["first", "second"]}},
+            "job",
+            False,
+            id="no-parent-covered",
+        ),
+        pytest.param(
+            {"parent": {"if": GUARD}, "job": {"needs": "parent", "if": 
"!cancelled()"}},
+            "job",
+            False,
+            id="status-function-overrides-covered-parent",
+        ),
+        pytest.param(
+            {"parent": {"if": GUARD}, "job": {"needs": "parent", "if": 
f"!cancelled() && {GUARD}"}},
+            "job",
+            True,
+            id="status-function-with-own-guard",
+        ),
+        pytest.param(
+            {
+                "parent": {"if": GUARD},
+                "other": {},
+                "job": {
+                    "needs": ["parent", "other"],
+                    "if": "!cancelled() && needs.parent.result == 'success'",
+                },
+            },
+            "job",
+            True,
+            id="status-function-requiring-a-covered-parent",
+        ),
+        pytest.param(
+            {
+                "parent": {"if": GUARD},
+                "other": {},
+                "job": {
+                    "needs": ["parent", "other"],
+                    "if": "!cancelled() && needs.other.result == 'success'",
+                },
+            },
+            "job",
+            False,
+            id="status-function-requiring-an-uncovered-parent",
+        ),
+        pytest.param(
+            {
+                "parent": {"if": GUARD},
+                "job": {"needs": [], "if": "always() && needs.parent.result == 
'success'"},
+            },
+            "job",
+            False,
+            id="status-function-requiring-a-job-it-does-not-need",
+        ),
+    ],
+)
+def test_is_covered(jobs, job_name, expected):
+    assert is_covered(jobs, job_name) is expected
+
+
[email protected](
+    ("workflow", "expected"),
+    [
+        pytest.param(
+            {"on": {"push": None}, "jobs": {"job": {}}},
+            [],
+            id="not-scheduled",
+        ),
+        pytest.param(
+            {"on": "push", "jobs": {"job": {}}},
+            [],
+            id="trigger-as-string",
+        ),
+        pytest.param(
+            {"on": SCHEDULE_ONLY, "jobs": {"guarded": {"if": GUARD}, 
"unguarded": {}}},
+            ["unguarded"],
+            id="scheduled-with-unguarded-job",
+        ),
+        # Real workflows load `on:` as the key True, so this case uses True 
instead of "on".
+        pytest.param(
+            {True: SCHEDULE_ONLY, "jobs": {"job": {}}},
+            ["job"],
+            id="bare-on-key",
+        ),
+    ],
+)
+def test_check_file(write_workflow_file, workflow, expected):
+    assert check_file(write_workflow_file(workflow)) == expected
+
+
+def test_check_file_skips_exempt_workflow(tmp_path):
+    path = tmp_path / "ci-amd.yml"
+    path.write_text(yaml.dump({"on": SCHEDULE_ONLY, "jobs": {"job": {}}}))
+    assert check_file(path) == []

Reply via email to