|
| 1 | +import { Octokit } from "@octokit/rest"; |
| 2 | +import { shellExecutor } from "../executor"; |
| 3 | +import { createError, err, ok, type Result } from "../result"; |
| 4 | + |
| 5 | +export interface RepoInfo { |
| 6 | + owner: string; |
| 7 | + repo: string; |
| 8 | +} |
| 9 | + |
| 10 | +// Module-level caches (keyed by cwd) |
| 11 | +const tokenCache = new Map<string, string>(); |
| 12 | +const repoCache = new Map<string, RepoInfo>(); |
| 13 | +const octokitCache = new Map<string, Octokit>(); |
| 14 | + |
| 15 | +export async function getToken(cwd: string): Promise<string> { |
| 16 | + const cached = tokenCache.get(cwd); |
| 17 | + if (cached) return cached; |
| 18 | + |
| 19 | + const result = await shellExecutor.execute("gh", ["auth", "token"], { cwd }); |
| 20 | + if (result.exitCode !== 0) { |
| 21 | + throw new Error(`Failed to get GitHub token: ${result.stderr}`); |
| 22 | + } |
| 23 | + const token = result.stdout.trim(); |
| 24 | + tokenCache.set(cwd, token); |
| 25 | + return token; |
| 26 | +} |
| 27 | + |
| 28 | +export async function getRepoInfo(cwd: string): Promise<Result<RepoInfo>> { |
| 29 | + const cached = repoCache.get(cwd); |
| 30 | + if (cached) return ok(cached); |
| 31 | + |
| 32 | + try { |
| 33 | + const result = await shellExecutor.execute( |
| 34 | + "git", |
| 35 | + ["config", "--get", "remote.origin.url"], |
| 36 | + { cwd }, |
| 37 | + ); |
| 38 | + |
| 39 | + if (result.exitCode !== 0) { |
| 40 | + return err(createError("COMMAND_FAILED", "No git remote found")); |
| 41 | + } |
| 42 | + |
| 43 | + const url = result.stdout.trim(); |
| 44 | + const match = url.match(/github\.com[:/]([^/]+)\/(.+?)(?:\.git)?$/); |
| 45 | + if (!match) { |
| 46 | + return err( |
| 47 | + createError( |
| 48 | + "COMMAND_FAILED", |
| 49 | + "Could not parse GitHub repo from remote URL", |
| 50 | + ), |
| 51 | + ); |
| 52 | + } |
| 53 | + |
| 54 | + const info = { owner: match[1], repo: match[2] }; |
| 55 | + repoCache.set(cwd, info); |
| 56 | + return ok(info); |
| 57 | + } catch (e) { |
| 58 | + return err(createError("COMMAND_FAILED", `Failed to get repo info: ${e}`)); |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +export async function getOctokit(cwd: string): Promise<Octokit> { |
| 63 | + const cached = octokitCache.get(cwd); |
| 64 | + if (cached) return cached; |
| 65 | + |
| 66 | + const token = await getToken(cwd); |
| 67 | + const octokit = new Octokit({ auth: token }); |
| 68 | + octokitCache.set(cwd, octokit); |
| 69 | + return octokit; |
| 70 | +} |
| 71 | + |
| 72 | +export interface GitHubContext { |
| 73 | + octokit: Octokit; |
| 74 | + owner: string; |
| 75 | + repo: string; |
| 76 | +} |
| 77 | + |
| 78 | +/** |
| 79 | + * Helper to reduce boilerplate for GitHub API calls. |
| 80 | + * Handles repo info lookup, octokit creation, and error wrapping. |
| 81 | + */ |
| 82 | +export async function withGitHub<T>( |
| 83 | + cwd: string, |
| 84 | + operation: string, |
| 85 | + fn: (ctx: GitHubContext) => Promise<T>, |
| 86 | +): Promise<Result<T>> { |
| 87 | + const repoResult = await getRepoInfo(cwd); |
| 88 | + if (!repoResult.ok) return repoResult; |
| 89 | + |
| 90 | + const { owner, repo } = repoResult.value; |
| 91 | + |
| 92 | + try { |
| 93 | + const octokit = await getOctokit(cwd); |
| 94 | + const result = await fn({ octokit, owner, repo }); |
| 95 | + return ok(result); |
| 96 | + } catch (e) { |
| 97 | + return err(createError("COMMAND_FAILED", `Failed to ${operation}: ${e}`)); |
| 98 | + } |
| 99 | +} |
0 commit comments