|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Service\Jira; |
| 4 | + |
| 5 | +use GuzzleHttp\Client; |
| 6 | +use GuzzleHttp\Exception\ClientException; |
| 7 | +use GuzzleHttp\UriTemplate; |
| 8 | +use Psr\Http\Message\ResponseInterface; |
| 9 | +use Psr\Http\Message\ServerRequestInterface; |
| 10 | + |
| 11 | +class JiraShowIssueCommand |
| 12 | +{ |
| 13 | + /** |
| 14 | + * @var Client |
| 15 | + */ |
| 16 | + private $jiraClient; |
| 17 | + /** |
| 18 | + * @var Client |
| 19 | + */ |
| 20 | + private $slackClient; |
| 21 | + /** |
| 22 | + * @var string |
| 23 | + */ |
| 24 | + private $jiraUrl; |
| 25 | + |
| 26 | + public function __construct(Client $jiraClient, Client $slackClient, string $jiraUrl) |
| 27 | + { |
| 28 | + $this->jiraClient = $jiraClient; |
| 29 | + $this->slackClient = $slackClient; |
| 30 | + $this->jiraUrl = $jiraUrl; |
| 31 | + } |
| 32 | + |
| 33 | + public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next) |
| 34 | + { |
| 35 | + $body = $request->getParsedBody(); |
| 36 | + if ($body['args'][0] !== "show") { |
| 37 | + return $next($request, $response); |
| 38 | + } |
| 39 | + $jobs = array_reverse(array_slice($body['args'], 1)); |
| 40 | + $uriTemplate = new UriTemplate(); |
| 41 | + $uri = $uriTemplate->expand( |
| 42 | + 'rest/api/2/search?jql=key in ({issueKeys*})&expand=editmeta&fields=customfield_10024&fields=summary' |
| 43 | + . '&fields=creator&fields=assignee&fields=issuetype&fields=priority&fields=status&fields=resolution', |
| 44 | + [ |
| 45 | + 'issueKeys' => $jobs, |
| 46 | + ] |
| 47 | + ); |
| 48 | + try { |
| 49 | + $json = json_decode($this->jiraClient->get($uri)->getBody()->getContents(), true); |
| 50 | + $attachments = $this->prepareSlackAttachments($json); |
| 51 | + $responseBody = [ |
| 52 | + 'text' => "Search Results for `{$body['command']} {$body['text']}`", |
| 53 | + 'response_type' => $body['response_type'] ?? 'ephemeral', |
| 54 | + 'attachments' => $attachments, |
| 55 | + ]; |
| 56 | + $this->slackClient->post($body['response_url'], ['body' => json_encode($responseBody)]); |
| 57 | + } catch (ClientException $e) { |
| 58 | + $error = $e->getMessage(); |
| 59 | + $this->slackClient->post( |
| 60 | + $body['response_url'], |
| 61 | + [ |
| 62 | + 'body' => json_encode( |
| 63 | + [ |
| 64 | + 'text' => "Running `{$body['command']} {$body['text']}` didn't work. Got " |
| 65 | + . $e->getCode() . " for an HTTP response", |
| 66 | + ] |
| 67 | + ), |
| 68 | + ] |
| 69 | + ); |
| 70 | + } |
| 71 | + |
| 72 | + return $next($request, $response, $error ?? null); |
| 73 | + } |
| 74 | + |
| 75 | + private function prepareSlackAttachments($json) |
| 76 | + { |
| 77 | + $results = []; |
| 78 | + $jobs = $json['issues'] ?? []; |
| 79 | + $colorMap = [ |
| 80 | + 'Blocker' => '#ec0909', // bright red |
| 81 | + 'Critical' => '#d9534f', // red |
| 82 | + 'Major' => '#f0ad4e', // orange |
| 83 | + 'Medium' => '#5bc0de', // blue |
| 84 | + 'Trivial' => '#c7ead4', // light-green |
| 85 | + ]; |
| 86 | + foreach ($jobs as $index => $job) { |
| 87 | + $priority = $job['fields']['priority']['name'] ?? '¯\_(ツ)_/¯'; |
| 88 | + $status = $job['fields']['status']['name'] ?? '¯\_(ツ)_/¯'; |
| 89 | + $type = $job['fields']['issuetype']['name'] ?? '¯\_(ツ)_/¯'; |
| 90 | + $creator = $job['fields']['creator']['displayName'] ?? 'no one'; |
| 91 | + $assignee = $job['fields']['assignee']['displayName'] ?? 'no one'; |
| 92 | + $blocked = ($job['fields']['customfield_10024']['value'] == "Yes" ? " that is currently *Blocked*" : ''); |
| 93 | + |
| 94 | + $results[$index]['title'] = "{$job['key']} - " . ($job['fields']['summary'] ?? ''); |
| 95 | + $results[$index]['title_link'] = "{$this->jiraUrl}/browse/{$job['key']}"; |
| 96 | + $results[$index]['fallback'] = $results[$index]['title']; |
| 97 | + $results[$index]['color'] = $colorMap[$job['fields']['priority']['name'] ?? null] ?? '#205081'; |
| 98 | + $results[$index]['text'] = "A *{$priority}* *{$type}* marked as *'{$status}'*," |
| 99 | + . " created by *{$creator}* assigned to *{$assignee}* {$blocked}"; |
| 100 | + $results[$index]['mrkdwn_in'] = ['text']; |
| 101 | + } |
| 102 | + |
| 103 | + return $results; |
| 104 | + } |
| 105 | +} |
0 commit comments