Skip to content
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

chore(prlint): add rule to reject pr contribution from main branch #27617

Merged
merged 5 commits into from
Oct 20, 2023
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
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
Loading