|
| 1 | +import type { CheckResult, CheckDefinition } from "../types" |
| 2 | +import { CHECK_IDS, CHECK_NAMES } from "../constants" |
| 3 | + |
| 4 | +export interface GhCliInfo { |
| 5 | + installed: boolean |
| 6 | + version: string | null |
| 7 | + path: string | null |
| 8 | + authenticated: boolean |
| 9 | + username: string | null |
| 10 | + scopes: string[] |
| 11 | + error: string | null |
| 12 | +} |
| 13 | + |
| 14 | +async function checkBinaryExists(binary: string): Promise<{ exists: boolean; path: string | null }> { |
| 15 | + try { |
| 16 | + const proc = Bun.spawn(["which", binary], { stdout: "pipe", stderr: "pipe" }) |
| 17 | + const output = await new Response(proc.stdout).text() |
| 18 | + await proc.exited |
| 19 | + if (proc.exitCode === 0) { |
| 20 | + return { exists: true, path: output.trim() } |
| 21 | + } |
| 22 | + } catch { |
| 23 | + // intentionally empty - binary not found |
| 24 | + } |
| 25 | + return { exists: false, path: null } |
| 26 | +} |
| 27 | + |
| 28 | +async function getGhVersion(): Promise<string | null> { |
| 29 | + try { |
| 30 | + const proc = Bun.spawn(["gh", "--version"], { stdout: "pipe", stderr: "pipe" }) |
| 31 | + const output = await new Response(proc.stdout).text() |
| 32 | + await proc.exited |
| 33 | + if (proc.exitCode === 0) { |
| 34 | + const match = output.match(/gh version (\S+)/) |
| 35 | + return match?.[1] ?? output.trim().split("\n")[0] |
| 36 | + } |
| 37 | + } catch { |
| 38 | + // intentionally empty - version unavailable |
| 39 | + } |
| 40 | + return null |
| 41 | +} |
| 42 | + |
| 43 | +async function getGhAuthStatus(): Promise<{ |
| 44 | + authenticated: boolean |
| 45 | + username: string | null |
| 46 | + scopes: string[] |
| 47 | + error: string | null |
| 48 | +}> { |
| 49 | + try { |
| 50 | + const proc = Bun.spawn(["gh", "auth", "status"], { |
| 51 | + stdout: "pipe", |
| 52 | + stderr: "pipe", |
| 53 | + env: { ...process.env, GH_NO_UPDATE_NOTIFIER: "1" }, |
| 54 | + }) |
| 55 | + const stdout = await new Response(proc.stdout).text() |
| 56 | + const stderr = await new Response(proc.stderr).text() |
| 57 | + await proc.exited |
| 58 | + |
| 59 | + const output = stderr || stdout |
| 60 | + |
| 61 | + if (proc.exitCode === 0) { |
| 62 | + const usernameMatch = output.match(/Logged in to github\.com account (\S+)/) |
| 63 | + const username = usernameMatch?.[1]?.replace(/[()]/g, "") ?? null |
| 64 | + |
| 65 | + const scopesMatch = output.match(/Token scopes?:\s*(.+)/i) |
| 66 | + const scopes = scopesMatch?.[1] |
| 67 | + ? scopesMatch[1] |
| 68 | + .split(/,\s*/) |
| 69 | + .map((s) => s.replace(/['"]/g, "").trim()) |
| 70 | + .filter(Boolean) |
| 71 | + : [] |
| 72 | + |
| 73 | + return { authenticated: true, username, scopes, error: null } |
| 74 | + } |
| 75 | + |
| 76 | + const errorMatch = output.match(/error[:\s]+(.+)/i) |
| 77 | + return { |
| 78 | + authenticated: false, |
| 79 | + username: null, |
| 80 | + scopes: [], |
| 81 | + error: errorMatch?.[1]?.trim() ?? "Not authenticated", |
| 82 | + } |
| 83 | + } catch (err) { |
| 84 | + return { |
| 85 | + authenticated: false, |
| 86 | + username: null, |
| 87 | + scopes: [], |
| 88 | + error: err instanceof Error ? err.message : "Failed to check auth status", |
| 89 | + } |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +export async function getGhCliInfo(): Promise<GhCliInfo> { |
| 94 | + const binaryCheck = await checkBinaryExists("gh") |
| 95 | + |
| 96 | + if (!binaryCheck.exists) { |
| 97 | + return { |
| 98 | + installed: false, |
| 99 | + version: null, |
| 100 | + path: null, |
| 101 | + authenticated: false, |
| 102 | + username: null, |
| 103 | + scopes: [], |
| 104 | + error: null, |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + const [version, authStatus] = await Promise.all([getGhVersion(), getGhAuthStatus()]) |
| 109 | + |
| 110 | + return { |
| 111 | + installed: true, |
| 112 | + version, |
| 113 | + path: binaryCheck.path, |
| 114 | + authenticated: authStatus.authenticated, |
| 115 | + username: authStatus.username, |
| 116 | + scopes: authStatus.scopes, |
| 117 | + error: authStatus.error, |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +export async function checkGhCli(): Promise<CheckResult> { |
| 122 | + const info = await getGhCliInfo() |
| 123 | + const name = CHECK_NAMES[CHECK_IDS.GH_CLI] |
| 124 | + |
| 125 | + if (!info.installed) { |
| 126 | + return { |
| 127 | + name, |
| 128 | + status: "warn", |
| 129 | + message: "Not installed (optional)", |
| 130 | + details: [ |
| 131 | + "GitHub CLI is used by librarian agent and scripts", |
| 132 | + "Install: https://cli.github.com/", |
| 133 | + ], |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + if (!info.authenticated) { |
| 138 | + return { |
| 139 | + name, |
| 140 | + status: "warn", |
| 141 | + message: `${info.version ?? "installed"} - not authenticated`, |
| 142 | + details: [ |
| 143 | + info.path ? `Path: ${info.path}` : null, |
| 144 | + "Authenticate: gh auth login", |
| 145 | + info.error ? `Error: ${info.error}` : null, |
| 146 | + ].filter((d): d is string => d !== null), |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + const details: string[] = [] |
| 151 | + if (info.path) details.push(`Path: ${info.path}`) |
| 152 | + if (info.username) details.push(`Account: ${info.username}`) |
| 153 | + if (info.scopes.length > 0) details.push(`Scopes: ${info.scopes.join(", ")}`) |
| 154 | + |
| 155 | + return { |
| 156 | + name, |
| 157 | + status: "pass", |
| 158 | + message: `${info.version ?? "installed"} - authenticated as ${info.username ?? "unknown"}`, |
| 159 | + details: details.length > 0 ? details : undefined, |
| 160 | + } |
| 161 | +} |
| 162 | + |
| 163 | +export function getGhCliCheckDefinition(): CheckDefinition { |
| 164 | + return { |
| 165 | + id: CHECK_IDS.GH_CLI, |
| 166 | + name: CHECK_NAMES[CHECK_IDS.GH_CLI], |
| 167 | + category: "tools", |
| 168 | + check: checkGhCli, |
| 169 | + critical: false, |
| 170 | + } |
| 171 | +} |
0 commit comments