From eb39f0f45cf162f4fe74777c3024e511f84008f3 Mon Sep 17 00:00:00 2001 From: okxlin <61420215+okxlin@users.noreply.github.com> Date: Fri, 6 Nov 2026 19:44:00 +0800 Subject: [PATCH] Fix Renovate app-version automation gates --- .../scripts/renovate_app_version_sources.py | 2 + .github/scripts/renovate_automerge_shape.py | 249 ++++++++++++++ .github/scripts/test_renovate_app_version.py | 96 +++++- .../scripts/test_renovate_automerge_shape.py | 139 ++++++++ .github/workflows/renovate-app-version.yml | 2 +- .github/workflows/renovate-automerge.yml | 306 +++++++----------- 6 files changed, 590 insertions(+), 204 deletions(-) create mode 100644 .github/scripts/renovate_automerge_shape.py create mode 100644 .github/scripts/test_renovate_automerge_shape.py diff --git a/.github/scripts/renovate_app_version_sources.py b/.github/scripts/renovate_app_version_sources.py index 46f9912cf..3f211a6ba 100644 --- a/.github/scripts/renovate_app_version_sources.py +++ b/.github/scripts/renovate_app_version_sources.py @@ -114,6 +114,8 @@ def select_sources( raise ValueError("base and head refs must be full Git commit SHAs") grouped: dict[str, list[str]] = {} for raw_path in paths: + if not raw_path.endswith("/docker-compose.yml"): + continue app, version = parse_compose_path(raw_path) if version in ROLLING_VERSIONS or not git_path_exists(base_ref, raw_path): continue diff --git a/.github/scripts/renovate_automerge_shape.py b/.github/scripts/renovate_automerge_shape.py new file mode 100644 index 000000000..f3fbd014e --- /dev/null +++ b/.github/scripts/renovate_automerge_shape.py @@ -0,0 +1,249 @@ +#!/usr/bin/env python3 +from __future__ import annotations + +import argparse +import copy +import json +import re +import subprocess +import sys +from pathlib import Path +from typing import Any + +import yaml + + +class GateReject(Exception): + pass + + +def run_git(*args: str) -> subprocess.CompletedProcess[str]: + return subprocess.run( + ["git", *args], check=False, text=True, capture_output=True + ) + + +def tree_files(ref: str, directory: str) -> dict[str, tuple[str, str]]: + result = run_git("ls-tree", "-r", "-z", ref, "--", directory) + if result.returncode != 0: + raise RuntimeError(result.stderr.strip() or f"unable to read tree {ref}:{directory}") + prefix = directory.rstrip("/") + "/" + files: dict[str, tuple[str, str]] = {} + for entry in result.stdout.split("\0"): + if not entry: + continue + metadata, path = entry.split("\t", 1) + mode, object_type, oid = metadata.split(" ", 2) + if object_type == "blob" and path.startswith(prefix): + files[path[len(prefix) :]] = (mode, oid) + return files + + +def git_show(ref: str, path: str) -> str: + result = run_git("show", f"{ref}:{path}") + if result.returncode != 0: + raise GateReject(f"cannot read Compose file {ref}:{path}") + return result.stdout + + +def read_compose(path: str, content: str) -> tuple[dict[str, Any], dict[str, Any]]: + try: + data = yaml.safe_load(content) or {} + except yaml.YAMLError as exc: + raise GateReject(f"invalid Compose YAML: {path}: {exc}") from exc + if not isinstance(data, dict): + raise GateReject(f"invalid Compose mapping: {path}") + services = data.get("services") + if not isinstance(services, dict) or len(services) != 1: + raise GateReject(f"Compose must contain exactly one service: {path}") + return data, services + + +def digest_reference(image: object) -> tuple[str, str] | None: + if not isinstance(image, str): + return None + match = re.fullmatch(r"([^@\s]+)@sha256:([0-9a-fA-F]{64})", image) + if match is None: + return None + return match.group(1), match.group(2).lower() + + +def verify_compose_pair( + old_path: str, old_raw: str, new_path: str, new_raw: str +) -> bool: + old_data, old_services = read_compose(old_path, old_raw) + new_data, new_services = read_compose(new_path, new_raw) + old_service = next(iter(old_services.values())) + new_service = next(iter(new_services.values())) + if not isinstance(old_service, dict) or not isinstance(new_service, dict): + raise GateReject(f"Compose service must be a mapping: {new_path}") + old_image = old_service.get("image") + new_image = new_service.get("image") + if not isinstance(old_image, str) or not isinstance(new_image, str) or old_image == new_image: + raise GateReject(f"Compose service image must change: {new_path}") + + old_normalized = copy.deepcopy(old_data) + new_normalized = copy.deepcopy(new_data) + old_normalized.pop("version", None) + new_normalized.pop("version", None) + for service in old_normalized["services"].values(): + if isinstance(service, dict): + service.pop("image", None) + for service in new_normalized["services"].values(): + if isinstance(service, dict): + service.pop("image", None) + if old_normalized != new_normalized: + raise GateReject(f"Compose changed beyond its service image: {new_path}") + + old_digest = digest_reference(old_image) + new_digest = digest_reference(new_image) + return bool( + old_digest is not None + and new_digest is not None + and old_digest[0] == new_digest[0] + and old_digest[1] != new_digest[1] + ) + + +def app_names(files: list[dict[str, Any]]) -> set[str]: + apps: set[str] = set() + for item in files: + for path in (item.get("filename"), item.get("previous_filename")): + if not path: + continue + match = re.fullmatch(r"apps/([^/]+)/.+", path) + if match is None: + raise GateReject(f"changed file outside apps/: {path}") + apps.add(match.group(1)) + if len(apps) != 1: + raise GateReject(f"PR must affect exactly one app, got {', '.join(sorted(apps))}") + return apps + + +def verify_added_version_package( + base_sha: str, head_sha: str, app: str, files: list[dict[str, Any]] +) -> bool: + target_dirs: set[str] = set() + changed_paths: set[str] = set() + for item in files: + path = item.get("filename", "") + parts = path.split("/") + if ( + item.get("status") != "added" + or item.get("previous_filename") + or len(parts) < 4 + or parts[0] != "apps" + or parts[1] != app + or any(part in {"", ".", ".."} for part in parts) + ): + raise GateReject("new version packages must contain only added files under one app directory") + target_dirs.add("/".join(parts[:3])) + changed_paths.add(path) + if len(target_dirs) != 1: + raise GateReject("PR must add exactly one version directory") + target_dir = next(iter(target_dirs)) + target_tree = tree_files(head_sha, target_dir) + if not target_tree or {f"{target_dir}/{path}" for path in target_tree} != changed_paths: + raise GateReject("added version directory must exactly match the PR file list") + compose_path = f"{target_dir}/docker-compose.yml" + if "docker-compose.yml" not in target_tree: + raise GateReject("added version directory must include docker-compose.yml") + + base_app = tree_files(base_sha, f"apps/{app}") + source_versions = sorted( + path.split("/", 1)[0] + for path in base_app + if path.count("/") == 1 and path.endswith("/docker-compose.yml") + ) + for source_version in source_versions: + source_dir = f"apps/{app}/{source_version}" + source_tree = tree_files(base_sha, source_dir) + if set(source_tree) != set(target_tree): + continue + if any(source_tree[path][0] != target_tree[path][0] for path in source_tree): + continue + if any( + source_tree[path][1] != target_tree[path][1] + for path in source_tree + if path != "docker-compose.yml" + ): + continue + old_compose = git_show(base_sha, f"{source_dir}/docker-compose.yml") + new_compose = git_show(head_sha, compose_path) + verify_compose_pair( + f"{source_dir}/docker-compose.yml", old_compose, compose_path, new_compose + ) + print( + f"verified added version package {target_dir} as an image-only copy of {source_dir}" + ) + return False + raise GateReject("added version directory does not exactly copy a base package except for image") + + +def verify_existing_compose_updates( + base_sha: str, head_sha: str, app: str, files: list[dict[str, Any]] +) -> bool: + compose_files: list[tuple[str, str]] = [] + for item in files: + filename = item.get("filename", "") + previous = item.get("previous_filename") + if not filename.endswith("/docker-compose.yml") and not ( + previous and previous.endswith("/docker-compose.yml") + ): + raise GateReject(f"whitelisted PR contains a non-Compose file: {filename}") + compose_files.append((previous or filename, filename)) + if not compose_files: + raise GateReject("PR must change at least one docker-compose.yml") + + digest_only = True + for old_path, new_path in compose_files: + old_match = re.fullmatch(r"apps/([^/]+)/.+/docker-compose\.yml", old_path) + new_match = re.fullmatch(r"apps/([^/]+)/.+/docker-compose\.yml", new_path) + if not old_match or not new_match or old_match.group(1) != app or new_match.group(1) != app: + raise GateReject(f"invalid Compose path for app {app}: {new_path}") + old_raw = git_show(base_sha, old_path) + new_raw = git_show(head_sha, new_path) + if not verify_compose_pair(old_path, old_raw, new_path, new_raw): + digest_only = False + print(f"verified {new_path}: image-only update") + return digest_only + + +def verify_shape(base_sha: str, head_sha: str, files: list[dict[str, Any]]) -> dict[str, Any]: + if not files: + raise GateReject("PR does not change files") + apps = app_names(files) + app = next(iter(apps)) + if all(item.get("status") == "added" for item in files): + digest_only = verify_added_version_package(base_sha, head_sha, app, files) + return {"app": app, "digest_only": digest_only, "kind": "added-version-package"} + digest_only = verify_existing_compose_updates(base_sha, head_sha, app, files) + return {"app": app, "digest_only": digest_only, "kind": "compose-update"} + + +def main() -> int: + parser = argparse.ArgumentParser(description="Verify a whitelist Renovate PR image-only shape.") + parser.add_argument("--base-sha", required=True) + parser.add_argument("--head-sha", required=True) + parser.add_argument("--files-json", required=True, type=Path) + parser.add_argument("--output-json", required=True, type=Path) + args = parser.parse_args() + if not all(re.fullmatch(r"[0-9a-fA-F]{40,64}", sha) for sha in (args.base_sha, args.head_sha)): + raise ValueError("base and head must be full Git commit SHAs") + files = json.loads(args.files_json.read_text(encoding="utf-8")) + if not isinstance(files, list) or any(not isinstance(item, dict) for item in files): + raise ValueError("PR file list must be a JSON array of objects") + result = verify_shape(args.base_sha, args.head_sha, files) + args.output_json.parent.mkdir(parents=True, exist_ok=True) + args.output_json.write_text(json.dumps(result, ensure_ascii=False, indent=2) + "\n", encoding="utf-8") + print(f"single-app image-only update verified: {result['app']} ({result['kind']})") + print(f"digest_only={'true' if result['digest_only'] else 'false'}") + return 0 + + +if __name__ == "__main__": + try: + raise SystemExit(main()) + except GateReject as exc: + print(str(exc), file=sys.stderr) + raise SystemExit(42) diff --git a/.github/scripts/test_renovate_app_version.py b/.github/scripts/test_renovate_app_version.py index 3f6a751c0..f6a100093 100644 --- a/.github/scripts/test_renovate_app_version.py +++ b/.github/scripts/test_renovate_app_version.py @@ -266,6 +266,74 @@ class RenovateAppVersionTests(unittest.TestCase): output_file.read_bytes().decode("utf-8").rstrip("\0").split("\0"), ) + def test_source_selector_ignores_base_only_non_compose_paths(self): + with tempfile.TemporaryDirectory(prefix="renovate-source-base-only-") as tmp: + repo = pathlib.Path(tmp) + app_compose = repo / "apps" / "demo" / "1.0.0" / "docker-compose.yml" + app_compose.parent.mkdir(parents=True) + app_compose.write_text( + "services:\n app:\n image: example/demo:1.0.0\n", + encoding="utf-8", + ) + subprocess.run(["git", "init", "-q"], cwd=repo, check=True) + subprocess.run(["git", "config", "user.email", "test@example.com"], cwd=repo, check=True) + subprocess.run(["git", "config", "user.name", "Test User"], cwd=repo, check=True) + subprocess.run(["git", "add", "apps"], cwd=repo, check=True) + subprocess.run(["git", "commit", "-qm", "add demo source"], cwd=repo, check=True) + common_ref = subprocess.run( + ["git", "rev-parse", "HEAD"], cwd=repo, check=True, text=True, capture_output=True + ).stdout.strip() + + unrelated = repo / "apps" / "gomodel" / "0.2.2" / ".env.sample" + unrelated.parent.mkdir(parents=True) + unrelated.write_text("EXAMPLE=value\n", encoding="utf-8") + subprocess.run(["git", "add", "apps/gomodel"], cwd=repo, check=True) + subprocess.run(["git", "commit", "-qm", "add base-only package"], cwd=repo, check=True) + base_ref = subprocess.run( + ["git", "rev-parse", "HEAD"], cwd=repo, check=True, text=True, capture_output=True + ).stdout.strip() + + subprocess.run(["git", "checkout", "-q", "-b", "renovate", common_ref], cwd=repo, check=True) + app_compose.write_text( + "services:\n app:\n image: example/demo:1.1.0\n", + encoding="utf-8", + ) + subprocess.run(["git", "add", "apps/demo"], cwd=repo, check=True) + subprocess.run(["git", "commit", "-qm", "update demo image"], cwd=repo, check=True) + head_ref = subprocess.run( + ["git", "rev-parse", "HEAD"], cwd=repo, check=True, text=True, capture_output=True + ).stdout.strip() + + two_tree = subprocess.run( + ["git", "diff", "--name-only", "-z", base_ref, head_ref], + cwd=repo, check=True, capture_output=True, + ).stdout + three_dot = subprocess.run( + ["git", "diff", "--name-only", "-z", f"{base_ref}...{head_ref}"], + cwd=repo, check=True, capture_output=True, + ).stdout + self.assertIn(b"apps/gomodel/0.2.2/.env.sample", two_tree) + self.assertNotIn(b"apps/gomodel/0.2.2/.env.sample", three_dot) + + changed_file_list = repo / "changed-files.zlist" + changed_file_list.write_bytes(two_tree) + policy_file = repo / "policy.json" + policy_file.write_text("{}\n", encoding="utf-8") + output_file = repo / "selected.zlist" + script = REPO_ROOT / ".github" / "scripts" / "renovate_app_version_sources.py" + result = subprocess.run( + [ + sys.executable, str(script), "--base-ref", base_ref, "--head-ref", head_ref, + "--changed-files", str(changed_file_list), "--policy-file", str(policy_file), + "--output-file", str(output_file), + ], cwd=repo, text=True, capture_output=True, check=False, + ) + self.assertEqual(0, result.returncode, result.stderr) + self.assertEqual( + ["apps/demo/1.0.0/docker-compose.yml"], + output_file.read_bytes().decode("utf-8").rstrip("\0").split("\0"), + ) + def test_source_selector_fails_on_ambiguous_newest_version(self): with tempfile.TemporaryDirectory(prefix="renovate-source-ambiguity-") as tmp: repo = pathlib.Path(tmp) @@ -1142,6 +1210,7 @@ class RenovateAppVersionTests(unittest.TestCase): self.assertIn("renovate_app_version_sources.py", app_version) self.assertIn('--head-ref "$(git rev-parse HEAD)"', app_version) self.assertIn("selected-source-files.zlist", app_version) + self.assertIn('git diff --name-only -z "$base_ref...HEAD"', app_version) self.assertIn('git cat-file -e "$BASE_REF:$changed_file"', app_version) self.assertIn('git diff --cached --quiet "$BASE_REF" -- "$source_dir"', app_version) self.assertIn('git restore --source="$BASE_REF" --staged --worktree -- "$changed_dir"', app_version) @@ -1162,16 +1231,31 @@ class RenovateAppVersionTests(unittest.TestCase): workflow = ( REPO_ROOT / ".github" / "workflows" / "renovate-automerge.yml" ).read_text(encoding="utf-8") + shape_helper = ( + REPO_ROOT / ".github" / "scripts" / "renovate_automerge_shape.py" + ).read_text(encoding="utf-8") - self.assertIn("def digest_reference(image):", workflow) + self.assertIn("renovate_automerge_shape.py", workflow) + self.assertIn("Verify Renovate PR identity", workflow) + self.assertIn('.user.login == "renovate[bot]"', workflow) + self.assertIn('.user.login == "app/renovate"', workflow) + self.assertIn('.head.repo.full_name == $repo', workflow) + self.assertIn('(.head.ref | startswith("renovate/"))', workflow) + self.assertIn('(.head.ref | startswith("selfhosted-renovate/"))', workflow) + self.assertIn("--files-json /tmp/renovate-pr-files.json", workflow) + self.assertIn('echo "head_sha=$HEAD_SHA"', workflow) + self.assertIn('--match-head-commit "$VERIFIED_HEAD_SHA"', workflow) + self.assertIn("PR head changed after shape verification", workflow) + self.assertIn('gh api "repos/$REPO/git/ref/heads/localApps"', workflow) + self.assertIn("localApps moved after shape verification", workflow) self.assertIn( 're.fullmatch(r"([^@\\s]+)@sha256:([0-9a-fA-F]{64})", image)', - workflow, + shape_helper, ) - self.assertIn("old_digest[0] != new_digest[0]", workflow) - self.assertIn("old_digest[1] == new_digest[1]", workflow) - self.assertIn("filename_is_compose != previous_is_compose", workflow) - self.assertIn("digest_only=true", workflow) + self.assertIn("old_digest[0] == new_digest[0]", shape_helper) + self.assertIn("old_digest[1] != new_digest[1]", shape_helper) + self.assertIn('echo "digest_only=$digest_only"', workflow) + self.assertIn('"digest_only": digest_only', shape_helper) self.assertIn("steps.shape.outputs.digest_only", workflow) self.assertIn("Digest-only image update; app-version marker is not required", workflow) self.assertIn("Update app version [skip ci]", workflow) diff --git a/.github/scripts/test_renovate_automerge_shape.py b/.github/scripts/test_renovate_automerge_shape.py new file mode 100644 index 000000000..9bf333b04 --- /dev/null +++ b/.github/scripts/test_renovate_automerge_shape.py @@ -0,0 +1,139 @@ +#!/usr/bin/env python3 +from __future__ import annotations + +import json +import pathlib +import shutil +import subprocess +import sys +import tempfile +import unittest + + +SCRIPT = pathlib.Path(__file__).with_name("renovate_automerge_shape.py") + + +def git(repo: pathlib.Path, *args: str) -> str: + result = subprocess.run( + ["git", *args], cwd=repo, check=True, text=True, capture_output=True + ) + return result.stdout.strip() + + +class RenovateAutomergeShapeTests(unittest.TestCase): + def setUp(self): + self.temp = tempfile.TemporaryDirectory(prefix="renovate-automerge-shape-") + self.repo = pathlib.Path(self.temp.name) + subprocess.run(["git", "init", "-q"], cwd=self.repo, check=True) + git(self.repo, "config", "user.email", "test@example.com") + git(self.repo, "config", "user.name", "Test User") + source = self.repo / "apps" / "demo" / "1.0.0" + source.mkdir(parents=True) + (source / "docker-compose.yml").write_text( + "services:\n app:\n image: example/demo:1.0.0\n restart: unless-stopped\n", + encoding="utf-8", + ) + (source / "data.yml").write_text("additionalProperties: {}\n", encoding="utf-8") + (source / ".env.sample").write_text("APP_DATA_DIR=./data\n", encoding="utf-8") + subprocess.run(["git", "add", "apps"], cwd=self.repo, check=True) + subprocess.run(["git", "commit", "-qm", "add source package"], cwd=self.repo, check=True) + self.base_sha = git(self.repo, "rev-parse", "HEAD") + + def tearDown(self): + self.temp.cleanup() + + def run_gate(self, files: list[dict[str, str]]) -> subprocess.CompletedProcess[str]: + head_sha = git(self.repo, "rev-parse", "HEAD") + files_path = self.repo / "pr-files.json" + output_path = self.repo / "shape.json" + files_path.write_text(json.dumps(files), encoding="utf-8") + return subprocess.run( + [ + sys.executable, + str(SCRIPT), + "--base-sha", + self.base_sha, + "--head-sha", + head_sha, + "--files-json", + str(files_path), + "--output-json", + str(output_path), + ], + cwd=self.repo, + text=True, + capture_output=True, + check=False, + ) + + def add_version_copy(self, *, modify_data: bool = False) -> list[dict[str, str]]: + source = self.repo / "apps" / "demo" / "1.0.0" + target = self.repo / "apps" / "demo" / "1.1.0" + shutil.copytree(source, target) + compose = target / "docker-compose.yml" + compose.write_text( + compose.read_text(encoding="utf-8").replace("example/demo:1.0.0", "example/demo:1.1.0"), + encoding="utf-8", + ) + if modify_data: + (target / "data.yml").write_text("additionalProperties:\n unexpected: true\n", encoding="utf-8") + subprocess.run(["git", "add", "apps/demo/1.1.0"], cwd=self.repo, check=True) + subprocess.run(["git", "commit", "-qm", "add target version"], cwd=self.repo, check=True) + return [ + {"filename": path.relative_to(self.repo).as_posix(), "status": "added"} + for path in sorted(target.rglob("*")) + if path.is_file() + ] + + def test_accepts_new_version_that_only_changes_image(self): + files = self.add_version_copy() + result = self.run_gate(files) + self.assertEqual(0, result.returncode, result.stderr) + payload = json.loads((self.repo / "shape.json").read_text(encoding="utf-8")) + self.assertEqual("added-version-package", payload["kind"]) + self.assertFalse(payload["digest_only"]) + + def test_rejects_new_version_with_non_image_package_drift(self): + files = self.add_version_copy(modify_data=True) + result = self.run_gate(files) + self.assertEqual(42, result.returncode, result.stderr) + self.assertIn("does not exactly copy a base package", result.stderr) + + def test_accepts_existing_single_service_image_only_update(self): + compose_path = self.repo / "apps" / "demo" / "1.0.0" / "docker-compose.yml" + compose_path.write_text( + compose_path.read_text(encoding="utf-8").replace("example/demo:1.0.0", "example/demo:1.1.0"), + encoding="utf-8", + ) + subprocess.run(["git", "add", "apps/demo/1.0.0/docker-compose.yml"], cwd=self.repo, check=True) + subprocess.run(["git", "commit", "-qm", "update image"], cwd=self.repo, check=True) + result = self.run_gate( + [{"filename": "apps/demo/1.0.0/docker-compose.yml", "status": "modified"}] + ) + self.assertEqual(0, result.returncode, result.stderr) + payload = json.loads((self.repo / "shape.json").read_text(encoding="utf-8")) + self.assertEqual("compose-update", payload["kind"]) + self.assertFalse(payload["digest_only"]) + + def test_rejects_non_compose_file_in_existing_update(self): + compose_path = self.repo / "apps" / "demo" / "1.0.0" / "docker-compose.yml" + compose_path.write_text( + compose_path.read_text(encoding="utf-8").replace("example/demo:1.0.0", "example/demo:1.1.0"), + encoding="utf-8", + ) + env_path = self.repo / "apps" / "demo" / "1.0.0" / ".env.sample" + env_path.write_text("APP_DATA_DIR=/unexpected\n", encoding="utf-8") + subprocess.run(["git", "add", "apps/demo/1.0.0"], cwd=self.repo, check=True) + subprocess.run(["git", "commit", "-qm", "change compose and env"], cwd=self.repo, check=True) + result = self.run_gate( + [ + {"filename": "apps/demo/1.0.0/docker-compose.yml", "status": "modified"}, + {"filename": "apps/demo/1.0.0/.env.sample", "status": "modified"}, + ] + ) + self.assertEqual(42, result.returncode, result.stderr) + self.assertIn("non-Compose file", result.stderr) + + +if __name__ == "__main__": + unittest.main() diff --git a/.github/workflows/renovate-app-version.yml b/.github/workflows/renovate-app-version.yml index bef50a07d..5a73f2c83 100644 --- a/.github/workflows/renovate-app-version.yml +++ b/.github/workflows/renovate-app-version.yml @@ -58,7 +58,7 @@ jobs: fi git cat-file -e "${base_ref}^{commit}" echo "base_ref=$base_ref" >> "$GITHUB_OUTPUT" - git diff --name-only -z "$base_ref" HEAD > "$RUNNER_TEMP/changed-files.zlist" + git diff --name-only -z "$base_ref...HEAD" > "$RUNNER_TEMP/changed-files.zlist" - name: Checkout trusted version updater uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7 diff --git a/.github/workflows/renovate-automerge.yml b/.github/workflows/renovate-automerge.yml index 4ace10564..1f17b2db1 100644 --- a/.github/workflows/renovate-automerge.yml +++ b/.github/workflows/renovate-automerge.yml @@ -46,14 +46,43 @@ jobs: - name: Checkout repository uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - - name: Verify changed files stay within whitelist - id: whitelist + - name: Verify Renovate PR identity + id: identity env: GH_TOKEN: ${{ github.token }} + PR_NUMBER: ${{ github.event.pull_request.number || inputs.pr_number }} + REPO: ${{ github.repository }} run: | - PR_NUMBER="${{ github.event.pull_request.number || inputs.pr_number }}" - REPO="${{ github.repository }}" + if [[ ! "$PR_NUMBER" =~ ^[0-9]+$ ]]; then + echo "allowed=false" >> "$GITHUB_OUTPUT" + exit 0 + fi + pull_json=$(gh api "repos/$REPO/pulls/$PR_NUMBER") + if ! jq -e --arg repo "$REPO" ' + (.user.login == "renovate[bot]" or .user.login == "app/renovate") and + .state == "open" and + (.draft | not) and + .base.ref == "localApps" and + .base.repo.full_name == $repo and + .head.repo.full_name == $repo and + ((.head.ref | startswith("renovate/")) or + (.head.ref | startswith("selfhosted-renovate/"))) + ' <<< "$pull_json" > /dev/null; then + echo "allowed=false" >> "$GITHUB_OUTPUT" + exit 0 + fi + + echo "allowed=true" >> "$GITHUB_OUTPUT" + + - name: Verify changed files stay within whitelist + id: whitelist + if: steps.identity.outputs.allowed == 'true' + env: + GH_TOKEN: ${{ github.token }} + PR_NUMBER: ${{ github.event.pull_request.number || inputs.pr_number }} + REPO: ${{ github.repository }} + run: | mapfile -t apps < <(sed '/^\s*#/d;/^\s*$/d' .github/renovate-automerge-whitelist.txt) mapfile -t files < <(gh api "repos/$REPO/pulls/$PR_NUMBER/files" --paginate --jq '.[].filename') @@ -92,203 +121,82 @@ jobs: - name: Verify PR is a single-service image-only update id: shape - if: steps.whitelist.outputs.allowed == 'true' + if: steps.identity.outputs.allowed == 'true' && steps.whitelist.outputs.allowed == 'true' env: GH_TOKEN: ${{ github.token }} + PR_NUMBER: ${{ github.event.pull_request.number || inputs.pr_number }} + REPO: ${{ github.repository }} run: | - PR_NUMBER="${{ github.event.pull_request.number || inputs.pr_number }}" - REPO="${{ github.repository }}" - - BASE_SHA=$(gh api "repos/$REPO/pulls/$PR_NUMBER" --jq '.base.sha') + BASE_SHA=$(gh api "repos/$REPO/git/ref/heads/localApps" --jq '.object.sha') HEAD_SHA=$(gh api "repos/$REPO/pulls/$PR_NUMBER" --jq '.head.sha') - export BASE_SHA HEAD_SHA - git fetch --no-tags --depth=1 origin "$BASE_SHA" "$HEAD_SHA" gh api "repos/$REPO/pulls/$PR_NUMBER/files" --paginate > /tmp/renovate-pr-files.json - python3 - <<'PY' + python3 - <<'PYAML' import importlib.util import subprocess import sys if importlib.util.find_spec("yaml") is None: subprocess.check_call([sys.executable, "-m", "pip", "install", "--user", "pyyaml"]) - PY + PYAML set +e - python3 <<'PY' 2> /tmp/renovate-shape.stderr - import json - import os - import re - import subprocess - import sys - import yaml - - base_sha = os.environ["BASE_SHA"] - head_sha = os.environ["HEAD_SHA"] - - class GateReject(Exception): - pass - - def reject(message): - raise GateReject(message) - - def run_git(*args): - return subprocess.run( - ["git", *args], - check=False, - text=True, - stdout=subprocess.PIPE, - stderr=subprocess.PIPE, - ) - - def git_show_first(ref, candidates): - for path in candidates: - if not path: - continue - proc = run_git("show", f"{ref}:{path}") - if proc.returncode == 0: - return path, proc.stdout - return None, "" - - def compose(path, content): - data = yaml.safe_load(content) or {} - if not isinstance(data, dict): - reject(f"invalid compose YAML: {path}") - services = data.get("services") - if not isinstance(services, dict) or len(services) != 1: - reject(f"compose must contain exactly one service: {path}") - return data, services - - def digest_reference(image): - if not isinstance(image, str): - return None - match = re.fullmatch(r"([^@\s]+)@sha256:([0-9a-fA-F]{64})", image) - if match is None: - return None - return match.group(1), match.group(2).lower() - - try: - with open("/tmp/renovate-pr-files.json", encoding="utf-8") as fh: - files = json.load(fh) - - apps = set() - compose_files = [] - digest_only = True - - for file_info in files: - filename = file_info["filename"] - previous = file_info.get("previous_filename") - - for path in [filename, previous]: - if not path: - continue - match = re.match(r"^apps/([^/]+)/", path) - if not match: - reject(f"changed file outside apps/: {path}") - apps.add(match.group(1)) - - filename_is_compose = filename.endswith("/docker-compose.yml") - previous_is_compose = bool(previous and previous.endswith("/docker-compose.yml")) - if filename_is_compose or previous_is_compose: - compose_files.append((previous or filename, filename)) - if previous and filename_is_compose != previous_is_compose: - digest_only = False - else: - digest_only = False - - if len(apps) != 1: - reject(f"PR must affect exactly one app, got {', '.join(sorted(apps))}") - if len(compose_files) < 1: - reject("PR must change at least one docker-compose.yml") - - app_name = next(iter(apps)) - checked = 0 - - for old_path, new_path in compose_files: - old_resolved_path, old_raw = git_show_first(base_sha, [old_path, new_path]) - new_resolved_path, new_raw = git_show_first(head_sha, [new_path, old_path]) - if not old_raw: - reject(f"cannot read base compose: {old_path}") - if not new_raw: - reject(f"cannot read head compose: {new_path}") - - old_data, old_services = compose(old_resolved_path, old_raw) - new_data, new_services = compose(new_resolved_path, new_raw) - - old_image = next(iter(old_services.values())).get("image") - new_image = next(iter(new_services.values())).get("image") - if not old_image or not new_image or old_image == new_image: - reject(f"compose service image must change: {new_resolved_path}") - - old_digest = digest_reference(old_image) - new_digest = digest_reference(new_image) - if ( - old_digest is None - or new_digest is None - or old_digest[0] != new_digest[0] - or old_digest[1] == new_digest[1] - ): - digest_only = False - - old_data.pop("version", None) - new_data.pop("version", None) - for service in old_services.values(): - if isinstance(service, dict): - service.pop("image", None) - for service in new_services.values(): - if isinstance(service, dict): - service.pop("image", None) - - if old_data != new_data: - reject(f"compose changed beyond service image tag: {new_resolved_path}") - - print(f"verified {new_resolved_path}: {old_image} -> {new_image}") - checked += 1 - - print(f"single-service image-only update verified for {app_name} across {checked} compose file(s)") - with open(os.environ["GITHUB_OUTPUT"], "a", encoding="utf-8") as output: - if digest_only: - output.write("digest_only=true\n") - else: - output.write("digest_only=false\n") - except GateReject as exc: - print(str(exc), file=sys.stderr) - sys.exit(42) - PY + python3 "$GITHUB_WORKSPACE/.github/scripts/renovate_automerge_shape.py" \ + --base-sha "$BASE_SHA" \ + --head-sha "$HEAD_SHA" \ + --files-json /tmp/renovate-pr-files.json \ + --output-json /tmp/renovate-shape.json \ + > /tmp/renovate-shape.stdout \ + 2> /tmp/renovate-shape.stderr shape_exit=$? set -e if [ "$shape_exit" -eq 0 ]; then - echo "allowed=true" >> "$GITHUB_OUTPUT" + cat /tmp/renovate-shape.stdout + digest_only=$(awk -F= '/^digest_only=/{print $2}' /tmp/renovate-shape.stdout) + if [[ "$digest_only" != "true" && "$digest_only" != "false" ]]; then + echo "shape verifier did not report digest_only" >&2 + exit 1 + fi + { + echo "allowed=true" + echo "digest_only=$digest_only" + echo "base_sha=$BASE_SHA" + echo "head_sha=$HEAD_SHA" + } >> "$GITHUB_OUTPUT" elif [ "$shape_exit" -eq 42 ]; then cat /tmp/renovate-shape.stderr { echo 'reason<> "$GITHUB_OUTPUT" - echo "allowed=false" >> "$GITHUB_OUTPUT" - exit 0 else cat /tmp/renovate-shape.stderr >&2 || true exit "$shape_exit" fi - name: Add marker label for eligible PR - if: steps.whitelist.outputs.allowed == 'true' && steps.shape.outputs.allowed == 'true' + if: steps.identity.outputs.allowed == 'true' && steps.whitelist.outputs.allowed == 'true' && steps.shape.outputs.allowed == 'true' env: GH_TOKEN: ${{ github.token }} + PR_NUMBER: ${{ github.event.pull_request.number || inputs.pr_number }} + REPO: ${{ github.repository }} run: | - PR_NUMBER="${{ github.event.pull_request.number || inputs.pr_number }}" gh pr edit "$PR_NUMBER" \ - --repo "${{ github.repository }}" \ + --repo "$REPO" \ --add-label renovate-auto - name: Check commits for app-version update marker id: marker - if: steps.whitelist.outputs.allowed == 'true' && steps.shape.outputs.allowed == 'true' + if: steps.identity.outputs.allowed == 'true' && steps.whitelist.outputs.allowed == 'true' && steps.shape.outputs.allowed == 'true' env: GH_TOKEN: ${{ github.token }} + PR_NUMBER: ${{ github.event.pull_request.number || inputs.pr_number }} + REPO: ${{ github.repository }} + VERIFIED_BASE_SHA: ${{ steps.shape.outputs.base_sha }} + VERIFIED_HEAD_SHA: ${{ steps.shape.outputs.head_sha }} run: | if [ "${{ steps.shape.outputs.digest_only }}" = "true" ]; then echo "Digest-only image update; app-version marker is not required" @@ -296,14 +204,19 @@ jobs: exit 0 fi - PR_NUMBER="${{ github.event.pull_request.number || inputs.pr_number }}" - REPO="${{ github.repository }}" - found=false for attempt in 1 2 3 4 5; do PULL_COMMITS=$(gh api "repos/$REPO/pulls/$PR_NUMBER/commits" --paginate --jq '.[].commit.message' || true) - BASE_SHA=$(gh api "repos/$REPO/pulls/$PR_NUMBER" --jq '.base.sha' || true) + BASE_SHA=$(gh api "repos/$REPO/git/ref/heads/localApps" --jq '.object.sha' || true) HEAD_SHA=$(gh api "repos/$REPO/pulls/$PR_NUMBER" --jq '.head.sha' || true) + if [ "$BASE_SHA" != "$VERIFIED_BASE_SHA" ]; then + echo "localApps moved after shape verification; waiting for a fresh gate run." >&2 + exit 1 + fi + if [ "$HEAD_SHA" != "$VERIFIED_HEAD_SHA" ]; then + echo "PR head changed after shape verification; waiting for a fresh gate run." >&2 + exit 1 + fi COMPARE_COMMITS="" if [ -n "$BASE_SHA" ] && [ -n "$HEAD_SHA" ]; then COMPARE_COMMITS=$(gh api "repos/$REPO/compare/$BASE_SHA...$HEAD_SHA" --jq '.commits[].commit.message' || true) @@ -329,42 +242,41 @@ jobs: echo "found=$found" >> "$GITHUB_OUTPUT" - name: Merge PR - if: steps.whitelist.outputs.allowed == 'true' && steps.shape.outputs.allowed == 'true' && steps.marker.outputs.found == 'true' + if: steps.identity.outputs.allowed == 'true' && steps.whitelist.outputs.allowed == 'true' && steps.shape.outputs.allowed == 'true' && steps.marker.outputs.found == 'true' env: GH_TOKEN: ${{ github.token }} + PR_NUMBER: ${{ github.event.pull_request.number || inputs.pr_number }} + REPO: ${{ github.repository }} + VERIFIED_BASE_SHA: ${{ steps.shape.outputs.base_sha }} + VERIFIED_HEAD_SHA: ${{ steps.shape.outputs.head_sha }} run: | - PR_NUMBER="${{ github.event.pull_request.number || inputs.pr_number }}" - REPO="${{ github.repository }}" + LIVE_BASE_SHA=$(gh api "repos/$REPO/git/ref/heads/localApps" --jq '.object.sha') + LIVE_HEAD_SHA=$(gh api "repos/$REPO/pulls/$PR_NUMBER" --jq '.head.sha') + if [ "$LIVE_BASE_SHA" != "$VERIFIED_BASE_SHA" ] || [ "$LIVE_HEAD_SHA" != "$VERIFIED_HEAD_SHA" ]; then + echo "PR or base changed after shape verification; waiting for a fresh gate run." >&2 + exit 1 + fi - for attempt in 1 2 3 4 5; do - echo "== merge attempt $attempt ==" + set +e + MERGE_OUTPUT=$(gh pr merge "$PR_NUMBER" \ + --repo "$REPO" \ + --match-head-commit "$VERIFIED_HEAD_SHA" \ + --merge \ + --delete-branch=false 2>&1) + MERGE_EXIT=$? + set -e - set +e - MERGE_OUTPUT=$(gh pr merge "$PR_NUMBER" \ - --repo "$REPO" \ - --merge \ - --delete-branch=false 2>&1) - MERGE_EXIT=$? - set -e + echo "$MERGE_OUTPUT" + if [ "$MERGE_EXIT" -eq 0 ]; then + exit 0 + fi - echo "$MERGE_OUTPUT" - - if [ "$MERGE_EXIT" -eq 0 ]; then - exit 0 - fi - - PR_STATE=$(gh pr view "$PR_NUMBER" --repo "$REPO" --json state,mergedAt --jq 'if .mergedAt then "MERGED" else .state end' 2>/dev/null || true) - if [ "$PR_STATE" = "MERGED" ] || [ "$PR_STATE" = "CLOSED" ]; then - echo "PR already resolved with state: $PR_STATE" - exit 0 - fi - - if echo "$MERGE_OUTPUT" | grep -Fq "Base branch was modified"; then - if [ "$attempt" -lt 5 ]; then - sleep $((attempt * 5)) - continue - fi - fi - - exit "$MERGE_EXIT" - done + PR_STATE=$(gh pr view "$PR_NUMBER" --repo "$REPO" --json state,mergedAt --jq 'if .mergedAt then "MERGED" else .state end' 2>/dev/null || true) + if [ "$PR_STATE" = "MERGED" ] || [ "$PR_STATE" = "CLOSED" ]; then + echo "PR already resolved with state: $PR_STATE" + exit 0 + fi + if echo "$MERGE_OUTPUT" | grep -Fq "Base branch was modified"; then + echo "localApps moved during merge; a new gate run is required." >&2 + fi + exit "$MERGE_EXIT"