Skip to content

AI-native design editor. Open-source Figma alternative.

fix(ci): measure a pull request from the base its merge commit merges into (#1364)

Danila Poyarkova45f1268c396
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index bcaf0646b..51bb8e546 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -30,6 +30,8 @@ jobs:
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1
with:
+ # A pull request's merge commit and its parents; the first parent is the base it merges into.
+ fetch-depth: 2
persist-credentials: false
- uses: oven-sh/setup-bun@0c5077e51419868618aeaa5fe8019c62421857d6 # v2.2.0
diff --git a/tools/AGENTS.md b/tools/AGENTS.md
index d146934e5..fb64fe096 100644
--- a/tools/AGENTS.md
+++ b/tools/AGENTS.md
@@ -25,6 +25,7 @@ Private tooling lives under `tools/<role>/<domain>/{src,tests}`; Steiger enforce
- `commitlint.config.ts` enforces commit structure in the **Commit messages** job; the separate **PR title** workflow validates titles. Preserve the `Release vX.Y.Z` and `Release vX.Y.Z-beta.N` exception, including the ` (#N)` the merge queue appends, and product casing when changing rules; the known AI co-author check does not rewrite base history. Gate policy lives in `tools/ci/policy/src/policy.ts`.
- `tools/ci/policy` runs right after Bun is set up, before any install, so it imports only Node built-ins; `oxlint.json` switches `open-pencil/prefer-es-toolkit` off there. Other CI tools install their workspace first, as `pr-review-guidance.yml` does through `.github/actions/setup-bun`.
- Required checks must also run on `merge_group`, the merge queue's event; read base and head from `merge_group.base_sha`/`head_sha` there (`ci.yml`, `pr-title.yml`).
+- Path classification measures a pull request from its merge commit's first parent, the base as it is now; `pull_request.base.sha` lags when the base moves and would count the base's newer commits as the pull request's (`tools/ci/policy/tests/classify.test.ts`).
- A passing pull request run records the tree it tested as the `CI verified tree` status on the PR head; a merge queue commit with that exact tree is classified `verified` and runs only the always-on checks. Any other tree, including a group with other PRs ahead, gets the full run (`tools/ci/policy/src/verified-tree/`).
- The app production workflow runs on `v*` tags, and the docs workflow when a release is published, since the download page reads GitHub's releases at build time; both also run on `workflow_dispatch`, never on ordinary `master` pushes. `build.yml` checks the tooling out under `.pipeline/` and runs `tools/release/release-packages` from there.
diff --git a/tools/ci/policy/src/classify.ts b/tools/ci/policy/src/classify.ts
index 1e4f62919..358c200c3 100644
--- a/tools/ci/policy/src/classify.ts
+++ b/tools/ci/policy/src/classify.ts
@@ -5,7 +5,7 @@ import { cloudInputs } from './cloud-inputs'
import { classifyPaths } from './policy'
import { isVerifiedTree } from './verified-tree/status'
-const base = process.env.CI_BASE_SHA
+const base = comparisonBase()
const output = process.env.GITHUB_OUTPUT
if (!base || !/^[a-f0-9]{40}$/.test(base) || !output)
throw new Error('Missing CI base SHA or output file')
@@ -27,6 +27,21 @@ const scope = (await queuedTreeVerified()) ? 'verified' : classifyPaths(paths, i
await appendFile(output, `scope=${scope}\n`)
console.log(`Selected ${scope} checks for ${paths.length} changed paths`)
+/**
+ * The commit a change is measured from. A pull request checks out GitHub's merge of it into the
+ * base branch as it is now, so that merge's first parent is the base. The event's `base.sha` can
+ * be older when the base branch moved, which would count the base's newer commits as the pull
+ * request's own. The merge queue's `base_sha` is exact.
+ */
+function comparisonBase(): string | undefined {
+ if (process.env.CI_EVENT !== 'pull_request') return process.env.CI_BASE_SHA
+ const parents = execFileSync('git', ['rev-list', '--parents', '-n', '1', 'HEAD'])
+ .toString('utf8')
+ .trim()
+ .split(' ')
+ return parents.length === 3 ? parents[1] : process.env.CI_BASE_SHA
+}
+
/** In the merge queue, whether the queued commit's tree already passed its pull request's CI. */
async function queuedTreeVerified(): Promise<boolean> {
const { CI_EVENT, CI_HEAD_REF, GITHUB_REPOSITORY, GITHUB_TOKEN } = process.env
diff --git a/tools/ci/policy/tests/classify.test.ts b/tools/ci/policy/tests/classify.test.ts
index 56cc7d93d..0e20ef3ae 100644
--- a/tools/ci/policy/tests/classify.test.ts
+++ b/tools/ci/policy/tests/classify.test.ts
@@ -46,3 +46,35 @@ test('classifier reads real Git deletions and renames without losing the origina
expect(failed.exitCode).not.toBe(0)
expect(await readFile(output, 'utf8')).toBe('scope=cloud\n')
})
+
+test('a pull request is measured from the base its merge commit merges into', async () => {
+ const root = await mkdtemp(join(tmpdir(), 'open-pencil-ci-'))
+ roots.push(root)
+ git(root, 'init', '-b', 'main')
+ await writeFile(join(root, 'runtime.ts'), 'export const version = 1\n')
+ git(root, 'add', '.')
+ git(root, 'commit', '-m', 'base')
+ const staleBase = git(root, 'rev-parse', 'HEAD')
+ git(root, 'checkout', '-b', 'docs-change')
+ await writeFile(join(root, 'README.md'), '# Docs\n')
+ git(root, 'add', '.')
+ git(root, 'commit', '-m', 'docs')
+ // The base branch moves on with code after the event recorded its base.
+ git(root, 'checkout', 'main')
+ await writeFile(join(root, 'runtime.ts'), 'export const version = 2\n')
+ git(root, 'commit', '-am', 'code on main')
+ git(root, 'merge', '--no-ff', '-m', 'Merge docs-change into main', 'docs-change')
+ const output = join(root, 'output')
+ const command = fileURLToPath(import.meta.resolve('#ci/classify'))
+ const classify = (event: string) =>
+ Bun.spawnSync([process.execPath, command], {
+ cwd: root,
+ env: { ...process.env, CI_BASE_SHA: staleBase, CI_EVENT: event, GITHUB_OUTPUT: output }
+ })
+
+ expect(classify('pull_request').exitCode).toBe(0)
+ expect(await readFile(output, 'utf8')).toBe('scope=docs\n')
+ // The merge queue's base is exact, so it is used as given.
+ expect(classify('merge_group').exitCode).toBe(0)
+ expect(await readFile(output, 'utf8')).toBe('scope=docs\nscope=cloud\n')
+})