From b29d5c2927461bec1227b88dc1267b48ef40b37f Mon Sep 17 00:00:00 2001 From: Vinayak Kukreja Date: Thu, 19 Oct 2023 14:27:28 -0700 Subject: [PATCH 1/4] feat(prlint): add rule to reject pr contribution from main branch Signed-off-by: Vinayak Kukreja --- tools/@aws-cdk/prlint/lint.ts | 19 +++++++++++++++++++ tools/@aws-cdk/prlint/test/lint.test.ts | 18 ++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/tools/@aws-cdk/prlint/lint.ts b/tools/@aws-cdk/prlint/lint.ts index 7f367705eb692..a1161a15ea2a4 100644 --- a/tools/@aws-cdk/prlint/lint.ts +++ b/tools/@aws-cdk/prlint/lint.ts @@ -508,6 +508,9 @@ export class PullRequestLinter { validationCollector.validateRuleSet({ testRuleSet: [{ test: validateTitleScope }], }); + validationCollector.validateRuleSet({ + testRuleSet: [{ test: validateBranch }], + }) validationCollector.validateRuleSet({ exemption: shouldExemptBreakingChange, @@ -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; diff --git a/tools/@aws-cdk/prlint/test/lint.test.ts b/tools/@aws-cdk/prlint/test/lint.test.ts index 50bd81b2c7f2b..42185b50f7368 100644 --- a/tools/@aws-cdk/prlint/test/lint.test.ts +++ b/tools/@aws-cdk/prlint/test/lint.test.ts @@ -93,6 +93,24 @@ describe('breaking changes format', () => { }); }); +test('disallow PRs from main branch of fork', async () => { + const issue: Subset = { + 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 = { From bc082d00f8888d931a81aacc15a5ea78b7e1c8f4 Mon Sep 17 00:00:00 2001 From: Vinayak Kukreja Date: Thu, 19 Oct 2023 14:57:32 -0700 Subject: [PATCH 2/4] close pr on this error Signed-off-by: Vinayak Kukreja --- tools/@aws-cdk/prlint/lint.ts | 12 +++++++++++- tools/@aws-cdk/prlint/test/lint.test.ts | 2 ++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/tools/@aws-cdk/prlint/lint.ts b/tools/@aws-cdk/prlint/lint.ts index a1161a15ea2a4..2a1aaa85b77ca 100644 --- a/tools/@aws-cdk/prlint/lint.ts +++ b/tools/@aws-cdk/prlint/lint.ts @@ -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. */ @@ -268,6 +270,14 @@ export class PullRequestLinter { body, }); + // 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); } @@ -700,7 +710,7 @@ 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.') + result.assessFailure(pr.head.ref === 'main', PR_FROM_MAIN_ERROR); } return result; diff --git a/tools/@aws-cdk/prlint/test/lint.test.ts b/tools/@aws-cdk/prlint/test/lint.test.ts index 42185b50f7368..33746d1249954 100644 --- a/tools/@aws-cdk/prlint/test/lint.test.ts +++ b/tools/@aws-cdk/prlint/test/lint.test.ts @@ -911,6 +911,8 @@ function configureMock(pr: Subset, prFiles?: linter.GitHubFile[ listReviews: mockListReviews, dismissReview() {}, + + update() {}, }; const issuesClient = { From 41063ac19b0196d8ed4d3fa3f753d25458517874 Mon Sep 17 00:00:00 2001 From: Vinayak Kukreja Date: Thu, 19 Oct 2023 15:14:43 -0700 Subject: [PATCH 3/4] comment closing pr logic to first test the linter rule Signed-off-by: Vinayak Kukreja --- tools/@aws-cdk/prlint/lint.ts | 16 +++++++++------- tools/@aws-cdk/prlint/test/lint.test.ts | 2 -- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/tools/@aws-cdk/prlint/lint.ts b/tools/@aws-cdk/prlint/lint.ts index 2a1aaa85b77ca..66e225fb8be81 100644 --- a/tools/@aws-cdk/prlint/lint.ts +++ b/tools/@aws-cdk/prlint/lint.ts @@ -270,13 +270,15 @@ export class PullRequestLinter { body, }); - // 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', - }); - } + // Commenting this code to first test that linter rule works + // since this can lead to other PRs closing if not setup + // // 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); } diff --git a/tools/@aws-cdk/prlint/test/lint.test.ts b/tools/@aws-cdk/prlint/test/lint.test.ts index 33746d1249954..42185b50f7368 100644 --- a/tools/@aws-cdk/prlint/test/lint.test.ts +++ b/tools/@aws-cdk/prlint/test/lint.test.ts @@ -911,8 +911,6 @@ function configureMock(pr: Subset, prFiles?: linter.GitHubFile[ listReviews: mockListReviews, dismissReview() {}, - - update() {}, }; const issuesClient = { From 25005432b9a5c6aa6a0581572434868ba89d5842 Mon Sep 17 00:00:00 2001 From: Vinayak Kukreja Date: Thu, 19 Oct 2023 15:16:54 -0700 Subject: [PATCH 4/4] comment update Signed-off-by: Vinayak Kukreja --- tools/@aws-cdk/prlint/lint.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/@aws-cdk/prlint/lint.ts b/tools/@aws-cdk/prlint/lint.ts index 66e225fb8be81..0f16e78ed746a 100644 --- a/tools/@aws-cdk/prlint/lint.ts +++ b/tools/@aws-cdk/prlint/lint.ts @@ -271,7 +271,7 @@ export class PullRequestLinter { }); // Commenting this code to first test that linter rule works - // since this can lead to other PRs closing if not setup + // 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({