From cc06489b67085a870e8ccd32573e5b894fb154a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oskar=20Kwas=CC=81niewski?= Date: Wed, 28 Oct 2026 01:03:11 +0100 Subject: [PATCH 1/2] ci: label PRs by size without counting tests --- .github/workflows/pr-size.yml | 35 ++++++++++++++++++ scripts/pr-size/label.ts | 70 +++++++++++++++++++++++++++++++++++ scripts/pr-size/size.test.ts | 35 ++++++++++++++++++ scripts/pr-size/size.ts | 64 ++++++++++++++++++++++++++++++++ 4 files changed, 204 insertions(+) create mode 100644 .github/workflows/pr-size.yml create mode 100644 scripts/pr-size/label.ts create mode 100644 scripts/pr-size/size.test.ts create mode 100644 scripts/pr-size/size.ts diff --git a/.github/workflows/pr-size.yml b/.github/workflows/pr-size.yml new file mode 100644 index 0000000000..4bb37ff12f --- /dev/null +++ b/.github/workflows/pr-size.yml @@ -0,0 +1,35 @@ +name: pr-size + +# Labels every PR size:XS..size:XXL by changed lines, not counting tests, +# fixtures, snapshots, or the lockfile (scripts/pr-size/label.ts). +# pull_request_target so fork PRs get a token that can label; the checkout is +# the base branch's script, never PR code. +on: + pull_request_target: + types: [opened, reopened, synchronize] + +permissions: + contents: read + issues: write + pull-requests: write + +concurrency: + group: ${{ github.workflow }}-${{ github.event.pull_request.number }} + cancel-in-progress: true + +jobs: + label: + runs-on: blacksmith-4vcpu-ubuntu-2404 + timeout-minutes: 5 + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + sparse-checkout: scripts/pr-size + persist-credentials: false + - uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0 + with: + node-version: 26 + - name: Label the PR + env: + GITHUB_TOKEN: ${{ github.token }} + run: node scripts/pr-size/label.ts diff --git a/scripts/pr-size/label.ts b/scripts/pr-size/label.ts new file mode 100644 index 0000000000..b3c813c7b5 --- /dev/null +++ b/scripts/pr-size/label.ts @@ -0,0 +1,70 @@ +/** + * Gives a pull request one `size:*` label from its changed lines + * (`.github/workflows/pr-size.yml`, on every open and push). + * + * Reads the PR number from the `pull_request_target` payload at + * `GITHUB_EVENT_PATH` and the changed files and labels live, over `fetch` + * with `GITHUB_TOKEN` against `GITHUB_REPOSITORY`. Never checks out or runs + * the PR's code. `--dry-run` prints the size and changes nothing. + */ + +import { readFileSync } from 'node:fs'; +import { SIZE_LABELS, sizeOf, type ChangedFile } from './size.ts'; + +const API = 'https://api.github.com'; +const token = process.env.GITHUB_TOKEN ?? process.env.GH_TOKEN; +const repo = process.env.GITHUB_REPOSITORY; +const eventPath = process.env.GITHUB_EVENT_PATH; +if (!token || !repo || !eventPath) { + process.stderr.write('GITHUB_TOKEN, GITHUB_REPOSITORY, and GITHUB_EVENT_PATH are required\n'); + process.exit(2); +} +const dryRun = process.argv.includes('--dry-run'); +const { number } = (JSON.parse(readFileSync(eventPath, 'utf8')) as { pull_request: { number: number } }).pull_request; + +/** One authenticated request under `/repos/{owner}/{name}`; throws on a non-2xx response. */ +async function rest(path: string, init: { method?: string; body?: unknown } = {}): Promise { + const response = await fetch(`${API}/repos/${repo}${path}`, { + method: init.method ?? 'GET', + headers: { + authorization: `Bearer ${token}`, + accept: 'application/vnd.github+json', + 'x-github-api-version': '2022-11-28', + ...(init.body === undefined ? {} : { 'content-type': 'application/json' }), + }, + ...(init.body === undefined ? {} : { body: JSON.stringify(init.body) }), + }); + if (!response.ok) throw new Error(`${init.method ?? 'GET'} ${path}: ${response.status} ${await response.text()}`); + return response.status === 204 ? undefined : response.json(); +} + +/** Every file the PR changes; GitHub lists at most 3,000, already past `size:XXL`. */ +async function changedFiles(): Promise { + const all: ChangedFile[] = []; + for (let page = 1; ; page++) { + const batch = (await rest(`/pulls/${number}/files?per_page=100&page=${page}`)) as ChangedFile[]; + all.push(...batch); + if (batch.length < 100) return all; + } +} + +const size = sizeOf(await changedFiles()); +console.log(`#${number}: ${size.counted} counted of ${size.total} changed lines -> ${size.label.name}`); +if (dryRun) process.exit(0); + +const current = ((await rest(`/issues/${number}/labels?per_page=100`)) as { name: string }[]).map(({ name }) => name); +for (const name of current) { + if (name !== size.label.name && SIZE_LABELS.some((label) => label.name === name)) { + await rest(`/issues/${number}/labels/${encodeURIComponent(name)}`, { method: 'DELETE' }); + } +} +if (!current.includes(size.label.name)) { + const { name, max, color } = size.label; + const description = `${max === Infinity ? '1,000+' : `Up to ${max}`} changed lines, not counting tests, fixtures, or the lockfile`; + try { + await rest('/labels', { method: 'POST', body: { name, color, description } }); + } catch (error) { + if (!String(error).includes(': 422 ')) throw error; + } + await rest(`/issues/${number}/labels`, { method: 'POST', body: { labels: [name] } }); +} diff --git a/scripts/pr-size/size.test.ts b/scripts/pr-size/size.test.ts new file mode 100644 index 0000000000..0a8893933a --- /dev/null +++ b/scripts/pr-size/size.test.ts @@ -0,0 +1,35 @@ +import assert from 'node:assert/strict'; +import { test } from 'node:test'; +import { isExcluded, sizeOf, type ChangedFile } from './size.ts'; + +const file = (filename: string, additions: number, deletions = 0): ChangedFile => ({ filename, additions, deletions }); + +test('leaves tests, fixtures, snapshots, and the lockfile out of the count', () => { + for (const path of [ + 'packages/e2e/tests/unit/config.test.ts', + 'packages/web/src/locator.test.ts', + 'apps/testbed/tests-agent/login.e2e.ts', + 'apps/testbed/tests/visual.e2e.ts-snapshots/home.png', + 'packages/e2e/schema/fixtures/report.json', + 'pnpm-lock.yaml', + ]) { + assert.equal(isExcluded(path), true, path); + } + for (const path of ['packages/e2e/src/run/fixture.ts', 'packages/e2e/src/run/execute.ts', 'docs/guide.mdx', 'packages/web/pnpm-lock.yaml.md']) { + assert.equal(isExcluded(path), false, path); + } +}); + +test('sizes a mixed PR by its non-test lines', () => { + const size = sizeOf([file('packages/e2e/src/a.ts', 12, 3), file('packages/e2e/tests/unit/a.test.ts', 400), file('pnpm-lock.yaml', 900, 200)]); + assert.deepEqual({ total: size.total, counted: size.counted, label: size.label.name }, { total: 1515, counted: 15, label: 'size:S' }); +}); + +test('sizes a test-only PR by all of its lines', () => { + assert.equal(sizeOf([file('packages/e2e/tests/unit/a.test.ts', 120, 30)]).label.name, 'size:L'); +}); + +test('puts each boundary in the right label', () => { + const labels = [0, 9, 10, 29, 30, 99, 100, 499, 500, 999, 1000].map((lines) => sizeOf([file('src/a.ts', lines)]).label.name); + assert.deepEqual(labels, ['size:XS', 'size:XS', 'size:S', 'size:S', 'size:M', 'size:M', 'size:L', 'size:L', 'size:XL', 'size:XL', 'size:XXL']); +}); diff --git a/scripts/pr-size/size.ts b/scripts/pr-size/size.ts new file mode 100644 index 0000000000..f4f6b19038 --- /dev/null +++ b/scripts/pr-size/size.ts @@ -0,0 +1,64 @@ +/** + * How big a pull request is, for the `size:*` labels (`label.ts`). Counts + * added plus deleted lines, leaving out tests, fixtures, snapshots, and the + * lockfile unless a PR changes nothing else. + */ + +import { matchesGlob } from 'node:path'; + +export interface SizeLabel { + name: string; + /** Largest effective line count that still gets this label. */ + max: number; + color: string; +} + +export const SIZE_LABELS: readonly SizeLabel[] = [ + { name: 'size:XS', max: 9, color: '0e8a16' }, + { name: 'size:S', max: 29, color: '5ebd3e' }, + { name: 'size:M', max: 99, color: 'fbca04' }, + { name: 'size:L', max: 499, color: 'fe7d37' }, + { name: 'size:XL', max: 999, color: 'd93f0b' }, + { name: 'size:XXL', max: Infinity, color: 'b60205' }, +]; + +export const EXCLUDED: readonly string[] = [ + '**/*.test.*', + '**/*.spec.*', + '**/*.e2e.*', + '**/tests/**', + '**/tests-*/**', + '**/__tests__/**', + '**/__snapshots__/**', + '**/*-snapshots/**', + '**/fixtures/**', + 'pnpm-lock.yaml', +]; + +export interface ChangedFile { + filename: string; + additions: number; + deletions: number; +} + +export interface Size { + total: number; + counted: number; + label: SizeLabel; +} + +/** Whether a path is left out of the count. */ +export function isExcluded(path: string): boolean { + return EXCLUDED.some((glob) => matchesGlob(path, glob)); +} + +/** Sizes a PR by its counted lines, or by all of them when every changed file is excluded. */ +export function sizeOf(files: readonly ChangedFile[]): Size { + const lines = ({ additions, deletions }: ChangedFile) => additions + deletions; + const total = files.reduce((sum, file) => sum + lines(file), 0); + const counted = files.filter(({ filename }) => !isExcluded(filename)).reduce((sum, file) => sum + lines(file), 0); + const effective = counted === 0 ? total : counted; + const label = SIZE_LABELS.find(({ max }) => effective <= max); + if (label === undefined) throw new Error(`no size label for ${effective} lines`); + return { total, counted, label }; +} From 8b9162a934ee27c15cd74f62a3723c949e63967f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oskar=20Kwas=CC=81niewski?= Date: Wed, 28 Oct 2026 02:58:12 +0100 Subject: [PATCH 2/2] ci: keep pr-size exclusions module-private --- scripts/pr-size/size.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/pr-size/size.ts b/scripts/pr-size/size.ts index f4f6b19038..288bd7a55b 100644 --- a/scripts/pr-size/size.ts +++ b/scripts/pr-size/size.ts @@ -22,7 +22,7 @@ export const SIZE_LABELS: readonly SizeLabel[] = [ { name: 'size:XXL', max: Infinity, color: 'b60205' }, ]; -export const EXCLUDED: readonly string[] = [ +const EXCLUDED: readonly string[] = [ '**/*.test.*', '**/*.spec.*', '**/*.e2e.*',