|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Subscriber; |
| 4 | + |
| 5 | +use App\Api\Issue\IssueApi; |
| 6 | +use App\Entity\Task; |
| 7 | +use App\Event\GitHubEvent; |
| 8 | +use App\GitHubEvents; |
| 9 | +use App\Service\ComplementGenerator; |
| 10 | +use App\Service\TaskScheduler; |
| 11 | +use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
| 12 | + |
| 13 | +/** |
| 14 | + * @author Tobias Nyholm <[email protected]> |
| 15 | + */ |
| 16 | +class CloseDraftPRSubscriber implements EventSubscriberInterface |
| 17 | +{ |
| 18 | + private $issueApi; |
| 19 | + private $complementGenerator; |
| 20 | + private $scheduler; |
| 21 | + |
| 22 | + public function __construct(IssueApi $issueApi, ComplementGenerator $complementGenerator, TaskScheduler $scheduler) |
| 23 | + { |
| 24 | + $this->issueApi = $issueApi; |
| 25 | + $this->complementGenerator = $complementGenerator; |
| 26 | + $this->scheduler = $scheduler; |
| 27 | + } |
| 28 | + |
| 29 | + public function onPullRequest(GitHubEvent $event) |
| 30 | + { |
| 31 | + $data = $event->getData(); |
| 32 | + $repository = $event->getRepository(); |
| 33 | + if ('opened' !== $data['action'] || !($data['pull_request']['draft'] ?? false)) { |
| 34 | + return; |
| 35 | + } |
| 36 | + |
| 37 | + $number = $data['pull_request']['number']; |
| 38 | + $complement = $this->complementGenerator->getPullRequestComplement(); |
| 39 | + $this->issueApi->commentOnIssue($repository, $number, <<<TXT |
| 40 | +Hey! |
| 41 | +
|
| 42 | +$complement |
| 43 | +
|
| 44 | +Could you please click the "ready for review" button? Or maybe close this PR and open a new one when you are done. |
| 45 | +Note that a pull request does not have to be "perfect" or "ready for merge" when you first open it, but it should be ready for a first review. |
| 46 | +
|
| 47 | +Cheers! |
| 48 | +
|
| 49 | +Carsonbot |
| 50 | +TXT |
| 51 | +); |
| 52 | + |
| 53 | + // Add a scheduled task to close the PR within 1 hour. |
| 54 | + $this->scheduler->runLater($repository, $number, Task::ACTION_CLOSE_DRAFT, new \DateTimeImmutable('+1hour')); |
| 55 | + |
| 56 | + $event->setResponseData([ |
| 57 | + 'pull_request' => $number, |
| 58 | + 'draft_comment' => true, |
| 59 | + ]); |
| 60 | + } |
| 61 | + |
| 62 | + public static function getSubscribedEvents() |
| 63 | + { |
| 64 | + return [ |
| 65 | + GitHubEvents::PULL_REQUEST => 'onPullRequest', |
| 66 | + ]; |
| 67 | + } |
| 68 | +} |
0 commit comments