Skip to content

Commit 37e7726

Browse files
committed
Add comment about squashing commits
1 parent 01eba9a commit 37e7726

8 files changed

+68
-6
lines changed

config/services.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ parameters:
1414
- 'App\Subscriber\CloseDraftPRSubscriber'
1515
- 'App\Subscriber\UnsupportedBranchSubscriber'
1616
- 'App\Subscriber\RemoveStalledLabelOnCommentSubscriber'
17+
- 'App\Subscriber\AllowEditFromMaintainerSubscriber'
1718
secret: '%env(SYMFONY_SECRET)%'
1819

1920
symfony/symfony-docs:
@@ -29,6 +30,7 @@ parameters:
2930
- 'subscriber.symfony_docs.milestone'
3031
- 'App\Subscriber\RemoveStalledLabelOnCommentSubscriber'
3132
- 'App\Subscriber\UpdateMilestoneWhenLabeledWaitingCodeMergeSubscriber'
33+
- 'App\Subscriber\AllowEditFromMaintainerSubscriber'
3234
secret: '%env(SYMFONY_DOCS_SECRET)%'
3335

3436
# used in a functional test
@@ -48,6 +50,7 @@ parameters:
4850
- 'App\Subscriber\UnsupportedBranchSubscriber'
4951
- 'App\Subscriber\RemoveStalledLabelOnCommentSubscriber'
5052
- 'App\Subscriber\UpdateMilestoneWhenLabeledWaitingCodeMergeSubscriber'
53+
- 'App\Subscriber\AllowEditFromMaintainerSubscriber'
5154

5255
services:
5356
_defaults:
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
namespace App\Subscriber;
4+
5+
use App\Api\Issue\IssueApi;
6+
use App\Api\PullRequest\PullRequestApi;
7+
use App\Event\GitHubEvent;
8+
use App\GitHubEvents;
9+
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
10+
11+
/**
12+
* @author Tobias Nyholm <[email protected]>
13+
*/
14+
class AllowEditFromMaintainerSubscriber implements EventSubscriberInterface
15+
{
16+
private $commentsApi;
17+
private $pullRequestApi;
18+
19+
public function __construct(IssueApi $commentsApi, PullRequestApi $pullRequestApi)
20+
{
21+
$this->commentsApi = $commentsApi;
22+
$this->pullRequestApi = $pullRequestApi;
23+
}
24+
25+
public function onPullRequest(GitHubEvent $event)
26+
{
27+
$data = $event->getData();
28+
if (!in_array($data['action'], ['opened', 'ready_for_review']) || ($data['pull_request']['draft'] ?? false)) {
29+
return;
30+
}
31+
32+
if ($data['pull_request']['maintainer_can_modify'] ?? true) {
33+
return;
34+
}
35+
36+
$repository = $event->getRepository();
37+
$pullRequestNumber = $data['pull_request']['number'];
38+
$this->commentsApi->commentOnIssue($repository, $pullRequestNumber, <<<TXT
39+
Please note that you need squash your commits before this PR can be merged. The maintainer can also squash the commits for you, but then you need to “Allow edits from maintainer” (there is a checkbox in the sidebar of the PR).
40+
41+
Cheers!
42+
43+
Carsonbot
44+
TXT
45+
);
46+
47+
$event->setResponseData([
48+
'pull_request' => $pullRequestNumber,
49+
'squash_comment' => true,
50+
]);
51+
}
52+
53+
public static function getSubscribedEvents()
54+
{
55+
return [
56+
GitHubEvents::PULL_REQUEST => 'onPullRequest',
57+
];
58+
}
59+
}

tests/Controller/WebhookControllerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public function getTests()
102102
'Welcome first users' => [
103103
'pull_request',
104104
'pull_request.new_contributor.json',
105-
['pull_request' => 4, 'status_change' => 'needs_review', 'pr_labels' => [], 'new_contributor' => true],
105+
['pull_request' => 4, 'status_change' => 'needs_review', 'pr_labels' => [], 'new_contributor' => true, 'squash_comment' => true],
106106
],
107107
'Waiting Code Merge' => [
108108
'pull_request',

tests/webhook_examples/pull_request.draft_to_ready.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@
361361
"merged_by": null,
362362
"comments": 0,
363363
"review_comments": 0,
364-
"maintainer_can_modify": false,
364+
"maintainer_can_modify": true,
365365
"commits": 1,
366366
"additions": 1,
367367
"deletions": 0,

tests/webhook_examples/pull_request.labeled.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@
411411
"merged_by": null,
412412
"comments": 0,
413413
"review_comments": 0,
414-
"maintainer_can_modify": false,
414+
"maintainer_can_modify": true,
415415
"commits": 1,
416416
"additions": 3,
417417
"deletions": 1,

tests/webhook_examples/pull_request.new_contributor.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@
340340
"merged_by": null,
341341
"comments": 0,
342342
"review_comments": 0,
343-
"maintainer_can_modify": true,
343+
"maintainer_can_modify": false,
344344
"commits": 1,
345345
"additions": 1,
346346
"deletions": 1,

tests/webhook_examples/pull_request.opened_draft.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@
353353
"merged_by": null,
354354
"comments": 0,
355355
"review_comments": 0,
356-
"maintainer_can_modify": false,
356+
"maintainer_can_modify": true,
357357
"commits": 1,
358358
"additions": 1,
359359
"deletions": 0,

tests/webhook_examples/pull_request.opened_target_branch.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@
352352
"merged_by": null,
353353
"comments": 0,
354354
"review_comments": 0,
355-
"maintainer_can_modify": false,
355+
"maintainer_can_modify": true,
356356
"commits": 1,
357357
"additions": 1,
358358
"deletions": 0,

0 commit comments

Comments
 (0)