Skip to content

Commit 556a88c

Browse files
otaviomacedogithub-actions
and
github-actions
authored
chore: large PR checker (#274)
Implement the PR size checker we had in the previous repository, but directly on a GitHub workflow. Adapted from https://github.com/adolfosilva/gh-large-pr-check (MIT License). --- By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license --------- Signed-off-by: github-actions <[email protected]> Co-authored-by: github-actions <[email protected]>
1 parent 2dc5a56 commit 556a88c

File tree

6 files changed

+137
-0
lines changed

6 files changed

+137
-0
lines changed

.gitattributes

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.github/workflows/large-pr-checker.yml

Lines changed: 44 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.gitignore

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.projen/files.json

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.projenrc.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { CodeCovWorkflow } from './projenrc/codecov';
99
import { ESLINT_RULES } from './projenrc/eslint';
1010
import { IssueLabeler } from './projenrc/issue-labeler';
1111
import { JsiiBuild } from './projenrc/jsii';
12+
import { LargePrChecker } from './projenrc/large-pr-checker';
1213
import { PrLabeler } from './projenrc/pr-labeler';
1314
import { RecordPublishingTimestamp } from './projenrc/record-publishing-timestamp';
1415
import { S3DocsPublishing } from './projenrc/s3-docs-publishing';
@@ -1515,4 +1516,8 @@ new CodeCovWorkflow(repo, {
15151516
new IssueLabeler(repo);
15161517
new PrLabeler(repo);
15171518

1519+
new LargePrChecker(repo, {
1520+
excludeFiles: ['*.md', '*.test.ts', '*.yml'],
1521+
});
1522+
15181523
repo.synth();

projenrc/large-pr-checker.ts

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import { github, Component } from 'projen';
2+
import { JobPermission } from 'projen/lib/github/workflows-model';
3+
import type { TypeScriptProject } from 'projen/lib/typescript';
4+
5+
export interface LargePrCheckerProps {
6+
/**
7+
* The number of lines changed in the PR that will trigger a comment and a failure.
8+
*
9+
* @default 1000
10+
*/
11+
readonly maxLinesChanged?: number;
12+
13+
/**
14+
* A list of files to exclude from the line count.
15+
*
16+
* @default - none
17+
*/
18+
readonly excludeFiles?: string[];
19+
}
20+
21+
export class LargePrChecker extends Component {
22+
private readonly workflow: github.GithubWorkflow;
23+
24+
constructor(repo: TypeScriptProject, props: LargePrCheckerProps = {}) {
25+
super(repo);
26+
27+
if (!repo.github) {
28+
throw new Error('Given repository does not have a GitHub component');
29+
}
30+
31+
const maxLinesChanged = props.maxLinesChanged ?? 1000;
32+
const excludeFiles = (props.excludeFiles ?? [])
33+
.map((pattern) => `':(exclude)${pattern}'`)
34+
.join(' ');
35+
36+
this.workflow = repo.github.addWorkflow('large-pr-checker');
37+
this.workflow.on({
38+
pullRequest: {
39+
branches: ['main'],
40+
types: ['labeled', 'edited', 'opened', 'reopened', 'unlabeled'],
41+
},
42+
});
43+
44+
this.workflow.addJob('check', {
45+
name: 'Check PR size',
46+
if: '${{ !contains(github.event.pull_request.labels.*.name, \'pr/exempt-size-check\') }}',
47+
runsOn: ['ubuntu-latest'],
48+
permissions: {
49+
pullRequests: JobPermission.WRITE,
50+
},
51+
steps: [
52+
github.WorkflowSteps.checkout(),
53+
{
54+
id: 'fetch_target_branch',
55+
run: 'git fetch origin main',
56+
},
57+
{
58+
id: 'get_total_lines_changed',
59+
run: `size=$(git diff --shortstat origin/main ${excludeFiles} \\
60+
| awk '{ print $4+$6 }' \\
61+
| awk -F- '{print $NF}' \\
62+
| bc)
63+
64+
echo "Total lines changed: $size"
65+
echo "total_lines_changed=$size" >> $GITHUB_OUTPUT`,
66+
},
67+
{
68+
id: 'comment_pr',
69+
if: `$\{{ fromJSON(steps.get_total_lines_changed.outputs.total_lines_changed) > fromJSON(${maxLinesChanged}) }}`,
70+
uses: 'thollander/actions-comment-pull-request@v2',
71+
with: {
72+
comment_tag: 'pr_size',
73+
mode: 'recreate',
74+
message: `Total lines changed $\{{ steps.get_total_lines_changed.outputs.total_lines_changed }} is greater than ${maxLinesChanged}. Please consider breaking this PR down.`,
75+
},
76+
},
77+
{
78+
id: 'fail',
79+
if: `$\{{ fromJSON(steps.get_total_lines_changed.outputs.total_lines_changed) > fromJSON(${maxLinesChanged}) }}`,
80+
run: 'exit 1',
81+
},
82+
],
83+
});
84+
}
85+
}

0 commit comments

Comments
 (0)