|
| 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 <tobias.nyholm@gmail.com> |
| 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 | +} |
0 commit comments