Reduce number of API calls to get list of files tracked by GitHub

This commit is contained in:
Michal Dorner 2021-03-07 09:31:04 +01:00
parent f285c4c6d7
commit 953bdcc20a
No known key found for this signature in database
GPG key ID: 9EEE04B48DA36786
3 changed files with 29 additions and 10 deletions

View file

@ -97,19 +97,29 @@ export async function listFiles(octokit: InstanceType<typeof GitHub>, sha: strin
}
async function listGitTree(octokit: InstanceType<typeof GitHub>, sha: string, path: string): Promise<string[]> {
const tree = await octokit.git.getTree({
let truncated = false
let tree = await octokit.git.getTree({
recursive: 'true',
tree_sha: sha,
...github.context.repo
})
if (tree.data.truncated) {
truncated = true
tree = await octokit.git.getTree({
tree_sha: sha,
...github.context.repo
})
}
const result: string[] = []
for (const tr of tree.data.tree) {
const file = `${path}${tr.path}`
if (tr.type === 'tree') {
if (tr.type === 'blob') {
result.push(file)
} else if (tr.type === 'tree' && truncated) {
const files = await listGitTree(octokit, tr.sha, `${file}/`)
result.push(...files)
} else {
result.push(file)
}
}