From fab7d8de52dc29f5c2a795b82bbfd52738aa4799 Mon Sep 17 00:00:00 2001 From: zhifu gao Date: Tue, 30 Jun 2026 12:34:02 +0800 Subject: [PATCH] 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. --- scripts/collect_growth_metrics.py | 17 ++++++++++++++++- tests/test_collect_growth_metrics.py | 18 +++++++++++++++++- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/scripts/collect_growth_metrics.py b/scripts/collect_growth_metrics.py index 6b997fe44..dd310ca93 100755 --- a/scripts/collect_growth_metrics.py +++ b/scripts/collect_growth_metrics.py @@ -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()) \ No newline at end of file diff --git a/tests/test_collect_growth_metrics.py b/tests/test_collect_growth_metrics.py index 1159de7b9..94d712c36 100644 --- a/tests/test_collect_growth_metrics.py +++ b/tests/test_collect_growth_metrics.py @@ -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" \ No newline at end of file