Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitattributes

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 44 additions & 0 deletions .github/workflows/large-pr-checker.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .projen/files.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions .projenrc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { CodeCovWorkflow } from './projenrc/codecov';
import { ESLINT_RULES } from './projenrc/eslint';
import { IssueLabeler } from './projenrc/issue-labeler';
import { JsiiBuild } from './projenrc/jsii';
import { LargePrChecker } from './projenrc/large-pr-checker';
import { PrLabeler } from './projenrc/pr-labeler';
import { RecordPublishingTimestamp } from './projenrc/record-publishing-timestamp';
import { S3DocsPublishing } from './projenrc/s3-docs-publishing';
Expand Down Expand Up @@ -1514,4 +1515,8 @@ new CodeCovWorkflow(repo, {
new IssueLabeler(repo);
new PrLabeler(repo);

new LargePrChecker(repo, {
excludeFiles: ['*.md', '*.test.ts', '*.yml'],
});

repo.synth();
85 changes: 85 additions & 0 deletions projenrc/large-pr-checker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import { github, Component } from 'projen';
import { JobPermission } from 'projen/lib/github/workflows-model';
import type { TypeScriptProject } from 'projen/lib/typescript';

export interface LargePrCheckerProps {
/**
* The number of lines changed in the PR that will trigger a comment and a failure.
*
* @default 1000
*/
readonly maxLinesChanged?: number;

/**
* A list of files to exclude from the line count.
*
* @default - none
*/
readonly excludeFiles?: string[];
}

export class LargePrChecker extends Component {
private readonly workflow: github.GithubWorkflow;

constructor(repo: TypeScriptProject, props: LargePrCheckerProps = {}) {
super(repo);

if (!repo.github) {
throw new Error('Given repository does not have a GitHub component');
}

const maxLinesChanged = props.maxLinesChanged ?? 1000;
const excludeFiles = (props.excludeFiles ?? [])
.map((pattern) => `':(exclude)${pattern}'`)
.join(' ');

this.workflow = repo.github.addWorkflow('large-pr-checker');
this.workflow.on({
pullRequest: {
branches: ['main'],
types: ['labeled', 'edited', 'opened', 'reopened', 'unlabeled'],
},
});

this.workflow.addJob('check', {
name: 'Check PR size',
if: '${{ !contains(github.event.pull_request.labels.*.name, \'pr/exempt-size-check\') }}',
runsOn: ['ubuntu-latest'],
permissions: {
pullRequests: JobPermission.WRITE,
},
steps: [
github.WorkflowSteps.checkout(),
{
id: 'fetch_target_branch',
run: 'git fetch origin main',
},
{
id: 'get_total_lines_changed',
run: `size=$(git diff --shortstat origin/main ${excludeFiles} \\
| awk '{ print $4+$6 }' \\
| awk -F- '{print $NF}' \\
| bc)

echo "Total lines changed: $size"
echo "total_lines_changed=$size" >> $GITHUB_OUTPUT`,
},
{
id: 'comment_pr',
if: `$\{{ fromJSON(steps.get_total_lines_changed.outputs.total_lines_changed) > fromJSON(${maxLinesChanged}) }}`,
uses: 'thollander/actions-comment-pull-request@v2',
with: {
comment_tag: 'pr_size',
mode: 'recreate',
message: `Total lines changed $\{{ steps.get_total_lines_changed.outputs.total_lines_changed }} is greater than ${maxLinesChanged}. Please consider breaking this PR down.`,
},
},
{
id: 'fail',
if: `$\{{ fromJSON(steps.get_total_lines_changed.outputs.total_lines_changed) > fromJSON(${maxLinesChanged}) }}`,
run: 'exit 1',
},
],
});
}
}