-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
chore(size-limit-action): Resolve baseline by walking git history #24310
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
Merged
+88
−92
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
180 changes: 88 additions & 92 deletions
180
dev-packages/size-limit-gh-action/utils/getArtifactsForBranchAndWorkflow.mjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,122 +1,118 @@ | ||
| import * as core from '@actions/core'; | ||
|
|
||
| // max pages of workflows to pagination through | ||
| const DEFAULT_MAX_PAGES = 50; | ||
| // max results per page | ||
| const DEFAULT_PAGE_LIMIT = 10; | ||
| // How many commits to walk back on the branch before giving up on finding a build with a size-limit artifact. | ||
| const DEFAULT_MAX_COMMITS = 50; | ||
| const COMMITS_PER_PAGE = 100; | ||
| const RUNS_PER_PAGE = 100; | ||
|
|
||
| /** | ||
| * Fetch artifacts from a workflow run from a branch | ||
| * Find the most recent build artifact for a branch by walking its git history. | ||
| * | ||
| * This is a bit hacky since GitHub Actions currently does not directly | ||
| * support downloading artifacts from other workflows | ||
| * We deliberately drive this from `repos.listCommits` (authoritative, strictly ordered git history) rather than from | ||
| * `listWorkflowRuns` filtered by branch + event. That listing is backed by an eventually-consistent index that can omit | ||
| * or reorder very recent runs, which previously made us silently compare against a days-old baseline while still | ||
| * reporting it as the latest one. Walking commits newest-first and pinning each run by `head_sha` sidesteps that: the | ||
| * order is exact, and if the tip commit has no artifact yet we fall back to its parent and correctly report the baseline | ||
| * as not-latest. | ||
| */ | ||
| export async function getArtifactsForBranchAndWorkflow(octokit, { owner, repo, workflowName, branch, artifactName }) { | ||
| let repositoryWorkflow = null; | ||
| const workflowId = await findWorkflowId(octokit, { owner, repo, workflowName }); | ||
|
|
||
| // For debugging | ||
| const allWorkflows = []; | ||
| if (!workflowId) { | ||
| return null; | ||
| } | ||
|
|
||
| // | ||
| // Find workflow id from `workflowName` | ||
| // | ||
| for await (const response of octokit.paginate.iterator(octokit.rest.actions.listRepoWorkflows, { | ||
| let commitsChecked = 0; | ||
|
|
||
| for await (const response of octokit.paginate.iterator(octokit.rest.repos.listCommits, { | ||
| owner, | ||
| repo, | ||
| sha: branch, | ||
| per_page: COMMITS_PER_PAGE, | ||
| })) { | ||
| const targetWorkflow = response.data.find(({ name }) => name === workflowName); | ||
| for (const { sha } of response.data) { | ||
| const isLatest = commitsChecked === 0; | ||
| commitsChecked++; | ||
|
|
||
| allWorkflows.push(...response.data.map(({ name }) => name)); | ||
| const found = await findArtifactForCommit(octokit, { owner, repo, workflowId, sha, artifactName }); | ||
|
|
||
| // If not found in responses, continue to search on next page | ||
| if (!targetWorkflow) { | ||
| continue; | ||
| } | ||
| if (found) { | ||
| if (!isLatest) { | ||
| core.info( | ||
| `Base artifact comes from commit ${sha}, which is ${commitsChecked - 1} commit(s) behind the tip of "${branch}".`, | ||
| ); | ||
| } | ||
| return { ...found, isLatest }; | ||
| } | ||
|
|
||
| repositoryWorkflow = targetWorkflow; | ||
| break; | ||
| if (commitsChecked >= DEFAULT_MAX_COMMITS) { | ||
| core.warning( | ||
| `No "${artifactName}" artifact found within the last ${DEFAULT_MAX_COMMITS} commits of "${branch}".`, | ||
| ); | ||
| return null; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if (!repositoryWorkflow) { | ||
| core.info( | ||
| `Unable to find workflow with name "${workflowName}" in the repository. Found workflows: ${allWorkflows.join( | ||
| ', ', | ||
| )}`, | ||
| ); | ||
| return null; | ||
| } | ||
| core.warning(`No "${artifactName}" artifact found on branch "${branch}".`); | ||
| return null; | ||
| } | ||
|
|
||
| const workflow_id = repositoryWorkflow.id; | ||
| /** | ||
| * Resolve a workflow's numeric id from its display name. | ||
| */ | ||
| async function findWorkflowId(octokit, { owner, repo, workflowName }) { | ||
| const allWorkflows = []; | ||
|
|
||
| let currentPage = 0; | ||
| let latestWorkflowRun = null; | ||
| for await (const response of octokit.paginate.iterator(octokit.rest.actions.listRepoWorkflows, { owner, repo })) { | ||
| const targetWorkflow = response.data.find(({ name }) => name === workflowName); | ||
| allWorkflows.push(...response.data.map(({ name }) => name)); | ||
|
|
||
| for await (const response of octokit.paginate.iterator(octokit.rest.actions.listWorkflowRuns, { | ||
| owner, | ||
| repo, | ||
| workflow_id, | ||
| branch, | ||
| per_page: DEFAULT_PAGE_LIMIT, | ||
| event: 'push', | ||
| })) { | ||
| if (!response.data.length) { | ||
| core.warning(`Workflow ${workflow_id} not found in branch ${branch}`); | ||
| return null; | ||
| if (targetWorkflow) { | ||
| return targetWorkflow.id; | ||
| } | ||
| } | ||
|
|
||
| // Do not allow downloading artifacts from a fork. | ||
| const filtered = response.data.filter(workflowRun => workflowRun.head_repository.full_name === `${owner}/${repo}`); | ||
| core.info( | ||
| `Unable to find workflow with name "${workflowName}" in the repository. Found workflows: ${allWorkflows.join(', ')}`, | ||
| ); | ||
| return null; | ||
| } | ||
|
|
||
| // Sort to ensure the latest workflow run is the first | ||
| filtered.sort((a, b) => { | ||
| return new Date(b.created_at).getTime() - new Date(a.created_at).getTime(); | ||
| /** | ||
| * Return the first `artifactName` artifact produced by any run of `workflowId` for the exact `sha`, preferring the most | ||
| * recent run (in case of re-runs). Runs originating from a fork are never trusted. | ||
| */ | ||
| async function findArtifactForCommit(octokit, { owner, repo, workflowId, sha, artifactName }) { | ||
| const { | ||
| data: { workflow_runs: workflowRuns }, | ||
| } = await octokit.rest.actions.listWorkflowRuns({ | ||
| owner, | ||
| repo, | ||
| workflow_id: workflowId, | ||
| head_sha: sha, | ||
| per_page: RUNS_PER_PAGE, | ||
| }); | ||
|
|
||
| const runs = workflowRuns | ||
| .filter(workflowRun => workflowRun.head_repository?.full_name === `${owner}/${repo}`) | ||
| .sort((a, b) => new Date(b.created_at).getTime() - new Date(a.created_at).getTime()); | ||
|
|
||
| for (const workflowRun of runs) { | ||
| const { | ||
| data: { artifacts }, | ||
| } = await octokit.rest.actions.listWorkflowRunArtifacts({ | ||
| owner, | ||
| repo, | ||
| run_id: workflowRun.id, | ||
| }); | ||
|
|
||
| // Store the first workflow run, to determine if this is the latest one... | ||
| if (!latestWorkflowRun) { | ||
| latestWorkflowRun = filtered[0]; | ||
| const artifact = artifacts?.find(({ name }) => name === artifactName); | ||
| if (artifact) { | ||
| core.info(`Found suitable artifact for commit ${sha}: ${artifact.url} (run ${workflowRun.html_url})`); | ||
| return { artifact, workflowRun }; | ||
| } | ||
|
|
||
| // Search through workflow artifacts until we find a workflow run w/ artifact name that we are looking for | ||
| for (const workflowRun of filtered) { | ||
| core.info(`Checking artifacts for workflow run: ${workflowRun.html_url}`); | ||
|
|
||
| const { | ||
| data: { artifacts }, | ||
| } = await octokit.rest.actions.listWorkflowRunArtifacts({ | ||
| owner, | ||
| repo, | ||
| run_id: workflowRun.id, | ||
| }); | ||
|
|
||
| if (!artifacts) { | ||
| core.warning( | ||
| `Unable to fetch artifacts for branch: ${branch}, workflow: ${workflow_id}, workflowRunId: ${workflowRun.id}`, | ||
| ); | ||
| } else { | ||
| const foundArtifact = artifacts.find(({ name }) => name === artifactName); | ||
| if (foundArtifact) { | ||
| core.info(`Found suitable artifact: ${foundArtifact.url}`); | ||
| return { | ||
| artifact: foundArtifact, | ||
| workflowRun, | ||
| isLatest: latestWorkflowRun.id === workflowRun.id, | ||
| }; | ||
| } else { | ||
| core.info(`No artifact found for ${artifactName}, trying next workflow run...`); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if (currentPage > DEFAULT_MAX_PAGES) { | ||
| core.warning(`Workflow ${workflow_id} not found in branch: ${branch}`); | ||
| return null; | ||
| } | ||
|
|
||
| currentPage++; | ||
| } | ||
|
|
||
| core.warning(`Artifact not found: ${artifactName}`); | ||
| core.endGroup(); | ||
| return null; | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix PR missing regression tests
Medium Severity
This
fixPR adds no unit, integration, or E2E test for the stale-baseline regression. Flagged because the review rules file asks for a test that fails without the change and passes with it, so thelistCommitswalk,head_shapinning, and truthfulisLatestbehavior are covered.Triggered by project rule: PR Review Guidelines for Cursor Bot
Reviewed by Cursor Bugbot for commit 4022882. Configure here.