Skip to content

Instantly share code, notes, and snippets.

@shazron
Created March 30, 2023 08:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shazron/bd0b141d92f8760ce0349114463e6913 to your computer and use it in GitHub Desktop.
Save shazron/bd0b141d92f8760ce0349114463e6913 to your computer and use it in GitHub Desktop.
Search for a substring in a Github Repo. Generated by ChatGPT-4.
const searchSubstringInRepo = async (owner, repo, substring) => {
const repoApiUrl = `https://api.github.com/repos/${owner}/${repo}/git/trees/master?recursive=1`;
const headers = {
'Accept': 'application/vnd.github+json',
'Authorization': 'token YOUR_PERSONAL_ACCESS_TOKEN'
};
try {
const response = await fetch(repoApiUrl, { headers });
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const json = await response.json();
const files = json.tree.filter(item => item.type === 'blob');
for (const file of files) {
const fileContentResponse = await fetch(file.url, { headers });
if (!fileContentResponse.ok) {
throw new Error(`HTTP error! status: ${fileContentResponse.status}`);
}
const fileContentJson = await fileContentResponse.json();
const fileContent = atob(fileContentJson.content);
if (fileContent.includes(substring)) {
console.log(`Substring found in file: ${file.path}`);
}
}
} catch (error) {
console.error('Error:', error);
}
};
searchSubstringInRepo('OWNER', 'REPO', 'SUBSTRING');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment