A local-first personal finance app
[AI] Pin bundle size baselines to the base commit (#8753)
Timba73a3ce1dbd
diff --git a/.github/workflows/size-compare.yml b/.github/workflows/size-compare.ymlindex f14de5db46..525b90bc77 100644--- a/.github/workflows/size-compare.yml+++ b/.github/workflows/size-compare.yml@@ -37,85 +37,74 @@ jobs:contents: readactions: readsteps:- - name: Checkout base branch+ # `pull_request_target` runs the trusted base workflow. Keep executable+ # helpers on that same revision; never execute code from the PR head.+ - name: Checkout base revisionuses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2with:- ref: ${{ github.base_ref }}+ ref: ${{ github.event.pull_request.base.sha }}persist-credentials: false- name: Set up environmentuses: ./.github/actions/setupwith:download-translations: 'false'- # Resolve one successful `build.yml` run for each side (master and PR- # head) up front, then pin every download below to its `run_id`. This- # ensures artifact downloads are consistent and prevents race conditions.+ # Resolve one successful `build.yml` run for each exact commit (base and+ # PR head) up front, then pin every download below to its `run_id`.- name: Resolve build runsuses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0id: build-runsenv:BASE_REF: ${{ github.base_ref }}+ BASE_SHA: ${{ github.event.pull_request.base.sha }}HEAD_SHA: ${{ github.event.pull_request.head.sha }}with:script: |- const TIMEOUT_MS = 30 * 60 * 1000;- const SLEEP_MS = 15000;-- async function resolveRun({ label, filter, notFoundHint }) {- const deadline = Date.now() + TIMEOUT_MS;- while (true) {- const { data } = await github.rest.actions.listWorkflowRuns({- owner: context.repo.owner,- repo: context.repo.repo,- workflow_id: 'build.yml',- ...filter,- status: 'success',- per_page: 1,- });- if (data.workflow_runs.length > 0) {- const run = data.workflow_runs[0];- core.info(`Found ${label} build run ${run.id} (${run.html_url})`);- return run.id;- }- if (Date.now() > deadline) {- throw new Error(- `No successful build.yml run found for ${label} within 30 min — ${notFoundHint}.`,- );- }- core.info(`No successful ${label} build run yet — sleeping 15s.`);- await new Promise(r => setTimeout(r, SLEEP_MS));- }- }-+ const { resolveBuildRun } = await import(+ `${process.env.GITHUB_WORKSPACE}/packages/ci-actions/src/build-runs/resolve.mjs`+ );const baseRef = process.env.BASE_REF;+ const baseSha = process.env.BASE_SHA;const headSha = process.env.HEAD_SHA;- const [masterRunId, headRunId] = await Promise.all([- resolveRun({- label: baseRef,- filter: { branch: baseRef },+ const common = {+ github,+ owner: context.repo.owner,+ repo: context.repo.repo,+ log: message => core.info(message),+ };+ // `build.yml` head artifacts use `refs/pull/N/merge`, which is+ // computed against `pull_request.base.sha`; use that exact commit+ // for the baseline so the two artifacts are a matched pair.+ const [baseRun, headRun] = await Promise.all([+ resolveBuildRun({+ ...common,+ label: `${baseRef} base ${baseSha}`,+ headSha: baseSha,notFoundHint: `${baseRef} may be broken`,}),- resolveRun({+ resolveBuildRun({+ ...common,label: `PR head ${headSha}`,- filter: { head_sha: headSha },+ headSha,notFoundHint:'build may still be running, have failed, or the branch may have been force-pushed',}),]);- core.setOutput('master_run_id', masterRunId);- core.setOutput('head_run_id', headRunId);+ core.info(`Using baseline commit ${baseSha}.`);+ core.setOutput('base_run_id', baseRun.id);+ core.setOutput('head_run_id', headRun.id);- name: Download web build artifact from ${{github.base_ref}}uses: dawidd6/action-download-artifact@8305c0f1062bb0d184d09ef4493ecb9288447732 # v20with:- run_id: ${{ steps.build-runs.outputs.master_run_id }}+ run_id: ${{ steps.build-runs.outputs.base_run_id }}workflow: build.ymlname: build-statspath: base- name: Download API build artifact from ${{github.base_ref}}uses: dawidd6/action-download-artifact@8305c0f1062bb0d184d09ef4493ecb9288447732 # v20with:- run_id: ${{ steps.build-runs.outputs.master_run_id }}+ run_id: ${{ steps.build-runs.outputs.base_run_id }}workflow: build.ymlname: api-build-statspath: base@@ -136,7 +125,7 @@ jobs:- name: Download CLI build artifact from ${{github.base_ref}}uses: dawidd6/action-download-artifact@8305c0f1062bb0d184d09ef4493ecb9288447732 # v20with:- run_id: ${{ steps.build-runs.outputs.master_run_id }}+ run_id: ${{ steps.build-runs.outputs.base_run_id }}workflow: build.ymlname: cli-build-statspath: base@@ -150,7 +139,7 @@ jobs:- name: Download CRDT build artifact from ${{github.base_ref}}uses: dawidd6/action-download-artifact@8305c0f1062bb0d184d09ef4493ecb9288447732 # v20with:- run_id: ${{ steps.build-runs.outputs.master_run_id }}+ run_id: ${{ steps.build-runs.outputs.base_run_id }}workflow: build.ymlname: crdt-build-statspath: basediff --git a/packages/ci-actions/src/build-runs/resolve.mjs b/packages/ci-actions/src/build-runs/resolve.mjsnew file mode 100644index 0000000000..9c7f851817--- /dev/null+++ b/packages/ci-actions/src/build-runs/resolve.mjs@@ -0,0 +1,62 @@+const DEFAULT_TIMEOUT_MS = 30 * 60 * 1000;+const DEFAULT_POLL_INTERVAL_MS = 15_000;++const defaultSleep = ms => new Promise(resolve => setTimeout(resolve, ms));++export async function resolveBuildRun({+ github,+ owner,+ repo,+ headSha,+ label,+ notFoundHint,+ workflowId = 'build.yml',+ timeoutMs = DEFAULT_TIMEOUT_MS,+ sleepMs = DEFAULT_POLL_INTERVAL_MS,+ now = Date.now,+ sleep = defaultSleep,+ log,+}) {+ if (!headSha) {+ throw new Error(`Cannot resolve ${label} build without a commit SHA.`);+ }++ const deadline = now() + timeoutMs;++ while (true) {+ const { data } = await github.rest.actions.listWorkflowRuns({+ owner,+ repo,+ workflow_id: workflowId,+ head_sha: headSha,+ status: 'success',+ per_page: 20,+ });+ const run = data.workflow_runs.find(+ candidate => candidate.head_sha === headSha,+ );++ if (run) {+ log?.(`Found ${label} build run ${run.id} (${run.html_url})`);+ return run;+ }++ const unexpectedRun = data.workflow_runs[0];+ if (unexpectedRun) {+ log?.(+ `Ignoring ${label} build run ${unexpectedRun.id}: expected ${headSha}, got ${unexpectedRun.head_sha}.`,+ );+ }++ if (now() >= deadline) {+ throw new Error(+ `No successful ${workflowId} run found for ${label} within ${timeoutMs / 60_000} min — ${notFoundHint}.`,+ );+ }++ log?.(+ `No successful ${label} build run yet — sleeping ${sleepMs / 1000}s.`,+ );+ await sleep(sleepMs);+ }+}diff --git a/packages/ci-actions/src/build-runs/resolve.test.js b/packages/ci-actions/src/build-runs/resolve.test.jsnew file mode 100644index 0000000000..e1f32a11e2--- /dev/null+++ b/packages/ci-actions/src/build-runs/resolve.test.js@@ -0,0 +1,69 @@+import { describe, expect, it, vi } from 'vitest';++import { resolveBuildRun } from './resolve.mjs';++describe('resolveBuildRun', () => {+ it('selects the exact commit and rejects a stale API result', async () => {+ const expectedSha = 'adf5d198282218f06ce0a6cbe847bee66f483332';+ const staleRun = {+ id: 30768952310,+ head_sha: '4e5c0d5f87f1c4277b12a1811023a03db9ccb4b8',+ html_url:+ 'https://github.com/actualbudget/actual/actions/runs/30768952310',+ };+ const expectedRun = {+ id: 32157684269,+ head_sha: expectedSha,+ html_url:+ 'https://github.com/actualbudget/actual/actions/runs/32157684269',+ };+ const listWorkflowRuns = vi.fn().mockResolvedValue({+ data: { workflow_runs: [staleRun, expectedRun] },+ });+ const sleep = vi.fn();++ const run = await resolveBuildRun({+ github: { rest: { actions: { listWorkflowRuns } } },+ owner: 'actualbudget',+ repo: 'actual',+ headSha: expectedSha,+ label: 'master base',+ notFoundHint: 'master may be broken',+ timeoutMs: 1_000,+ sleep,+ });++ expect(run).toBe(expectedRun);+ expect(listWorkflowRuns).toHaveBeenCalledOnce();+ expect(listWorkflowRuns).toHaveBeenCalledWith({+ owner: 'actualbudget',+ repo: 'actual',+ workflow_id: 'build.yml',+ head_sha: expectedSha,+ status: 'success',+ per_page: 20,+ });+ expect(listWorkflowRuns.mock.calls[0][0]).not.toHaveProperty('branch');+ expect(sleep).not.toHaveBeenCalled();+ });++ it('fails when no successful run exists for the commit', async () => {+ const listWorkflowRuns = vi+ .fn()+ .mockResolvedValue({ data: { workflow_runs: [] } });+ const now = vi.fn().mockReturnValueOnce(0).mockReturnValue(11);++ await expect(+ resolveBuildRun({+ github: { rest: { actions: { listWorkflowRuns } } },+ owner: 'actualbudget',+ repo: 'actual',+ headSha: 'base-sha',+ label: 'master base',+ notFoundHint: 'master may be broken',+ timeoutMs: 10,+ now,+ }),+ ).rejects.toThrow(/No successful build\.yml run found for master base/);+ });+});diff --git a/upcoming-release-notes/bundle-size-base-commit.md b/upcoming-release-notes/bundle-size-base-commit.mdnew file mode 100644index 0000000000..310f001e78--- /dev/null+++ b/upcoming-release-notes/bundle-size-base-commit.md@@ -0,0 +1,6 @@+---+category: Maintenance+authors: [tim-smart]+---++Compare pull request bundle sizes against the exact base commit