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

19
dist/index.js generated vendored
View file

@ -1490,20 +1490,29 @@ async function listFiles(octokit, sha) {
}
exports.listFiles = listFiles;
async function listGitTree(octokit, sha, path) {
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 = [];
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);
}
}
return result;
}

2
dist/index.js.map generated vendored

File diff suppressed because one or more lines are too long

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)
}
}