|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
|
4
|
|
|
|
|
5
|
|
|
namespace AppBuilder\Application\Module\BitBucket; |
|
6
|
|
|
|
|
7
|
|
|
use AppBuilder\Application\Module\BitBucket\Exception\NullResultReturned; |
|
8
|
|
|
use AppBuilder\Application\Module\HttpClient\ExternalLibraryHttpClient; |
|
9
|
|
|
use AppBuilder\Application\Utils\Mapper\Factory\BitbucketMapperFactory; |
|
10
|
|
|
use AppBuilder\Event\Application\BitbucketTicketMappedEvent; |
|
11
|
|
|
use AppBuilder\Event\Application\JiraTicketMappedEvent; |
|
12
|
|
|
use AppBuilder\Event\Application\JiraTicketMappedEventAware; |
|
13
|
|
|
use GuzzleHttp\Psr7\Response; |
|
14
|
|
|
use Psr\Log\LoggerInterface; |
|
15
|
|
|
use RuntimeException; |
|
16
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
|
17
|
|
|
|
|
18
|
|
|
class ExternalLibraryBitBucketService implements JiraTicketMappedEventAware, BitBucketService |
|
19
|
|
|
{ |
|
20
|
|
|
/** @var string */ |
|
21
|
|
|
private const URL_PREFIX = 'https://farpoint.get-ag.com/jira/rest/dev-status/1.0/issue/detail?issueId='; |
|
22
|
|
|
|
|
23
|
|
|
/** @var string */ |
|
24
|
|
|
private const URL_SUFFIX = '&applicationType=stash&dataType=pullrequest'; |
|
25
|
|
|
|
|
26
|
|
|
/** @var string */ |
|
27
|
|
|
private $url; |
|
28
|
|
|
|
|
29
|
|
|
/** @var LoggerInterface */ |
|
30
|
|
|
private $logger; |
|
31
|
|
|
|
|
32
|
|
|
/** @var ExternalLibraryHttpClient */ |
|
33
|
|
|
private $httpClient; |
|
34
|
|
|
|
|
35
|
|
|
/** @var EventDispatcherInterface */ |
|
36
|
|
|
private $dispatcher; |
|
37
|
|
|
|
|
38
|
1 |
|
public function __construct( |
|
39
|
|
|
ExternalLibraryHttpClient $httpClient, |
|
40
|
|
|
LoggerInterface $logger, |
|
41
|
|
|
EventDispatcherInterface $dispatcher |
|
42
|
|
|
) { |
|
43
|
1 |
|
$this->logger = $logger; |
|
44
|
1 |
|
$this->httpClient = $httpClient; |
|
45
|
1 |
|
$this->dispatcher = $dispatcher; |
|
46
|
1 |
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** Recives event with mapped Jira ticket. */ |
|
49
|
1 |
|
public function onJiraTicketMapped(JiraTicketMappedEvent $event) : void |
|
50
|
|
|
{ |
|
51
|
1 |
|
$ticketId = $event->ticket()['id']; |
|
52
|
1 |
|
$this->createUrl($ticketId); |
|
53
|
|
|
try { |
|
54
|
1 |
|
$this->fetchBitBucketData($ticketId); |
|
55
|
|
|
} catch (RuntimeException $exception) { |
|
56
|
|
|
$this->logger->warning('Error: ' . $exception->getMessage(), [$exception]); |
|
57
|
|
|
} |
|
58
|
1 |
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Fetches data from BitBucket by tickets id. |
|
62
|
|
|
* |
|
63
|
|
|
* @throws RuntimeException |
|
64
|
|
|
* @throws NullResultReturned |
|
65
|
|
|
*/ |
|
66
|
1 |
|
public function fetchBitBucketData(int $ticketId) : void |
|
67
|
|
|
{ |
|
68
|
1 |
|
$bbFullTicket = []; |
|
69
|
|
|
$response = $this |
|
70
|
1 |
|
->httpClient |
|
71
|
1 |
|
->request(ExternalLibraryHttpClient::GET, $this->url); |
|
72
|
1 |
|
$bbFullTicket[$ticketId] = $this->getResponseContent($response); |
|
73
|
|
|
|
|
74
|
1 |
|
if (null === $bbFullTicket[$ticketId]) { |
|
75
|
|
|
throw new NullResultReturned('Error. Bitbucket fetching method returned null'); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
1 |
|
$this->mapToBitbucketTicket($bbFullTicket, $ticketId); |
|
79
|
1 |
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Gets content from response and decodes it into array. |
|
83
|
|
|
* |
|
84
|
|
|
* @throws RuntimeException |
|
85
|
|
|
*/ |
|
86
|
1 |
|
private function getResponseContent(Response $response) : ?array |
|
87
|
|
|
{ |
|
88
|
1 |
|
$content = $response->getBody()->getContents(); |
|
89
|
|
|
|
|
90
|
1 |
|
return json_decode($content, true); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Maps and filters received BitBucket data to array. |
|
95
|
|
|
*/ |
|
96
|
1 |
|
private function mapToBitbucketTicket(array $bbTicket, int $ticketId) : void |
|
97
|
|
|
{ |
|
98
|
1 |
|
$mappedTicket = []; |
|
99
|
1 |
|
$ticketMappers = BitbucketMapperFactory::create(); |
|
100
|
1 |
|
foreach ($ticketMappers as $mapper) { |
|
101
|
1 |
|
$mappedTicket[$ticketId][$mapper->outputKey()] = $mapper->map($bbTicket[$ticketId]); |
|
102
|
|
|
} |
|
103
|
1 |
|
$this->dispatcher->dispatch( |
|
104
|
1 |
|
BitbucketTicketMappedEvent::NAME, |
|
105
|
1 |
|
new BitbucketTicketMappedEvent($mappedTicket) |
|
106
|
|
|
); |
|
107
|
1 |
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Combines url with given ticket id. |
|
111
|
|
|
*/ |
|
112
|
1 |
|
private function createUrl(int $ticketId) : void |
|
113
|
|
|
{ |
|
114
|
1 |
|
$this->url = self::URL_PREFIX . $ticketId . self::URL_SUFFIX; |
|
115
|
1 |
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|