Skip to content

Commit

Permalink
Merge pull request #69 from dorny/avoid-exceeding-api-limit
Browse files Browse the repository at this point in the history
Reduce number of API calls to get list of files tracked by GitHub
  • Loading branch information
dorny authored Mar 7, 2021
2 parents f285c4c + cf4814e commit a1cbedb
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 30 deletions.
43 changes: 28 additions & 15 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

41 changes: 27 additions & 14 deletions src/utils/github-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
}

Expand Down

0 comments on commit a1cbedb

Please sign in to comment.