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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1161,7 +1161,9 @@ export function registerGitHubHandlers(store: Store, stats: StatsCollector): voi
|
||||
getRateLimit(args?.force ? { force: true } : undefined)
|
||||
)
|
||||
|
||||
ipcMain.handle('gh:diagnoseAuth', () => diagnoseGhAuth())
|
||||
ipcMain.handle('gh:diagnoseAuth', (_event, args?: { host?: string }) =>
|
||||
diagnoseGhAuth(args?.host)
|
||||
)
|
||||
|
||||
// ── GitHub ProjectV2 view handlers ─────────────────────────────────
|
||||
// Why: registered unconditionally so enabling the experimental flag at
|
||||
|
||||
@@ -1758,7 +1758,7 @@ export type PreloadApi = {
|
||||
* shell and silently shadows the keyring credential — in that case
|
||||
* `gh auth refresh` is a no-op and the UI must say so.
|
||||
*/
|
||||
diagnoseAuth: () => Promise<GhAuthDiagnostic>
|
||||
diagnoseAuth: (args?: { host?: string }) => Promise<GhAuthDiagnostic>
|
||||
// ── ProjectV2 (GitHub Projects) ─────────────────────────────────
|
||||
listAccessibleProjects: (
|
||||
args?: ListAccessibleProjectsArgs
|
||||
|
||||
@@ -1540,7 +1540,8 @@ const api = {
|
||||
rateLimit: (args?: { force?: boolean }): Promise<GetRateLimitResult> =>
|
||||
ipcRenderer.invoke('gh:rateLimit', args),
|
||||
|
||||
diagnoseAuth: (): Promise<GhAuthDiagnostic> => ipcRenderer.invoke('gh:diagnoseAuth'),
|
||||
diagnoseAuth: (args?: { host?: string }): Promise<GhAuthDiagnostic> =>
|
||||
ipcRenderer.invoke('gh:diagnoseAuth', args),
|
||||
|
||||
// ── ProjectV2 (GitHub Projects) ───────────────────────────────────
|
||||
listAccessibleProjects: (
|
||||
|
||||
@@ -21,6 +21,20 @@ type AuthErrorKind = 'auth_required' | 'scope_missing'
|
||||
const REFRESH_CMD = 'gh auth refresh -s project -s read:org -s repo'
|
||||
const LOGIN_CMD = 'gh auth login'
|
||||
|
||||
// Why: GHES credentials are per-host — a bare `gh auth login` signs into
|
||||
// github.com and leaves the Enterprise host exactly as broken as before.
|
||||
function loginCommandForHost(host: string | null | undefined): string {
|
||||
return host && host.toLowerCase() !== 'github.com'
|
||||
? `gh auth login --hostname ${host}`
|
||||
: LOGIN_CMD
|
||||
}
|
||||
|
||||
function refreshCommandForHost(host: string | null | undefined): string {
|
||||
return host && host.toLowerCase() !== 'github.com'
|
||||
? `gh auth refresh --hostname ${host} -s project -s read:org -s repo`
|
||||
: REFRESH_CMD
|
||||
}
|
||||
|
||||
// AGENTS.md requires platform-specific shell guidance. The env-shadow
|
||||
// remediation needs different commands per host shell — bash/zsh on
|
||||
// macOS/Linux vs PowerShell on Windows.
|
||||
@@ -130,6 +144,24 @@ function buildRemediation(
|
||||
}
|
||||
}
|
||||
|
||||
// Why: an Enterprise host with no gh account at all outranks every other
|
||||
// diagnosis — env-token or scope advice for github.com would be misleading.
|
||||
if (diag.requiredHost && diag.requiredHostAuthenticated === false) {
|
||||
return {
|
||||
summary: `\`gh\` is not signed in to ${diag.requiredHost}.`,
|
||||
detail: `GitHub Enterprise hosts need their own \`gh\` login, separate from github.com. Run the login command in a terminal, complete the browser flow on ${diag.requiredHost}, then reload.`,
|
||||
commands: [
|
||||
{
|
||||
label: translate(
|
||||
'auto.components.github.project.GhAuthErrorHelp.9c2da6353b',
|
||||
'Copy login command'
|
||||
),
|
||||
command: loginCommandForHost(diag.requiredHost)
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
if (!diag.ghAvailable) {
|
||||
return {
|
||||
summary: 'GitHub CLI (`gh`) is not installed or not on PATH.',
|
||||
@@ -191,7 +223,7 @@ function buildRemediation(
|
||||
'auto.components.github.project.GhAuthErrorHelp.9c2da6353b',
|
||||
'Copy login command'
|
||||
),
|
||||
command: LOGIN_CMD
|
||||
command: loginCommandForHost(diag.requiredHost)
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -213,7 +245,7 @@ function buildRemediation(
|
||||
'auto.components.github.project.GhAuthErrorHelp.3fefeebde4',
|
||||
'Copy refresh command'
|
||||
),
|
||||
command: REFRESH_CMD
|
||||
command: refreshCommandForHost(diag.requiredHost)
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -242,16 +274,19 @@ function buildRemediation(
|
||||
|
||||
export function GhAuthErrorHelp({
|
||||
error,
|
||||
variant = 'block'
|
||||
variant = 'block',
|
||||
host
|
||||
}: {
|
||||
error: GitHubProjectViewError & { type: AuthErrorKind }
|
||||
variant?: 'block' | 'banner'
|
||||
/** GHES host the failing surface talks to; scopes the diagnosis per host. */
|
||||
host?: string
|
||||
}): React.JSX.Element {
|
||||
const [diag, setDiag] = useState<GhAuthDiagnostic | null>(null)
|
||||
useEffect(() => {
|
||||
let cancelled = false
|
||||
window.api.gh
|
||||
.diagnoseAuth()
|
||||
.diagnoseAuth(host ? { host } : undefined)
|
||||
.then((d) => {
|
||||
if (!cancelled) {
|
||||
setDiag(d)
|
||||
@@ -262,7 +297,7 @@ export function GhAuthErrorHelp({
|
||||
return () => {
|
||||
cancelled = true
|
||||
}
|
||||
}, [])
|
||||
}, [host])
|
||||
|
||||
const remedy = buildRemediation(error.message, error.type, diag)
|
||||
const docsUrl = remedy.docsUrl
|
||||
|
||||
@@ -36,4 +36,11 @@ export type GhAuthDiagnostic = {
|
||||
* that the user could fall back to by unsetting the env var.
|
||||
*/
|
||||
hasKeyringFallback: boolean
|
||||
/**
|
||||
* The GitHub host the caller needs credentials for (e.g. a GHES origin).
|
||||
* Null when the probe ran without host context.
|
||||
*/
|
||||
requiredHost: string | null
|
||||
/** Whether gh has any account for `requiredHost`; null without host context. */
|
||||
requiredHostAuthenticated: boolean | null
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user