fix: improve gh auth detection to work with GH_TOKEN env var

Use gh api user to verify authentication instead of gh auth status,
which can return non-zero even when GH_TOKEN is valid (due to stale
config file entries).

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
Kacper
2025-12-28 20:03:39 +01:00
parent 6012e8312b
commit bad4393dda

View File

@@ -94,23 +94,30 @@ async function getGhStatus(): Promise<GhStatus> {
// Version command failed // Version command failed
} }
// Check authentication status // Check authentication status by actually making an API call
// gh auth status can return non-zero even when GH_TOKEN is valid
try { try {
const { stdout } = await execAsync('gh auth status', { env: execEnv }); const { stdout } = await execAsync('gh api user --jq ".login"', { env: execEnv });
// If this succeeds without error, we're authenticated const user = stdout.trim();
status.authenticated = true; if (user) {
status.authenticated = true;
// Try to extract username from output status.user = user;
const userMatch =
stdout.match(/Logged in to [^\s]+ account ([^\s]+)/i) ||
stdout.match(/Logged in to [^\s]+ as ([^\s]+)/i);
if (userMatch) {
status.user = userMatch[1];
} }
} catch (error: unknown) { } catch {
// Auth status returns non-zero if not authenticated // API call failed - try gh auth status as fallback
const err = error as { stderr?: string }; try {
if (err.stderr?.includes('not logged in')) { const { stdout } = await execAsync('gh auth status', { env: execEnv });
status.authenticated = true;
// Try to extract username from output
const userMatch =
stdout.match(/Logged in to [^\s]+ account ([^\s]+)/i) ||
stdout.match(/Logged in to [^\s]+ as ([^\s]+)/i);
if (userMatch) {
status.user = userMatch[1];
}
} catch {
// Auth status returns non-zero if not authenticated
status.authenticated = false; status.authenticated = false;
} }
} }