|
| 1 | +"""Core detection logic: match git diff against metadata to find triggered playbooks.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import fnmatch |
| 6 | +import json |
| 7 | +import subprocess |
| 8 | +from pathlib import Path |
| 9 | +from typing import Any |
| 10 | + |
| 11 | +import yaml |
| 12 | + |
| 13 | + |
| 14 | +def get_changed_files(start: str, end: str, repo_dir: str = ".") -> list[str]: |
| 15 | + """Return list of files changed between two git refs.""" |
| 16 | + result = subprocess.run( |
| 17 | + ["git", "diff", "--name-only", f"{start}...{end}"], |
| 18 | + capture_output=True, |
| 19 | + text=True, |
| 20 | + cwd=repo_dir, |
| 21 | + check=True, |
| 22 | + ) |
| 23 | + return [f.strip() for f in result.stdout.strip().splitlines() if f.strip()] |
| 24 | + |
| 25 | + |
| 26 | +def load_metadata(path: str) -> dict[str, Any]: |
| 27 | + """Load a .metadata.yml file. |
| 28 | +
|
| 29 | + Expected format: |
| 30 | + playbook_name: |
| 31 | + stage: 1 |
| 32 | + depends_on: [] |
| 33 | + paths: |
| 34 | + - "ansible/roles/my_role/**" |
| 35 | + - "ansible/inventory/*/group_vars/**" |
| 36 | + """ |
| 37 | + with open(path) as f: |
| 38 | + data = yaml.safe_load(f) |
| 39 | + return data or {} |
| 40 | + |
| 41 | + |
| 42 | +def match_paths(changed_files: list[str], patterns: list[str]) -> bool: |
| 43 | + """Check if any changed file matches any of the glob patterns.""" |
| 44 | + for changed in changed_files: |
| 45 | + for pattern in patterns: |
| 46 | + if fnmatch.fnmatch(changed, pattern): |
| 47 | + return True |
| 48 | + return False |
| 49 | + |
| 50 | + |
| 51 | +def detect_changes( |
| 52 | + start: str, |
| 53 | + end: str, |
| 54 | + metadata_files: list[str], |
| 55 | + repo_dir: str = ".", |
| 56 | +) -> dict[str, Any]: |
| 57 | + """Run change detection and return playbook trigger map. |
| 58 | +
|
| 59 | + Returns a dict like: |
| 60 | + { |
| 61 | + "playbook_name": { |
| 62 | + "triggered": true, |
| 63 | + "stage": 1, |
| 64 | + "depends_on": [], |
| 65 | + "matched_files": ["path/to/file"] |
| 66 | + } |
| 67 | + } |
| 68 | + """ |
| 69 | + changed_files = get_changed_files(start, end, repo_dir) |
| 70 | + |
| 71 | + result: dict[str, Any] = {} |
| 72 | + |
| 73 | + for meta_path in metadata_files: |
| 74 | + metadata = load_metadata(meta_path) |
| 75 | + |
| 76 | + for playbook_name, config in metadata.items(): |
| 77 | + if not isinstance(config, dict): |
| 78 | + continue |
| 79 | + |
| 80 | + patterns = config.get("paths", []) |
| 81 | + stage = config.get("stage", 0) |
| 82 | + depends_on = config.get("depends_on", []) |
| 83 | + |
| 84 | + matched = [ |
| 85 | + f for f in changed_files |
| 86 | + if any(fnmatch.fnmatch(f, p) for p in patterns) |
| 87 | + ] |
| 88 | + |
| 89 | + triggered = len(matched) > 0 |
| 90 | + |
| 91 | + if playbook_name in result: |
| 92 | + if triggered: |
| 93 | + result[playbook_name]["triggered"] = True |
| 94 | + result[playbook_name]["matched_files"].extend(matched) |
| 95 | + else: |
| 96 | + result[playbook_name] = { |
| 97 | + "triggered": triggered, |
| 98 | + "stage": stage, |
| 99 | + "depends_on": depends_on, |
| 100 | + "matched_files": matched, |
| 101 | + } |
| 102 | + |
| 103 | + # Propagate triggers through dependencies |
| 104 | + changed = True |
| 105 | + while changed: |
| 106 | + changed = False |
| 107 | + for name, info in result.items(): |
| 108 | + if info["triggered"]: |
| 109 | + continue |
| 110 | + for dep in info["depends_on"]: |
| 111 | + if dep in result and result[dep]["triggered"]: |
| 112 | + info["triggered"] = True |
| 113 | + changed = True |
| 114 | + break |
| 115 | + |
| 116 | + return result |
0 commit comments