diff --git a/CHANGELOG.md b/CHANGELOG.md index 55e8de48..195c9034 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,13 @@ All notable changes to pathiel. The format follows ## [Unreleased] +### Added + +- `scripts/glama_health.py` (maintainers): Glama's maintenance data for the + server next to GitHub's own Dependabot, code scanning and CI state, so an + "unavailable" line on Glama traces to a GitHub setting or to the Glama AI + app not being installed on the repo. + ## [0.7.2] - 2026-11-17 ### Security diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 8077e411..7adbb02e 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -133,6 +133,15 @@ it touches, and paste the gate's last line. 5. If a push is refused: package settings, Manage Actions access, give `Julian-dev28/pathiel` the Write role, re-run the failed jobs. +Glama's Maintenance grade reads Dependabot, code scanning and CI from +GitHub through the Glama AI GitHub App. If its score page says any of them +is "unavailable", run `python3 scripts/glama_health.py`: it shows what +GitHub has next to what Glama's public page shows. When every GitHub row +is ok, the app is not installed on the repo: install it from +https://github.com/apps/glama-ai/installations/new (only `pathiel`), then +Sync on Glama's admin page. Signing in to Glama with GitHub authorizes +the app but does not install it. + The website (GitHub Pages) is the `gh-pages` branch: the landing page in `site/` and the scoreboard in `docs/scoreboard/`, nothing else. After a change to either, including each pool build's `scoreboard.json`, publish it diff --git a/scripts/glama_health.py b/scripts/glama_health.py new file mode 100644 index 00000000..b7315646 --- /dev/null +++ b/scripts/glama_health.py @@ -0,0 +1,208 @@ +#!/usr/bin/env python3 +"""What Glama's maintenance grade sees for a repo, next to what GitHub has. + +Glama's Maintenance checklist reads seven signals. Four come from GitHub +through the Glama AI GitHub App (Dependabot alerts, Dependabot security +updates, code scanning, CI on the default branch): when the app is not +installed on the repo, all four read "unavailable" however well the repo is +set up. That was pathiel's case on 2026-11-20: Glama AI was authorized for +sign-in but never installed, so every one of those lines said +"unavailable" while GitHub had them all on and green. + +This prints both sides so the gap is a fact, not a guess: + + Glama side the public server page's maintenanceHealth (commits per + week, issue responsiveness and how fresh that data is, + releases), decoded from the page's embedded data + GitHub side .github/dependabot.yml, Dependabot alerts on, Dependabot + security updates on, code scanning set up, CI check runs on + the default branch's head, glama.json maintainers + + python3 scripts/glama_health.py # pathiel + python3 scripts/glama_health.py OWNER/REPO + +Whether the app is installed cannot be read with a user token; the report +names the install page when the GitHub side is green, so a remaining +"unavailable" on Glama means the app. Read only. Uses GITHUB_TOKEN or +`gh auth token` for the GitHub API (only ever sent to api.github.com). +""" +from __future__ import annotations + +import json +import re +import sys +from pathlib import Path +from typing import Callable, Optional, Tuple + +sys.path.insert(0, str(Path(__file__).resolve().parent)) +import seo_compare as C # noqa: E402 (_get, sends_token) +import seo_rank as S # noqa: E402 (_token) + +PATHIEL = "Julian-dev28/pathiel" +INSTALL_URL = "https://github.com/apps/glama-ai/installations/new" +API = "https://api.github.com" + +# Check-run conclusions that mean red; cancelled, skipped, neutral and stale do not. +FAILED = frozenset({"failure", "timed_out", "action_required", "startup_failure"}) + +Getter = Callable[[str], Tuple[int, Optional[bytes]]] + + +# ── Glama side ────────────────────────────────────────────────────────────── + +def decode_stream(html: str) -> Optional[list]: + """The flat array of the page's turbo-stream payload (React Router): a + JSON string passed to enqueue(...) whose first line is the array. Objects + in it map "_" to a value index; negative ints are null-like.""" + for lit in re.findall(r'enqueue\((".*?")\)\s*;?\s*', html, re.S): + try: + arr = json.loads(json.loads(lit).splitlines()[0]) + except (ValueError, IndexError): + continue + if isinstance(arr, list): + return arr + return None + + +def _deref(arr: list, i: int, depth: int = 0): + v = arr[i] + if depth > 8: + return v + if isinstance(v, dict): + out = {} + for k, x in v.items(): + key = arr[int(k[1:])] if k.startswith("_") and k[1:].isdigit() else k + out[key] = _deref(arr, x, depth + 1) if isinstance(x, int) and x >= 0 else (None if isinstance(x, int) else x) + return out + if isinstance(v, list): + return [_deref(arr, x, depth + 1) if isinstance(x, int) and x >= 0 else x for x in v] + return v + + +def maintenance(html: str) -> Optional[dict]: + """Glama's maintenanceHealth object for the server page, or None.""" + arr = decode_stream(html) + if not arr or "maintenanceHealth" not in arr: + return None + return _deref(arr, arr.index("maintenanceHealth") + 1) + + +# ── GitHub side ───────────────────────────────────────────────────────────── + +def _json(body: Optional[bytes]): + try: + return json.loads(body) if body else None + except ValueError: + return None + + +def github_side(repo: str, get: Getter) -> dict: + out: dict = {} + status, _ = get(f"{API}/repos/{repo}/contents/.github/dependabot.yml") + out["dependabot_config"] = status == 200 + _, r = get(f"{API}/repos/{repo}") + r = _json(r) or {} + # Alerts and default setup are only readable by an admin; for anyone else + # a 404 means "not yours to see", not "off". + admin = bool((r.get("permissions") or {}).get("admin")) + status, _ = get(f"{API}/repos/{repo}/vulnerability-alerts") + out["dependabot_alerts"] = {204: True, 404: False}.get(status) if admin else (True if status == 204 else None) + sec = r.get("security_and_analysis") or {} + upd = (sec.get("dependabot_security_updates") or {}).get("status") + out["dependabot_security_updates"] = None if upd is None else upd == "enabled" + branch = r.get("default_branch") or "main" + status, body = get(f"{API}/repos/{repo}/code-scanning/default-setup") + setup = (_json(body) or {}).get("state") if status == 200 else None + _, wf = get(f"{API}/repos/{repo}/contents/.github/workflows") + codeql_file = any("codeql" in (f.get("name") or "").lower() for f in (_json(wf) or []) if isinstance(f, dict)) + out["code_scanning"] = True if (setup == "configured" or codeql_file) else (False if admin else None) + _, head = get(f"{API}/repos/{repo}/commits/{branch}") + sha = (_json(head) or {}).get("sha") + runs = [] + if sha: + _, cr = get(f"{API}/repos/{repo}/commits/{sha}/check-runs?per_page=100") + runs = (_json(cr) or {}).get("check_runs") or [] + done = [c for c in runs if c.get("status") == "completed"] + bad = sorted({c.get("name", "?") for c in done if c.get("conclusion") in FAILED}) + out["ci"] = {"branch": branch, "sha": (sha or "")[:7], "check_runs": len(runs), "failing": bad} + status, body = get(f"{API}/repos/{repo}/contents/glama.json") + meta = _json(body) or {} + if status == 200 and meta.get("content"): + import base64 + try: + meta = json.loads(base64.b64decode(meta["content"])) + except ValueError: + meta = {} + out["glama_maintainers"] = meta.get("maintainers") or [] + return out + + +# ── verdict ───────────────────────────────────────────────────────────────── + +def _yn(v) -> str: + return "on" if v is True else "OFF" if v is False else "unknown (needs an admin token)" + + +def report(repo: str, glama: Optional[dict], gh: dict) -> Tuple[bool, str]: + """(GitHub side all green, text). Pure.""" + ci = gh["ci"] + ci_ok = ci["check_runs"] > 0 and not ci["failing"] + rows = [ + ("Dependabot config", "present" if gh["dependabot_config"] else "MISSING .github/dependabot.yml", + gh["dependabot_config"]), + ("Dependabot alerts", _yn(gh["dependabot_alerts"]), gh["dependabot_alerts"] is not False), + ("Dependabot security updates", _yn(gh["dependabot_security_updates"]), + gh["dependabot_security_updates"] is not False), + ("Code scanning", {True: "set up", False: "NOT set up (enable CodeQL default setup)"}.get( + gh["code_scanning"], "unknown (needs an admin token)"), gh["code_scanning"] is not False), + ("CI on " + ci["branch"], (f"{ci['check_runs']} check runs on {ci['sha']}, " + + (f"failing: {', '.join(ci['failing'])}" if ci["failing"] else "all green")), + ci_ok), + ("glama.json maintainers", ", ".join(gh["glama_maintainers"]) or "MISSING", bool(gh["glama_maintainers"])), + ] + green = all(ok for _, _, ok in rows) + lines = [f"GitHub side for {repo}:"] + lines += [f" {'ok ' if ok else 'FIX'} {name:<28} {text}" for name, text, ok in rows] + lines.append("Glama side (public page):") + if not glama: + lines.append(" page data not found (layout changed or the server is not listed)") + else: + weeks = glama.get("activitySeries") or [] + commits = sum(int(w.get("commitCount") or 0) for w in weeks) + iss = glama.get("issueResponsiveness") or {} + rel = glama.get("releaseSeries") or [] + lines.append(f" commits, last {len(weeks)} weeks: {commits}") + if iss: + hours = iss.get("medianResponseHours") + median = f"median first response {hours / 24:.0f} days, " if isinstance(hours, (int, float)) else "" + lines.append(f" issues: {iss.get('handledCount')} of {iss.get('totalCount')} answered or closed, " + f"{median}collected {str(iss.get('collectedAt'))[:10]}" + + (" (stale)" if iss.get("stale") else "")) + lines.append(f" releases: {sum(int(r.get('releaseCount') or 0) for r in rel)} " + f"({', '.join(r.get('month', '?') for r in rel) or 'none'})") + if green: + lines.append("GitHub has every signal on. If Glama still says Dependabot, code scanning or CI " + f"\"unavailable\", the Glama AI app is not installed on {repo}: install it at " + f"{INSTALL_URL} (Only select repositories: {repo.split('/')[1]}), then Sync on Glama's admin page.") + else: + lines.append("Fix the FIX rows on GitHub first; Glama cannot report a signal GitHub does not have.") + return green, "\n".join(lines) + + +def main(argv=None) -> int: + args = sys.argv[1:] if argv is None else argv + repo = args[0] if args else PATHIEL + token = S._token() + + def get(url: str): + return C._get(url, token if C.sends_token(url) else None, accept_json=False) + gh = github_side(repo, get) + status, page = C._get(f"https://glama.ai/mcp/servers/{repo}", None, accept_json=False) + glama = maintenance(page.decode("utf-8", "replace")) if status == 200 and page else None + green, text = report(repo, glama, gh) + print(text) + return 0 if green else 1 + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/tests/test_glama_health.py b/tests/test_glama_health.py new file mode 100644 index 00000000..6cf38c5c --- /dev/null +++ b/tests/test_glama_health.py @@ -0,0 +1,113 @@ +"""scripts/glama_health.py: Glama's maintenance view next to GitHub's (2026-11-20). + +Glama read Dependabot, code scanning and CI as "unavailable" for pathiel while +GitHub had all of them on and green: the Glama AI app was authorized for +sign-in but never installed. These pin the decoding of Glama's page data and +the verdict, offline, with a fake GitHub.""" +from __future__ import annotations + +import base64 +import importlib.util +import json +from pathlib import Path + +ROOT = Path(__file__).resolve().parents[1] +_spec = importlib.util.spec_from_file_location("glama_health", ROOT / "scripts" / "glama_health.py") +G = importlib.util.module_from_spec(_spec) +_spec.loader.exec_module(G) + +REPO = "me/srv" +API = "https://api.github.com/repos/me/srv" + + +def _page(arr: list) -> str: + """A server page as React Router streams it: the flat array as the first + line of a JSON string handed to enqueue(...).""" + lit = json.dumps(json.dumps(arr) + "\n") + return f"" + + +# keys and values by index, as Glama's page holds them (2026-11-20 shape) +STREAM = ["x", "maintenanceHealth", + {"_3": 4, "_6": 7, "_10": -5}, # 2 + "activitySeries", [5], {"_8": 9}, # 3, 4, 5 + "issueResponsiveness", {"_11": 12, "_13": 14, "_15": 16, "_17": 18, "_19": 20}, # 6, 7 + "commitCount", 77, # 8, 9 + "maintainerCount", # 10 + "handledCount", 1, "totalCount", 1, "medianResponseHours", 3305.5, # 11..16 + "collectedAt", "2026-11-18T00:11:34Z", "stale", True] # 17..20 + + +def test_maintenance_decodes_the_stream_and_resolves_nulls(): + m = G.maintenance(_page(STREAM)) + assert m["activitySeries"] == [{"commitCount": 77}] + assert m["maintainerCount"] is None + assert m["issueResponsiveness"]["medianResponseHours"] == 3305.5 + assert m["issueResponsiveness"]["stale"] is True + + +def test_a_page_without_the_data_is_none_not_a_crash(): + assert G.maintenance("no stream") is None + assert G.maintenance(_page(["nothing", "here"])) is None + assert G.maintenance("") is None + + +def fake_github(*, admin=True, alerts=204, setup="configured", runs=None, glama=("me",), dependabot=True): + runs = [{"name": "Tests", "status": "completed", "conclusion": "success"}] if runs is None else runs + glama_json = base64.b64encode(json.dumps({"maintainers": list(glama)}).encode()).decode() + table = { + f"{API}/contents/.github/dependabot.yml": (200 if dependabot else 404, b"{}"), + f"{API}/vulnerability-alerts": (alerts, b""), + API: (200, json.dumps({"default_branch": "main", "permissions": {"admin": admin}, + "security_and_analysis": {"dependabot_security_updates": {"status": "enabled"}} + if admin else None}).encode()), + f"{API}/code-scanning/default-setup": (200, json.dumps({"state": setup}).encode()) if admin else (403, b""), + f"{API}/contents/.github/workflows": (200, json.dumps([{"name": "ci.yml"}]).encode()), + f"{API}/commits/main": (200, json.dumps({"sha": "abc1234def"}).encode()), + f"{API}/commits/abc1234def/check-runs?per_page=100": (200, json.dumps({"check_runs": runs}).encode()), + f"{API}/contents/glama.json": (200, json.dumps({"content": glama_json}).encode()), + } + return lambda url: table.get(url, (404, None)) + + +def test_all_green_names_the_app_install_as_the_remaining_cause(): + gh = G.github_side(REPO, fake_github()) + assert gh["dependabot_alerts"] and gh["dependabot_security_updates"] and gh["code_scanning"] + assert gh["ci"] == {"branch": "main", "sha": "abc1234", "check_runs": 1, "failing": []} + green, text = G.report(REPO, G.maintenance(_page(STREAM)), gh) + assert green and G.INSTALL_URL in text and "FIX" not in text + assert "median first response 138 days" in text and "(stale)" in text + + +def test_a_missing_github_signal_is_named_before_the_app(): + green, text = G.report(REPO, None, G.github_side(REPO, fake_github(alerts=404, setup="not-configured", + dependabot=False, glama=()))) + assert not green and G.INSTALL_URL not in text + for row in ("FIX Dependabot config", "FIX Dependabot alerts", "FIX Code scanning", "FIX glama.json"): + assert row in text, row + assert "page data not found" in text + + +def test_without_admin_a_404_is_unknown_not_off(): + gh = G.github_side(REPO, fake_github(admin=False, alerts=404)) + assert gh["dependabot_alerts"] is None and gh["dependabot_security_updates"] is None + assert gh["code_scanning"] is None # default setup unreadable, no CodeQL workflow file + green, text = G.report(REPO, None, gh) + assert green and "unknown (needs an admin token)" in text + + +def test_only_real_failures_turn_ci_red(): + runs = [{"name": n, "status": "completed", "conclusion": c} for n, c in + (("a", "success"), ("b", "cancelled"), ("c", "skipped"), ("d", "neutral"))] + runs.append({"name": "e", "status": "in_progress", "conclusion": None}) + assert G.github_side(REPO, fake_github(runs=runs))["ci"]["failing"] == [] + runs.append({"name": "tests", "status": "completed", "conclusion": "failure"}) + gh = G.github_side(REPO, fake_github(runs=runs)) + assert gh["ci"]["failing"] == ["tests"] + assert not G.report(REPO, None, gh)[0] + assert not G.report(REPO, None, G.github_side(REPO, fake_github(runs=[])))[0] + + +def test_the_token_only_goes_to_the_github_api(): + assert G.C.sends_token(f"{API}/vulnerability-alerts") + assert not G.C.sends_token("https://glama.ai/mcp/servers/me/srv")