|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import json |
| 6 | +import os |
| 7 | +import subprocess |
| 8 | +import sys |
| 9 | +from pathlib import Path |
| 10 | + |
| 11 | +if __package__ in {None, ""}: |
| 12 | + sys.path.insert(0, str(Path(__file__).resolve().parents[2])) |
| 13 | + |
| 14 | +from scripts.validate_plugins.plugins_map import load_plugins_map_text |
| 15 | + |
| 16 | + |
| 17 | +DEFAULT_ASTRBOT_REF = "master" |
| 18 | +ASTRBOT_REMOTE_URL = "https://github.com/AstrBotDevs/AstrBot" |
| 19 | + |
| 20 | + |
| 21 | +def load_plugins_map(text: str, *, source_name: str) -> dict[str, dict]: |
| 22 | + return load_plugins_map_text(text, source_name=source_name) |
| 23 | + |
| 24 | + |
| 25 | +def detect_changed_plugin_names(*, base: dict[str, dict], head: dict[str, dict]) -> list[str]: |
| 26 | + return [name for name, payload in head.items() if base.get(name) != payload] |
| 27 | + |
| 28 | + |
| 29 | +def fetch_base_ref(base_ref: str) -> None: |
| 30 | + subprocess.run(["git", "fetch", "origin", base_ref, "--depth", "1"], check=True) |
| 31 | + |
| 32 | + |
| 33 | +def read_base_plugins_json(base_ref: str) -> str: |
| 34 | + return subprocess.check_output( |
| 35 | + ["git", "show", f"origin/{base_ref}:plugins.json"], |
| 36 | + text=True, |
| 37 | + stderr=subprocess.DEVNULL, |
| 38 | + ) |
| 39 | + |
| 40 | + |
| 41 | +def resolve_astrbot_ref() -> str: |
| 42 | + try: |
| 43 | + default_head = subprocess.check_output( |
| 44 | + ["git", "ls-remote", "--symref", ASTRBOT_REMOTE_URL, "HEAD"], |
| 45 | + text=True, |
| 46 | + stderr=subprocess.DEVNULL, |
| 47 | + ) |
| 48 | + except subprocess.CalledProcessError: |
| 49 | + return DEFAULT_ASTRBOT_REF |
| 50 | + |
| 51 | + for line in default_head.splitlines(): |
| 52 | + if line.startswith("ref: refs/heads/") and line.endswith("\tHEAD"): |
| 53 | + return line.split("refs/heads/", 1)[1].split("\t", 1)[0] |
| 54 | + return DEFAULT_ASTRBOT_REF |
| 55 | + |
| 56 | + |
| 57 | +def detect_pull_request_selection(*, repo_root: Path, base_ref: str) -> dict[str, object]: |
| 58 | + try: |
| 59 | + fetch_base_ref(base_ref) |
| 60 | + base = load_plugins_map(read_base_plugins_json(base_ref), source_name=f"base ref {base_ref}") |
| 61 | + except (subprocess.CalledProcessError, ValueError): |
| 62 | + base = {} |
| 63 | + |
| 64 | + head_text = (repo_root / "plugins.json").read_text(encoding="utf-8") |
| 65 | + try: |
| 66 | + head = load_plugins_map(head_text, source_name="PR head") |
| 67 | + except ValueError as exc: |
| 68 | + raise ValueError(f"plugins.json is invalid on the PR head: {exc}") from exc |
| 69 | + |
| 70 | + changed = detect_changed_plugin_names(base=base, head=head) |
| 71 | + validation_note = "" |
| 72 | + if not changed: |
| 73 | + validation_note = "No plugin entries changed in plugins.json; skipping smoke validation." |
| 74 | + |
| 75 | + return { |
| 76 | + "changed": changed, |
| 77 | + "should_validate": bool(changed), |
| 78 | + "validation_note": validation_note, |
| 79 | + } |
| 80 | + |
| 81 | + |
| 82 | +def write_github_env( |
| 83 | + *, |
| 84 | + env_path: Path, |
| 85 | + astrbot_ref: str, |
| 86 | + changed: list[str], |
| 87 | + should_validate: bool, |
| 88 | + validation_note: str, |
| 89 | +) -> None: |
| 90 | + with env_path.open("a", encoding="utf-8") as handle: |
| 91 | + handle.write(f"ASTRBOT_REF={astrbot_ref}\n") |
| 92 | + handle.write(f"PLUGIN_NAME_LIST={','.join(changed)}\n") |
| 93 | + handle.write("PLUGIN_LIMIT=\n") |
| 94 | + handle.write(f"SHOULD_VALIDATE={'true' if should_validate else 'false'}\n") |
| 95 | + handle.write(f"VALIDATION_NOTE={validation_note}\n") |
| 96 | + |
| 97 | + |
| 98 | +def main() -> int: |
| 99 | + base_ref = os.environ["GITHUB_BASE_REF"] |
| 100 | + github_env = Path(os.environ["GITHUB_ENV"]) |
| 101 | + repo_root = Path.cwd() |
| 102 | + |
| 103 | + try: |
| 104 | + result = detect_pull_request_selection(repo_root=repo_root, base_ref=base_ref) |
| 105 | + except ValueError as exc: |
| 106 | + print(str(exc), file=sys.stderr) |
| 107 | + return 1 |
| 108 | + |
| 109 | + write_github_env( |
| 110 | + env_path=github_env, |
| 111 | + astrbot_ref=resolve_astrbot_ref(), |
| 112 | + changed=result["changed"], |
| 113 | + should_validate=result["should_validate"], |
| 114 | + validation_note=result["validation_note"], |
| 115 | + ) |
| 116 | + return 0 |
| 117 | + |
| 118 | + |
| 119 | +if __name__ == "__main__": |
| 120 | + raise SystemExit(main()) |
0 commit comments