Use gh auth token for growth metrics

Fallback to the local gh auth token when GITHUB_TOKEN is not set so growth metrics runs avoid anonymous API rate limits. Also mock the gh token lookup in the regression test for cross-platform compatibility.
This commit is contained in:
zhifu gao
2026-06-30 12:34:02 +08:00
committed by GitHub
parent 40428dc266
commit fab7d8de52
2 changed files with 33 additions and 2 deletions
+16 -1
View File
@@ -11,6 +11,7 @@ import argparse
import datetime as dt
import json
import os
import subprocess
import sys
import urllib.error
import urllib.request
@@ -62,6 +63,20 @@ def github_headers() -> Dict[str, str]:
"User-Agent": "funasr-growth-metrics",
}
token = os.environ.get("GITHUB_TOKEN")
if not token:
try:
completed = subprocess.run(
["gh", "auth", "token"],
check=True,
stdout=subprocess.PIPE,
stderr=subprocess.DEVNULL,
text=True,
timeout=5,
)
except (OSError, subprocess.SubprocessError):
token = None
else:
token = completed.stdout.strip()
if token:
headers["Authorization"] = f"Bearer {token}"
return headers
@@ -547,4 +562,4 @@ def main() -> int:
if __name__ == "__main__":
raise SystemExit(main())
raise SystemExit(main())
+17 -1
View File
@@ -1,6 +1,7 @@
import importlib.util
import io
import json
import subprocess
import sys
from datetime import date, datetime, timezone
from contextlib import redirect_stdout
@@ -21,6 +22,21 @@ def test_default_integration_prs_include_sglang_omni_fun_asr():
assert "sgl-project/sglang-omni#898" in module.DEFAULT_INTEGRATION_PRS
def test_github_headers_falls_back_to_gh_auth_token(monkeypatch):
module = load_growth_metrics_module()
monkeypatch.delenv("GITHUB_TOKEN", raising=False)
def fake_run(args, **kwargs):
assert args == ["gh", "auth", "token"]
return subprocess.CompletedProcess(args=args, returncode=0, stdout="cli-token\n")
monkeypatch.setattr(module.subprocess, "run", fake_run)
headers = module.github_headers()
assert headers["Authorization"] == "Bearer cli-token"
def test_default_integration_prs_include_high_visibility_external_queue():
module = load_growth_metrics_module()
@@ -622,4 +638,4 @@ def test_main_outputs_integrations_json(monkeypatch):
integration = payload["integrations"][0]
assert integration["pr"] == "ray-project/ray#64053"
assert integration["checks"]["state"] == "success"
assert integration["next_action"] == "request review"
assert integration["next_action"] == "request review"