Skip to content

AI Agents & MCPs & AI Workflow Automation • (~400 MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents

7.6 KB
Raw Blame
import { execSync } from 'child_process'
import { appendFileSync } from 'fs'

// Generated / machine-authored files never count toward review size.
const EXCLUDE_PATTERNS: RegExp[] = [
    /(^|\/)bun\.lock$/,
    /(^|\/)package-lock\.json$/,
    /\/i18n\/translation\.json$/,
    /^packages\/web\/public\/locales\//,
    /\.snap$/,
    /(^|\/)dist\//,
]

// Test code, wherever it lives. This bucket is listed first so a test file is charged here
// instead of the product area it covers: a budget that counts tests taxes the PR for adding them.
const TEST_PATTERNS: RegExp[] = [
    /\.(test|spec)\.[cm]?[jt]sx?$/,
    /(^|\/)(test|tests|__tests__|__mocks__|__snapshots__)\//,
    /(^|\/)(vitest|jest|playwright|checkly)\.config\.[cm]?[jt]s$/,
    /^packages\/tests-e2e\//,
    /^smoke-test\//,
]

// First matching bucket wins; `budget: null` means the area is exempt (measured, never gated).
// packages/pieces is exempt on purpose: line count can't tell a cohesive new piece from a
// codemod or a schema dump, and pieces are @pieces-owned, self-contained, and low blast radius.
const BUCKETS: SizeBucket[] = [
    { name: 'tests', tests: TEST_PATTERNS, budget: null },
    {
        name: 'engine / worker / execution',
        tests: [/^packages\/server\/engine\//, /^packages\/server\/worker\//, /^packages\/core\/execution\//],
        budget: 300,
    },
    { name: 'core/shared', tests: [/^packages\/core\/shared\//], budget: 250 },
    { name: 'server/api', tests: [/^packages\/server\/api\//], budget: 600 },
    { name: 'packages/web', tests: [/^packages\/web\//], budget: 1200 },
    { name: 'packages/pieces', tests: [/^packages\/pieces\//], budget: null },
]

// Everything outside the gated logic areas (docs, tooling, cli, ee, sandbox, utils, …) is exempt too.
const DEFAULT_BUCKET: SizeBucket = { name: 'other (default)', tests: [], budget: null }
const BYPASS_LABEL = 'large-pr-ok'

function resolveRenamePath({ raw }: { raw: string }): string {
    if (!raw.includes(' => ')) {
        return raw
    }
    const braced = /^(.*)\{(.*) => (.*)\}(.*)$/.exec(raw)
    if (braced) {
        const [, prefix, , to, suffix] = braced
        return `${prefix}${to}${suffix}`.replace(/\/{2,}/g, '/')
    }
    const [, , to] = /^(.*) => (.*)$/.exec(raw) ?? []
    return to ?? raw
}

// On a `pull_request` run the checkout is the base←head merge commit, so its two
// parents are the true base (HEAD^1) and the PR head (HEAD^2); diffing them isolates
// the PR's own changes. This matters for stacked PRs: GitHub folds the ancestor PRs
// into the merge's base parent, so `origin/<base>...HEAD` collapses the merge-base to
// main and re-counts the whole stack. A non-merge checkout (local run) has no HEAD^2,
// so it falls back to diffing against the base ref.
function diffRange({ baseRef, parentCount }: { baseRef: string, parentCount: number }): string {
    return parentCount >= 2 ? 'HEAD^1...HEAD^2' : `origin/${baseRef}...HEAD`
}

function isExcluded({ path }: { path: string }): boolean {
    return EXCLUDE_PATTERNS.some((pattern) => pattern.test(path))
}

function bucketFor({ path }: { path: string }): SizeBucket {
    const match = BUCKETS.find((bucket) => bucket.tests.some((test) => test.test(path)))
    return match ?? DEFAULT_BUCKET
}

function collectSizes({ numstat }: { numstat: string }): SizeReport {
    const totals = new Map<string, number>()
    let meaningfulTotal = 0
    let excludedTotal = 0

    for (const line of numstat.split('\n')) {
        if (line.trim() === '') {
            continue
        }
        const [added, deleted, ...rest] = line.split('\t')
        const changes = Number(added) + Number(deleted)
        if (!Number.isFinite(changes)) {
            continue // binary files report "-" for counts
        }
        const path = resolveRenamePath({ raw: rest.join('\t') })
        if (isExcluded({ path })) {
            excludedTotal += changes
            continue
        }
        meaningfulTotal += changes
        const bucket = bucketFor({ path })
        totals.set(bucket.name, (totals.get(bucket.name) ?? 0) + changes)
    }

    const rows: SizeRow[] = [...BUCKETS, DEFAULT_BUCKET].map((bucket) => {
        const lines = totals.get(bucket.name) ?? 0
        const over = bucket.budget !== null && lines > bucket.budget
        return { name: bucket.name, lines, budget: bucket.budget, over }
    })

    return { rows, meaningfulTotal, excludedTotal }
}

function statusText({ row }: { row: SizeRow }): string {
    if (row.budget === null) {
        return `${row.lines.toLocaleString()} · exempt`
    }
    return row.over
        ? `⚠️ ${row.lines.toLocaleString()} / ${row.budget.toLocaleString()} — over by ${(row.lines - row.budget).toLocaleString()}`
        : `✅ ${row.lines.toLocaleString()} / ${row.budget.toLocaleString()}`
}

function isBlocked({ report, bypassReason }: { report: SizeReport, bypassReason: string | null }): boolean {
    return report.rows.some((row) => row.over) && bypassReason === null
}

function renderSummary({ report, bypassReason }: { report: SizeReport, bypassReason: string | null }): string {
    const overRows = report.rows.filter((row) => row.over)
    const lines: string[] = []
    lines.push('## PR size check')
    lines.push('')
    lines.push('Meaningful lines = additions + deletions, excluding generated files (lockfiles, translations, locales, snapshots, dist).')
    lines.push('')
    lines.push('| Area | Meaningful / budget |')
    lines.push('| --- | --- |')
    for (const row of report.rows) {
        lines.push(`| ${row.name} | ${statusText({ row })} |`)
    }
    lines.push('')
    lines.push(`**Total meaningful:** ${report.meaningfulTotal.toLocaleString()} · **excluded (generated):** ${report.excludedTotal.toLocaleString()}`)
    lines.push('')
    const over = overRows.map((row) => row.name).join(', ')
    if (overRows.length === 0) {
        lines.push('> ✅ All gated areas are within budget.')
    }
    else if (bypassReason !== null) {
        lines.push(`> ⚠️ Over budget in ${over}, but bypassed (${bypassReason}) — not blocking.`)
    }
    else {
        lines.push(`> ❌ **Blocked:** ${over} exceed the review-size budget. Split this into smaller PRs (see CONTRIBUTING.md), or add the \`${BYPASS_LABEL}\` label to bypass.`)
    }
    return lines.join('\n')
}

function main(): void {
    const baseRef = process.env.GITHUB_BASE_REF ?? 'main'
    const labels: string[] = JSON.parse(process.env.PR_LABELS ?? '[]')
    const title = process.env.PR_TITLE ?? ''

    const parentCount = execSync('git rev-list --parents -n 1 HEAD', { encoding: 'utf-8' })
        .trim().split(/\s+/).length - 1
    const numstat = execSync(`git diff --numstat ${diffRange({ baseRef, parentCount })}`, {
        encoding: 'utf-8',
        maxBuffer: 256 * 1024 * 1024,
    })
    const report = collectSizes({ numstat })

    const revert = /^revert/i.test(title)
    const bypassReason = labels.includes(BYPASS_LABEL)
        ? `\`${BYPASS_LABEL}\` label present`
        : revert
            ? 'revert PR'
            : null

    const summary = renderSummary({ report, bypassReason })
    console.log(summary)
    if (process.env.GITHUB_STEP_SUMMARY) {
        appendFileSync(process.env.GITHUB_STEP_SUMMARY, `${summary}\n`)
    }
    if (isBlocked({ report, bypassReason })) {
        process.exitCode = 1
    }
}

type SizeBucket = {
    name: string
    tests: RegExp[]
    budget: number | null
}

type SizeRow = {
    name: string
    lines: number
    budget: number | null
    over: boolean
}

type SizeReport = {
    rows: SizeRow[]
    meaningfulTotal: number
    excludedTotal: number
}

export const prSizeCheck = { collectSizes, renderSummary, bucketFor, resolveRenamePath, isBlocked, diffRange }

if (import.meta.main) {
    main()
}