|
| 1 | +#!/usr/bin/env python3 |
| 2 | +from __future__ import annotations |
| 3 | + |
| 4 | +import argparse |
| 5 | +import json |
| 6 | +import subprocess |
| 7 | +import sys |
| 8 | +from dataclasses import dataclass |
| 9 | +from pathlib import Path |
| 10 | +from typing import Iterable |
| 11 | + |
| 12 | + |
| 13 | +NULL_SHA = "0" * 40 |
| 14 | + |
| 15 | + |
| 16 | +@dataclass(frozen=True) |
| 17 | +class GuideMetadata: |
| 18 | + slug: str |
| 19 | + publish: bool |
| 20 | + base: str | None |
| 21 | + |
| 22 | + |
| 23 | +def kebab_case_to_gradle_name(name: str) -> str: |
| 24 | + gradle_name = "".join(part[:1].upper() + part[1:] for part in name.split("-")) |
| 25 | + return gradle_name[:1].lower() + gradle_name[1:] |
| 26 | + |
| 27 | + |
| 28 | +def guide_slug_from_path(path: str) -> str | None: |
| 29 | + parts = path.strip().strip("/").split("/") |
| 30 | + if len(parts) >= 2 and parts[0] == "guides" and parts[1]: |
| 31 | + return parts[1] |
| 32 | + return None |
| 33 | + |
| 34 | + |
| 35 | +def guide_slugs_from_paths(paths: Iterable[str]) -> set[str]: |
| 36 | + return { |
| 37 | + slug |
| 38 | + for path in paths |
| 39 | + if (slug := guide_slug_from_path(path)) is not None |
| 40 | + } |
| 41 | + |
| 42 | + |
| 43 | +def load_guides(guides_dir: Path) -> dict[str, GuideMetadata]: |
| 44 | + guides = {} |
| 45 | + for metadata_path in sorted(guides_dir.glob("*/metadata.json")): |
| 46 | + with metadata_path.open(encoding="utf-8") as metadata_file: |
| 47 | + metadata = json.load(metadata_file) |
| 48 | + slug = metadata_path.parent.name |
| 49 | + guides[slug] = GuideMetadata( |
| 50 | + slug=slug, |
| 51 | + publish=metadata.get("publish", True) is not False, |
| 52 | + base=metadata.get("base"), |
| 53 | + ) |
| 54 | + return guides |
| 55 | + |
| 56 | + |
| 57 | +def resolve_impacted_guide_slugs( |
| 58 | + changed_files: Iterable[str], |
| 59 | + guides: dict[str, GuideMetadata], |
| 60 | +) -> list[str]: |
| 61 | + impacted_slugs = guide_slugs_from_paths(changed_files) |
| 62 | + |
| 63 | + changed = True |
| 64 | + while changed: |
| 65 | + changed = False |
| 66 | + for guide in guides.values(): |
| 67 | + if guide.base in impacted_slugs and guide.slug not in impacted_slugs: |
| 68 | + impacted_slugs.add(guide.slug) |
| 69 | + changed = True |
| 70 | + |
| 71 | + return sorted( |
| 72 | + guide.slug |
| 73 | + for guide in guides.values() |
| 74 | + if guide.publish and guide.slug in impacted_slugs |
| 75 | + ) |
| 76 | + |
| 77 | + |
| 78 | +def tasks_for_changed_files(changed_files: Iterable[str], guides_dir: Path) -> list[str]: |
| 79 | + guides = load_guides(guides_dir) |
| 80 | + return [ |
| 81 | + f"{kebab_case_to_gradle_name(slug)}Build" |
| 82 | + for slug in resolve_impacted_guide_slugs(changed_files, guides) |
| 83 | + ] |
| 84 | + |
| 85 | + |
| 86 | +def matrix_for_tasks(tasks: Iterable[str]) -> dict[str, list[str]]: |
| 87 | + return {"group_test_tasks": list(tasks)} |
| 88 | + |
| 89 | + |
| 90 | +def git_changed_files( |
| 91 | + repo: Path, |
| 92 | + base: str | None, |
| 93 | + head: str, |
| 94 | + diff_mode: str = "range", |
| 95 | +) -> list[str]: |
| 96 | + if base and base != NULL_SHA: |
| 97 | + revisions = [f"{base}...{head}"] if diff_mode == "merge-base" else [base, head] |
| 98 | + command = ["git", "diff", "--name-only", "--diff-filter=ACMRT", *revisions] |
| 99 | + else: |
| 100 | + command = ["git", "diff-tree", "--no-commit-id", "--name-only", "-r", head] |
| 101 | + |
| 102 | + result = subprocess.run( |
| 103 | + command, |
| 104 | + cwd=repo, |
| 105 | + check=True, |
| 106 | + text=True, |
| 107 | + stdout=subprocess.PIPE, |
| 108 | + ) |
| 109 | + return [line for line in result.stdout.splitlines() if line] |
| 110 | + |
| 111 | + |
| 112 | +def write_github_output(output_path: Path, matrix: dict[str, list[str]]) -> None: |
| 113 | + tasks = matrix["group_test_tasks"] |
| 114 | + with output_path.open("a", encoding="utf-8") as output_file: |
| 115 | + output_file.write(f"matrix={json.dumps(matrix, separators=(',', ':'))}\n") |
| 116 | + output_file.write(f"has_tasks={str(bool(tasks)).lower()}\n") |
| 117 | + |
| 118 | + |
| 119 | +def parse_args() -> argparse.Namespace: |
| 120 | + parser = argparse.ArgumentParser( |
| 121 | + description="Resolve changed guide paths to Micronaut guide Gradle build tasks.", |
| 122 | + ) |
| 123 | + parser.add_argument("--repo", default=".", help="Repository root.") |
| 124 | + parser.add_argument("--guides-dir", help="Guides directory. Defaults to <repo>/guides.") |
| 125 | + parser.add_argument("--base", help="Base commit for git diff.") |
| 126 | + parser.add_argument("--head", default="HEAD", help="Head commit for git diff.") |
| 127 | + parser.add_argument( |
| 128 | + "--diff-mode", |
| 129 | + choices=("range", "merge-base"), |
| 130 | + default="range", |
| 131 | + help="Use range for base..head or merge-base for base...head.", |
| 132 | + ) |
| 133 | + parser.add_argument( |
| 134 | + "--changed-file", |
| 135 | + action="append", |
| 136 | + dest="changed_files", |
| 137 | + help="Changed file path. May be supplied more than once; skips git diff.", |
| 138 | + ) |
| 139 | + parser.add_argument( |
| 140 | + "--github-output", |
| 141 | + help="Path to the GitHub Actions output file.", |
| 142 | + ) |
| 143 | + return parser.parse_args() |
| 144 | + |
| 145 | + |
| 146 | +def main() -> int: |
| 147 | + args = parse_args() |
| 148 | + repo = Path(args.repo).resolve() |
| 149 | + guides_dir = Path(args.guides_dir).resolve() if args.guides_dir else repo / "guides" |
| 150 | + |
| 151 | + changed_files = ( |
| 152 | + args.changed_files |
| 153 | + if args.changed_files is not None |
| 154 | + else git_changed_files(repo, args.base, args.head, args.diff_mode) |
| 155 | + ) |
| 156 | + tasks = tasks_for_changed_files(changed_files, guides_dir) |
| 157 | + matrix = matrix_for_tasks(tasks) |
| 158 | + |
| 159 | + print(f"Changed files: {json.dumps(changed_files)}", file=sys.stderr) |
| 160 | + print(f"Selected Gradle tasks: {json.dumps(tasks)}", file=sys.stderr) |
| 161 | + print(json.dumps(matrix, separators=(",", ":"))) |
| 162 | + |
| 163 | + if args.github_output: |
| 164 | + write_github_output(Path(args.github_output), matrix) |
| 165 | + |
| 166 | + return 0 |
| 167 | + |
| 168 | + |
| 169 | +if __name__ == "__main__": |
| 170 | + raise SystemExit(main()) |
0 commit comments