Skip to content

feat(api): fallback to gh auth token when GITHUB_TOKEN is not set #94

@shouze

Description

@shouze

Context

Part of the gh-gcs epic (#93).

When github-code-search is invoked as a gh extension (gh gcs), users have already authenticated via gh auth login. Requiring them to also export GITHUB_TOKEN is unnecessary friction.

Goal

In src/api.ts, before throwing on a missing token, attempt to retrieve it from the gh CLI:

// Fallback: delegate to gh CLI if GITHUB_TOKEN is not set
const ghToken = Bun.spawnSync(["gh", "auth", "token"]).stdout.toString().trim();
if (ghToken) return ghToken;

The fallback must be silent if gh is not installed (no crash, no warning).

Behaviour

Situation Expected result
GITHUB_TOKEN set Used as-is (no change)
GITHUB_TOKEN unset, gh auth login done Token retrieved from gh auth token
GITHUB_TOKEN unset, gh not installed Error: "No GitHub token found. Set GITHUB_TOKEN or run gh auth login."

Implementation notes

  • The change lives in src/api.ts in the token resolution function
  • Bun.spawnSync exits with a non-zero code if not authenticated — handle this gracefully
  • No new dependency required

Acceptance criteria

  • Running gh gcs --org fulll "query" works without GITHUB_TOKEN after gh auth login
  • GITHUB_TOKEN continues to take precedence over gh auth token
  • Absence of gh CLI does not crash the process — it falls through to the existing error
  • bun test && bun run lint pass

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions