|
| 1 | +"""Determine which GitHub Actions workflows to run. |
| 2 | +
|
| 3 | +Called by ``.github/workflows/reusable-change-detection.yml``. |
| 4 | +We only want to run tests on PRs when related files are changed, |
| 5 | +or when someone triggers a manual workflow run. |
| 6 | +This improves developer experience by not doing (slow) |
| 7 | +unnecessary work in GHA, and saves CI resources. |
| 8 | +""" |
| 9 | + |
| 10 | +from __future__ import annotations |
| 11 | + |
| 12 | +import os |
| 13 | +import subprocess |
| 14 | +from dataclasses import dataclass |
| 15 | +from pathlib import Path |
| 16 | + |
| 17 | +TYPE_CHECKING = False |
| 18 | +if TYPE_CHECKING: |
| 19 | + from collections.abc import Set |
| 20 | + |
| 21 | +GITHUB_CODEOWNERS_PATH = Path(".github/CODEOWNERS") |
| 22 | +GITHUB_WORKFLOWS_PATH = Path(".github/workflows") |
| 23 | +CONFIGURATION_FILE_NAMES = frozenset({ |
| 24 | + ".pre-commit-config.yaml", |
| 25 | + ".ruff.toml", |
| 26 | + "mypy.ini", |
| 27 | +}) |
| 28 | +SUFFIXES_DOCUMENTATION = frozenset({".rst", ".md"}) |
| 29 | +SUFFIXES_C_OR_CPP = frozenset({".c", ".h", ".cpp"}) |
| 30 | + |
| 31 | + |
| 32 | +@dataclass(kw_only=True, slots=True) |
| 33 | +class Outputs: |
| 34 | + run_ci_fuzz: bool = False |
| 35 | + run_docs: bool = False |
| 36 | + run_hypothesis: bool = False |
| 37 | + run_tests: bool = False |
| 38 | + run_windows_msi: bool = False |
| 39 | + |
| 40 | + |
| 41 | +def compute_changes(): |
| 42 | + target_branch, head_branch = git_branches() |
| 43 | + if target_branch and head_branch: |
| 44 | + # Getting changed files only makes sense on a pull request |
| 45 | + files = get_changed_files( |
| 46 | + f"origin/{target_branch}", f"origin/{head_branch}" |
| 47 | + ) |
| 48 | + outputs = process_changed_files(files) |
| 49 | + else: |
| 50 | + # Otherwise, just run the tests |
| 51 | + outputs = Outputs(run_tests=True) |
| 52 | + outputs = process_target_branch(outputs, target_branch) |
| 53 | + |
| 54 | + if outputs.run_tests: |
| 55 | + print("Run tests") |
| 56 | + |
| 57 | + if outputs.run_hypothesis: |
| 58 | + print("Run hypothesis tests") |
| 59 | + |
| 60 | + if outputs.run_ci_fuzz: |
| 61 | + print("Run CIFuzz tests") |
| 62 | + else: |
| 63 | + print("Branch too old for CIFuzz tests; or no C files were changed") |
| 64 | + |
| 65 | + if outputs.run_docs: |
| 66 | + print("Build documentation") |
| 67 | + |
| 68 | + if outputs.run_windows_msi: |
| 69 | + print("Build Windows MSI") |
| 70 | + |
| 71 | + print(outputs) |
| 72 | + |
| 73 | + write_github_output(outputs) |
| 74 | + |
| 75 | + |
| 76 | +def git_branches() -> tuple[str, str]: |
| 77 | + target_branch = os.environ.get("GITHUB_BASE_REF", "") |
| 78 | + target_branch = target_branch.removeprefix("refs/heads/") |
| 79 | + print(f"target branch: {target_branch!r}") |
| 80 | + |
| 81 | + head_branch = os.environ.get("GITHUB_HEAD_REF", "") |
| 82 | + head_branch = head_branch.removeprefix("refs/heads/") |
| 83 | + print(f"head branch: {head_branch!r}") |
| 84 | + return target_branch, head_branch |
| 85 | + |
| 86 | + |
| 87 | +def get_changed_files(ref_a: str = "main", ref_b: str = "HEAD") -> Set[Path]: |
| 88 | + """List the files changed between two Git refs, filtered by change type.""" |
| 89 | + args = ("git", "diff", "--name-only", f"{ref_a}...{ref_b}", "--") |
| 90 | + print(*args) |
| 91 | + changed_files_result = subprocess.run( |
| 92 | + args, stdout=subprocess.PIPE, check=True, encoding="utf-8" |
| 93 | + ) |
| 94 | + changed_files = changed_files_result.stdout.strip().splitlines() |
| 95 | + return frozenset(map(Path, filter(None, map(str.strip, changed_files)))) |
| 96 | + |
| 97 | + |
| 98 | +def process_changed_files(changed_files: Set[Path]) -> Outputs: |
| 99 | + run_tests = False |
| 100 | + run_ci_fuzz = False |
| 101 | + run_docs = False |
| 102 | + run_windows_msi = False |
| 103 | + |
| 104 | + for file in changed_files: |
| 105 | + file_name = file.name |
| 106 | + file_suffix = file.suffix |
| 107 | + file_parts = file.parts |
| 108 | + |
| 109 | + # Documentation files |
| 110 | + doc_or_misc = file_parts[0] in {"Doc", "Misc"} |
| 111 | + doc_file = file_suffix in SUFFIXES_DOCUMENTATION or doc_or_misc |
| 112 | + |
| 113 | + if file.parent == GITHUB_WORKFLOWS_PATH: |
| 114 | + if file_name == "build.yml": |
| 115 | + run_tests = run_ci_fuzz = True |
| 116 | + if file_name == "reusable-docs.yml": |
| 117 | + run_docs = True |
| 118 | + if file_name == "reusable-windows-msi.yml": |
| 119 | + run_windows_msi = True |
| 120 | + |
| 121 | + if not ( |
| 122 | + doc_file |
| 123 | + or file == GITHUB_CODEOWNERS_PATH |
| 124 | + or file_name in CONFIGURATION_FILE_NAMES |
| 125 | + ): |
| 126 | + run_tests = True |
| 127 | + |
| 128 | + # The fuzz tests are pretty slow so they are executed only for PRs |
| 129 | + # changing relevant files. |
| 130 | + if file_suffix in SUFFIXES_C_OR_CPP: |
| 131 | + run_ci_fuzz = True |
| 132 | + if file_parts[:2] in { |
| 133 | + ("configure",), |
| 134 | + ("Modules", "_xxtestfuzz"), |
| 135 | + }: |
| 136 | + run_ci_fuzz = True |
| 137 | + |
| 138 | + # Check for changed documentation-related files |
| 139 | + if doc_file: |
| 140 | + run_docs = True |
| 141 | + |
| 142 | + # Check for changed MSI installer-related files |
| 143 | + if file_parts[:2] == ("Tools", "msi"): |
| 144 | + run_windows_msi = True |
| 145 | + |
| 146 | + return Outputs( |
| 147 | + run_ci_fuzz=run_ci_fuzz, |
| 148 | + run_docs=run_docs, |
| 149 | + run_tests=run_tests, |
| 150 | + run_windows_msi=run_windows_msi, |
| 151 | + ) |
| 152 | + |
| 153 | + |
| 154 | +def process_target_branch(outputs: Outputs, git_branch: str) -> Outputs: |
| 155 | + if not git_branch: |
| 156 | + outputs.run_tests = True |
| 157 | + |
| 158 | + # Check if we should run the hypothesis tests |
| 159 | + if git_branch in {"3.8", "3.9", "3.10", "3.11"}: |
| 160 | + print("Branch too old for hypothesis tests") |
| 161 | + outputs.run_hypothesis = False |
| 162 | + else: |
| 163 | + outputs.run_hypothesis = outputs.run_tests |
| 164 | + |
| 165 | + # oss-fuzz maintains a configuration for fuzzing the main branch of |
| 166 | + # CPython, so CIFuzz should be run only for code that is likely to be |
| 167 | + # merged into the main branch; compatibility with older branches may |
| 168 | + # be broken. |
| 169 | + if git_branch != "main": |
| 170 | + outputs.run_ci_fuzz = False |
| 171 | + |
| 172 | + if os.environ.get("GITHUB_EVENT_NAME", "").lower() == "workflow_dispatch": |
| 173 | + outputs.run_docs = True |
| 174 | + outputs.run_windows_msi = True |
| 175 | + |
| 176 | + return outputs |
| 177 | + |
| 178 | + |
| 179 | +def write_github_output(outputs: Outputs) -> None: |
| 180 | + # https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/store-information-in-variables#default-environment-variables |
| 181 | + # https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/workflow-commands-for-github-actions#setting-an-output-parameter |
| 182 | + if "GITHUB_OUTPUT" not in os.environ: |
| 183 | + print("GITHUB_OUTPUT not defined!") |
| 184 | + return |
| 185 | + |
| 186 | + with open(os.environ["GITHUB_OUTPUT"], "a", encoding="utf-8") as f: |
| 187 | + f.write(f"run-cifuzz={bool_lower(outputs.run_ci_fuzz)}\n") |
| 188 | + f.write(f"run-docs={bool_lower(outputs.run_docs)}\n") |
| 189 | + f.write(f"run-hypothesis={bool_lower(outputs.run_hypothesis)}\n") |
| 190 | + f.write(f"run-tests={bool_lower(outputs.run_tests)}\n") |
| 191 | + f.write(f"run-win-msi={bool_lower(outputs.run_windows_msi)}\n") |
| 192 | + |
| 193 | + |
| 194 | +def bool_lower(value: bool, /) -> str: |
| 195 | + return "true" if value else "false" |
| 196 | + |
| 197 | + |
| 198 | +if __name__ == "__main__": |
| 199 | + compute_changes() |
0 commit comments