|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Command; |
| 4 | + |
| 5 | +use App\Api\Issue\IssueApi; |
| 6 | +use App\Api\Issue\IssueType; |
| 7 | +use App\Api\Label\LabelApi; |
| 8 | +use App\Entity\Task; |
| 9 | +use App\Service\RepositoryProvider; |
| 10 | +use App\Service\StaleIssueCommentGenerator; |
| 11 | +use App\Service\TaskScheduler; |
| 12 | +use Symfony\Component\Console\Command\Command; |
| 13 | +use Symfony\Component\Console\Input\InputArgument; |
| 14 | +use Symfony\Component\Console\Input\InputInterface; |
| 15 | +use Symfony\Component\Console\Input\InputOption; |
| 16 | +use Symfony\Component\Console\Output\OutputInterface; |
| 17 | + |
| 18 | +/** |
| 19 | + * Close issues not been updated in a long while. |
| 20 | + * |
| 21 | + * @author Tobias Nyholm <[email protected]> |
| 22 | + */ |
| 23 | +class PingStaleIssuesCommand extends Command |
| 24 | +{ |
| 25 | + public const STALE_IF_NOT_UPDATED_SINCE = '-12months'; |
| 26 | + public const MESSAGE_TWO_AFTER = '+2weeks'; |
| 27 | + public const MESSAGE_THREE_AND_CLOSE_AFTER = '+2weeks'; |
| 28 | + |
| 29 | + protected static $defaultName = 'app:issue:ping-stale'; |
| 30 | + |
| 31 | + private $repositoryProvider; |
| 32 | + private $issueApi; |
| 33 | + private $scheduler; |
| 34 | + private $commentGenerator; |
| 35 | + private $labelApi; |
| 36 | + |
| 37 | + public function __construct(RepositoryProvider $repositoryProvider, IssueApi $issueApi, TaskScheduler $scheduler, StaleIssueCommentGenerator $commentGenerator, LabelApi $labelApi) |
| 38 | + { |
| 39 | + parent::__construct(); |
| 40 | + $this->repositoryProvider = $repositoryProvider; |
| 41 | + $this->issueApi = $issueApi; |
| 42 | + $this->scheduler = $scheduler; |
| 43 | + $this->commentGenerator = $commentGenerator; |
| 44 | + $this->labelApi = $labelApi; |
| 45 | + } |
| 46 | + |
| 47 | + protected function configure() |
| 48 | + { |
| 49 | + $this->addArgument('repository', InputArgument::REQUIRED, 'The full name to the repository, eg symfony/symfony-docs'); |
| 50 | + $this->addOption('dry-run', null, InputOption::VALUE_NONE, 'Do a test search without making any comments or changes'); |
| 51 | + } |
| 52 | + |
| 53 | + protected function execute(InputInterface $input, OutputInterface $output) |
| 54 | + { |
| 55 | + /** @var string $repositoryName */ |
| 56 | + $repositoryName = $input->getArgument('repository'); |
| 57 | + $repository = $this->repositoryProvider->getRepository($repositoryName); |
| 58 | + if (null === $repository) { |
| 59 | + $output->writeln('Repository not configured'); |
| 60 | + |
| 61 | + return 1; |
| 62 | + } |
| 63 | + |
| 64 | + $notUpdatedAfter = new \DateTimeImmutable(self::STALE_IF_NOT_UPDATED_SINCE); |
| 65 | + $issues = $this->issueApi->findStaleIssues($repository, $notUpdatedAfter); |
| 66 | + |
| 67 | + if ($input->getOption('dry-run')) { |
| 68 | + foreach ($issues as $issue) { |
| 69 | + $output->writeln(sprintf('Marking issue #%s as "Staled". Link https://github.com/%s/issues/%s', $issue['number'], $repository->getFullName(), $issue['number'])); |
| 70 | + } |
| 71 | + |
| 72 | + return 0; |
| 73 | + } |
| 74 | + |
| 75 | + foreach ($issues as $issue) { |
| 76 | + $comment = $this->commentGenerator->getComment($this->extractType($issue)); |
| 77 | + $this->issueApi->commentOnIssue($repository, $issue['number'], $comment); |
| 78 | + $this->labelApi->addIssueLabel($issue['number'], 'Staled', $repository); |
| 79 | + |
| 80 | + // add a scheduled task to process this issue again after 2 weeks |
| 81 | + $this->scheduler->runLater($repository, $issue['number'], Task::ACTION_INFORM_CLOSE_STALE, new \DateTimeImmutable(self::MESSAGE_TWO_AFTER)); |
| 82 | + } |
| 83 | + |
| 84 | + return 0; |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Extract type from issue array. Make sure we priorities labels if there are |
| 89 | + * more than one type defined. |
| 90 | + */ |
| 91 | + private function extractType(array $issue) |
| 92 | + { |
| 93 | + $types = [ |
| 94 | + IssueType::FEATURE => false, |
| 95 | + IssueType::BUG => false, |
| 96 | + IssueType::RFC => false, |
| 97 | + ]; |
| 98 | + |
| 99 | + foreach ($issue['labels'] as $label) { |
| 100 | + if (isset($types[$label['name']])) { |
| 101 | + $types[$label['name']] = true; |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + foreach ($types as $type => $exists) { |
| 106 | + if ($exists) { |
| 107 | + return $type; |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + return IssueType::UNKNOWN; |
| 112 | + } |
| 113 | +} |
0 commit comments