Skip to content

Commit

Permalink
Fix network error in getRandomRepository
Browse files Browse the repository at this point in the history
Use the tree API instead of the search API to obtain the list of files
in a repository. (Search API is unavailable without authentication.)
  • Loading branch information
abt8601 committed Apr 28, 2023
1 parent 4f3c376 commit 3cb4b65
Showing 1 changed file with 30 additions and 8 deletions.
38 changes: 30 additions & 8 deletions pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,44 @@ async function getRandomRepository(
const pickFrom = repositories.filter((r) => !skip.includes(r));

const repository = pickFrom[Math.floor(Math.random() * pickFrom.length)];
const searchResponse = await fetch(
`https://api.github.com/search/code?q=%20+repo:${repository}`,
{ referrerPolicy: 'no-referrer' }

const repositoryResponse = await fetch(
`https://api.github.com/repos/${repository}`,
{
referrerPolicy: 'no-referrer',
headers: {
'Accept': 'application/vnd.github+json',
'X-GitHub-Api-Version': '2022-11-28'
}
}
);
const repositoryJSON = await repositoryResponse.json();
const defaultBranch: string = repositoryJSON.default_branch;

const treeResponse = await fetch(
`https://api.github.com/repos/${repository}/git/trees/${defaultBranch}?recursive=1`,
{
referrerPolicy: 'no-referrer',
headers: {
'Accept': 'application/vnd.github+json',
'X-GitHub-Api-Version': '2022-11-28'
}
}
);
const searchJSON = await searchResponse.json();
const filesWithURLs = searchJSON.items.filter((item: { url: undefined; }) => item.url !== undefined)
const randomFile = filesWithURLs[Math.floor(Math.random() * filesWithURLs.length)];
const treeJSON = await treeResponse.json();
const files = treeJSON.tree.filter(
(item: { type: string; }) => item.type == 'blob');
const randomFile = files[Math.floor(Math.random() * files.length)];

// Rarely (say, 1 in 100) we get an error here because `randomFile` is undefined.
// To ship this, I'm just adding some retry logic. TODO: fix me.
if (randomFile === undefined) {
return getRandomRepository(skip)
}

const { url } = randomFile;
const downloadURL = (await (await fetch(url, { referrerPolicy: 'no-referrer' })).json()).download_url;
const { path } = randomFile;
const downloadURL =
`https://raw.githubusercontent.com/${repository}/${defaultBranch}/${path}`;

return {
repository,
Expand Down

0 comments on commit 3cb4b65

Please sign in to comment.