pr-monitor/server/github/token.ts
2026-07-28 14:21:25 +00:00

21 lines
561 B
TypeScript

import { execFileSync } from 'node:child_process';
let cached: string | null = null;
export function getToken(): string {
if (cached) return cached;
const env = process.env.GITHUB_TOKEN;
if (env) {
cached = env;
return cached;
}
cached = execFileSync('gh', ['auth', 'token'], { encoding: 'utf8' }).trim();
if (!cached) throw new Error('gh auth token returned an empty token');
return cached;
}
/** Drop the cached token (called once on a 401 so the next call re-fetches). */
export function invalidateToken(): void {
cached = null;
}