kanban_pr_acceptance.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. """Exact-head GitHub acceptance for explicitly declared PR tasks.
  2. Network work happens outside SQLite transactions. The lifecycle owner persists
  3. receipts only after rechecking the captured run/status/contract under its lock.
  4. ``gh`` runs as the card's assignee profile (``profile_home``), not the ambient
  5. login: :func:`_gh_env` resolves that profile's own credentials/config for the
  6. subprocess — a multi-profile host's default ``gh`` login cannot read another
  7. org's private repos (#122689).
  8. """
  9. from __future__ import annotations
  10. import json
  11. import re
  12. import subprocess
  13. from pathlib import Path
  14. from urllib.parse import quote
  15. _REPO = re.compile(r"[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+")
  16. _PR = re.compile(r"https://github\.com/([A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+)/pull/([1-9][0-9]*)")
  17. def validate_contract(value: str | None) -> str:
  18. if value is None or value == "local-only":
  19. return "local-only"
  20. if not isinstance(value, str) or not (_REPO.fullmatch(value) or _PR.fullmatch(value)):
  21. raise ValueError("completion_contract must be local-only, OWNER/REPO, or an exact GitHub PR URL")
  22. return value
  23. def _api(endpoint: str, *, query: str | None = None, paginate: bool = False,
  24. profile_home: str | None = None):
  25. command = ["gh", "api", endpoint, "--hostname", "github.com"]
  26. if query is not None:
  27. command += ["-f", "query=" + query]
  28. if paginate:
  29. command += ["--paginate", "--slurp"]
  30. try:
  31. result = subprocess.run(command, stdin=subprocess.DEVNULL, capture_output=True,
  32. text=True, encoding="utf-8", errors="replace", timeout=30,
  33. check=True, env=_gh_env(profile_home))
  34. except subprocess.CalledProcessError as exc:
  35. # 401/403/404 = the login cannot see this repository (wrong profile identity
  36. # or missing grant), not a transient API failure. Persist only the status
  37. # code + endpoint, never gh's stderr (credentials/host details).
  38. denied = re.search(r"HTTP (40[134])", exc.stderr or "")
  39. if denied:
  40. raise _GateAuthError(f"HTTP {denied[1]} on {endpoint.split('?')[0]}") from None
  41. if exc.returncode == 4: # gh's authentication-required exit: this profile has no login
  42. raise _GateAuthError(f"gh has no login for {endpoint.split('?')[0]}") from None
  43. raise
  44. value = json.loads(result.stdout)
  45. if isinstance(value, dict) and value.get("errors"):
  46. raise ValueError("GitHub returned incomplete GraphQL evidence")
  47. return value
  48. class _GateAuthError(RuntimeError):
  49. """gh was refused at HTTP 401/403/404 (or GraphQL returned no repository):
  50. this profile's login cannot see the repo — an identity problem to fix, not
  51. an infrastructure blip to retry."""
  52. def _gh_env(profile_home: str | None) -> dict[str, str] | None:
  53. """Child env for ``gh``: the card's profile identity when one is resolvable.
  54. The completion boundary runs in the worker (assignee), the CLI, or a
  55. reviewer/dispatcher turn, so an ambient ``gh`` login is whichever process
  56. happened to call it (#122689). ``served_profile_child_env(inherit_credentials=True)``
  57. is the seam for "this child acts for that profile": it scrubs the launch
  58. profile's credential residue and overlays the target profile's own
  59. ``GH_TOKEN``/``GH_CONFIG_DIR`` (its ``.env`` + external secret sources).
  60. ``None`` keeps the ambient env — unassigned cards behave exactly as before.
  61. """
  62. if not profile_home:
  63. return None
  64. from tools.environments.local import _is_routed_home, hermes_subprocess_env, served_profile_child_env
  65. base = hermes_subprocess_env(inherit_credentials=True)
  66. routed = _is_routed_home(profile_home)
  67. if routed:
  68. # gh's config dir decides which login `gh api` uses, yet it is a path, not a
  69. # credential, so no scrub list sees it; the target's own value is overlaid from its .env.
  70. base.pop("GH_CONFIG_DIR", None)
  71. env = served_profile_child_env(base=base, target_home=profile_home, inherit_credentials=True)
  72. if routed and not (env.keys() & {"GH_TOKEN", "GITHUB_TOKEN", "GH_CONFIG_DIR"}):
  73. # HOME/XDG_CONFIG_HOME are still the launch process's: without a login of its own the
  74. # child would fall through to ~/.config/gh/hosts.yml — the ambient login. Pin gh's config
  75. # to a profile-owned dir so it fails "not logged in" (exit 4 -> auth) instead.
  76. env["GH_CONFIG_DIR"] = str(Path(profile_home) / "gh")
  77. return env
  78. def _assignee_profile_home(assignee: str | None) -> str | None:
  79. """Home whose ``gh`` login must read the contract repo — the assignee's, resolved
  80. exactly as the dispatcher resolves the worker's home — or None (unassigned) so the
  81. ambient login is used. An assigned card whose profile cannot be resolved is an
  82. identity failure (``auth``), never a silent fall-through to the ambient login."""
  83. if not assignee:
  84. return None
  85. from hermes_cli.profiles import normalize_profile_name, resolve_profile_env
  86. try:
  87. return resolve_profile_env(normalize_profile_name(assignee))
  88. except (FileNotFoundError, ValueError):
  89. raise _GateAuthError(f"assignee profile {assignee!r} cannot be resolved") from None
  90. def collect_acceptance(contract: str, published_pr: str | None,
  91. assignee: str | None = None) -> dict:
  92. receipt = {"ok": False, "classification": "missing", "head_sha": None,
  93. "pr_url": published_pr, "checks": [],
  94. "recovery": "Fix required failures, rerun infrastructure checks or wait, then retry completion. "
  95. "Use kanban_block if human input is needed; receipts remain on the task event log."}
  96. try:
  97. profile_home = _assignee_profile_home(assignee)
  98. declared = _PR.fullmatch(contract)
  99. url = contract if declared else published_pr
  100. match = _PR.fullmatch(url or "")
  101. if not match or (not declared and match[1] != contract) or (declared and published_pr and published_pr != contract):
  102. receipt["detail"] = "Supply metadata.published_pr matching the persisted completion contract."
  103. return receipt
  104. repo, number = match[1], int(match[2])
  105. receipt["pr_url"] = url
  106. owner, name = repo.split("/")
  107. query = f'{{repository(owner:{json.dumps(owner)},name:{json.dumps(name)}){{pullRequest(number:{number:d}){{headRefOid baseRefName state\n baseRef{{branchProtectionRule{{requiredStatusChecks{{context app{{databaseId}}}}}}}}}}}}}}'
  108. repository = _api("graphql", query=query, profile_home=profile_home)["data"]["repository"]
  109. if repository is None:
  110. # A private repo the login cannot read resolves to null, not an error.
  111. raise _GateAuthError(f"HTTP 404 on graphql {repo}")
  112. pr = repository["pullRequest"]
  113. sha, branch = pr["headRefOid"], pr["baseRefName"]
  114. receipt["head_sha"] = sha
  115. if not re.fullmatch(r"[0-9a-f]{40}", sha) or pr["state"] not in {"OPEN", "MERGED"}:
  116. raise ValueError("PR is closed or current head is unavailable")
  117. protection = (pr.get("baseRef") or {}).get("branchProtectionRule") or {}
  118. required = {(r["context"], (r.get("app") or {}).get("databaseId")) for r in protection.get("requiredStatusChecks", [])}
  119. rules = _api(f"repos/{repo}/rules/branches/{quote(branch, safe='')}?per_page=100",
  120. paginate=True, profile_home=profile_home)
  121. for page in rules:
  122. for rule in page:
  123. if rule["type"] == "required_status_checks":
  124. required.update((r["context"], r.get("integration_id"))
  125. for r in rule["parameters"]["required_status_checks"])
  126. receipt["required"] = [{"context": c, "app_id": a} for c, a in sorted(required, key=str)]
  127. if not required:
  128. receipt["detail"] = "No repository-required checks are configured; explicitly use a local-only contract for non-CI tasks."
  129. return receipt
  130. pages = _api(f"repos/{repo}/commits/{sha}/check-runs?per_page=100&filter=latest",
  131. paginate=True, profile_home=profile_home)
  132. runs = [run for page in pages for run in page["check_runs"]]
  133. if len({r["id"] for r in runs}) != pages[0]["total_count"]:
  134. raise ValueError("Incomplete check-run pagination")
  135. statuses = [{**s, "sha": sha} for page in _api(f"repos/{repo}/commits/{sha}/statuses?per_page=100",
  136. paginate=True, profile_home=profile_home) for s in page]
  137. outcomes = []
  138. for context, app_id in sorted(required, key=str):
  139. matching = [r for r in runs if r["name"] == context and
  140. (app_id in (None, -1) or r["app"]["id"] == app_id)]
  141. # A legacy status can satisfy an unpinned context, but never a check pinned to an app.
  142. legacy = [s for s in statuses if s["context"] == context] if app_id in (None, -1) else []
  143. selected = matching + ([max(legacy, key=lambda s: s["id"])] if legacy else [])
  144. if not selected:
  145. outcomes.append("missing")
  146. receipt["checks"].append({"name": context, "classification": "missing", "head_sha": sha})
  147. for check in selected:
  148. is_run = "conclusion" in check
  149. outcome = check.get("conclusion") if is_run else check["state"]
  150. classification = _classify(check, sha, outcome, is_run)
  151. outcomes.append(classification)
  152. receipt["checks"].append({"name": context, "id": check["id"],
  153. "url": check.get("html_url") or check.get("target_url"),
  154. "head_sha": check.get("head_sha", check.get("sha")),
  155. "classification": classification, "conclusion": outcome})
  156. # Re-read after all pages: old-head successes are never transferable.
  157. current = _api(f"repos/{repo}/pulls/{number}", profile_home=profile_home)
  158. if current["head"]["sha"] != sha or current["base"]["ref"] != branch or (current["state"] == "closed" and not current.get("merged")):
  159. receipt.update(classification="stale", detail="PR head/base changed while collecting evidence; retry.")
  160. return receipt
  161. receipt["classification"] = next((x for x in outcomes if x != "success"), "missing" if not outcomes else "success")
  162. receipt["ok"] = receipt["classification"] == "success"
  163. return receipt
  164. except _GateAuthError as exc:
  165. login = f"assignee profile {assignee!r}'s gh login" if assignee else "the ambient gh login"
  166. receipt.update(classification="auth",
  167. detail=f"GitHub refused the acceptance read ({exc}) as {login}; "
  168. "fix that profile's GitHub credentials/access to the repository, then retry completion.")
  169. return receipt
  170. except (OSError, subprocess.SubprocessError, ValueError, KeyError, TypeError, IndexError):
  171. # Never persist gh stderr (credentials/host details); the failed phase is actionable.
  172. receipt.update(classification="infra", detail="GitHub acceptance evidence unavailable or incomplete; check gh authentication/API access and retry.")
  173. return receipt
  174. def _classify(check: dict, sha: str, outcome: str | None, is_run: bool) -> str:
  175. if check.get("head_sha", check.get("sha")) != sha:
  176. return "stale"
  177. if is_run and check.get("status") != "completed":
  178. return "pending"
  179. return {"success": "success", "failure": "failure", "error": "infra", "pending": "pending"}.get(outcome, "infra")