Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 42 additions & 2 deletions src/utils/documents.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@
}

// find all instances of markdown inline images
const markdownInlineImageRegex = /\!(\[([^\]]+)\]\(([^)]+)\))/g

Check warning on line 233 in src/utils/documents.server.ts

View workflow job for this annotation

GitHub Actions / PR

eslint(no-useless-escape)

Unnecessary escape character '!'
const inlineMarkdownImageMatches = text.matchAll(markdownInlineImageRegex)
for (const match of inlineMarkdownImageMatches) {
const [fullMatch, _, __, src] = match
Expand Down Expand Up @@ -541,10 +541,16 @@
branch: string,
startingPath: string,
): Promise<Array<GitHubFileNode> | null> {
const githubToken = env.GITHUB_AUTH_TOKEN
const hasConfiguredGitHubToken =
Boolean(githubToken) && githubToken !== 'USE_A_REAL_KEY_IN_PRODUCTION'

const fetchOptions: RequestInit = {
headers: {
'X-GitHub-Api-Version': '2022-11-28',
Authorization: `Bearer ${env.GITHUB_AUTH_TOKEN}`,
...(hasConfiguredGitHubToken
? { Authorization: `Bearer ${githubToken}` }
: {}),
},
}
const res = await fetch(
Expand All @@ -556,8 +562,42 @@
if (res.status === 404) {
return null
}

const githubRequestId = res.headers.get('x-github-request-id')
const rateLimitLimit = res.headers.get('x-ratelimit-limit')
const rateLimitRemaining = res.headers.get('x-ratelimit-remaining')
const rateLimitReset = res.headers.get('x-ratelimit-reset')

let errorBody = ''
try {
errorBody = (await res.text()).replace(/\s+/g, ' ').trim()
} catch {
// Ignore parse failures for error response body
}

if (res.status === 403) {
console.error('[GitHub API] 403 while fetching repository contents', {
repo,
branch,
startingPath,
hasConfiguredGitHubToken,
githubRequestId,
rateLimitLimit,
rateLimitRemaining,
rateLimitReset,
errorBody: errorBody.slice(0, 500),
})
}

const hint =
res.status === 403
? rateLimitRemaining === '0'
? 'GitHub rate limit exceeded.'
: 'GitHub forbidden. Check token permissions/access.'
: 'GitHub request failed.'

throw new Error(
`Failed to fetch repo contents for ${repo}/${branch}/${startingPath}: Status is ${res.statusText} - ${res.status}`,
`${hint} Failed to fetch repo contents for ${repo}/${branch}/${startingPath}: Status is ${res.statusText} - ${res.status}. requestId=${githubRequestId ?? 'unknown'} rateLimitRemaining=${rateLimitRemaining ?? 'unknown'} rateLimitLimit=${rateLimitLimit ?? 'unknown'} rateLimitReset=${rateLimitReset ?? 'unknown'}${errorBody ? ` body=${errorBody.slice(0, 500)}` : ''}`,
)
}

Expand Down
Loading