Files
crank/scripts/ci-release-tooling-scope.py
bsodfather cf2909273b
CI / UI Checks (pull_request) Successful in 8s
CI / Rust Checks (pull_request) Successful in 24m39s
CI / Rust Feature Validation (pull_request) Skipped
CI / Frontend E2E (pull_request) Skipped
CI / Community Image Smoke (pull_request) Skipped
CI / Required Checks (pull_request) Successful in 1s
CI / Deploy (pull_request) Skipped
perf(ci): scope release tooling changes without unrelated Cargo builds
2026-11-08 14:47:35 +03:00

85 lines
2.8 KiB
Python

#!/usr/bin/env python3
"""Fail-closed detector for release-tooling-only CI changes."""
from __future__ import annotations
import os
import re
import subprocess
import sys
from pathlib import Path
_SHA = re.compile(r"[0-9a-fA-F]{40}\Z")
_ALLOWED = {
".gitea/workflows/release.yml",
"scripts/scan-images.sh",
"scripts/check-image-scan.py",
"scripts/ci-release-assets.py",
"scripts/ci-release-resume.py",
"tests/unit/test_scan_images.py",
"tests/unit/test_check_image_scan.py",
"tests/unit/test_ci_release_assets.py",
"tests/unit/test_ci_release_resume.py",
}
_ALLOWED_BYTES = {path.encode("utf-8") for path in _ALLOWED}
def _git(repo: Path, *args: str) -> subprocess.CompletedProcess[bytes] | None:
try:
return subprocess.run(
["git", "-C", str(repo), *args],
check=False,
stdout=subprocess.PIPE,
stderr=subprocess.DEVNULL,
)
except OSError:
return None
def release_tooling_only(
repo: Path, event_name: str, before_sha: str, pr_base_sha: str, force_full: str
) -> bool:
if force_full.lower() != "false" or event_name not in {"push", "pull_request"}:
return False
raw_base = before_sha if event_name == "push" else pr_base_sha
if not _SHA.fullmatch(raw_base):
return False
available = _git(repo, "cat-file", "-e", f"{raw_base}^{{commit}}")
if available is None or available.returncode != 0:
return False
if event_name == "pull_request":
merge_base = _git(repo, "merge-base", "HEAD", raw_base)
if merge_base is None or merge_base.returncode != 0:
return False
raw_base = merge_base.stdout.decode("ascii", errors="ignore").strip()
if not _SHA.fullmatch(raw_base):
return False
diff = _git(repo, "diff", "--no-renames", "--name-only", "-z", raw_base, "HEAD")
if diff is None or diff.returncode != 0:
return False
paths = diff.stdout.split(b"\0")
paths = [path for path in paths if path]
return bool(paths) and all(path in _ALLOWED_BYTES for path in paths)
def main() -> int:
env = os.environ
ci_only = release_tooling_only(
Path.cwd(),
env.get("GITEA_EVENT_NAME", ""),
env.get("GITEA_EVENT_BEFORE", ""),
env.get("GITEA_PR_BASE_SHA", ""),
env.get("FORCE_FULL", ""),
)
output = env.get("GITHUB_OUTPUT")
if not output:
print("Release-tooling CI scope unavailable; using full Rust checks.")
return 0
with open(output, "a", encoding="utf-8") as stream:
stream.write(f"ci_only={'true' if ci_only else 'false'}\n")
print("Release-tooling-only change detected." if ci_only else "Full Rust checks required.")
return 0
if __name__ == "__main__":
raise SystemExit(main())