How to Fail a PR That Exceeds a Diff Size
Read additions plus deletions from the PR payload and fail the check when the total exceeds your cap.
The pull_request payload exposes additions and deletions. Use actions/github-script to sum them and call core.setFailed when the total crosses your limit, with an override label as an escape hatch.
Steps
- Trigger on
pull_requestopened and synchronize. - Sum
additionsanddeletionsfrom the payload. - Fail when the total exceeds the cap unless an override label is set.
Workflow
.github/workflows/ci.yml
on:
pull_request:
types: [opened, synchronize]
jobs:
size-gate:
runs-on: ubuntu-latest
steps:
- uses: actions/github-script@v7
with:
script: |
const pr = context.payload.pull_request
const total = pr.additions + pr.deletions
const override = pr.labels.some((l) => l.name === 'large-pr-ok')
const cap = 800
if (total > cap && !override) {
core.setFailed(`PR changes ${total} lines, over the ${cap} line cap.`)
}Gotchas
additionsanddeletionscount all changed lines, including generated files; exclude those with a Danger rule if needed.- The
synchronizeevent refreshes the numbers on each new push.
Related guides
How to Add Size Labels (XS/S/M/L) to Pull RequestsAutomatically tag pull requests with a size label from XS to XL based on lines changed using the pascalgn/siz…
How to Enforce PR Review Rules With Danger JSCodify pull request review rules in a dangerfile.js and run Danger JS in CI to warn or fail when a PR skips a…