diff --git a/.gitea/workflows/ci.yml b/.gitea/workflows/ci.yml index db6ef3c..6c5684e 100644 --- a/.gitea/workflows/ci.yml +++ b/.gitea/workflows/ci.yml @@ -40,6 +40,15 @@ jobs: with: fetch-depth: 0 + - name: Detect release-tooling-only changes + id: release_tooling + env: + GITEA_EVENT_NAME: ${{ gitea.event_name }} + GITEA_EVENT_BEFORE: ${{ gitea.event.before }} + GITEA_PR_BASE_SHA: ${{ gitea.event.pull_request.base.sha }} + FORCE_FULL: ${{ inputs.validation_level == 'feature' || contains(gitea.event.pull_request.labels.*.name, 'feature-ready') }} + run: python3 scripts/ci-release-tooling-scope.py + - name: Install Rust toolchain run: | set -eu @@ -78,15 +87,18 @@ jobs: docker info - name: Check typed runtime configuration contract + if: steps.release_tooling.outputs.ci_only != 'true' run: | cargo run -p crank-config --bin crank-config-contract -- --check python3 scripts/check-runtime-config.py --root . python3 scripts/check-config-boundaries.py --root . - name: Check canonical migration contract + if: steps.release_tooling.outputs.ci_only != 'true' run: cargo run -p admin-api --bin crank-migrate -- plan --check - name: Check typed metrics contract + if: steps.release_tooling.outputs.ci_only != 'true' run: | cargo run -p crank-metrics --bin crank-metrics-contract -- --check python3 scripts/check-metrics-boundaries.py --root . @@ -142,6 +154,7 @@ jobs: python3 scripts/check-openapi-matrix-compatibility.py --base "$base_ref" - name: Run Crank story ATDD + if: steps.release_tooling.outputs.ci_only != 'true' run: scripts/ci-story-checks.sh ui: name: UI Checks diff --git a/scripts/ci-release-tooling-scope.py b/scripts/ci-release-tooling-scope.py new file mode 100644 index 0000000..e9eb915 --- /dev/null +++ b/scripts/ci-release-tooling-scope.py @@ -0,0 +1,84 @@ +#!/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()) diff --git a/tests/unit/test_ci_release_tooling_scope.py b/tests/unit/test_ci_release_tooling_scope.py new file mode 100644 index 0000000..c6cc0ea --- /dev/null +++ b/tests/unit/test_ci_release_tooling_scope.py @@ -0,0 +1,140 @@ +from __future__ import annotations + +import subprocess +import tempfile +import unittest +import importlib.util +from pathlib import Path + +ROOT = Path(__file__).resolve().parents[2] +SCRIPT = ROOT / "scripts" / "ci-release-tooling-scope.py" +spec = importlib.util.spec_from_file_location("ci_release_tooling_scope", SCRIPT) +assert spec and spec.loader +scope = importlib.util.module_from_spec(spec) +spec.loader.exec_module(scope) + +WORKFLOW = ROOT / ".gitea/workflows/ci.yml" +_FALSE = "false" + + +class ReleaseToolingScopeTests(unittest.TestCase): + def setUp(self): + self.temp = tempfile.TemporaryDirectory() + self.addCleanup(self.temp.cleanup) + self.repo = Path(self.temp.name) + self.git("init", "-q") + self.git("config", "user.email", "ci-scope@example.test") + self.git("config", "user.name", "CI Scope Test") + self.write("src/lib.rs", "pub fn initial() {}\n") + self.write("scripts/scan-images.sh", "#!/bin/sh\nexit 0\n") + self.write("tests/unit/test_scan_images.py", "# initial\n") + self.git("add", ".") + self.git("commit", "-qm", "baseline") + self.base = self.head() + + def git(self, *args: str) -> str: + result = subprocess.run( + ["git", "-C", str(self.repo), *args], + check=True, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + text=True, + ) + return result.stdout.strip() + + def head(self) -> str: + return self.git("rev-parse", "HEAD") + + def write(self, relative: str, content: str) -> None: + path = self.repo / relative + path.parent.mkdir(parents=True, exist_ok=True) + path.write_text(content, encoding="utf-8") + + def commit(self, message: str = "change") -> None: + self.git("add", "-A") + self.git("commit", "-qm", message) + + def classify(self, event="push", before=None, base=None, force=_FALSE) -> bool: + return scope.release_tooling_only( + self.repo, + event, + self.base if before is None else before, + self.base if base is None else base, + force, + ) + + def test_release_only_diff_is_safe_for_push_and_pull_request(self): + self.write(".gitea/workflows/release.yml", "# release tooling update\n") + self.write("scripts/scan-images.sh", "#!/bin/sh\n# scanner update\n") + self.commit() + self.assertTrue(self.classify("push", before=self.base)) + self.assertTrue(self.classify("pull_request", base=self.base)) + + def test_rust_or_mixed_diff_requires_full_ci(self): + self.write("src/lib.rs", "pub fn changed() {}\n") + self.commit("Rust change") + self.assertFalse(self.classify("push", before=self.base)) + + self.write("scripts/scan-images.sh", "#!/bin/sh\n# tooling change\n") + self.write("src/lib.rs", "pub fn changed_again() {}\n") + self.commit("mixed change") + self.assertFalse(self.classify("push", before=self.base)) + + def test_rename_from_rust_into_allowlist_is_not_safe(self): + (self.repo / ".gitea/workflows").mkdir(parents=True) + self.git("mv", "src/lib.rs", ".gitea/workflows/release.yml") + self.commit("rename Rust source into release path") + self.assertFalse(self.classify("push", before=self.base)) + + def test_unavailable_zero_empty_and_missing_bases_require_full_ci(self): + self.write("scripts/scan-images.sh", "#!/bin/sh\n# allowed\n") + self.commit() + self.assertFalse(self.classify("push", before="")) + self.assertFalse(self.classify("push", before="0" * 40)) + self.assertFalse(self.classify("push", before="1" * 40)) + self.assertFalse(self.classify("push", before=self.head())) + self.assertFalse(self.classify("pull_request", base="")) + + def test_manual_feature_and_unknown_force_metadata_require_full_ci(self): + self.write("scripts/scan-images.sh", "#!/bin/sh\n# allowed\n") + self.commit() + self.assertFalse(self.classify("workflow_dispatch", before=self.base)) + self.assertFalse(self.classify("push", before=self.base, force="true")) + self.assertFalse(self.classify("push", before=self.base, force="")) + + def test_non_allowlisted_ci_self_change_requires_full_ci(self): + self.write(".gitea/workflows/ci.yml", "# detector workflow change\n") + self.commit() + self.assertFalse(self.classify("push", before=self.base)) + + +class WorkflowScopeTests(unittest.TestCase): + def test_only_four_heavy_rust_steps_are_skippable(self): + workflow = WORKFLOW.read_text(encoding="utf-8") + self.assertIn("run: python3 scripts/ci-release-tooling-scope.py", workflow) + self.assertIn("FORCE_FULL: ${{ inputs.validation_level == 'feature' || contains(gitea.event.pull_request.labels.*.name, 'feature-ready') }}", workflow) + conditional = "if: steps.release_tooling.outputs.ci_only != 'true'" + for name in ( + "Check typed runtime configuration contract", + "Check canonical migration contract", + "Check typed metrics contract", + "Run Crank story ATDD", + ): + start = workflow.index(f"- name: {name}") + self.assertIn(conditional, workflow[start : start + 180]) + for name in ( + "Install Rust toolchain", + "Run tooling unit tests", + "Check formatting", + "Check Rust boundaries", + "Check OpenAPI matrix and corpus compatibility versions", + ): + start = workflow.index(f"- name: {name}") + self.assertNotIn("if:", workflow[start : start + 160]) + self.assertIn("name: Rust Checks", workflow) + self.assertIn("name: UI Checks", workflow) + self.assertIn("name: Required Checks", workflow) + + +if __name__ == "__main__": + unittest.main()