Skip to content

armgit / Ix

Public Imported from GitHub
0

Understand any codebase instantly. System intelligence for codebases, built for humans and AI.

feat: add GitHub auth resolution (token flag -> env -> gh CLI)

riley022721fc2aee9470
diff --git a/ix-cli/src/cli/__tests__/github-auth.test.ts b/ix-cli/src/cli/__tests__/github-auth.test.ts
new file mode 100644
index 00000000..67b6722f
--- /dev/null
+++ b/ix-cli/src/cli/__tests__/github-auth.test.ts
@@ -0,0 +1,26 @@
+import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
+
+describe("GitHub auth resolution", () => {
+ const originalEnv = process.env;
+
+ beforeEach(() => {
+ process.env = { ...originalEnv };
+ });
+
+ afterEach(() => {
+ process.env = originalEnv;
+ });
+
+ it("uses --token flag when provided", async () => {
+ const { resolveGitHubToken } = await import("../github/auth.js");
+ const token = await resolveGitHubToken("my-pat-token");
+ expect(token).toBe("my-pat-token");
+ });
+
+ it("uses GITHUB_TOKEN env when no --token", async () => {
+ process.env.GITHUB_TOKEN = "env-token-123";
+ const { resolveGitHubToken } = await import("../github/auth.js");
+ const token = await resolveGitHubToken(undefined);
+ expect(token).toBe("env-token-123");
+ });
+});
diff --git a/ix-cli/src/cli/github/auth.ts b/ix-cli/src/cli/github/auth.ts
new file mode 100644
index 00000000..bddc82c6
--- /dev/null
+++ b/ix-cli/src/cli/github/auth.ts
@@ -0,0 +1,31 @@
+import { execFile } from "node:child_process";
+import { promisify } from "node:util";
+
+const execFileAsync = promisify(execFile);
+
+/**
+ * Resolve a GitHub token using this priority:
+ * 1. Explicit --token flag
+ * 2. GITHUB_TOKEN environment variable
+ * 3. `gh auth token` CLI command
+ */
+export async function resolveGitHubToken(explicitToken?: string): Promise<string> {
+ if (explicitToken) return explicitToken;
+
+ if (process.env.GITHUB_TOKEN) return process.env.GITHUB_TOKEN;
+
+ try {
+ const { stdout } = await execFileAsync("gh", ["auth", "token"]);
+ const token = stdout.trim();
+ if (token) return token;
+ } catch {
+ // gh not installed or not authenticated
+ }
+
+ throw new Error(
+ "GitHub authentication required. Provide one of:\n" +
+ " --token <pat> Personal access token\n" +
+ " GITHUB_TOKEN=<pat> Environment variable\n" +
+ " gh auth login GitHub CLI authentication"
+ );
+}