|
| 1 | +"""Semver gate: require a major version bump when an existing pinned |
| 2 | +golden output is modified or deleted. |
| 3 | +
|
| 4 | +Usage (CI): |
| 5 | + python tests/golden/check_semver_bump.py --base origin/develop --head HEAD |
| 6 | +""" |
| 7 | + |
| 8 | +from __future__ import annotations |
| 9 | + |
| 10 | +import argparse |
| 11 | +import re |
| 12 | +import subprocess |
| 13 | +import sys |
| 14 | +from dataclasses import dataclass |
| 15 | +from typing import List, Optional, Tuple |
| 16 | + |
| 17 | +EXPECTED_GLOB = re.compile(r"^tests/golden/templates/.+/expected\.(build|package)\.yaml$") |
| 18 | + |
| 19 | + |
| 20 | +@dataclass(frozen=True) |
| 21 | +class Change: |
| 22 | + path: str |
| 23 | + status: str # "A" added, "M" modified, "D" deleted |
| 24 | + |
| 25 | + |
| 26 | +def _parse_version(s: str) -> Tuple[int, int, int]: |
| 27 | + m = re.fullmatch(r"(\d+)\.(\d+)\.(\d+)(?:.*)?", s.strip()) |
| 28 | + if not m: |
| 29 | + raise ValueError(f"unparseable version: {s!r}") |
| 30 | + return int(m.group(1)), int(m.group(2)), int(m.group(3)) |
| 31 | + |
| 32 | + |
| 33 | +def _is_corpus_pin(path: str) -> bool: |
| 34 | + return bool(EXPECTED_GLOB.match(path)) |
| 35 | + |
| 36 | + |
| 37 | +def check( |
| 38 | + changed: List[Change], |
| 39 | + base_version: str, |
| 40 | + head_version: str, |
| 41 | +) -> Tuple[int, str]: |
| 42 | + """Return (exit_code, message).""" |
| 43 | + relevant = [c for c in changed if _is_corpus_pin(c.path)] |
| 44 | + |
| 45 | + if not relevant: |
| 46 | + return 0, "No corpus pin changes; gate passes." |
| 47 | + |
| 48 | + modifications = [c for c in relevant if c.status == "M"] |
| 49 | + deletions = [c for c in relevant if c.status == "D"] |
| 50 | + additions = [c for c in relevant if c.status == "A"] |
| 51 | + |
| 52 | + if not (modifications or deletions): |
| 53 | + # Additions only — no bump required. |
| 54 | + return 0, f"{len(additions)} new pin(s); no version bump required." |
| 55 | + |
| 56 | + base_major, _, _ = _parse_version(base_version) |
| 57 | + head_major, _, _ = _parse_version(head_version) |
| 58 | + |
| 59 | + if head_major > base_major: |
| 60 | + return 0, "Major version bumped; gate passes." |
| 61 | + |
| 62 | + suggested = f"{base_major + 1}.0.0" |
| 63 | + summary_lines = [ |
| 64 | + "Semver gate FAILED.", |
| 65 | + f" base version: {base_version}", |
| 66 | + f" head version: {head_version}", |
| 67 | + f" required: major bump (suggested {suggested})", |
| 68 | + "", |
| 69 | + "Reason: an existing pinned golden output was modified or deleted.", |
| 70 | + " Modifications:", |
| 71 | + *[f" M {c.path}" for c in modifications], |
| 72 | + " Deletions:", |
| 73 | + *[f" D {c.path}" for c in deletions], |
| 74 | + "", |
| 75 | + f'To fix: edit samcli/__init__.py and set __version__ = "{suggested}".', |
| 76 | + "If the change is intentional, the major bump signals that to consumers.", |
| 77 | + "If unintentional, run python tests/golden/update_goldens.py --diff to inspect.", |
| 78 | + ] |
| 79 | + return 1, "\n".join(summary_lines) |
| 80 | + |
| 81 | + |
| 82 | +def _read_version_at_ref(ref: str) -> str: |
| 83 | + """Read __version__ from samcli/__init__.py at a git ref.""" |
| 84 | + out = subprocess.check_output( |
| 85 | + ["git", "show", f"{ref}:samcli/__init__.py"], text=True |
| 86 | + ) |
| 87 | + m = re.search(r'__version__\s*=\s*"([^"]+)"', out) |
| 88 | + if not m: |
| 89 | + raise RuntimeError(f"cannot find __version__ at {ref}") |
| 90 | + return m.group(1) |
| 91 | + |
| 92 | + |
| 93 | +def _git_changed_files(base: str, head: str) -> List[Change]: |
| 94 | + """Return all files changed between base and head with their git status.""" |
| 95 | + out = subprocess.check_output( |
| 96 | + ["git", "diff", "--name-status", f"{base}...{head}"], text=True |
| 97 | + ) |
| 98 | + changes: List[Change] = [] |
| 99 | + for line in out.splitlines(): |
| 100 | + if not line.strip(): |
| 101 | + continue |
| 102 | + parts = line.split("\t") |
| 103 | + status, path = parts[0], parts[-1] |
| 104 | + # Renames present as "R<score>\tOLD\tNEW" — split into D + A for our purposes. |
| 105 | + if status.startswith("R") and len(parts) == 3: |
| 106 | + old, new = parts[1], parts[2] |
| 107 | + changes.append(Change(old, "D")) |
| 108 | + changes.append(Change(new, "A")) |
| 109 | + else: |
| 110 | + changes.append(Change(path, status[0])) |
| 111 | + return changes |
| 112 | + |
| 113 | + |
| 114 | +def main(argv: Optional[List[str]] = None) -> int: |
| 115 | + parser = argparse.ArgumentParser() |
| 116 | + parser.add_argument("--base", required=True, help="git ref of merge base / target branch") |
| 117 | + parser.add_argument("--head", required=True, help="git ref of PR head") |
| 118 | + args = parser.parse_args(argv) |
| 119 | + |
| 120 | + changed = _git_changed_files(args.base, args.head) |
| 121 | + base_version = _read_version_at_ref(args.base) |
| 122 | + head_version = _read_version_at_ref(args.head) |
| 123 | + rc, msg = check(changed, base_version, head_version) |
| 124 | + print(msg) |
| 125 | + return rc |
| 126 | + |
| 127 | + |
| 128 | +if __name__ == "__main__": |
| 129 | + sys.exit(main()) |
0 commit comments