Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1730,6 +1730,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:
Expand Down
3 changes: 2 additions & 1 deletion dev/breeze/doc/ci/08_fork_ci_for_external_contributors.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,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.

Expand Down
104 changes: 104 additions & 0 deletions scripts/ci/prek/check_scheduled_workflows_skip_forks.py
Original file line number Diff line number Diff line change
@@ -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:]))
138 changes: 138 additions & 0 deletions scripts/tests/ci/prek/test_check_scheduled_workflows_skip_forks.py
Original file line number Diff line number Diff line change
@@ -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 * * *"}]}


@pytest.mark.parametrize(
("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


@pytest.mark.parametrize(
("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) == []