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