Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 3 commits into from
Mar 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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