Files
libretro/scripts/core/github.py
T
Abdessamad Derraz 83e810d1b3 fix: let core.upstream refuse requests offline
RETROBIOS_OFFLINE was read by six callers and never by the network
gateway: a caller that did not pass offline=True sent the request,
untokened. core.offline holds the rule, open_retrying refuses with
OfflineError and the cached reads answer as an offline run. The callers
read the same predicate, and the tests that fake the network clear the
variable for themselves (tests.ONLINE).
2026-11-24 00:20:03 +01:00

52 lines
1.6 KiB
Python

"""The token a run uses for the GitHub API."""
from __future__ import annotations
import functools
import os
from core.offline import run_is_offline
@functools.lru_cache(maxsize=1)
def _gh_cli_token() -> str:
"""What `gh auth token` prints, or "" when gh is absent or logged out."""
import logging
import subprocess
log = logging.getLogger(__name__)
try:
proc = subprocess.run(
["gh", "auth", "token"], capture_output=True, text=True,
timeout=10, check=False,
)
except FileNotFoundError:
log.debug("gh is not installed, GitHub API calls go anonymous")
return ""
except (subprocess.TimeoutExpired, OSError) as exc:
log.warning("gh auth token did not answer: %s", exc)
return ""
if proc.returncode != 0:
log.debug("gh auth token: %s", (proc.stderr or "").strip())
return ""
return (proc.stdout or "").strip()
def github_token() -> str:
"""The token for api.github.com: GITHUB_TOKEN, GH_TOKEN, then gh's own.
A maintainer logged in to gh had to export GITHUB_TOKEN by hand before
profile_sync, the scrapers or an online pipeline, which refused the run
or burnt the anonymous quota of 60 calls an hour. The token gh returns
is put in the environment, so the subprocesses a run starts and the
readers of os.environ get it too. RETROBIOS_OFFLINE runs make no API
call and never ask gh.
"""
token = os.environ.get("GITHUB_TOKEN") or os.environ.get("GH_TOKEN") or ""
if token or run_is_offline():
return token
token = _gh_cli_token()
if token:
os.environ["GITHUB_TOKEN"] = token
return token