From fccbb7da4697f7f4fc1427dc79c8ae272a133a80 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Tue, 26 May 2020 09:06:20 +0300 Subject: [PATCH 1/3] Add Note matching route handler --- src/Event/Subscriber/EventumUnfurler.php | 1 + src/Route/EventumRoutes.php | 1 + src/Route/Note.php | 68 +++++++++++++++++++ .../EventumUnfurlServiceProvider.php | 7 ++ 4 files changed, 77 insertions(+) create mode 100644 src/Route/Note.php diff --git a/src/Event/Subscriber/EventumUnfurler.php b/src/Event/Subscriber/EventumUnfurler.php index 6e7acf7..fa08241 100644 --- a/src/Event/Subscriber/EventumUnfurler.php +++ b/src/Event/Subscriber/EventumUnfurler.php @@ -22,6 +22,7 @@ class EventumUnfurler implements EventSubscriberInterface private const ROUTES = [ 'issue' => Route\Issue::class, + 'note' => Route\Note::class, ]; /** @var RouteMatcher */ diff --git a/src/Route/EventumRoutes.php b/src/Route/EventumRoutes.php index 23c62ce..c61749c 100644 --- a/src/Route/EventumRoutes.php +++ b/src/Route/EventumRoutes.php @@ -40,6 +40,7 @@ protected function buildRoutes(string $domain): array return [ 'issue' => "^${base}/view\.php\?id=(?P\d+)", + 'note' => "^${base}/view_note\.php\?id=(?P\d+)", ]; } } diff --git a/src/Route/Note.php b/src/Route/Note.php new file mode 100644 index 0000000..c7c6a0c --- /dev/null +++ b/src/Route/Note.php @@ -0,0 +1,68 @@ +apiClient = $apiClient; + $this->logger = $logger; + } + + public function unfurl(string $url, array $parts): ?array + { + $noteId = (int)$parts['id']; + $note = $this->getNoteDetails($noteId); + $this->debug(' note', ['note' => $note]); + $issueId = $note['not_iss_id']; + + return [ + 'title' => "Note in <$url|Issue #{$issueId}>: {$note['not_title']}", + 'text' => $note['not_note'], + 'color' => '#006486', + 'ts' => $note['not_created_date_ts'], + 'footer' => "Note by {$note['not_from']}", + ]; + } + + /** + * @param int $noteId + * @throws XmlRpcException + * @return array + */ + private function getNoteDetails(int $noteId): array + { + return $this->apiClient->getNoteDetails($noteId, self::MATCH_KEYS); + } +} diff --git a/src/ServiceProvider/EventumUnfurlServiceProvider.php b/src/ServiceProvider/EventumUnfurlServiceProvider.php index 86fced2..f1c148f 100644 --- a/src/ServiceProvider/EventumUnfurlServiceProvider.php +++ b/src/ServiceProvider/EventumUnfurlServiceProvider.php @@ -49,6 +49,13 @@ public function register(Container $app): void $app['logger'] ); }; + + $app[Route\Note::class] = static function ($app) { + return new Route\Note( + $app[EventumXmlRpcClient::class], + $app['logger'] + ); + }; } public function subscribe(Container $app, EventDispatcherInterface $dispatcher): void From 84a7aaff28c7533c2d97414b634075848280e69d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Tue, 26 May 2020 13:29:01 +0300 Subject: [PATCH 2/3] Extract eventum api interaction logic to separate class --- src/ApiClient.php | 68 +++++++++++++++++++ src/Route/Issue.php | 48 ++----------- src/Route/Note.php | 36 ++-------- .../EventumUnfurlServiceProvider.php | 9 ++- 4 files changed, 87 insertions(+), 74 deletions(-) create mode 100644 src/ApiClient.php diff --git a/src/ApiClient.php b/src/ApiClient.php new file mode 100644 index 0000000..26dc339 --- /dev/null +++ b/src/ApiClient.php @@ -0,0 +1,68 @@ +client = $client; + } + + /** + * Get issue details, but filter only needed keys. + * + * @param int $issueId + * @throws XmlRpcException + * @return array + */ + public function getIssueDetails(int $issueId): array + { + /** @var array $issue */ + $issue = $this->client->getIssueDetails($issueId); + + $fields = [ + 'assignments', + 'iss_created_date', + 'iss_created_date_ts', + 'iss_description', + 'iss_id', + 'iss_last_internal_action_date', + 'iss_last_public_action_date', + 'iss_original_description', + 'iss_summary', + 'iss_updated_date', + 'prc_title', + 'pri_title', + 'reporter', + 'sta_title', + ]; + + return array_intersect_key($issue, array_flip($fields)); + } + + /** + * @param int $noteId + * @throws XmlRpcException + * @return array + */ + public function getNoteDetails(int $noteId): array + { + $fields = [ + 'not_id', + 'not_iss_id', + 'not_title', + 'not_from', + 'not_note', + 'not_created_date_ts', + ]; + + return $this->client->getNoteDetails($noteId, $fields); + } +} diff --git a/src/Route/Issue.php b/src/Route/Issue.php index 0a7c7d8..3c8903a 100644 --- a/src/Route/Issue.php +++ b/src/Route/Issue.php @@ -4,8 +4,7 @@ use DateTime; use DateTimeZone; -use Eventum\RPC\EventumXmlRpcClient; -use Eventum\RPC\XmlRpcException; +use Eventum\SlackUnfurl\ApiClient; use Psr\Log\LoggerInterface; use SlackUnfurl\Traits\LoggerTrait; @@ -13,40 +12,19 @@ class Issue { use LoggerTrait; - /** @var EventumXmlRpcClient */ - private $apiClient; + /** @var ApiClient */ + private $client; /** @var DateTimeZone */ private $utc; /** @var DateTimeZone */ private $timeZone; - /** - * getDetails keys to retrieve - * @see getIssueDetails - */ - private const MATCH_KEYS = [ - 'assignments', - 'iss_created_date', - 'iss_created_date_ts', - 'iss_description', - 'iss_id', - 'iss_last_internal_action_date', - 'iss_last_public_action_date', - 'iss_original_description', - 'iss_summary', - 'iss_updated_date', - 'prc_title', - 'pri_title', - 'reporter', - 'sta_title', - ]; - public function __construct( - EventumXmlRpcClient $apiClient, + ApiClient $client, string $timeZone, LoggerInterface $logger ) { - $this->apiClient = $apiClient; + $this->client = $client; $this->logger = $logger; $this->utc = new DateTimeZone('UTC'); $this->timeZone = new DateTimeZone($timeZone); @@ -55,7 +33,7 @@ public function __construct( public function unfurl(string $url, array $parts): ?array { $issueId = (int)$parts['id']; - $issue = $this->getIssueDetails($issueId); + $issue = $this->client->getIssueDetails($issueId); $this->debug('issue', ['issue' => $issue]); return [ @@ -88,20 +66,6 @@ public function unfurl(string $url, array $parts): ?array ]; } - /** - * Get issue details, but filter only needed keys. - * - * @param int $issueId - * @throws XmlRpcException - * @return array - */ - private function getIssueDetails(int $issueId): array - { - $issue = $this->apiClient->getIssueDetails($issueId); - - return array_intersect_key($issue, array_flip(self::MATCH_KEYS)); - } - /** * Get issue last update in local timezone * diff --git a/src/Route/Note.php b/src/Route/Note.php index c7c6a0c..8d6c09f 100644 --- a/src/Route/Note.php +++ b/src/Route/Note.php @@ -3,8 +3,7 @@ namespace Eventum\SlackUnfurl\Route; use DateTimeZone; -use Eventum\RPC\EventumXmlRpcClient; -use Eventum\RPC\XmlRpcException; +use Eventum\SlackUnfurl\ApiClient; use Psr\Log\LoggerInterface; use SlackUnfurl\Traits\LoggerTrait; @@ -12,38 +11,25 @@ class Note { use LoggerTrait; - /** @var EventumXmlRpcClient */ - private $apiClient; + /** @var ApiClient */ + private $client; /** @var DateTimeZone */ private $utc; /** @var DateTimeZone */ private $timeZone; - /** - * getDetails keys to retrieve - * @see getNoteDetails - */ - private const MATCH_KEYS = [ - 'not_id', - 'not_iss_id', - 'not_title', - 'not_from', - 'not_note', - 'not_created_date_ts', - ]; - public function __construct( - EventumXmlRpcClient $apiClient, + ApiClient $client, LoggerInterface $logger ) { - $this->apiClient = $apiClient; + $this->client = $client; $this->logger = $logger; } public function unfurl(string $url, array $parts): ?array { $noteId = (int)$parts['id']; - $note = $this->getNoteDetails($noteId); + $note = $this->client->getNoteDetails($noteId); $this->debug(' note', ['note' => $note]); $issueId = $note['not_iss_id']; @@ -55,14 +41,4 @@ public function unfurl(string $url, array $parts): ?array 'footer' => "Note by {$note['not_from']}", ]; } - - /** - * @param int $noteId - * @throws XmlRpcException - * @return array - */ - private function getNoteDetails(int $noteId): array - { - return $this->apiClient->getNoteDetails($noteId, self::MATCH_KEYS); - } } diff --git a/src/ServiceProvider/EventumUnfurlServiceProvider.php b/src/ServiceProvider/EventumUnfurlServiceProvider.php index f1c148f..780790c 100644 --- a/src/ServiceProvider/EventumUnfurlServiceProvider.php +++ b/src/ServiceProvider/EventumUnfurlServiceProvider.php @@ -3,6 +3,7 @@ namespace Eventum\SlackUnfurl\ServiceProvider; use Eventum\RPC\EventumXmlRpcClient; +use Eventum\SlackUnfurl\ApiClient; use Eventum\SlackUnfurl\Event\Subscriber\EventumUnfurler; use Eventum\SlackUnfurl\Route; use Eventum\SlackUnfurl\Route\EventumRoutes; @@ -29,6 +30,10 @@ public function register(Container $app): void return $client; }; + $app[ApiClient::class] = static function ($app) { + return new ApiClient($app[EventumXmlRpcClient::class]); + }; + $app[EventumRoutes::class] = static function ($app) { return new EventumRoutes($app['eventum.domain']); }; @@ -44,7 +49,7 @@ public function register(Container $app): void $app[Route\Issue::class] = static function ($app) { return new Route\Issue( - $app[EventumXmlRpcClient::class], + $app[ApiClient::class], $app['eventum.timezone'], $app['logger'] ); @@ -52,7 +57,7 @@ public function register(Container $app): void $app[Route\Note::class] = static function ($app) { return new Route\Note( - $app[EventumXmlRpcClient::class], + $app[ApiClient::class], $app['logger'] ); }; From 8546953e437c368f56b81dfd88db1acd3af3a082 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Thu, 28 May 2020 10:26:09 +0300 Subject: [PATCH 3/3] Use issue_url in note link --- src/ApiClient.php | 1 + src/Route/Note.php | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/ApiClient.php b/src/ApiClient.php index 26dc339..eca7f5b 100644 --- a/src/ApiClient.php +++ b/src/ApiClient.php @@ -55,6 +55,7 @@ public function getIssueDetails(int $issueId): array public function getNoteDetails(int $noteId): array { $fields = [ + 'issue_url', 'not_id', 'not_iss_id', 'not_title', diff --git a/src/Route/Note.php b/src/Route/Note.php index 8d6c09f..f61944f 100644 --- a/src/Route/Note.php +++ b/src/Route/Note.php @@ -32,9 +32,10 @@ public function unfurl(string $url, array $parts): ?array $note = $this->client->getNoteDetails($noteId); $this->debug(' note', ['note' => $note]); $issueId = $note['not_iss_id']; + $issueUrl = $note['issue_url']; return [ - 'title' => "Note in <$url|Issue #{$issueId}>: {$note['not_title']}", + 'title' => "Note in <$issueUrl|Issue #{$issueId}>: {$note['not_title']}", 'text' => $note['not_note'], 'color' => '#006486', 'ts' => $note['not_created_date_ts'],