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

31
dist/index.js generated vendored
View file

@ -1420,9 +1420,6 @@ function getCheckRunContext() {
if (!event.workflow_run) { if (!event.workflow_run) {
throw new Error("Event of type 'workflow_run' is missing 'workflow_run' field"); 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 { return {
sha: event.workflow_run.head_commit.id, sha: event.workflow_run.head_commit.id,
runId: event.workflow_run.id runId: event.workflow_run.id
@ -1480,7 +1477,8 @@ async function downloadArtifact(octokit, artifactId, fileName, token) {
} }
exports.downloadArtifact = downloadArtifact; exports.downloadArtifact = downloadArtifact;
async function listFiles(octokit, sha) { async function listFiles(octokit, sha) {
core.info('Fetching list of tracked files from GitHub'); core.startGroup('Fetching list of tracked files from GitHub');
try {
const commit = await octokit.git.getCommit({ const commit = await octokit.git.getCommit({
commit_sha: sha, commit_sha: sha,
...github.context.repo ...github.context.repo
@ -1488,22 +1486,37 @@ async function listFiles(octokit, sha) {
const files = await listGitTree(octokit, commit.data.tree.sha, ''); const files = await listGitTree(octokit, commit.data.tree.sha, '');
return files; return files;
} }
finally {
core.endGroup();
}
}
exports.listFiles = listFiles; exports.listFiles = listFiles;
async function listGitTree(octokit, sha, path) { 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, tree_sha: sha,
...github.context.repo ...github.context.repo
}); });
if (tree.data.truncated) {
truncated = true;
tree = await octokit.git.getTree({
tree_sha: sha,
...github.context.repo
});
}
const result = []; const result = [];
for (const tr of tree.data.tree) { for (const tr of tree.data.tree) {
const file = `${path}${tr.path}`; 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}/`); const files = await listGitTree(octokit, tr.sha, `${file}/`);
result.push(...files); result.push(...files);
} }
else {
result.push(file);
}
} }
return result; 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) { if (!event.workflow_run) {
throw new Error("Event of type 'workflow_run' is missing 'workflow_run' field") 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 { return {
sha: event.workflow_run.head_commit.id, sha: event.workflow_run.head_commit.id,
runId: event.workflow_run.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[]> { export async function listFiles(octokit: InstanceType<typeof GitHub>, sha: string): Promise<string[]> {
core.info('Fetching list of tracked files from GitHub') core.startGroup('Fetching list of tracked files from GitHub')
try {
const commit = await octokit.git.getCommit({ const commit = await octokit.git.getCommit({
commit_sha: sha, commit_sha: sha,
...github.context.repo ...github.context.repo
}) })
const files = await listGitTree(octokit, commit.data.tree.sha, '') const files = await listGitTree(octokit, commit.data.tree.sha, '')
return files return files
} finally {
core.endGroup()
}
} }
async function listGitTree(octokit: InstanceType<typeof GitHub>, sha: string, path: string): Promise<string[]> { 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, tree_sha: sha,
...github.context.repo ...github.context.repo
}) })
if (tree.data.truncated) {
truncated = true
tree = await octokit.git.getTree({
tree_sha: sha,
...github.context.repo
})
}
const result: string[] = [] const result: string[] = []
for (const tr of tree.data.tree) { for (const tr of tree.data.tree) {
const file = `${path}${tr.path}` 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}/`) const files = await listGitTree(octokit, tr.sha, `${file}/`)
result.push(...files) result.push(...files)
} else {
result.push(file)
} }
} }