| #!/usr/bin/env python3 | |
| # | |
| # 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. | |
| """Capture advisory upgrade evidence in CI; never upgrade or accept a baseline.""" | |
| import argparse | |
| import json | |
| import os | |
| from pathlib import Path | |
| import re | |
| import subprocess | |
| import sys | |
| import tempfile | |
| import update_oink as upgrade | |
| from oink_module import MODULE, VERSION | |
| from site_toolchain import load_toolchain | |
| from versioning import require_no_symlinked_output_components, require_output_outside_git_checkouts | |
| def git(root, *args): | |
| return subprocess.check_output(["git", *args], cwd=root, text=True).strip() | |
| def pin_at(root, sha): | |
| if not re.fullmatch(r"[0-9a-f]{40}", sha): | |
| raise ValueError("evidence refs must be immutable commit SHAs") | |
| source = git(root, "show", f"{sha}:go.mod") | |
| pins = re.findall(rf"(?m)^\s*(?:require\s+)?{re.escape(MODULE)}\s+({VERSION.pattern})(?:\s+//[^\n]*)?\s*$", source) | |
| if len(pins) != 1: | |
| raise ValueError(f"cannot identify one explicit OINK pin at {sha}") | |
| return pins[0] | |
| def comparison(root, pr_base): | |
| """Use the nearest pin transition, ignoring later fixes and Go-only edits.""" | |
| if not re.fullmatch(r"[0-9a-f]{40}", pr_base): | |
| raise ValueError("evidence refs must be immutable commit SHAs") | |
| head = git(root, "rev-parse", "HEAD") | |
| current = pin_at(root, head) | |
| merge_base = git(root, "merge-base", pr_base, head) | |
| if pin_at(root, merge_base) == current: | |
| return None | |
| for commit in git(root, "rev-list", "--first-parent", f"{merge_base}..{head}", "--", "go.mod").splitlines(): | |
| parents = git(root, "rev-list", "--parents", "-n", "1", commit).split()[1:] | |
| if not parents: | |
| break | |
| previous = pin_at(root, parents[0]) | |
| if previous != current: | |
| return {"baseHead": parents[0], "head": head, "from": previous, "to": current} | |
| raise ValueError("theme pin changed but its previous commit is unavailable; fetch complete history") | |
| def require_committed_sources(expected_head): | |
| # Git ignores do not govern Hugo inputs (e.g. static/resources/). Hugo skips | |
| # Finder metadata in content, but copies it from static like any other file. | |
| # Downloaded CI artifacts live outside these input roots. | |
| # NUL records preserve Unicode, newlines and whitespace without Git quoting. | |
| paths = subprocess.check_output(["git", "ls-files", "-z", "--others", "--", | |
| "content", "static", "assets", "layouts", "data", "i18n", "config", "_vendor"], | |
| cwd=upgrade.ROOT) | |
| untracked = [name for name in os.fsdecode(paths).split("\0") | |
| if name and not (name.startswith("content/") and Path(name).name == ".DS_Store")] | |
| if git(upgrade.ROOT, "rev-parse", "HEAD") != expected_head or git(upgrade.ROOT, "diff", "--name-only", "HEAD") or untracked: | |
| raise ValueError("capture requires committed sources at the recorded head; keep the checkout unchanged until capture finishes") | |
| def capture(work, selected): | |
| if work.exists() and any(work.iterdir()): | |
| raise ValueError("use an empty evidence directory; existing measurements are never overwritten") | |
| work.mkdir(parents=True, exist_ok=True) | |
| upgrade.write_report(work, {**selected, "mode": "ci-evidence-only", "status": "capturing", | |
| "scope": "Paired latest-only screenshots and measurements; not full acceptance."}) | |
| upgrade.ACTIVE_WORK = work | |
| try: | |
| with upgrade.isolated_release(): | |
| before = load_toolchain(upgrade.ROOT, ref=selected["baseHead"]) | |
| current = load_toolchain(upgrade.ROOT) | |
| if before["go"] != current["go"]: | |
| raise ValueError("baseline needs Go " + before["go"] + "; current needs Go " + current["go"] + | |
| "; paired evidence requires matching Go pins; cross-Go capture is not supported") | |
| if before["hugo"] != current["hugo"] and not os.environ.get("BEFORE_HUGO_BIN"): | |
| raise ValueError("baseline needs Hugo " + before["hugo"] + "; install it and set BEFORE_HUGO_BIN; " | |
| "the current Hugo is not assumed compatible with old content") | |
| if os.environ.get("BEFORE_HUGO_BIN"): | |
| actual = upgrade.tool_output([os.environ["BEFORE_HUGO_BIN"], "version"]) | |
| if upgrade.version_tuple(actual) != upgrade.version_tuple(before["hugo"]): | |
| raise ValueError("BEFORE_HUGO_BIN must match the baseline's toolchain.json") | |
| versions = upgrade.check_toolchain(upgrade.ROOT, selected["to"]) | |
| (work / "toolchain.json").write_text(json.dumps(versions, indent=2) + "\n") | |
| upgrade.capture_before(work) | |
| require_committed_sources(selected["head"]) | |
| upgrade.capture_after(work) | |
| require_committed_sources(selected["head"]) | |
| upgrade.update_report(work, status="ci-evidence-only", performance=upgrade.performance_comparison(work), | |
| acceptance="Advisory evidence only; blocking build/E2E and ASF Staging remain separate.") | |
| except (ValueError, OSError, subprocess.CalledProcessError) as error: | |
| upgrade.update_report(work, status="failed", error=str(error)) | |
| raise | |
| finally: | |
| upgrade.ACTIVE_WORK = None | |
| upgrade.BROWSER_DEPENDENCIES = None | |
| def main(): | |
| parser = argparse.ArgumentParser(description=__doc__) | |
| parser.add_argument("--pr-base", required=True, help="immutable PR base SHA") | |
| parser.add_argument("--plan", action="store_true", help="emit the changed flag for conditional CI setup") | |
| parser.add_argument("--output", type=Path) | |
| args = parser.parse_args() | |
| selected = comparison(upgrade.ROOT, args.pr_base) | |
| if args.plan: | |
| print("changed=" + ("true" if selected else "false")) | |
| return | |
| if not args.output: | |
| parser.error("--output is required for capture") | |
| if selected is None: | |
| print("OINK pin unchanged; no paired upgrade capture needed") | |
| return | |
| raw = args.output.expanduser() | |
| try: | |
| require_no_symlinked_output_components(raw, "--output", {Path(tempfile.gettempdir()), Path("/tmp")}) | |
| output = raw.resolve() | |
| require_output_outside_git_checkouts(output, "--output") | |
| except SystemExit as error: | |
| raise ValueError(str(error)) from None | |
| require_committed_sources(selected["head"]) | |
| capture(output, selected) | |
| if __name__ == "__main__": | |
| try: | |
| main() | |
| except (ValueError, OSError, subprocess.CalledProcessError) as error: | |
| sys.exit(f"OINK evidence incomplete: {error}") |