Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/loopzero/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,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"
Expand Down Expand Up @@ -297,7 +298,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():
Expand Down Expand Up @@ -1463,7 +1464,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)
Expand Down
21 changes: 20 additions & 1 deletion tests/test_github.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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}}}}

Expand Down
Loading