diff --git a/src/loopzero/github.py b/src/loopzero/github.py index e40611b9..bff2f18e 100644 --- a/src/loopzero/github.py +++ b/src/loopzero/github.py @@ -24,7 +24,8 @@ from loopzero.runners import review_settings from loopzero.types import BLOCKING, Finding, ReviewResult -GH_ENV = ("PATH", "HOME", "LANG", "LC_ALL", "TERM", "GH_TOKEN", "GITHUB_TOKEN", "GH_HOST") +GH_BASE_ENV = ("PATH", "HOME", "LANG", "LC_ALL", "TERM", "GH_HOST") +GH_ENV = (*GH_BASE_ENV, "GH_TOKEN", "GITHUB_TOKEN") APP_TOKEN_HELPER = Path.home() / ".config/loopzero/app-token" _app_token_cache = ("", 0.0) MISSING_REVIEW_REASON = "no review recorded for the current head" @@ -294,7 +295,7 @@ def app_token() -> str: return "" minted = _app_token_clock() # before minting so a suspend mid-call ages the token try: - result = run([str(APP_TOKEN_HELPER)], cwd=Path("/"), env_allowlist=GH_ENV, timeout=_read_timeout(10)) + result = run([str(APP_TOKEN_HELPER)], cwd=Path("/"), env_allowlist=GH_BASE_ENV, timeout=_read_timeout(10)) except (ToolMissing, ProcTimeout, ReadDeadline, OSError): return "" if result.exit_code or not result.stdout.strip(): @@ -1518,7 +1519,7 @@ def delete_remote_branch(repo: str, branch: str, expected_head: str) -> bool: env = {"GIT_CONFIG_NOSYSTEM": "1", "GIT_CONFIG_GLOBAL": os.devnull, "GIT_TERMINAL_PROMPT": "0"} with tempfile.TemporaryDirectory(prefix="loopzero-cleanup-") as scratch: init = ("git", "init", "--bare", "--template=") - done = run(init, cwd=scratch, env_allowlist=GH_ENV, extra_env=env, timeout=120) + done = run(init, cwd=scratch, env_allowlist=GH_BASE_ENV, extra_env=env, timeout=120) if done.exit_code != 0: raise GhError(init, done.stderr or done.stdout) done = run(argv, cwd=scratch, env_allowlist=GH_ENV, extra_env=env, timeout=120) diff --git a/tests/test_github.py b/tests/test_github.py index 8585774c..e2843559 100644 --- a/tests/test_github.py +++ b/tests/test_github.py @@ -742,7 +742,11 @@ def test_api_get_and_login_helpers(gh: FakeGh) -> None: def test_app_identity_selection_and_cache(gh: FakeGh, tmp_path: Path, monkeypatch, app_token_helper) -> None: counter = tmp_path / "helper-calls" - app_token_helper(f"echo called >> {counter}\necho synthetic-app-token\n") + app_token_helper( + 'test -z "${GH_TOKEN+x}${GITHUB_TOKEN+x}" || exit 1\n' + f"echo called >> {counter}\necho synthetic-app-token\n" + ) + monkeypatch.setenv("GITHUB_TOKEN", "secondary-operator-token") wall_time = boot_time = 10_000.0 monkeypatch.setattr(github.time, "time", lambda: wall_time) monkeypatch.setattr(github.time, "CLOCK_BOOTTIME", 7, raising=False) @@ -1187,6 +1191,21 @@ def test_delete_remote_branch_checks_encoded_ref(gh: FakeGh, remote_head: str | assert len(gh.calls) == 1 +def test_delete_remote_branch_scopes_operator_tokens(gh: FakeGh, fake_tool, monkeypatch) -> None: + monkeypatch.setenv("GH_TOKEN", "operator-token") + monkeypatch.setenv("GITHUB_TOKEN", "secondary-operator-token") + gh.respond(f"api repos/{REPO}/git/ref/heads/probe", {"object": {"sha": HEAD}}) + fake_tool("git", f'''case "$1" in + init) test -z "${{GH_TOKEN+x}}${{GITHUB_TOKEN+x}}" ;; + -c) test "$GH_TOKEN:$GITHUB_TOKEN" = "operator-token:secondary-operator-token" && + test "$2" = 'credential.helper=!gh auth git-credential' && + test "$3" = push && + test "$4" = '--force-with-lease=refs/heads/probe:{HEAD}' ;; + *) exit 1 ;; + esac''') + assert github.delete_remote_branch(REPO, "probe", HEAD) + + def _queue_json(queued: bool) -> dict: return {"data": {"repository": {"pullRequest": {"isInMergeQueue": queued}}}}