File size: 5,984 Bytes
21ff762
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3f657c8
 
 
 
21ff762
 
 
 
3f657c8
 
 
02daf08
21ff762
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78d8c9d
 
 
 
21ff762
 
 
 
 
 
 
 
 
 
 
 
3f657c8
 
 
 
21ff762
 
 
 
 
3f657c8
 
 
 
02daf08
 
 
 
3f657c8
 
21ff762
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
"""Enforce that product/CI contract changes include test changes."""

from __future__ import annotations

import argparse
import json
import re
import subprocess
from dataclasses import dataclass
from typing import Iterable

RELEASE_METADATA_FILES = {
    "CHANGELOG.md",
    "pyproject.toml",
    "src/__init__.py",
    "src/ctx/__init__.py",
}
RELEASE_GENERATED_STATS_FILES = {
    "README.md",
    "docs/index.md",
}
VERSION_LINE_RE = re.compile(r'version = "\d+\.\d+\.\d+(?:[-+._a-zA-Z0-9]*)?"')
INIT_VERSION_LINE_RE = re.compile(
    r'__version__ = "\d+\.\d+\.\d+(?:[-+._a-zA-Z0-9]*)?"'
)
TEST_COUNT_STATS_RE = re.compile(
    r".*(Tests-\d+_collected|[\d,]+ tests collected).*"
)
RELEASE_DOCS_LINE_RE = re.compile(r"\*\*v\d+\.\d+\.\d+(?:[-+._a-zA-Z0-9]*)?\*\*.*")


@dataclass(frozen=True)
class PolicyResult:
    passed: bool
    message: str
    contract_files: tuple[str, ...] = ()
    test_files: tuple[str, ...] = ()


def is_contract_file(path: str) -> bool:
    return (
        (path.startswith("src/") and path.endswith((".py", ".json")))
        or path.startswith("scripts/ci_")
        or path == "scripts/clean_host_contract.py"
        or path == "pyproject.toml"
        or (
            path.startswith(".github/workflows/")
            and path.endswith((".yml", ".yaml"))
        )
    ) and not path.startswith("src/tests/")


def is_test_file(path: str) -> bool:
    return path.startswith("src/tests/") and path.endswith((".py", ".json"))


def is_release_metadata_only(

    changed_files: Iterable[str],

    diffs_by_file: dict[str, str],

) -> bool:
    files = tuple(path.strip().replace("\\", "/") for path in changed_files if path)
    allowed_files = RELEASE_METADATA_FILES | RELEASE_GENERATED_STATS_FILES
    if not files or any(path not in allowed_files for path in files):
        return False
    if not any(path in RELEASE_METADATA_FILES for path in files):
        return False

    for path in files:
        if path == "CHANGELOG.md":
            continue
        if path in RELEASE_GENERATED_STATS_FILES:
            for line in diffs_by_file.get(path, "").splitlines():
                if not line.startswith(("+", "-")) or line.startswith(("+++", "---")):
                    continue
                text = line[1:].strip()
                if not TEST_COUNT_STATS_RE.fullmatch(text) and not (
                    path == "docs/index.md" and RELEASE_DOCS_LINE_RE.fullmatch(text)
                ):
                    return False
            continue
        expected = VERSION_LINE_RE if path == "pyproject.toml" else INIT_VERSION_LINE_RE
        for line in diffs_by_file.get(path, "").splitlines():
            if not line.startswith(("+", "-")) or line.startswith(("+++", "---")):
                continue
            if not expected.fullmatch(line[1:].strip()):
                return False
    return True


def evaluate_policy(

    changed_files: Iterable[str],

    labels: Iterable[str],

    diffs_by_file: dict[str, str],

) -> PolicyResult:
    files = tuple(path.strip().replace("\\", "/") for path in changed_files if path)
    contract = tuple(path for path in files if is_contract_file(path))
    tests = tuple(path for path in files if is_test_file(path))
    if not contract:
        return PolicyResult(True, "No product or CI/package contract changes.")
    if tests:
        return PolicyResult(True, "Policy satisfied.", contract, tests)
    if "no-tests-needed" in set(labels):
        return PolicyResult(True, "Policy exempted by no-tests-needed label.", contract)
    if is_release_metadata_only(files, diffs_by_file):
        return PolicyResult(True, "Policy exempted for release metadata-only changes.", contract)
    return PolicyResult(False, "Contract files changed without accompanying tests.", contract)


def _git_lines(*args: str) -> tuple[str, ...]:
    proc = subprocess.run(
        ["git", *args],
        check=True,
        text=True,
        stdout=subprocess.PIPE,
    )
    return tuple(line.strip() for line in proc.stdout.splitlines() if line.strip())


def _git_text(*args: str) -> str:
    proc = subprocess.run(
        ["git", *args],
        check=True,
        text=True,
        stdout=subprocess.PIPE,
    )
    return proc.stdout


def _changed_files(base: str, head: str) -> tuple[str, ...]:
    return _git_lines("diff", "--name-only", base, head)


def _diffs_by_file(base: str, head: str, files: Iterable[str]) -> dict[str, str]:
    return {
        path: _git_text("diff", "--unified=0", base, head, "--", path)
        for path in files
    }


def _parse_labels(raw: str) -> tuple[str, ...]:
    try:
        labels = json.loads(raw)
    except json.JSONDecodeError:
        return ()
    if not isinstance(labels, list):
        return ()
    return tuple(label for label in labels if isinstance(label, str))


def main(argv: list[str] | None = None) -> int:
    parser = argparse.ArgumentParser()
    parser.add_argument("--base", required=True)
    parser.add_argument("--head", required=True)
    parser.add_argument("--labels-json", default="[]")
    args = parser.parse_args(argv)

    files = _changed_files(args.base, args.head)
    result = evaluate_policy(
        files,
        _parse_labels(args.labels_json),
        _diffs_by_file(args.base, args.head, files),
    )
    print(result.message)
    if result.contract_files:
        print("Contract files:")
        print("\n".join(result.contract_files))
    if result.test_files:
        print("Test files:")
        print("\n".join(result.test_files))
    if not result.passed:
        print("::error::Policy violation - contract files changed but no tests changed.")
        print("Fix: add/update tests, or use release metadata-only changes.")
        return 1
    return 0


if __name__ == "__main__":
    raise SystemExit(main())