Skip to content

Commit

Permalink
chore: auto close pr if opened from fork's main branch (#27627)
Browse files Browse the repository at this point in the history
This PR would enable closing a pull request automatically if it is opened from `main` branch of their fork.

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
vinayak-kukreja authored Oct 20, 2023
1 parent 70acc84 commit 93904e8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
29 changes: 20 additions & 9 deletions tools/@aws-cdk/prlint/lint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,15 +270,26 @@ 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',
// });
// }
// Closing the PR if it is opened from main branch of author's fork
if (failureMessages.includes(PR_FROM_MAIN_ERROR)) {

const errorMessageBody = 'Your pull request must be based off of a branch in a personal account '
+ '(not an organization owned account, and not the main branch). You must also have the setting '
+ 'enabled that <a href="https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/working-with-forks/allowing-changes-to-a-pull-request-branch-created-from-a-fork">allows the CDK team to push changes to your branch</a> '
+ '(this setting is enabled by default for personal accounts, and cannot be enabled for organization owned accounts). '
+ 'The reason for this is that our automation needs to synchronize your branch with our main after it has been approved, '
+ 'and we cannot do that if we cannot push to your branch.'

await this.client.issues.createComment({
...this.issueParams,
body: errorMessageBody,
});

await this.client.pulls.update({
...this.prParams,
state: 'closed',
});
}

throw new LinterError(body);
}
Expand Down
2 changes: 2 additions & 0 deletions tools/@aws-cdk/prlint/test/lint.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -911,6 +911,8 @@ function configureMock(pr: Subset<linter.GitHubPr>, prFiles?: linter.GitHubFile[
listReviews: mockListReviews,

dismissReview() {},

update() {},
};

const issuesClient = {
Expand Down

0 comments on commit 93904e8

Please sign in to comment.