Skip to content

refactor: centralize gh token environment handling - #162

Merged
JounQin merged 1 commit into
mainfrom
refactor/gh-token-env
Nov 13, 2026
Merged

JounQin merged 1 commit into
mainfrom
refactor/gh-token-env

Conversation

@JounQin

@JounQin JounQin commented Nov 12, 2026 •

Copy link
Copy Markdown
Member

Centralizes the repeated gh token environment logic.

  • New packages/host/src/gh-env.ts owns the host classification and token keys:
    • github.com + *.ghe.com → GH_TOKEN / GITHUB_TOKEN
    • GitHub Enterprise Server → GH_ENTERPRISE_TOKEN / GITHUB_ENTERPRISE_TOKEN
  • host.ts account selection now uses clearInheritedTokens, tokenKeys, and setHostToken instead of three copies of the same delete/set blocks.
  • gh.ts action execution uses setHostToken too, which fixes GHES actions that previously set the ignored GH_TOKEN.
  • Regression test covers a GHES action using GH_ENTERPRISE_TOKEN.
  • yarn check: 653/653.

No behavior change for github.com or Enterprise Cloud.

Summary by CodeRabbit

  • Bug Fixes
    • GitHub CLI actions now use the appropriate token environment variable for the selected host, including enterprise hosts.
    • Host account selection now applies the matching token for GitHub.com or an enterprise host and clears inherited GitHub token variables to prevent conflicting credentials.

@coderabbitai

coderabbitai Bot commented Nov 12, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The change centralizes host classification and GitHub token environment handling. Host account handling and gh action execution use the shared helpers. A GHES-host test checks the enterprise token variable.

Changes: Host-aware token environment

Layer / File(s) Summary
Host classification and token helpers
packages/host/src/gh-env.ts
Adds host classification and host-specific token keys. Adds helpers to clear all four token variables and set the token key selected for a host.
Host and gh token integration
packages/host/src/commands/host.ts, packages/host/src/gh.ts, test/unit/gh.test.ts, .changeset/gh-token-env.md
Host account handling uses the shared helpers to check, clear, and set tokens. For runGhAction invocations without --jq, token setup uses setHostToken; the --jq path is unchanged. Adds a GHES-host test and a patch changeset.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Refactor

Possibly related PRs

Suggested reviewers: i1stg

Merge Risk: Minimal · up to 345c2

This change makes GitHub Enterprise Server actions use the correct enterprise token variable, with no behavior change for github.com or Enterprise Cloud. No merge-blocking risk was identified.

Security architecture risk: Low · up to 345c2

Credential selection changes for Enterprise Server actions, while existing host restrictions, repository authorization, and per-invocation credential isolation remain in place. No introduced security bypass was identified. Compatibility with a deployed GitHub CLI was not exercised.

Retained concerns
No architecture-level concerns identified.

Security review details

Security Blast Radius

  • observed — The changed credential delivery reaches the existing GitHub action subprocess. Installation-token requests remain restricted to the requested repository and action permissions; user-identity actions use the separately authorized personal token. These upstream token-selection branches are unchanged by the PR.

Trust Boundaries and Controls

  • observed — The existing broker rejects requests without a configured host, selects the provider using that configured host, and checks repository eligibility, action arguments, configured denials, and identity support before execution. These controls counter the concern that choosing the Enterprise token variable creates an unrestricted caller-selected credential destination.

Resilience and Maintainability Implications

  • observed — The action runner retains its existing timeout, output-limit, process-group termination, and temporary-directory cleanup paths. The provider attempts installation-token revocation in finally after action execution, preserving its warning behavior when revocation fails.
  • observed — The new Enterprise Server regression test asserts that the subprocess receives GH_ENTERPRISE_TOKEN. It uses a shell shim, demonstrating environment assignment rather than actual GitHub CLI authentication behavior. Tests were inspected, not executed during this assessment.
Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title "refactor: centralize gh token environment handling" matches the main change. The PR adds gh-env.ts, which centralizes host classification and GitHub CLI token-key selection, and updates hos…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title "refactor: centralize gh token environment handling" matches the main change. The PR adds gh-env.ts, which centralizes host classification and GitHub CLI token-key selection, and updates host.ts and gh.ts to use the shared helpers. The title is concise, uses a conventional type prefix, and avoids vague terms. The title does not mention the GHES fix (GH_ENTERPRISE_TOKEN for GHES actions), but the title does not need to cover every detail.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 7ce62193-b18f-4190-99cf-eae081bc16b6
Commits

Reviewing files that changed from the base of the PR and between 686b638 and 345c2f3.

Files selected for processing: 5

  • .changeset/gh-token-env.md
  • packages/host/src/commands/host.ts
  • packages/host/src/gh-env.ts
  • packages/host/src/gh.ts
  • test/unit/gh.test.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 8 remain after this review.

@JounQin
JounQin merged commit d462a04 into main Nov 13, 2026
10 checks passed
@JounQin
JounQin deleted the refactor/gh-token-env branch November 13, 2026 04:12
@github-actions github-actions Bot mentioned this pull request Nov 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant