|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Artack\Monolog\JiraHandler; |
|
6
|
|
|
|
|
7
|
|
|
use Buzz\Browser; |
|
8
|
|
|
use Buzz\Client\Curl; |
|
9
|
|
|
use Buzz\Middleware\BasicAuthMiddleware; |
|
10
|
|
|
use Monolog\Handler\AbstractProcessingHandler; |
|
11
|
|
|
use Monolog\Logger; |
|
12
|
|
|
use Nyholm\Psr7\Factory\Psr17Factory; |
|
13
|
|
|
use Nyholm\Psr7\Request; |
|
14
|
|
|
|
|
15
|
|
|
class JiraHandler extends AbstractProcessingHandler |
|
16
|
|
|
{ |
|
17
|
|
|
private $hostname; |
|
18
|
|
|
private $username; |
|
19
|
|
|
private $password; |
|
20
|
|
|
private $jql; |
|
21
|
|
|
private $hashFieldName; |
|
22
|
|
|
private $projectKey; |
|
23
|
|
|
private $issueTypeName; |
|
24
|
|
|
|
|
25
|
|
|
public function __construct(string $hostname, string $username, string $password, string $jql, string $hashFieldName, string $projectKey, string $issueTypeName, $level = Logger::DEBUG, $bubble = true) |
|
26
|
|
|
{ |
|
27
|
|
|
parent::__construct($level, $bubble); |
|
28
|
|
|
|
|
29
|
|
|
$this->hostname = $hostname; |
|
30
|
|
|
$this->username = $username; |
|
31
|
|
|
$this->password = $password; |
|
32
|
|
|
$this->jql = $jql; |
|
33
|
|
|
$this->hashFieldName = $hashFieldName; |
|
34
|
|
|
$this->projectKey = $projectKey; |
|
35
|
|
|
$this->issueTypeName = $issueTypeName; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
protected function write(array $record): void |
|
39
|
|
|
{ |
|
40
|
|
|
$client = new Curl(); |
|
41
|
|
|
$browser = new Browser($client, new Psr17Factory()); |
|
42
|
|
|
$browser->addMiddleware(new BasicAuthMiddleware($this->username, $this->password)); |
|
43
|
|
|
|
|
44
|
|
|
$recordForHash = $record; |
|
45
|
|
|
unset($recordForHash['datetime'], $recordForHash['formatted']); |
|
46
|
|
|
$hash = md5(serialize($recordForHash)); |
|
47
|
|
|
|
|
48
|
|
|
$url = sprintf('https://%s/rest/api/2/search', $this->hostname); |
|
49
|
|
|
|
|
50
|
|
|
$jql = sprintf('%s AND %s ~ \'%s\'', $this->jql, $this->hashFieldName, $hash); |
|
51
|
|
|
$str = json_encode([ |
|
52
|
|
|
'jql' => $jql, |
|
53
|
|
|
'fields' => [ |
|
54
|
|
|
'issuetype', |
|
55
|
|
|
'status', |
|
56
|
|
|
'summary', |
|
57
|
|
|
$this->hashFieldName, |
|
58
|
|
|
], |
|
59
|
|
|
]); |
|
60
|
|
|
$request = new Request('POST', $url, ['Content-Type' => 'application/json'], $str); |
|
61
|
|
|
$response = $browser->sendRequest($request); |
|
62
|
|
|
$contents = $response->getBody()->getContents(); |
|
63
|
|
|
$data = json_decode($contents, true); |
|
64
|
|
|
|
|
65
|
|
|
if ($data['total'] > 0) { |
|
66
|
|
|
// issue already exists > do nothing |
|
67
|
|
|
echo "GOT IT ALREADY\n"; |
|
68
|
|
|
|
|
69
|
|
|
return; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
$createMetaUrl = sprintf('https://%s/rest/api/2/issue/createmeta', $this->hostname).'?'.http_build_query([ |
|
73
|
|
|
'projectKeys' => $this->projectKey, |
|
74
|
|
|
'expand' => 'projects.issuetypes.fields', |
|
75
|
|
|
]); |
|
76
|
|
|
$request = new Request('GET', $createMetaUrl); |
|
77
|
|
|
$response = $browser->sendRequest($request); |
|
78
|
|
|
$contents = $response->getBody()->getContents(); |
|
79
|
|
|
$data = json_decode($contents, true); |
|
80
|
|
|
|
|
81
|
|
|
$projectId = $data['projects'][0]['id']; |
|
|
|
|
|
|
82
|
|
|
|
|
83
|
|
|
$issueTypeName = $this->issueTypeName; |
|
84
|
|
|
$issueType = array_values(array_filter($data['projects'][0]['issuetypes'], function ($data) use ($issueTypeName) { |
|
85
|
|
|
return $data['name'] === $issueTypeName; |
|
86
|
|
|
}))[0]; |
|
87
|
|
|
$issueTypeId = $issueType['id']; |
|
88
|
|
|
|
|
89
|
|
|
$hashFieldName = $this->hashFieldName; |
|
90
|
|
|
$hashFieldId = array_keys(array_filter($issueType['fields'], function ($data) use ($hashFieldName) { |
|
91
|
|
|
return $data['name'] === $hashFieldName; |
|
92
|
|
|
}))[0]; |
|
93
|
|
|
|
|
94
|
|
|
$createIssueUrl = sprintf('https://%s/rest/api/2/issue', $this->hostname); |
|
95
|
|
|
$createIssueData = [ |
|
96
|
|
|
'fields' => [ |
|
97
|
|
|
'project' => [ |
|
98
|
|
|
'key' => 'MX', |
|
99
|
|
|
], |
|
100
|
|
|
'issuetype' => ['id' => $issueTypeId], |
|
101
|
|
|
'summary' => $record['message'], |
|
102
|
|
|
'description' => $record['formatted'], |
|
103
|
|
|
$hashFieldId => $hash, |
|
104
|
|
|
], |
|
105
|
|
|
]; |
|
106
|
|
|
$request = new Request('POST', $createIssueUrl, ['Content-Type' => 'application/json'], json_encode($createIssueData)); |
|
107
|
|
|
$response = $browser->sendRequest($request); |
|
|
|
|
|
|
108
|
|
|
// $contents = $response->getBody()->getContents(); |
|
109
|
|
|
// $data = json_decode($contents, true); |
|
110
|
|
|
// dump($data); |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|