Skip to content

Commit

Permalink
standalone provider for slack-unfurl project
Browse files Browse the repository at this point in the history
  • Loading branch information
glensc committed Mar 1, 2018
0 parents commit 19ac7d7
Show file tree
Hide file tree
Showing 7 changed files with 289 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
*~
/.env
/.idea/
/composer.lock
/vendor/
22 changes: 22 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
MIT License

Copyright (c) 2018 Elan Ruusamäe
Copyright (c) 2018 Eventum Issue tracking system

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Slack unfurl Eventum Provider

Eventum issue links unfurler for [slack-unfurl].

[slack-unfurl]: https://github.com/glensc/slack-unfurl
21 changes: 21 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "eventum/eventum-slack-unfurl",
"description": "Eventum issue links unfurler for slack-unfurl",
"license": "BSD",
"type": "project",
"config": {
"sort-packages": true
},
"autoload": {
"psr-4": {
"Eventum\\SlackUnfurl\\": "src/"
}
},
"require": {
"php": "^7.1.3",
"eventum/rpc": "^4.1"
},
"require-dev": {
"eventum/slack-unfurl": "~0.3.0"
}
}
13 changes: 13 additions & 0 deletions env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Env variables setup
# copy env.example to .env to configure

# Timezone in what dates should be converted to
TIMEZONE=Europe/Tallinn

# Hostname where Eventum is installed
EVENTUM_DOMAIN=eventum.example.net

# Access to Eventum RPC
EVENTUM_RPC_URL=https://eventum.example.net/rpc/xmlrpc.php
[email protected]
EVENTUM_ACCESS_TOKEN=xxx
177 changes: 177 additions & 0 deletions src/Event/Subscriber/EventumUnfurler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
<?php

namespace Eventum\SlackUnfurl\Event\Subscriber;

use DateTime;
use DateTimeZone;
use Eventum_RPC;
use Eventum_RPC_Exception;
use InvalidArgumentException;
use Psr\Log\LoggerInterface;
use SlackUnfurl\Event\Events;
use SlackUnfurl\Event\UnfurlEvent;
use SlackUnfurl\LoggerTrait;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class EventumUnfurler implements EventSubscriberInterface
{
use LoggerTrait;

/** @var Eventum_RPC */
private $apiClient;
/** @var DateTimeZone */
private $utc;
/** @var string */
private $domain;
/** @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(
Eventum_RPC $apiClient,
string $domain,
string $timeZone,
LoggerInterface $logger
) {
$this->logger = $logger;
$this->domain = $domain;
$this->apiClient = $apiClient;
$this->utc = new DateTimeZone('UTC');
$this->timeZone = new DateTimeZone($timeZone);

if (!$this->domain) {
throw new InvalidArgumentException('Domain not set');
}
}

/**
* {@inheritdoc}
*/
public static function getSubscribedEvents()
{
return [
Events::SLACK_UNFURL => ['unfurl', 10],
];
}

public function unfurl(UnfurlEvent $event)
{
foreach ($this->getMatchingLinks($event) as $link) {
$issueId = $this->getIssueId($link);
if (!$issueId) {
$this->error('Could not extract issueId', ['link' => $link]);
continue;
}

$url = $link['url'];
$unfurl = $this->getIssueUnfurl($issueId, $url);
$event->addUnfurl($url, $unfurl);
}
}

public function getIssueUnfurl(int $issueId, string $url)
{
$issue = $this->getIssueDetails($issueId);
$this->debug('issue', ['issue' => $issue]);

return [
'title' => "{$issue['prc_title']} <$url|Issue #{$issueId}> : {$issue['iss_summary']}",
'color' => '#006486',
'footer' => "Created by {$issue['reporter']}",
'fields' => [
[
'title' => 'Priority',
'value' => $issue['pri_title'],
'short' => true,
],
[
'title' => 'Assignment',
'value' => $issue['assignments'],
'short' => true,
],
[
'title' => 'Status',
'value' => $issue['sta_title'],
'short' => true,
],
[
'title' => 'Last update',
'value' => $this->getLastUpdate($issue)->format('Y-m-d H:i:s'),
'short' => true,
],
],
];
}

/**
* Get issue details, but filter only needed keys.
*
* @param int $issueId
* @return array
* @throws Eventum_RPC_Exception
*/
private function getIssueDetails(int $issueId)
{
$issue = $this->apiClient->getIssueDetails($issueId);

return array_intersect_key($issue, array_flip(self::MATCH_KEYS));
}

/**
* Get last update whether internal or public last action date
*
* @param array $issue
* @return DateTime last action date in specified timeZone
*/
private function getLastUpdate(array $issue)
{
$ts1 = new DateTime($issue['iss_last_internal_action_date'], $this->utc);
$ts2 = new DateTime($issue['iss_last_public_action_date'], $this->utc);

$lastUpdated = $ts1 > $ts2 ? $ts1 : $ts2;
$lastUpdated->setTimezone($this->timeZone);

return $lastUpdated;
}

private function getMatchingLinks(UnfurlEvent $event)
{
foreach ($event->getLinks() as $link) {
$domain = $link['domain'] ?? null;
if ($domain !== $this->domain) {
continue;
}

yield $link;
}
}

private function getIssueId($link): ?int
{
if (!preg_match('#view.php\?id=(?P<id>\d+)#', $link['url'], $m)) {
return null;
}

return (int)$m['id'];
}
}
46 changes: 46 additions & 0 deletions src/ServiceProvider/EventumUnfurlServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace Eventum\SlackUnfurl\ServiceProvider;

use Eventum\SlackUnfurl\Event\Subscriber\EventumUnfurler;
use Eventum_RPC;
use Pimple\Container;
use Pimple\ServiceProviderInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

class EventumUnfurlServiceProvider implements ServiceProviderInterface
{
/**
* {@inheritdoc}
*/
public function register(Container $app)
{
$app['eventum.rpc_url'] = getenv('EVENTUM_RPC_URL');
$app['eventum.username'] = getenv('EVENTUM_USERNAME');
$app['eventum.access_token'] = getenv('EVENTUM_ACCESS_TOKEN');
$app['eventum.domain'] = getenv('EVENTUM_DOMAIN');
$app['eventum.timezone'] = getenv('TIMEZONE');

$app[Eventum_RPC::class] = function ($app) {
$client = new Eventum_RPC($app['eventum.rpc_url']);
$client->setCredentials($app['eventum.username'], $app['eventum.access_token']);

return $client;
};

$app[EventumUnfurler::class] = function ($app) {
return new EventumUnfurler(
$app[Eventum_RPC::class],
$app['eventum.domain'],
$app['eventum.timezone'],
$app['logger']
);
};

$app->extend('unfurl.dispatcher', function (EventDispatcherInterface $dispatcher, $app) {
$dispatcher->addSubscriber($app[EventumUnfurler::class]);

return $dispatcher;
});
}
}

0 comments on commit 19ac7d7

Please sign in to comment.