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 1 commit
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
19 changes: 19 additions & 0 deletions tools/@aws-cdk/prlint/lint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,9 @@ export class PullRequestLinter {
validationCollector.validateRuleSet({
testRuleSet: [{ test: validateTitleScope }],
});
validationCollector.validateRuleSet({
testRuleSet: [{ test: validateBranch }],
})

validationCollector.validateRuleSet({
exemption: shouldExemptBreakingChange,
Expand Down Expand Up @@ -687,6 +690,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', '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.')
}

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