|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Command; |
| 4 | + |
| 5 | +use App\Api\Issue\IssueApi; |
| 6 | +use App\Entity\Task; |
| 7 | +use App\Service\RepositoryProvider; |
| 8 | +use App\Service\TaskScheduler; |
| 9 | +use Symfony\Component\Console\Command\Command; |
| 10 | +use Symfony\Component\Console\Input\InputArgument; |
| 11 | +use Symfony\Component\Console\Input\InputInterface; |
| 12 | +use Symfony\Component\Console\Output\OutputInterface; |
| 13 | + |
| 14 | +/** |
| 15 | + * Close issues not been updated in a long while. |
| 16 | + * |
| 17 | + * @author Tobias Nyholm <[email protected]> |
| 18 | + */ |
| 19 | +class CloseStaleIssuesCommand extends Command |
| 20 | +{ |
| 21 | + protected static $defaultName = 'app:issue:close-stale'; |
| 22 | + private $repositoryProvider; |
| 23 | + private $issueApi; |
| 24 | + private $scheduler; |
| 25 | + |
| 26 | + public function __construct(RepositoryProvider $repositoryProvider, IssueApi $issueApi, TaskScheduler $scheduler) |
| 27 | + { |
| 28 | + parent::__construct(); |
| 29 | + $this->repositoryProvider = $repositoryProvider; |
| 30 | + $this->issueApi = $issueApi; |
| 31 | + $this->scheduler = $scheduler; |
| 32 | + } |
| 33 | + |
| 34 | + protected function configure() |
| 35 | + { |
| 36 | + $this->addArgument('repository', InputArgument::REQUIRED, 'The full name to the repository, eg symfony/symfony-docs'); |
| 37 | + } |
| 38 | + |
| 39 | + protected function execute(InputInterface $input, OutputInterface $output) |
| 40 | + { |
| 41 | + /** @var string $repositoryName */ |
| 42 | + $repositoryName = $input->getArgument('repository'); |
| 43 | + $repository = $this->repositoryProvider->getRepository($repositoryName); |
| 44 | + if (null === $repository) { |
| 45 | + $output->writeln('Repository not configured'); |
| 46 | + |
| 47 | + return 1; |
| 48 | + } |
| 49 | + |
| 50 | + $notUpdatedAfter = new \DateTimeImmutable('-12months'); |
| 51 | + $issues = $this->issueApi->findStaleIssues($repository, $notUpdatedAfter); |
| 52 | + |
| 53 | + foreach ($issues as $issue) { |
| 54 | + $this->issueApi->commentOnIssue($repository, $issue['number'], <<<TXT |
| 55 | +Hey, |
| 56 | +
|
| 57 | +Is this issue still relevant? It has not been any activity in a while. I will close this if nobody makes a comment soon. |
| 58 | +
|
| 59 | +Cheers! |
| 60 | +
|
| 61 | +Carsonbot |
| 62 | +TXT |
| 63 | +); |
| 64 | + |
| 65 | + // add a scheduled task to process this issue again after 2 weeks |
| 66 | + $this->scheduler->runLater($repository, $issue['number'], Task::ACTION_CLOSE_STALE, new \DateTimeImmutable('+2weeks')); |
| 67 | + } |
| 68 | + |
| 69 | + return 0; |
| 70 | + } |
| 71 | +} |
0 commit comments