-
Notifications
You must be signed in to change notification settings - Fork 27
Add PR size checker workflow #414
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
drsteinerdj
wants to merge
2
commits into
vjuliaife:main
Choose a base branch
from
drsteinerdj:bounty/pr-size-checker
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,187 @@ | ||
| name: PR Size | ||
|
|
||
| on: | ||
| pull_request: | ||
| types: [opened, synchronize, reopened, labeled, unlabeled] | ||
|
|
||
| permissions: | ||
| contents: read | ||
| issues: write | ||
| pull-requests: write | ||
|
|
||
| env: | ||
| PR_SIZE_WARN: 400 | ||
| PR_SIZE_FAIL: 1000 | ||
|
|
||
| jobs: | ||
| size: | ||
| name: Check PR diff size | ||
| runs-on: ubuntu-latest | ||
|
|
||
| steps: | ||
| - name: Checkout repository | ||
| uses: actions/checkout@v4 | ||
| with: | ||
| fetch-depth: 0 | ||
|
|
||
| - name: Count changed lines | ||
| id: count | ||
| shell: bash | ||
| env: | ||
| BASE_SHA: ${{ github.event.pull_request.base.sha }} | ||
| HEAD_SHA: ${{ github.event.pull_request.head.sha }} | ||
| run: | | ||
| node <<'NODE' | ||
| const { execFileSync } = require('node:child_process'); | ||
| const fs = require('node:fs'); | ||
|
|
||
| const base = process.env.BASE_SHA; | ||
| const head = process.env.HEAD_SHA; | ||
| const ignorePath = '.prsize-ignore'; | ||
|
|
||
| let ignoreText = ''; | ||
| try { | ||
| ignoreText = execFileSync('git', ['show', `${base}:${ignorePath}`], { | ||
| encoding: 'utf8', | ||
| stdio: ['ignore', 'pipe', 'ignore'], | ||
| }); | ||
| } catch { | ||
| ignoreText = ''; | ||
| } | ||
|
|
||
| const patterns = ignoreText | ||
| .split(/\r?\n/) | ||
| .map((line) => line.trim()) | ||
| .filter((line) => line && !line.startsWith('#')); | ||
|
|
||
| function escapeRegex(value) { | ||
| return value.replace(/[|\\{}()[\]^$+?.]/g, '\\$&'); | ||
| } | ||
|
|
||
| function globToRegex(pattern) { | ||
| let anchored = pattern.startsWith('/'); | ||
| let directoryOnly = pattern.endsWith('/'); | ||
| let body = pattern.replace(/^\/+/, '').replace(/\/+$/, ''); | ||
| let regex = ''; | ||
|
|
||
| for (let i = 0; i < body.length; i += 1) { | ||
| const char = body[i]; | ||
| const next = body[i + 1]; | ||
| if (char === '*' && next === '*') { | ||
| regex += '.*'; | ||
| i += 1; | ||
| } else if (char === '*') { | ||
| regex += '[^/]*'; | ||
| } else if (char === '?') { | ||
| regex += '[^/]'; | ||
| } else { | ||
| regex += escapeRegex(char); | ||
| } | ||
| } | ||
|
|
||
| if (directoryOnly) { | ||
| regex += '(?:/.*)?'; | ||
| } | ||
|
|
||
| return new RegExp(anchored ? `^${regex}$` : `(^|/)${regex}$`); | ||
| } | ||
|
|
||
| const matchers = patterns.map(globToRegex); | ||
| const isIgnored = (file) => matchers.some((matcher) => matcher.test(file)); | ||
|
|
||
| const output = execFileSync('git', ['diff', '--numstat', base, head], { | ||
| encoding: 'utf8', | ||
| }); | ||
|
|
||
| let total = 0; | ||
| const countedFiles = []; | ||
| const ignoredFiles = []; | ||
|
|
||
| for (const line of output.trim().split(/\r?\n/).filter(Boolean)) { | ||
| const [added, deleted, file] = line.split(/\t/); | ||
| if (!file) { | ||
| continue; | ||
| } | ||
| if (isIgnored(file)) { | ||
| ignoredFiles.push(file); | ||
| continue; | ||
| } | ||
| const addedLines = Number.parseInt(added, 10); | ||
| const deletedLines = Number.parseInt(deleted, 10); | ||
| if (Number.isNaN(addedLines) || Number.isNaN(deletedLines)) { | ||
| continue; | ||
| } | ||
| total += addedLines + deletedLines; | ||
| countedFiles.push(file); | ||
| } | ||
|
|
||
| fs.appendFileSync(process.env.GITHUB_OUTPUT, `lines=${total}\n`); | ||
| fs.appendFileSync(process.env.GITHUB_OUTPUT, `counted_files=${countedFiles.length}\n`); | ||
| fs.appendFileSync(process.env.GITHUB_OUTPUT, `ignored_files=${ignoredFiles.length}\n`); | ||
| console.log(`PR size: ${total} changed lines`); | ||
| console.log(`Counted files: ${countedFiles.length}`); | ||
| console.log(`Ignored files: ${ignoredFiles.length}`); | ||
| NODE | ||
|
|
||
| - name: Enforce PR size policy | ||
| uses: actions/github-script@v7 | ||
| env: | ||
| LINE_COUNT: ${{ steps.count.outputs.lines }} | ||
| COUNTED_FILES: ${{ steps.count.outputs.counted_files }} | ||
| IGNORED_FILES: ${{ steps.count.outputs.ignored_files }} | ||
| with: | ||
| script: | | ||
| const marker = '<!-- tariffshield-pr-size-check -->'; | ||
| const warnAt = Number(process.env.PR_SIZE_WARN || 400); | ||
| const failAt = Number(process.env.PR_SIZE_FAIL || 1000); | ||
| const lines = Number(process.env.LINE_COUNT || 0); | ||
| const countedFiles = Number(process.env.COUNTED_FILES || 0); | ||
| const ignoredFiles = Number(process.env.IGNORED_FILES || 0); | ||
| const { owner, repo } = context.repo; | ||
| const pull_number = context.payload.pull_request.number; | ||
| const labels = context.payload.pull_request.labels.map((label) => label.name); | ||
| const bypassed = labels.includes('large-pr-approved'); | ||
|
|
||
| async function upsertComment(body) { | ||
| const comments = await github.rest.issues.listComments({ | ||
| owner, | ||
| repo, | ||
| issue_number: pull_number, | ||
| per_page: 100, | ||
| }); | ||
| const previous = comments.data.find((comment) => comment.body?.includes(marker)); | ||
| if (previous) { | ||
| await github.rest.issues.updateComment({ | ||
| owner, | ||
| repo, | ||
| comment_id: previous.id, | ||
| body, | ||
| }); | ||
| return; | ||
| } | ||
| await github.rest.issues.createComment({ | ||
| owner, | ||
| repo, | ||
| issue_number: pull_number, | ||
| body, | ||
| }); | ||
| } | ||
|
|
||
| if (bypassed) { | ||
| core.notice(`PR size check bypassed by large-pr-approved label (${lines} changed lines).`); | ||
| return; | ||
| } | ||
|
|
||
| if (lines >= failAt) { | ||
| await upsertComment(`${marker}\nThis PR is very large (${lines} lines). The hard limit is ${failAt} changed lines. Please split this into smaller PRs or ask a maintainer to apply the \`large-pr-approved\` label for an explicit bypass.\n\nCounted files: ${countedFiles}. Ignored generated files: ${ignoredFiles}.`); | ||
| core.setFailed(`PR has ${lines} changed lines, which meets or exceeds the ${failAt}-line hard limit.`); | ||
| return; | ||
| } | ||
|
|
||
| if (lines >= warnAt) { | ||
| await upsertComment(`${marker}\nThis PR is large (${lines} lines). Consider splitting it into smaller PRs for easier review.\n\nCounted files: ${countedFiles}. Ignored generated files: ${ignoredFiles}.`); | ||
| core.warning(`PR has ${lines} changed lines, which meets or exceeds the ${warnAt}-line warning threshold.`); | ||
| return; | ||
| } | ||
|
|
||
| core.notice(`PR size is ${lines} changed lines; below warning threshold ${warnAt}.`); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| package-lock.json | ||
| Cargo.lock | ||
| *.snap | ||
| dist/ | ||
| target/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The repository's branch protection is restored from
.github/protection.jsonanddocs/repo-config.mdenumerates the required status checks, but this commit only adds the optionalPR Size / Check PR diff sizejob and does not add that context to the protected checks. In the current protected-branch setup, a PR over 1000 lines can still merge once the existing required checks pass, so the advertised hard limit is not actually enforced.Useful? React with 👍 / 👎.