Skip to content

Commit 6d8527d

Browse files
authored
fix: github unauthenticated fetch (#69)
1 parent 57d1fef commit 6d8527d

File tree

3 files changed

+30
-7
lines changed

3 files changed

+30
-7
lines changed

src/api/tools/commonTools.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,12 +123,26 @@ export async function fetchDocumentation({
123123
// Create array of all location+branch combinations to try
124124
const fetchPromises = possibleLocations.flatMap((location) => [
125125
{
126-
promise: fetchFileFromGitHub(owner, repo, "main", location, env),
126+
promise: fetchFileFromGitHub(
127+
owner,
128+
repo,
129+
"main",
130+
location,
131+
env,
132+
false,
133+
),
127134
location,
128135
branch: "main",
129136
},
130137
{
131-
promise: fetchFileFromGitHub(owner, repo, "master", location, env),
138+
promise: fetchFileFromGitHub(
139+
owner,
140+
repo,
141+
"master",
142+
location,
143+
env,
144+
false,
145+
),
132146
location,
133147
branch: "master",
134148
},
@@ -186,6 +200,7 @@ export async function fetchDocumentation({
186200
"main",
187201
"README.md",
188202
env,
203+
false,
189204
);
190205
fileUsed = "readme.md (main branch)";
191206

src/api/utils/github.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import { fetchRawFile } from "./githubClient.js";
88
* @param repo - Repository name
99
* @param branch - Branch name (main, master)
1010
* @param path - File path within the repository
11+
* @param env - Environment for GitHub token
12+
* @param useAuth - Whether to use authentication
1113
* @returns File content or null if not found
1214
*/
1315
export async function fetchFileFromGitHub(
@@ -16,8 +18,9 @@ export async function fetchFileFromGitHub(
1618
branch: string,
1719
path: string,
1820
env: any,
21+
useAuth = false,
1922
): Promise<string | null> {
20-
return await fetchRawFile(owner, repo, branch, path, env);
23+
return await fetchRawFile(owner, repo, branch, path, env, useAuth);
2124
}
2225

2326
// Helper: search for a file in a GitHub repository using the GitHub Search API
@@ -38,6 +41,7 @@ export async function searchGitHubRepo(
3841
cachedPath.branch,
3942
cachedPath.path,
4043
env,
44+
true,
4145
);
4246
if (content) {
4347
return content;

src/api/utils/githubClient.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,20 +117,22 @@ async function respectRateLimits(): Promise<void> {
117117
* @param options - Fetch options
118118
* @param env - Environment containing GitHub token if available
119119
* @param retryCount - Current retry attempt (used internally)
120+
* @param useAuth - Whether to include authorization header if token is available (default: true)
120121
* @returns The API response or null if failed
121122
*/
122123
export async function githubApiRequest(
123124
url: string,
124125
options: RequestInit = {},
125126
env: any,
126127
retryCount = 0,
128+
useAuth = true,
127129
): Promise<Response | null> {
128130
try {
129131
// Extract repository context for metrics
130132
const repoContext = extractRepoContextFromUrl(url);
131133

132134
// Track GitHub query count using Cloudflare analytics
133-
if (env?.CLOUDFLARE_ANALYTICS && retryCount === 0) {
135+
if (env.CLOUDFLARE_ANALYTICS && retryCount === 0) {
134136
env.CLOUDFLARE_ANALYTICS.writeDataPoint({
135137
blobs: [url, repoContext],
136138
doubles: [1],
@@ -141,15 +143,15 @@ export async function githubApiRequest(
141143
// Wait for rate limit if necessary
142144
await respectRateLimits();
143145

144-
// Add GitHub authentication if token is available
146+
// Add GitHub authentication if token is available and useAuth is true
145147
const headers = new Headers(options.headers || {});
146148
headers.set("Accept", "application/vnd.github.v3+json");
147149
headers.set(
148150
"User-Agent",
149151
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36",
150152
);
151153

152-
if (env?.GITHUB_TOKEN) {
154+
if (useAuth && env.GITHUB_TOKEN) {
153155
headers.set("Authorization", `token ${env.GITHUB_TOKEN}`);
154156
}
155157

@@ -286,19 +288,21 @@ export async function searchFileByName(
286288
* @param branch - Branch name
287289
* @param path - File path
288290
* @param env - Environment for GitHub token
291+
* @param useAuth - Whether to use authentication
289292
*/
290293
export async function fetchRawFile(
291294
owner: string,
292295
repo: string,
293296
branch: string,
294297
path: string,
295298
env: any,
299+
useAuth = false,
296300
): Promise<string | null> {
297301
const url = `https://raw.githubusercontent.com/${owner}/${repo}/${branch}/${path}`;
298302

299303
// Raw GitHub content doesn't need the GitHub API token
300304
// But we still use the client for rate limiting
301-
const response = await githubApiRequest(url, {}, env);
305+
const response = await githubApiRequest(url, {}, env, 0, useAuth);
302306

303307
if (!response || !response.ok) {
304308
return null;

0 commit comments

Comments
 (0)