Skip to content

Commit

Permalink
chore(prlint): add rule to reject pr contribution from main branch (#…
Browse files Browse the repository at this point in the history
…27617)

This PR will add a rule to our `prlint` to reject PRs opened from an author's `main` branch on their fork.
  • Loading branch information
vinayak-kukreja committed Oct 20, 2023
1 parent 382a0ed commit 0e81c19
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
31 changes: 31 additions & 0 deletions tools/@aws-cdk/prlint/lint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ export type GitHubPr =

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

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.';

/**
* Types of exemption labels in aws-cdk project.
*/
Expand Down Expand Up @@ -268,6 +270,16 @@ export class PullRequestLinter {
body,
});

// Commenting this code to first test that linter rule works
// since this can lead to other PRs closing if not setup correctly
// // Closing the PR if it is opened from main branch of author's fork
// if (failureMessages.includes(PR_FROM_MAIN_ERROR)) {
// await this.client.pulls.update({
// ...this.prParams,
// state: 'closed',
// });
// }

throw new LinterError(body);
}

Expand Down Expand Up @@ -508,6 +520,9 @@ export class PullRequestLinter {
validationCollector.validateRuleSet({
testRuleSet: [{ test: validateTitleScope }],
});
validationCollector.validateRuleSet({
testRuleSet: [{ test: validateBranch }],
})

validationCollector.validateRuleSet({
exemption: shouldExemptBreakingChange,
Expand Down Expand Up @@ -687,6 +702,22 @@ function validateTitleScope(pr: GitHubPr): TestResult {
return result;
}

/**
* Check that the PR is not opened from main branch of author's fork
*
* @param pr github pr
* @returns test result
*/
function validateBranch(pr: GitHubPr): TestResult {
const result = new TestResult();

if (pr.head && pr.head.ref) {
result.assessFailure(pr.head.ref === 'main', PR_FROM_MAIN_ERROR);
}

return result;
}

function assertStability(pr: GitHubPr, _files: GitHubFile[]): TestResult {
const title = pr.title;
const body = pr.body;
Expand Down
18 changes: 18 additions & 0 deletions tools/@aws-cdk/prlint/test/lint.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,24 @@ describe('breaking changes format', () => {
});
});

test('disallow PRs from main branch of fork', async () => {
const issue: Subset<linter.GitHubPr> = {
number: 1,
title: 'chore: some title',
body: 'making a pr from main of my fork',
labels: [{ name: 'pr-linter/exempt-test' }, { name: 'pr-linter/exempt-readme' }],
user: {
login: 'author',
},
head: {
label: 'author:main',
ref: 'main'
}
};
const prLinter = configureMock(issue, undefined);
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./);
});

describe('commit message format', () => {
test('valid scope', async () => {
const issue = {
Expand Down

0 comments on commit 0e81c19

Please sign in to comment.