Skip to content

Commit 5892eb6

Browse files
njb90claude
andcommitted
fix: retry GitHub API request with auth on 401/403/404
Unauthenticated fetch works for public repos with no UI. When the repo is private (or rate-limited), it retries once after prompting the user for GitHub auth (createIfNone: true). Auth prompt is skipped entirely once the repo is public. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent f4ed82f commit 5892eb6

1 file changed

Lines changed: 15 additions & 8 deletions

File tree

src/commands/configureAiTools.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,18 +61,25 @@ function getMcpFilePath(editor: EditorType): string {
6161
const SKILLS_BASE = "https://api.github.com/repos/cloudinary-devs/skills/contents";
6262

6363
async function githubFetchJson<T>(url: string): Promise<T> {
64-
const headers: Record<string, string> = { Accept: "application/vnd.github+json" };
64+
const baseHeaders: Record<string, string> = { Accept: "application/vnd.github+json" };
6565

66-
try {
67-
const session = await vscode.authentication.getSession("github", ["repo"], { silent: true });
68-
if (session) {
69-
headers["Authorization"] = `Bearer ${session.accessToken}`;
66+
// Try unauthenticated first (works for public repos, no UI)
67+
let response = await fetch(url, { headers: baseHeaders });
68+
69+
// On 401/403/404 attempt GitHub auth and retry once
70+
if (!response.ok && [401, 403, 404].includes(response.status)) {
71+
try {
72+
const session = await vscode.authentication.getSession("github", ["repo"], { createIfNone: true });
73+
if (session) {
74+
response = await fetch(url, {
75+
headers: { ...baseHeaders, Authorization: `Bearer ${session.accessToken}` },
76+
});
77+
}
78+
} catch {
79+
// auth declined or unavailable — fall through with original error
7080
}
71-
} catch {
72-
// auth not available — proceed unauthenticated
7381
}
7482

75-
const response = await fetch(url, { headers });
7683
if (!response.ok) {
7784
throw new Error(`GitHub API ${response.status}: ${url}`);
7885
}

0 commit comments

Comments
 (0)