Skip to content

Commit 0e81c19

Browse files
chore(prlint): add rule to reject pr contribution from main branch (#27617)
This PR will add a rule to our `prlint` to reject PRs opened from an author's `main` branch on their fork.
1 parent 382a0ed commit 0e81c19

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

tools/@aws-cdk/prlint/lint.ts

+31
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ export type GitHubPr =
1111

1212
export const CODE_BUILD_CONTEXT = 'AWS CodeBuild us-east-1 (AutoBuildv2Project1C6BFA3F-wQm2hXv2jqQv)';
1313

14+
const PR_FROM_MAIN_ERROR = 'Pull requests from `main` branch of a fork cannot be accepted. Please reopen this contribution from another branch on your fork. For more information, see https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md#step-4-pull-request.';
15+
1416
/**
1517
* Types of exemption labels in aws-cdk project.
1618
*/
@@ -268,6 +270,16 @@ export class PullRequestLinter {
268270
body,
269271
});
270272

273+
// Commenting this code to first test that linter rule works
274+
// since this can lead to other PRs closing if not setup correctly
275+
// // Closing the PR if it is opened from main branch of author's fork
276+
// if (failureMessages.includes(PR_FROM_MAIN_ERROR)) {
277+
// await this.client.pulls.update({
278+
// ...this.prParams,
279+
// state: 'closed',
280+
// });
281+
// }
282+
271283
throw new LinterError(body);
272284
}
273285

@@ -508,6 +520,9 @@ export class PullRequestLinter {
508520
validationCollector.validateRuleSet({
509521
testRuleSet: [{ test: validateTitleScope }],
510522
});
523+
validationCollector.validateRuleSet({
524+
testRuleSet: [{ test: validateBranch }],
525+
})
511526

512527
validationCollector.validateRuleSet({
513528
exemption: shouldExemptBreakingChange,
@@ -687,6 +702,22 @@ function validateTitleScope(pr: GitHubPr): TestResult {
687702
return result;
688703
}
689704

705+
/**
706+
* Check that the PR is not opened from main branch of author's fork
707+
*
708+
* @param pr github pr
709+
* @returns test result
710+
*/
711+
function validateBranch(pr: GitHubPr): TestResult {
712+
const result = new TestResult();
713+
714+
if (pr.head && pr.head.ref) {
715+
result.assessFailure(pr.head.ref === 'main', PR_FROM_MAIN_ERROR);
716+
}
717+
718+
return result;
719+
}
720+
690721
function assertStability(pr: GitHubPr, _files: GitHubFile[]): TestResult {
691722
const title = pr.title;
692723
const body = pr.body;

tools/@aws-cdk/prlint/test/lint.test.ts

+18
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,24 @@ describe('breaking changes format', () => {
9393
});
9494
});
9595

96+
test('disallow PRs from main branch of fork', async () => {
97+
const issue: Subset<linter.GitHubPr> = {
98+
number: 1,
99+
title: 'chore: some title',
100+
body: 'making a pr from main of my fork',
101+
labels: [{ name: 'pr-linter/exempt-test' }, { name: 'pr-linter/exempt-readme' }],
102+
user: {
103+
login: 'author',
104+
},
105+
head: {
106+
label: 'author:main',
107+
ref: 'main'
108+
}
109+
};
110+
const prLinter = configureMock(issue, undefined);
111+
await expect(prLinter.validatePullRequestTarget(SHA)).rejects.toThrow(/Pull requests from `main` branch of a fork cannot be accepted. Please reopen this contribution from another branch on your fork./);
112+
});
113+
96114
describe('commit message format', () => {
97115
test('valid scope', async () => {
98116
const issue = {

0 commit comments

Comments
 (0)