-
Notifications
You must be signed in to change notification settings - Fork 3
/
GitlabUnfurler.php
79 lines (66 loc) · 2.08 KB
/
GitlabUnfurler.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php
namespace GitlabSlackUnfurl\Event\Subscriber;
use GitlabSlackUnfurl\Route;
use GitlabSlackUnfurl\Route\AbstractRouteHandler;
use Psr\Log\LoggerInterface;
use SlackUnfurl\CommandResolver;
use SlackUnfurl\Event\Events;
use SlackUnfurl\Event\UnfurlEvent;
use SlackUnfurl\Route\RouteMatcher;
use SlackUnfurl\RuntimeException;
use SlackUnfurl\Traits\LoggerTrait;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class GitlabUnfurler implements EventSubscriberInterface
{
use LoggerTrait;
private const ROUTES = [
'issue' => Route\Issue::class,
'merge_request' => Route\MergeRequest::class,
'note' => Route\Note::class,
];
/** @var string */
private $domain;
/** @var RouteMatcher */
private $routeMatcher;
/** @var CommandResolver */
private $commandResolver;
public function __construct(
RouteMatcher $routeMatcher,
CommandResolver $commandResolver,
string $domain,
LoggerInterface $logger
) {
$this->domain = $domain;
$this->routeMatcher = $routeMatcher;
$this->commandResolver = $commandResolver;
$this->logger = $logger;
}
public static function getSubscribedEvents(): array
{
return [
Events::SLACK_UNFURL => ['unfurl', 10],
];
}
public function unfurl(UnfurlEvent $event): void
{
foreach ($event->getMatchingLinks($this->domain) as $link) {
try {
$unfurl = $this->unfurlByUrl($link['url']);
if ($unfurl) {
$event->addUnfurl($link['url'], $unfurl);
}
} catch (RuntimeException $e) {
$this->debug("gitlab: {$e->getMessage()}");
}
}
}
private function unfurlByUrl(string $url): ?array
{
[$router, $matches] = $this->routeMatcher->match($url);
/** @var AbstractRouteHandler $command */
$command = $this->commandResolver
->configure(self::ROUTES)
->resolve($router);
return $command->unfurl($url, $matches);
}
}