|
20 | 20 |
|
21 | 21 | import argparse |
22 | 22 | import json |
| 23 | +import os |
23 | 24 | import re |
24 | 25 | import subprocess |
25 | 26 | import sys |
|
98 | 99 | # ---------------------------------------------------------------- gh helpers |
99 | 100 |
|
100 | 101 |
|
101 | | -def gh_api(path: str, paginate: bool = False) -> Any: |
| 102 | +def gh_api(path: str, paginate: bool = False, token: str | None = None) -> Any: |
102 | 103 | cmd = ["gh", "api", "-H", "Accept: application/vnd.github+json"] |
103 | 104 | if paginate: |
104 | 105 | cmd += ["--paginate", "--slurp"] |
105 | 106 | cmd.append(path) |
| 107 | + env = {**os.environ, "GH_TOKEN": token} if token else None |
106 | 108 | proc = subprocess.run( |
107 | 109 | cmd, capture_output=True, text=True, check=False, |
108 | 110 | encoding="utf-8", errors="replace", |
| 111 | + env=env, |
109 | 112 | ) |
110 | 113 | if proc.returncode != 0: |
111 | 114 | raise RuntimeError(f"gh api {path} failed: {proc.stderr.strip()}") |
@@ -219,14 +222,22 @@ def detect_repo() -> str: |
219 | 222 |
|
220 | 223 |
|
221 | 224 | def load_reviewer_set(org: str) -> set[str]: |
| 225 | + # Reading org team membership requires a token with org:read scope. |
| 226 | + # The default Actions GITHUB_TOKEN can't do this, so use OTELBOT_TOKEN |
| 227 | + # (a GitHub App installation token) when present; fall back to the |
| 228 | + # default GH_TOKEN otherwise (useful for local runs with a user token). |
| 229 | + token = os.environ.get("OTELBOT_TOKEN") or None |
222 | 230 | reviewers: set[str] = set() |
223 | 231 | for slug in APPROVER_TEAM_SLUGS: |
224 | | - members = gh_api(f"/orgs/{org}/teams/{slug}/members?per_page=100", paginate=True) |
| 232 | + members = gh_api( |
| 233 | + f"/orgs/{org}/teams/{slug}/members?per_page=100", |
| 234 | + paginate=True, token=token, |
| 235 | + ) |
225 | 236 | reviewers.update(m["login"] for m in members) |
226 | 237 | if not reviewers: |
227 | 238 | raise RuntimeError( |
228 | 239 | f"no reviewers found in teams {APPROVER_TEAM_SLUGS}; " |
229 | | - f"the GH_TOKEN must have org:read permission" |
| 240 | + f"the token must have org:read permission" |
230 | 241 | ) |
231 | 242 | return {r.lower() for r in reviewers} |
232 | 243 |
|
|
0 commit comments