1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* T3Bot. |
4
|
|
|
* |
5
|
|
|
* @author Frank Nägler <[email protected]> |
6
|
|
|
* |
7
|
|
|
* @link http://www.t3bot.de |
8
|
|
|
* @link http://wiki.typo3.org/T3Bot |
9
|
|
|
*/ |
10
|
|
|
namespace T3Bot\Controller; |
11
|
|
|
|
12
|
|
|
use T3Bot\Slack\Message; |
13
|
|
|
use T3Bot\Traits\GerritTrait; |
14
|
|
|
use T3Bot\Traits\LoggerTrait; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class GerritHookController. |
18
|
|
|
*/ |
19
|
|
|
class GerritHookController extends AbstractHookController |
20
|
|
|
{ |
21
|
|
|
use GerritTrait; |
22
|
|
|
use LoggerTrait; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* public method to start processing the request. |
26
|
|
|
* |
27
|
|
|
* @param string $hook |
28
|
|
|
* @param string $input |
29
|
|
|
* |
30
|
|
|
* @throws \Doctrine\DBAL\DBALException |
31
|
|
|
* @throws \Exception |
32
|
|
|
*/ |
33
|
11 |
|
public function process($hook, $input = 'php://input') |
34
|
|
|
{ |
35
|
11 |
|
$this->getLogger()->debug(file_get_contents($input)); |
36
|
11 |
|
$json = json_decode(file_get_contents($input)); |
37
|
|
|
|
38
|
|
|
if ( |
39
|
11 |
|
$json->project !== 'Packages/TYPO3.CMS' || |
40
|
11 |
|
$json->token !== $this->configuration['gerrit']['webhookToken'] |
41
|
|
|
) { |
42
|
3 |
|
return; |
43
|
|
|
} |
44
|
8 |
|
$this->processHook($hook, $json); |
45
|
8 |
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param string $hook |
49
|
|
|
* @param \stdClass $json |
50
|
|
|
* |
51
|
|
|
* @throws \Doctrine\DBAL\DBALException |
52
|
|
|
*/ |
53
|
8 |
|
protected function processHook(string $hook, \stdClass $json) |
54
|
|
|
{ |
55
|
8 |
|
$patchId = (int) $this->resolvePatchIdFromUrl($json->{'change-url'}); |
56
|
8 |
|
$patchSet = property_exists($json, 'patchset') ? (int) $json->patchset : 0; |
57
|
|
|
|
58
|
8 |
|
$item = $this->queryGerrit('change:' . $patchId)[0]; |
59
|
8 |
|
$created = substr($item->created, 0, 19); |
60
|
8 |
|
$text = "Branch: {$json->branch} | :calendar: {$created} | ID: {$item->_number}" . chr(10); |
61
|
8 |
|
$text .= ":link: <https://review.typo3.org/{$item->_number}|Goto Review>"; |
62
|
8 |
|
if ($hook === 'patchset-created' && $patchSet === 1 && $json->branch === 'master') { |
63
|
2 |
|
$message = $this->buildMessage('[NEW] ' . $item->subject, $text); |
64
|
2 |
|
$this->sendMessageToChannel($hook, $message); |
65
|
6 |
|
} elseif ($hook === 'change-merged') { |
66
|
5 |
|
$message = $this->buildMessage(':white_check_mark: [MERGED] ' . $item->subject, $text, Message\Attachment::COLOR_GOOD); |
67
|
5 |
|
$this->sendMessageToChannel($hook, $message); |
68
|
5 |
|
$this->checkFiles($patchId, $json->commit); |
69
|
|
|
} |
70
|
8 |
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param string $title |
74
|
|
|
* @param string $text |
75
|
|
|
* @param string $color |
76
|
|
|
* |
77
|
|
|
* @return Message |
78
|
|
|
*/ |
79
|
7 |
|
protected function buildMessage(string $title, string $text, string $color = Message\Attachment::COLOR_NOTICE) : Message |
80
|
|
|
{ |
81
|
7 |
|
$message = new Message(); |
82
|
7 |
|
$message->setText(' '); |
83
|
7 |
|
$attachment = new Message\Attachment(); |
84
|
|
|
|
85
|
7 |
|
$attachment->setColor($color); |
86
|
7 |
|
$attachment->setTitle($title); |
87
|
|
|
|
88
|
7 |
|
$attachment->setText($text); |
89
|
7 |
|
$attachment->setFallback($text); |
90
|
7 |
|
$message->addAttachment($attachment); |
91
|
7 |
|
return $message; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param int $patchId |
96
|
|
|
* @param int $commit |
97
|
|
|
* |
98
|
|
|
* @throws \Doctrine\DBAL\DBALException |
99
|
|
|
*/ |
100
|
5 |
|
protected function checkFiles($patchId, $commit) |
101
|
|
|
{ |
102
|
5 |
|
$files = $this->getFilesForPatch($patchId, $commit); |
103
|
5 |
|
$rstFiles = []; |
104
|
5 |
|
if (!empty($files)) { |
105
|
5 |
|
foreach ($files as $fileName => $changeInfo) { |
106
|
5 |
|
if ($this->endsWith(strtolower($fileName), '.rst')) { |
107
|
5 |
|
$rstFiles[$fileName] = $changeInfo; |
108
|
|
|
} |
109
|
|
|
} |
110
|
5 |
|
if (!empty($rstFiles)) { |
111
|
3 |
|
$message = new Message(); |
112
|
3 |
|
$message->setText(' '); |
113
|
3 |
|
foreach ($rstFiles as $fileName => $changeInfo) { |
114
|
3 |
|
$message->addAttachment($this->buildFileAttachment($fileName, $changeInfo)); |
115
|
|
|
} |
116
|
3 |
|
$this->sendMessageToChannel('rst-merged', $message); |
117
|
|
|
} |
118
|
|
|
} |
119
|
5 |
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param string $fileName |
123
|
|
|
* @param \stdClass $changeInfo |
124
|
|
|
* |
125
|
|
|
* @return Message\Attachment |
126
|
|
|
*/ |
127
|
3 |
|
protected function buildFileAttachment(string $fileName, \stdClass $changeInfo) : Message\Attachment |
128
|
|
|
{ |
129
|
3 |
|
$attachment = new Message\Attachment(); |
130
|
3 |
|
$status = $changeInfo->status ?? 'default'; |
131
|
|
|
$color = [ |
132
|
3 |
|
'A' => Message\Attachment::COLOR_GOOD, |
133
|
|
|
'D' => Message\Attachment::COLOR_WARNING, |
134
|
|
|
'default' => Message\Attachment::COLOR_WARNING, |
135
|
|
|
]; |
136
|
|
|
$text = [ |
137
|
3 |
|
'A' => 'A new documentation file has been added', |
138
|
|
|
'D' => 'A documentation file has been removed', |
139
|
|
|
'default' => 'A documentation file has been updated', |
140
|
|
|
]; |
141
|
3 |
|
$attachment->setColor($color[$status]); |
142
|
3 |
|
$attachment->setTitle($text[$status]); |
143
|
|
|
|
144
|
3 |
|
$text = ':link: <https://git.typo3.org/Packages/TYPO3.CMS.git/blob/HEAD:/' . $fileName . '|' . $fileName . '>'; |
145
|
3 |
|
$attachment->setText($text); |
146
|
3 |
|
$attachment->setFallback($text); |
147
|
3 |
|
return $attachment; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @param string $hook |
152
|
|
|
* @param Message $message |
153
|
|
|
* |
154
|
|
|
* @throws \Doctrine\DBAL\DBALException |
155
|
|
|
*/ |
156
|
7 |
|
protected function sendMessageToChannel(string $hook, Message $message) |
157
|
|
|
{ |
158
|
7 |
|
if (is_array($this->configuration['gerrit'][$hook]['channels'])) { |
159
|
7 |
|
foreach ($this->configuration['gerrit'][$hook]['channels'] as $channel) { |
160
|
7 |
|
$this->postToSlack($message, $channel); |
161
|
|
|
} |
162
|
|
|
} |
163
|
7 |
|
} |
164
|
|
|
} |
165
|
|
|
|