Surface per-host gh auth state for GitHub Enterprise
diagnoseGhAuth accepts the host a surface needs credentials for, scopes the account/scope diagnosis to that host, and reports whether gh has any login there; GhAuthErrorHelp renders host-qualified login/refresh commands so an unauthenticated GHES host stops masquerading as a github.com scope problem. Also fixes the mobile paste-intent expectation for host-carrying parsed links. Co-authored-by: Orca <help@stably.ai>
This commit is contained in:
@@ -1,5 +1,11 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import { parseAuthStatus } from './auth-diagnose'
|
||||
import { describe, expect, it, vi } from 'vitest'
|
||||
|
||||
const { ghExecFileAsyncMock } = vi.hoisted(() => ({ ghExecFileAsyncMock: vi.fn() }))
|
||||
|
||||
vi.mock('../git/runner', () => ({
|
||||
ghExecFileAsync: ghExecFileAsyncMock
|
||||
}))
|
||||
import { diagnoseGhAuth, parseAuthStatus } from './auth-diagnose'
|
||||
|
||||
describe('parseAuthStatus', () => {
|
||||
it('parses an env-shadowed login alongside a keyring login (real gh output)', () => {
|
||||
@@ -95,3 +101,48 @@ ghe.acme.io
|
||||
expect(accounts[0].host).toBe('github.acme.io')
|
||||
})
|
||||
})
|
||||
|
||||
describe('diagnoseGhAuth host scoping', () => {
|
||||
const TWO_HOST_STATUS = `github.com
|
||||
✓ Logged in to github.com account alice (keyring)
|
||||
- Active account: true
|
||||
- Token scopes: 'project', 'read:org', 'repo'
|
||||
ghe.acme.io
|
||||
✓ Logged in to ghe.acme.io account bob (keyring)
|
||||
- Active account: true
|
||||
- Token scopes: 'repo'
|
||||
`
|
||||
|
||||
it('reports an unauthenticated required host and keeps scope advice host-scoped', async () => {
|
||||
ghExecFileAsyncMock.mockResolvedValue({ stdout: TWO_HOST_STATUS, stderr: '' })
|
||||
|
||||
const diag = await diagnoseGhAuth('github.other-corp.com')
|
||||
|
||||
expect(diag.requiredHost).toBe('github.other-corp.com')
|
||||
expect(diag.requiredHostAuthenticated).toBe(false)
|
||||
expect(diag.activeAccount).toBeNull()
|
||||
})
|
||||
|
||||
it("scopes the diagnosis to the required host's account", async () => {
|
||||
ghExecFileAsyncMock.mockResolvedValue({ stdout: TWO_HOST_STATUS, stderr: '' })
|
||||
|
||||
const diag = await diagnoseGhAuth('GHE.Acme.IO')
|
||||
|
||||
expect(diag.requiredHost).toBe('ghe.acme.io')
|
||||
expect(diag.requiredHostAuthenticated).toBe(true)
|
||||
expect(diag.activeAccount?.user).toBe('bob')
|
||||
// Why: bob's GHES token lacks project/read:org even though alice's
|
||||
// github.com token has them — advice must reflect the required host.
|
||||
expect(diag.missingScopes).toEqual(['project', 'read:org'])
|
||||
})
|
||||
|
||||
it('keeps legacy host-less behavior byte-identical', async () => {
|
||||
ghExecFileAsyncMock.mockResolvedValue({ stdout: TWO_HOST_STATUS, stderr: '' })
|
||||
|
||||
const diag = await diagnoseGhAuth()
|
||||
|
||||
expect(diag.requiredHost).toBeNull()
|
||||
expect(diag.requiredHostAuthenticated).toBeNull()
|
||||
expect(diag.activeAccount?.user).toBe('alice')
|
||||
})
|
||||
})
|
||||
|
||||
@@ -90,7 +90,7 @@ export function parseAuthStatus(text: string): GhAuthAccount[] {
|
||||
return accounts
|
||||
}
|
||||
|
||||
export async function diagnoseGhAuth(): Promise<GhAuthDiagnostic> {
|
||||
export async function diagnoseGhAuth(requiredHost?: string): Promise<GhAuthDiagnostic> {
|
||||
let raw = ''
|
||||
let ghAvailable = true
|
||||
try {
|
||||
@@ -118,7 +118,16 @@ export async function diagnoseGhAuth(): Promise<GhAuthDiagnostic> {
|
||||
}
|
||||
}
|
||||
const accounts = parseAuthStatus(raw)
|
||||
const active = accounts.find((a) => a.active) ?? accounts[0] ?? null
|
||||
// Why: when the caller names a host (a GHES origin), scope the diagnosis to
|
||||
// that host's account — the github.com account's scopes are irrelevant to it.
|
||||
const normalizedRequiredHost = requiredHost?.trim().toLowerCase() || null
|
||||
const hostAccounts = normalizedRequiredHost
|
||||
? accounts.filter((a) => a.host.toLowerCase() === normalizedRequiredHost)
|
||||
: accounts
|
||||
const active =
|
||||
hostAccounts.find((a) => a.active) ??
|
||||
hostAccounts[0] ??
|
||||
(normalizedRequiredHost ? null : (accounts.find((a) => a.active) ?? accounts[0] ?? null))
|
||||
const envTokenInProcess: 'GITHUB_TOKEN' | 'GH_TOKEN' | null = process.env.GH_TOKEN
|
||||
? 'GH_TOKEN'
|
||||
: process.env.GITHUB_TOKEN
|
||||
@@ -141,6 +150,8 @@ export async function diagnoseGhAuth(): Promise<GhAuthDiagnostic> {
|
||||
envTokenInProcess,
|
||||
missingScopes,
|
||||
requiredScopes: [...REQUIRED_SCOPES],
|
||||
hasKeyringFallback: Boolean(keyringFallback && keyringFallback !== active)
|
||||
hasKeyringFallback: Boolean(keyringFallback && keyringFallback !== active),
|
||||
requiredHost: normalizedRequiredHost,
|
||||
requiredHostAuthenticated: normalizedRequiredHost ? hostAccounts.length > 0 : null
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user