Created
March 30, 2023 08:50
-
-
Save shazron/bd0b141d92f8760ce0349114463e6913 to your computer and use it in GitHub Desktop.
Search for a substring in a Github Repo. Generated by ChatGPT-4.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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