Merge pull request #69 from dorny/avoid-exceeding-api-limit

Reduce number of API calls to get list of files tracked by GitHub
This commit is contained in:
Michal Dorner 2021-03-07 12:01:46 +01:00 committed by GitHub
commit a1cbedbccb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 56 additions and 30 deletions

43
dist/index.js generated vendored
View file

@ -1420,9 +1420,6 @@ function getCheckRunContext() {
if (!event.workflow_run) {
throw new Error("Event of type 'workflow_run' is missing 'workflow_run' field");
}
if (event.workflow_run.conclusion === 'cancelled') {
throw new Error(`Workflow run ${event.workflow_run.id} has been cancelled`);
}
return {
sha: event.workflow_run.head_commit.id,
runId: event.workflow_run.id
@ -1480,30 +1477,46 @@ async function downloadArtifact(octokit, artifactId, fileName, token) {
}
exports.downloadArtifact = downloadArtifact;
async function listFiles(octokit, sha) {
core.info('Fetching list of tracked files from GitHub');
const commit = await octokit.git.getCommit({
commit_sha: sha,
...github.context.repo
});
const files = await listGitTree(octokit, commit.data.tree.sha, '');
return files;
core.startGroup('Fetching list of tracked files from GitHub');
try {
const commit = await octokit.git.getCommit({
commit_sha: sha,
...github.context.repo
});
const files = await listGitTree(octokit, commit.data.tree.sha, '');
return files;
}
finally {
core.endGroup();
}
}
exports.listFiles = listFiles;
async function listGitTree(octokit, sha, path) {
const tree = await octokit.git.getTree({
const pathLog = path ? ` at ${path}` : '';
core.info(`Fetching tree ${sha}${pathLog}`);
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

@ -15,9 +15,6 @@ export function getCheckRunContext(): {sha: string; runId: number} {
if (!event.workflow_run) {
throw new Error("Event of type 'workflow_run' is missing 'workflow_run' field")
}
if (event.workflow_run.conclusion === 'cancelled') {
throw new Error(`Workflow run ${event.workflow_run.id} has been cancelled`)
}
return {
sha: event.workflow_run.head_commit.id,
runId: event.workflow_run.id
@ -87,29 +84,45 @@ export async function downloadArtifact(
}
export async function listFiles(octokit: InstanceType<typeof GitHub>, sha: string): Promise<string[]> {
core.info('Fetching list of tracked files from GitHub')
const commit = await octokit.git.getCommit({
commit_sha: sha,
...github.context.repo
})
const files = await listGitTree(octokit, commit.data.tree.sha, '')
return files
core.startGroup('Fetching list of tracked files from GitHub')
try {
const commit = await octokit.git.getCommit({
commit_sha: sha,
...github.context.repo
})
const files = await listGitTree(octokit, commit.data.tree.sha, '')
return files
} finally {
core.endGroup()
}
}
async function listGitTree(octokit: InstanceType<typeof GitHub>, sha: string, path: string): Promise<string[]> {
const tree = await octokit.git.getTree({
const pathLog = path ? ` at ${path}` : ''
core.info(`Fetching tree ${sha}${pathLog}`)
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)
}
}