|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Artemeon\M2G\Command; |
| 6 | + |
| 7 | +use Artemeon\M2G\Config\ConfigValues; |
| 8 | +use Artemeon\M2G\Helper\CliTableConverter; |
| 9 | +use Artemeon\M2G\Helper\HtmlTableConverter; |
| 10 | +use Artemeon\M2G\Helper\UpstreamIssueParser; |
| 11 | +use Artemeon\M2G\Service\GithubConnector; |
| 12 | +use Artemeon\M2G\Service\MantisConnector; |
| 13 | + |
| 14 | +class IssuesListCommand extends Command |
| 15 | +{ |
| 16 | + protected string $signature = 'issues:list {--output= : Output Format}'; |
| 17 | + protected ?string $description = 'Get a list of Mantis Tickets with their associated GitHub Issues.'; |
| 18 | + |
| 19 | + private MantisConnector $mantisConnector; |
| 20 | + private GithubConnector $githubConnector; |
| 21 | + private ?ConfigValues $config; |
| 22 | + |
| 23 | + public function __construct(MantisConnector $mantisConnector, GithubConnector $githubConnector, ?ConfigValues $config) |
| 24 | + { |
| 25 | + parent::__construct(); |
| 26 | + |
| 27 | + $this->mantisConnector = $mantisConnector; |
| 28 | + $this->githubConnector = $githubConnector; |
| 29 | + $this->config = $config; |
| 30 | + } |
| 31 | + |
| 32 | + public function __invoke(): int |
| 33 | + { |
| 34 | + $mantisIssues = $this->mantisConnector->fetchIssues(410); |
| 35 | + |
| 36 | + $githubIssueIds = []; |
| 37 | + foreach ($mantisIssues as $issue) { |
| 38 | + $parsedIssues = array_map(static fn (array $data) => $data['id'], UpstreamIssueParser::parse($issue->getUpstreamTicket())); |
| 39 | + $githubIssueIds = [...$githubIssueIds, ...$parsedIssues]; |
| 40 | + } |
| 41 | + |
| 42 | + $parts = []; |
| 43 | + foreach (array_unique($githubIssueIds) as $id) { |
| 44 | + $parts[] = <<<GRAPHQL |
| 45 | +issue$id: issue(number: $id) { |
| 46 | + ...IssueFragment |
| 47 | +} |
| 48 | +GRAPHQL; |
| 49 | + } |
| 50 | + $issuesQuery = implode(PHP_EOL, $parts); |
| 51 | + |
| 52 | + $issueFragment = <<<GRAPHQL |
| 53 | +fragment IssueFragment on Issue { |
| 54 | + title |
| 55 | + url |
| 56 | + closed |
| 57 | +} |
| 58 | +GRAPHQL; |
| 59 | + |
| 60 | + $repo = $this->config->getGithubRepo(); |
| 61 | + [$owner, $name] = explode('/', $repo); |
| 62 | + |
| 63 | + $query = <<<GRAPHQL |
| 64 | +{ |
| 65 | + repository(name: "$name", owner: "$owner") { |
| 66 | + $issuesQuery |
| 67 | + } |
| 68 | +} |
| 69 | +
|
| 70 | +$issueFragment |
| 71 | +GRAPHQL; |
| 72 | + |
| 73 | + $githubResult = $this->githubConnector->graphql($query)['data']['repository']; |
| 74 | + |
| 75 | + switch ($this->option('output')) { |
| 76 | + case 'html': |
| 77 | + HtmlTableConverter::convert($this, $mantisIssues, $githubResult); |
| 78 | + |
| 79 | + break; |
| 80 | + default: |
| 81 | + CliTableConverter::convert($this, $mantisIssues, $githubResult); |
| 82 | + } |
| 83 | + |
| 84 | + return self::SUCCESS; |
| 85 | + } |
| 86 | +} |
0 commit comments