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\Commands; |
11
|
|
|
|
12
|
|
|
use Slack\Payload; |
13
|
|
|
use Slack\RealTimeClient; |
14
|
|
|
use T3Bot\Slack\Message; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class ReviewCommand. |
18
|
|
|
* |
19
|
|
|
* @property string commandName |
20
|
|
|
* @property array helpCommands |
21
|
|
|
*/ |
22
|
|
|
class ReviewCommand extends AbstractCommand |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* AbstractCommand constructor. |
26
|
|
|
* |
27
|
|
|
* @param Payload $payload |
28
|
|
|
* @param RealTimeClient $client |
29
|
|
|
* @param array|null $configuration |
30
|
|
|
*/ |
31
|
27 |
|
public function __construct(Payload $payload, RealTimeClient $client, array $configuration = null) |
32
|
|
|
{ |
33
|
27 |
|
$this->commandName = 'review'; |
34
|
27 |
|
$this->helpCommands = [ |
35
|
|
|
'help' => 'shows this help', |
36
|
|
|
'count [PROJECT=Packages/TYPO3.CMS]' => 'shows the number of currently open reviews for [PROJECT]', |
37
|
|
|
'random' => 'shows a random open review', |
38
|
|
|
'show [Ref-ID] [[Ref-ID-2] [[Ref-ID-n]]]' => 'shows the review by given change number(s). Do not use separators other than space.', |
39
|
|
|
'user [username] [PROJECT=Packages/TYPO3.CMS]' => 'shows the open reviews by given username for [PROJECT]', |
40
|
|
|
'query [searchQuery]' => 'shows the results for given [searchQuery], max limit is 50', |
41
|
|
|
'merged [YYYY-MM-DD]' => 'shows a count of merged patches on master since given date', |
42
|
|
|
]; |
43
|
27 |
|
parent::__construct($payload, $client, $configuration); |
44
|
27 |
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* process count. |
48
|
|
|
* |
49
|
|
|
* @return string |
50
|
|
|
*/ |
51
|
1 |
|
protected function processCount() : string |
52
|
|
|
{ |
53
|
1 |
|
$project = !empty($this->params[1]) ? $this->params[1] : 'Packages/TYPO3.CMS'; |
54
|
1 |
|
$result = $this->queryGerrit("is:open branch:master -message:WIP project:{$project}"); |
55
|
1 |
|
$count = count($result); |
56
|
1 |
|
$result = $this->queryGerrit("label:Code-Review=-1 is:open branch:master -message:WIP project:{$project}"); |
57
|
1 |
|
$countMinus1 = count($result); |
58
|
1 |
|
$result = $this->queryGerrit("label:Code-Review=-2 is:open branch:master -message:WIP project:{$project}"); |
59
|
1 |
|
$countMinus2 = count($result); |
60
|
|
|
|
61
|
1 |
|
$returnString = ''; |
62
|
1 |
|
$returnString .= 'There are currently ' . $this->bold($count) . ' open reviews for project ' |
63
|
1 |
|
. $this->italic($project) . ' and branch master on <https://review.typo3.org/#/q/project:' . $project |
64
|
1 |
|
. '+status:open+branch:master|https://review.typo3.org>' . chr(10); |
65
|
1 |
|
$returnString .= $this->bold($countMinus1) . ' of ' . $this->bold($count) . ' open reviews voted with ' |
66
|
1 |
|
. $this->bold('-1') . ' <https://review.typo3.org/#/q/label:Code-Review%253D-1+is:open+branch:' |
67
|
1 |
|
. 'master+project:' . $project . '|Check now> ' . chr(10); |
68
|
1 |
|
$returnString .= $this->bold($countMinus2) . ' of ' . $this->bold($count) . ' open reviews voted with ' |
69
|
1 |
|
. $this->bold('-2') . ' <https://review.typo3.org/#/q/label:Code-Review%253D-2+is:open+branch:' |
70
|
1 |
|
. 'master+project:' . $project . '|Check now>'; |
71
|
|
|
|
72
|
1 |
|
return $returnString; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* process random. |
77
|
|
|
* |
78
|
|
|
* @return Message |
79
|
|
|
*/ |
80
|
1 |
|
protected function processRandom() : Message |
81
|
|
|
{ |
82
|
|
|
/** @var array $result */ |
83
|
1 |
|
$result = $this->queryGerrit('is:open project:Packages/TYPO3.CMS'); |
84
|
1 |
|
$item = $result[array_rand($result)]; |
85
|
|
|
|
86
|
1 |
|
return $this->buildReviewMessage($item); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* process user. |
91
|
|
|
* |
92
|
|
|
* @return string |
93
|
|
|
*/ |
94
|
3 |
|
protected function processUser() : string |
95
|
|
|
{ |
96
|
3 |
|
$username = !empty($this->params[1]) ? $this->params[1] : null; |
97
|
3 |
|
$project = !empty($this->params[2]) ? $this->params[2] : 'Packages/TYPO3.CMS'; |
98
|
3 |
|
if ($username === null) { |
99
|
1 |
|
return 'hey, I need a username!'; |
100
|
|
|
} |
101
|
2 |
|
$results = $this->queryGerrit('is:open owner:"' . $username . '" project:' . $project); |
102
|
2 |
|
if (count($results) > 0) { |
103
|
1 |
|
$listOfItems = ['*Here are the results for ' . $username . '*:']; |
104
|
1 |
|
if (is_array($results)) { |
105
|
1 |
|
foreach ($results as $item) { |
106
|
1 |
|
$listOfItems[] = $this->buildReviewLine($item); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
1 |
|
return implode(chr(10), $listOfItems); |
111
|
|
|
} |
112
|
1 |
|
return $username . ' has no open reviews or username is unknown'; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* process count. |
117
|
|
|
* |
118
|
|
|
* @return string|Message |
119
|
|
|
*/ |
120
|
14 |
|
protected function processShow() |
121
|
|
|
{ |
122
|
14 |
|
$urlPattern = '/http[s]*:\/\/review.typo3.org\/[#\/c]*([\d]*)(?:.*)/i'; |
123
|
14 |
|
$refId = $this->params[1] ?? 0; |
124
|
14 |
|
if (preg_match_all($urlPattern, $refId, $matches)) { |
125
|
4 |
|
$refId = (int) $matches[1][0]; |
126
|
|
|
} else { |
127
|
10 |
|
$refId = (int) $refId; |
128
|
|
|
} |
129
|
14 |
|
if ($refId === 0) { |
130
|
2 |
|
return 'hey, I need at least one change number!'; |
131
|
|
|
} |
132
|
12 |
|
$paramsCount = count($this->params); |
133
|
12 |
|
if ($paramsCount > 2) { |
134
|
1 |
|
$returnMessage = $this->buildReviewLineOutput(); |
135
|
|
|
} else { |
136
|
11 |
|
$returnMessage = $this->buildReviewMessageOutput($refId); |
137
|
|
|
} |
138
|
|
|
|
139
|
12 |
|
return $returnMessage; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @return string |
144
|
|
|
*/ |
145
|
1 |
|
protected function buildReviewLineOutput() : string |
146
|
|
|
{ |
147
|
1 |
|
$paramsCount = count($this->params); |
148
|
1 |
|
$changeIds = []; |
149
|
1 |
|
for ($i = 1; $i < $paramsCount; ++$i) { |
150
|
1 |
|
$changeIds[] = 'change:' . $this->params[$i]; |
151
|
|
|
} |
152
|
1 |
|
$result = $this->queryGerrit(implode(' OR ', $changeIds)); |
153
|
1 |
|
$listOfItems = []; |
154
|
1 |
|
if (is_array($result)) { |
155
|
1 |
|
foreach ($result as $item) { |
156
|
1 |
|
$listOfItems[] = $this->buildReviewLine($item); |
157
|
|
|
} |
158
|
|
|
} |
159
|
1 |
|
return implode(chr(10), $listOfItems); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @param int $refId |
164
|
|
|
* |
165
|
|
|
* @return string|Message |
166
|
|
|
*/ |
167
|
11 |
|
protected function buildReviewMessageOutput(int $refId) |
168
|
|
|
{ |
169
|
11 |
|
$result = $this->queryGerrit('change:' . $refId); |
170
|
11 |
|
if (!empty($result)) { |
171
|
10 |
|
foreach ($result as $item) { |
|
|
|
|
172
|
10 |
|
if ($item->_number === $refId) { |
173
|
10 |
|
return $this->buildReviewMessage($item); |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
} |
177
|
1 |
|
return "{$refId} not found, sorry!"; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* process query. |
182
|
|
|
* |
183
|
|
|
* @return string |
184
|
|
|
*/ |
185
|
3 |
|
protected function processQuery() : string |
186
|
|
|
{ |
187
|
3 |
|
array_shift($this->params); |
188
|
3 |
|
$query = trim(implode(' ', $this->params)); |
189
|
3 |
|
if ($query === '') { |
190
|
1 |
|
return 'hey, I need a query!'; |
191
|
|
|
} |
192
|
|
|
|
193
|
2 |
|
$results = $this->queryGerrit('limit:50 ' . $query); |
194
|
2 |
|
if (!empty($results)) { |
195
|
1 |
|
$listOfItems = ["*Here are the results for {$query}*:"]; |
196
|
1 |
|
foreach ($results as $item) { |
|
|
|
|
197
|
1 |
|
$listOfItems[] = $this->buildReviewLine($item); |
198
|
|
|
} |
199
|
1 |
|
return implode(chr(10), $listOfItems); |
200
|
|
|
} |
201
|
|
|
|
202
|
1 |
|
return "{$query} not found, sorry!"; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* @return string |
207
|
|
|
*/ |
208
|
3 |
|
protected function processMerged() : string |
209
|
|
|
{ |
210
|
3 |
|
$query = 'project:Packages/TYPO3.CMS status:merged after:###DATE### branch:master'; |
211
|
|
|
|
212
|
3 |
|
$date = !empty($this->params[1]) ? $this->params[1] : ''; |
213
|
3 |
|
if (!$this->isDateFormatCorrect($date)) { |
214
|
2 |
|
return 'hey, I need a date in the format YYYY-MM-DD!'; |
215
|
|
|
} |
216
|
1 |
|
$query = str_replace('###DATE###', $date, $query); |
217
|
1 |
|
$result = $this->queryGerrit($query); |
218
|
|
|
|
219
|
1 |
|
$cnt = count($result); |
220
|
|
|
|
221
|
1 |
|
return 'Good job folks, since ' . $date . ' you merged *' . $cnt . '* patches into master'; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* check format of given date. |
226
|
|
|
* |
227
|
|
|
* @param $date |
228
|
|
|
* |
229
|
|
|
* @return bool |
230
|
|
|
*/ |
231
|
3 |
|
protected function isDateFormatCorrect($date) : bool |
232
|
|
|
{ |
233
|
3 |
|
return preg_match('/[\d]{4}-[\d]{2}-[\d]{2}/', $date) === 1; |
234
|
|
|
} |
235
|
|
|
} |
236
|
|
|
|
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.