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 |
|
$entityBody = file_get_contents($input); |
33
|
11 |
|
$json = json_decode($entityBody); |
34
|
|
|
|
35
|
11 |
|
if ($this->configuration['gerrit']['webhookToken'] !== $json->token) { |
36
|
2 |
|
return; |
37
|
|
|
} |
38
|
9 |
|
if ($json->project !== 'Packages/TYPO3.CMS') { |
39
|
|
|
// only core patches please... |
40
|
1 |
|
return; |
41
|
|
|
} |
42
|
8 |
|
$patchId = (int) str_replace('https://review.typo3.org/', '', $json->{'change-url'}); |
43
|
8 |
|
$patchSet = property_exists($json, 'patchset') ? (int) $json->patchset : 0; |
44
|
8 |
|
$commit = $json->commit; |
45
|
8 |
|
$branch = $json->branch; |
46
|
|
|
|
47
|
8 |
|
$item = $this->queryGerrit('change:' . $patchId); |
48
|
8 |
|
$item = $item[0]; |
49
|
8 |
|
$created = substr($item->created, 0, 19); |
50
|
8 |
|
$text = "Branch: {$branch} | :calendar: {$created} | ID: {$item->_number}\n"; |
51
|
8 |
|
$text .= ":link: <https://review.typo3.org/{$item->_number}|Goto Review>"; |
52
|
8 |
|
if ($hook === 'patchset-created' && $patchSet === 1 && $branch === 'master') { |
53
|
2 |
|
$message = $this->buildMessage('[NEW] ' . $item->subject, $text); |
54
|
2 |
|
$this->sendMessageToChannel($hook, $message); |
55
|
6 |
|
} elseif ($hook === 'change-merged') { |
56
|
5 |
|
$message = $this->buildMessage(':white_check_mark: [MERGED] ' . $item->subject, $text, Message\Attachment::COLOR_GOOD); |
57
|
5 |
|
$this->sendMessageToChannel($hook, $message); |
58
|
5 |
|
$this->checkFiles($patchId, $commit); |
59
|
|
|
} |
60
|
8 |
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param string $title |
64
|
|
|
* @param string $text |
65
|
|
|
* @param string $color |
66
|
|
|
* |
67
|
|
|
* @return Message |
68
|
|
|
*/ |
69
|
7 |
|
protected function buildMessage(string $title, string $text, string $color = Message\Attachment::COLOR_NOTICE) : Message |
70
|
|
|
{ |
71
|
7 |
|
$message = new Message(); |
72
|
7 |
|
$message->setText(' '); |
73
|
7 |
|
$attachment = new Message\Attachment(); |
74
|
|
|
|
75
|
7 |
|
$attachment->setColor($color); |
76
|
7 |
|
$attachment->setTitle($title); |
77
|
|
|
|
78
|
7 |
|
$attachment->setText($text); |
79
|
7 |
|
$attachment->setFallback($text); |
80
|
7 |
|
$message->addAttachment($attachment); |
81
|
7 |
|
return $message; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param int $patchId |
86
|
|
|
* @param int $commit |
87
|
|
|
* |
88
|
|
|
* @throws \Doctrine\DBAL\DBALException |
89
|
|
|
*/ |
90
|
5 |
|
protected function checkFiles($patchId, $commit) |
91
|
|
|
{ |
92
|
5 |
|
$files = $this->getFilesForPatch($patchId, $commit); |
93
|
5 |
|
$rstFiles = []; |
94
|
5 |
|
if (is_array($files)) { |
95
|
|
|
foreach ($files as $fileName => $changeInfo) { |
96
|
|
|
if ($this->endsWith(strtolower($fileName), '.rst')) { |
97
|
|
|
$rstFiles[$fileName] = $changeInfo; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
} |
101
|
5 |
|
if (!empty($rstFiles)) { |
102
|
|
|
$message = new Message(); |
103
|
|
|
$message->setText(' '); |
104
|
|
|
foreach ($rstFiles as $fileName => $changeInfo) { |
105
|
|
|
$attachment = new Message\Attachment(); |
106
|
|
|
$status = $changeInfo['status'] ?? 'default'; |
107
|
|
|
$color = [ |
108
|
|
|
'A' => Message\Attachment::COLOR_GOOD, |
109
|
|
|
'D' => Message\Attachment::COLOR_WARNING, |
110
|
|
|
'default' => Message\Attachment::COLOR_WARNING, |
111
|
|
|
]; |
112
|
|
|
$text = [ |
113
|
|
|
'A' => 'A new documentation file has been added', |
114
|
|
|
'D' => 'A documentation file has been removed', |
115
|
|
|
'default' => 'A documentation file has been updated', |
116
|
|
|
]; |
117
|
|
|
$attachment->setColor($color[$status]); |
118
|
|
|
$attachment->setTitle($text[$status]); |
119
|
|
|
|
120
|
|
|
$text = ':link: <https://git.typo3.org/Packages/TYPO3.CMS.git/blob/HEAD:/' . $fileName . '|' . $fileName . '>'; |
121
|
|
|
$attachment->setText($text); |
122
|
|
|
$attachment->setFallback($text); |
123
|
|
|
$message->addAttachment($attachment); |
124
|
|
|
} |
125
|
|
|
$this->sendMessageToChannel('rst-merged', $message); |
126
|
|
|
} |
127
|
5 |
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @param string $hook |
131
|
|
|
* @param Message $message |
132
|
|
|
* |
133
|
|
|
* @throws \Doctrine\DBAL\DBALException |
134
|
|
|
*/ |
135
|
7 |
|
protected function sendMessageToChannel(string $hook, Message $message) |
136
|
|
|
{ |
137
|
7 |
|
if (is_array($this->configuration['gerrit'][$hook]['channels'])) { |
138
|
7 |
|
foreach ($this->configuration['gerrit'][$hook]['channels'] as $channel) { |
139
|
4 |
|
$this->postToSlack($message, $channel); |
140
|
|
|
} |
141
|
|
|
} |
142
|
7 |
|
} |
143
|
|
|
} |
144
|
|
|
|