|
| 1 | +#!/usr/bin/env python3 |
| 2 | +import argparse |
| 3 | +import json |
| 4 | +import subprocess |
| 5 | +import sys |
| 6 | +from pathlib import Path |
| 7 | + |
| 8 | + |
| 9 | +VALID_RELEASE_TYPES = {"patch", "minor", "major", "none"} |
| 10 | + |
| 11 | + |
| 12 | +def run(cmd: list[str], cwd: Path) -> str: |
| 13 | + result = subprocess.run( |
| 14 | + cmd, |
| 15 | + cwd=str(cwd), |
| 16 | + check=True, |
| 17 | + capture_output=True, |
| 18 | + text=True, |
| 19 | + ) |
| 20 | + return result.stdout.strip() |
| 21 | + |
| 22 | + |
| 23 | +def find_repo_root(start: Path) -> Path: |
| 24 | + return Path(run(["git", "rev-parse", "--show-toplevel"], start)) |
| 25 | + |
| 26 | + |
| 27 | +def load_changeset_config(repo_root: Path) -> dict: |
| 28 | + return json.loads((repo_root / ".changeset" / "config.json").read_text()) |
| 29 | + |
| 30 | + |
| 31 | +def discover_packages(repo_root: Path) -> dict[str, dict]: |
| 32 | + packages: dict[str, dict] = {} |
| 33 | + for pkg_json in repo_root.rglob("package.json"): |
| 34 | + if "node_modules" in pkg_json.parts: |
| 35 | + continue |
| 36 | + try: |
| 37 | + data = json.loads(pkg_json.read_text()) |
| 38 | + except Exception: |
| 39 | + continue |
| 40 | + name = data.get("name") |
| 41 | + if not name: |
| 42 | + continue |
| 43 | + rel_dir = pkg_json.parent.relative_to(repo_root) |
| 44 | + private = bool(data.get("private", False)) |
| 45 | + packages[name] = { |
| 46 | + "dir": str(rel_dir), |
| 47 | + "private": private, |
| 48 | + } |
| 49 | + return packages |
| 50 | + |
| 51 | + |
| 52 | +def changed_files(repo_root: Path, base: str) -> list[str]: |
| 53 | + merge_base = run(["git", "merge-base", "HEAD", base], repo_root) |
| 54 | + out = run(["git", "diff", "--name-only", f"{merge_base}...HEAD"], repo_root) |
| 55 | + if not out: |
| 56 | + return [] |
| 57 | + return [line for line in out.splitlines() if line] |
| 58 | + |
| 59 | + |
| 60 | +def package_for_file(path_str: str, packages: dict[str, dict]) -> str | None: |
| 61 | + best_name = None |
| 62 | + best_len = -1 |
| 63 | + for name, meta in packages.items(): |
| 64 | + pkg_dir = meta["dir"].rstrip("/") |
| 65 | + if not pkg_dir: |
| 66 | + continue |
| 67 | + if path_str == pkg_dir or path_str.startswith(f"{pkg_dir}/"): |
| 68 | + if len(pkg_dir) > best_len: |
| 69 | + best_len = len(pkg_dir) |
| 70 | + best_name = name |
| 71 | + return best_name |
| 72 | + |
| 73 | + |
| 74 | +def parse_changeset_file(path: Path) -> dict: |
| 75 | + text = path.read_text().strip() |
| 76 | + lines = text.splitlines() |
| 77 | + if len(lines) < 3 or lines[0].strip() != "---": |
| 78 | + raise ValueError("Changeset file must start with frontmatter delimited by ---") |
| 79 | + end_index = None |
| 80 | + for i in range(1, len(lines)): |
| 81 | + if lines[i].strip() == "---": |
| 82 | + end_index = i |
| 83 | + break |
| 84 | + if end_index is None: |
| 85 | + raise ValueError("Changeset file frontmatter is missing the closing ---") |
| 86 | + |
| 87 | + releases: dict[str, str] = {} |
| 88 | + for raw in lines[1:end_index]: |
| 89 | + line = raw.strip() |
| 90 | + if not line: |
| 91 | + continue |
| 92 | + if ":" not in line: |
| 93 | + raise ValueError(f"Invalid frontmatter line: {raw}") |
| 94 | + pkg, release_type = line.split(":", 1) |
| 95 | + pkg = pkg.strip().strip("\"'") |
| 96 | + release_type = release_type.strip().strip("\"'") |
| 97 | + if release_type not in VALID_RELEASE_TYPES: |
| 98 | + raise ValueError(f"Invalid release type for {pkg}: {release_type}") |
| 99 | + releases[pkg] = release_type |
| 100 | + |
| 101 | + summary = "\n".join(lines[end_index + 1 :]).strip() |
| 102 | + return {"releases": releases, "summary": summary} |
| 103 | + |
| 104 | + |
| 105 | +def build_report(repo_root: Path, base: str, file_path: Path | None) -> dict: |
| 106 | + config = load_changeset_config(repo_root) |
| 107 | + packages = discover_packages(repo_root) |
| 108 | + ignored = set(config.get("ignore", [])) |
| 109 | + fixed_groups = [set(group) for group in config.get("fixed", [])] |
| 110 | + files = changed_files(repo_root, base) |
| 111 | + |
| 112 | + touched_packages = sorted( |
| 113 | + { |
| 114 | + pkg |
| 115 | + for file_path_str in files |
| 116 | + for pkg in [package_for_file(file_path_str, packages)] |
| 117 | + if pkg |
| 118 | + } |
| 119 | + ) |
| 120 | + touched_publishable = [pkg for pkg in touched_packages if pkg not in ignored] |
| 121 | + |
| 122 | + report = { |
| 123 | + "base": base, |
| 124 | + "changed_files_count": len(files), |
| 125 | + "touched_packages": touched_packages, |
| 126 | + "touched_publishable_packages": touched_publishable, |
| 127 | + "ignored_touched_packages": [pkg for pkg in touched_packages if pkg in ignored], |
| 128 | + "fixed_groups_hit": [ |
| 129 | + sorted(group) |
| 130 | + for group in fixed_groups |
| 131 | + if any(pkg in group for pkg in touched_publishable) |
| 132 | + ], |
| 133 | + } |
| 134 | + |
| 135 | + if file_path is not None: |
| 136 | + parsed = parse_changeset_file(file_path) |
| 137 | + listed = sorted(parsed["releases"].keys()) |
| 138 | + unknown = [pkg for pkg in listed if pkg not in packages] |
| 139 | + ignored_listed = [pkg for pkg in listed if pkg in ignored] |
| 140 | + missing_for_touched = [pkg for pkg in touched_publishable if pkg not in listed] |
| 141 | + extra_without_touched_files = [pkg for pkg in listed if pkg not in touched_publishable] |
| 142 | + report["changeset"] = { |
| 143 | + "path": str(file_path.relative_to(repo_root)), |
| 144 | + "releases": parsed["releases"], |
| 145 | + "summary_present": bool(parsed["summary"]), |
| 146 | + "unknown_packages": unknown, |
| 147 | + "ignored_packages": ignored_listed, |
| 148 | + "missing_touched_publishable_packages": missing_for_touched, |
| 149 | + "packages_without_touched_files": extra_without_touched_files, |
| 150 | + } |
| 151 | + |
| 152 | + return report |
| 153 | + |
| 154 | + |
| 155 | +def print_text(report: dict) -> None: |
| 156 | + print(f"Base: {report['base']}") |
| 157 | + print(f"Changed files: {report['changed_files_count']}") |
| 158 | + |
| 159 | + def print_list(label: str, values: list[str]) -> None: |
| 160 | + print(f"{label}:") |
| 161 | + if not values: |
| 162 | + print("- none") |
| 163 | + return |
| 164 | + for value in values: |
| 165 | + print(f"- {value}") |
| 166 | + |
| 167 | + print_list("Touched packages", report["touched_packages"]) |
| 168 | + print_list("Touched publishable packages", report["touched_publishable_packages"]) |
| 169 | + print_list("Ignored touched packages", report["ignored_touched_packages"]) |
| 170 | + |
| 171 | + fixed_groups = report["fixed_groups_hit"] |
| 172 | + print("Fixed groups hit:") |
| 173 | + if not fixed_groups: |
| 174 | + print("- none") |
| 175 | + else: |
| 176 | + for group in fixed_groups: |
| 177 | + print(f"- {', '.join(group)}") |
| 178 | + |
| 179 | + changeset = report.get("changeset") |
| 180 | + if not changeset: |
| 181 | + return |
| 182 | + |
| 183 | + print(f"Changeset file: {changeset['path']}") |
| 184 | + print("Listed releases:") |
| 185 | + if not changeset["releases"]: |
| 186 | + print("- none") |
| 187 | + else: |
| 188 | + for pkg, release_type in changeset["releases"].items(): |
| 189 | + print(f"- {pkg}: {release_type}") |
| 190 | + print(f"Summary present: {'yes' if changeset['summary_present'] else 'no'}") |
| 191 | + print_list("Unknown packages", changeset["unknown_packages"]) |
| 192 | + print_list("Ignored packages in changeset", changeset["ignored_packages"]) |
| 193 | + print_list( |
| 194 | + "Touched publishable packages missing from changeset", |
| 195 | + changeset["missing_touched_publishable_packages"], |
| 196 | + ) |
| 197 | + print_list( |
| 198 | + "Changeset packages without touched files", |
| 199 | + changeset["packages_without_touched_files"], |
| 200 | + ) |
| 201 | + |
| 202 | + |
| 203 | +def main() -> int: |
| 204 | + parser = argparse.ArgumentParser( |
| 205 | + description="Inspect changed packages on the branch and validate a changeset file against repo config." |
| 206 | + ) |
| 207 | + parser.add_argument("--base", default="origin/main", help="Branch or ref to diff against") |
| 208 | + parser.add_argument("--file", help="Optional .changeset/*.md file to validate") |
| 209 | + parser.add_argument("--json", action="store_true", help="Emit JSON") |
| 210 | + args = parser.parse_args() |
| 211 | + |
| 212 | + cwd = Path.cwd() |
| 213 | + repo_root = find_repo_root(cwd) |
| 214 | + file_path = Path(args.file).resolve() if args.file else None |
| 215 | + |
| 216 | + try: |
| 217 | + report = build_report(repo_root, args.base, file_path) |
| 218 | + except subprocess.CalledProcessError as exc: |
| 219 | + sys.stderr.write(exc.stderr) |
| 220 | + return exc.returncode |
| 221 | + except Exception as exc: |
| 222 | + sys.stderr.write(f"{exc}\n") |
| 223 | + return 1 |
| 224 | + |
| 225 | + if args.json: |
| 226 | + print(json.dumps(report, indent=2, sort_keys=True)) |
| 227 | + else: |
| 228 | + print_text(report) |
| 229 | + return 0 |
| 230 | + |
| 231 | + |
| 232 | +if __name__ == "__main__": |
| 233 | + raise SystemExit(main()) |
0 commit comments