-
Notifications
You must be signed in to change notification settings - Fork 506
/
Copy pathEvaluateCommand.php
319 lines (274 loc) · 9.18 KB
/
EvaluateCommand.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
<?php declare(strict_types = 1);
namespace PHPStan\IssueBot\Console;
use Exception;
use Github\Api\Issue as GitHubIssueApi;
use Github\Client;
use PHPStan\IssueBot\Comment\BotComment;
use PHPStan\IssueBot\Comment\IssueCommentDownloader;
use PHPStan\IssueBot\Issue\IssueCache;
use PHPStan\IssueBot\Playground\PlaygroundCache;
use PHPStan\IssueBot\Playground\PlaygroundError;
use PHPStan\IssueBot\Playground\TabCreator;
use PHPStan\IssueBot\PostGenerator;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Finder\Finder;
use function array_key_exists;
use function array_keys;
use function array_map;
use function array_unique;
use function array_values;
use function count;
use function file_get_contents;
use function implode;
use function in_array;
use function is_file;
use function sprintf;
use function unserialize;
class EvaluateCommand extends Command
{
public function __construct(
private TabCreator $tabCreator,
private PostGenerator $postGenerator,
private Client $githubClient,
private IssueCommentDownloader $issueCommentDownloader,
private string $issueCachePath,
private string $playgroundCachePath,
private string $tmpDir,
private string $gitBranch,
private string $phpstanSrcCommitBefore,
private string $phpstanSrcCommitAfter,
)
{
parent::__construct();
}
protected function configure(): void
{
$this->setName('evaluate');
$this->addOption('post-comments', null, InputOption::VALUE_NONE);
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$issueCache = $this->loadIssueCache();
$originalResults = $this->loadPlaygroundCache()->getResults();
$newResults = $this->loadResults();
$toPost = [];
$totalCodeSnippets = 0;
foreach ($issueCache->getIssues() as $issue) {
$botComments = [];
$deduplicatedExamples = [];
foreach ($issue->getComments() as $comment) {
if ($comment instanceof BotComment) {
$botComments[] = $comment;
continue;
}
foreach ($comment->getPlaygroundExamples() as $example) {
if (isset($deduplicatedExamples[$example->getHash()])) {
$deduplicatedExamples[$example->getHash()]['users'][] = $comment->getAuthor();
$deduplicatedExamples[$example->getHash()]['users'] = array_values(array_unique($deduplicatedExamples[$example->getHash()]['users']));
continue;
}
$deduplicatedExamples[$example->getHash()] = [
'example' => $example,
'users' => [$comment->getAuthor()],
];
}
}
$totalCodeSnippets += count($deduplicatedExamples);
foreach ($deduplicatedExamples as ['example' => $example, 'users' => $users]) {
$hash = $example->getHash();
if (!array_key_exists($hash, $originalResults)) {
throw new Exception(sprintf('Hash %s does not exist in original results.', $hash));
}
$originalErrors = $originalResults[$hash]->getVersionedErrors();
$originalTabs = $this->tabCreator->create($originalErrors);
if (!array_key_exists($hash, $newResults)) {
throw new Exception(sprintf('Hash %s does not exist in new results.', $hash));
}
$originalPhpVersions = array_keys($originalErrors);
$newResult = $newResults[$hash];
if (array_key_exists(70100, $originalErrors) || $originalPhpVersions === [70400]) {
$newResult[70100] = $newResult[70300];
}
if (array_key_exists(70200, $originalErrors)) {
$newResult[70200] = $newResult[70300];
}
$newTabs = $this->tabCreator->create($this->filterErrors($originalErrors, $newResult));
$text = $this->postGenerator->createText($hash, $originalTabs, $newTabs, $botComments);
if ($text === null) {
continue;
}
if ($this->isIssueClosed($issue->getNumber())) {
continue;
}
$freshBotComments = $this->getFreshBotComments($issue->getNumber());
$textAgain = $this->postGenerator->createText($hash, $originalTabs, $newTabs, $freshBotComments);
if ($textAgain === null) {
continue;
}
$toPost[] = [
'issue' => $issue->getNumber(),
'hash' => $hash,
'users' => $users,
'diff' => $text['diff'],
'details' => $text['details'],
];
}
}
if (count($toPost) === 0) {
$output->writeln(sprintf('No changes in results in %d code snippets from %d GitHub issues. :tada:', $totalCodeSnippets, count($issueCache->getIssues())));
}
foreach ($toPost as ['issue' => $issue, 'hash' => $hash, 'users' => $users, 'diff' => $diff, 'details' => $details]) {
$text = sprintf(
"Result of the [code snippet](https://phpstan.org/r/%s) from %s in [#%d](https://github.com/phpstan/phpstan/issues/%d) changed:\n\n```diff\n%s```",
$hash,
implode(' ', array_map(static fn (string $user): string => sprintf('@%s', $user), $users)),
$issue,
$issue,
$diff,
);
if ($details !== null) {
$text .= "\n\n" . sprintf('<details>
<summary>Full report</summary>
%s
</details>', $details);
}
$text .= "\n\n---\n";
$output->writeln($text);
}
$postComments = (bool) $input->getOption('post-comments');
if ($postComments) {
if (count($toPost) > 20) {
$output->writeln('Too many comments to post, something is probably wrong.');
return 1;
}
foreach ($toPost as ['issue' => $issue, 'hash' => $hash, 'users' => $users, 'diff' => $diff, 'details' => $details]) {
$text = sprintf(
"%s After [the latest push in %s](https://github.com/phpstan/phpstan-src/compare/%s...%s), PHPStan now reports different result with your [code snippet](https://phpstan.org/r/%s):\n\n```diff\n%s```",
implode(' ', array_map(static fn (string $user): string => sprintf('@%s', $user), $users)),
$this->gitBranch,
$this->phpstanSrcCommitBefore,
$this->phpstanSrcCommitAfter,
$hash,
$diff,
);
if ($details !== null) {
$text .= "\n\n" . sprintf('<details>
<summary>Full report</summary>
%s
</details>', $details);
}
/** @var GitHubIssueApi $issueApi */
$issueApi = $this->githubClient->api('issue');
$issueApi->comments()->create('phpstan', 'phpstan', $issue, [
'body' => $text,
]);
}
}
return 0;
}
/**
* @return list<BotComment>
*/
private function getFreshBotComments(int $issueNumber): array
{
$comments = [];
foreach ($this->issueCommentDownloader->getComments($issueNumber) as $issueComment) {
if (!$issueComment instanceof BotComment) {
continue;
}
$comments[] = $issueComment;
}
return $comments;
}
private function isIssueClosed(int $issueNumber): bool
{
/** @var GitHubIssueApi $issueApi */
$issueApi = $this->githubClient->api('issue');
$issue = $issueApi->show('phpstan', 'phpstan', $issueNumber);
return $issue['state'] === 'closed';
}
private function loadIssueCache(): IssueCache
{
if (!is_file($this->issueCachePath)) {
throw new Exception('Issue cache must exist');
}
$contents = file_get_contents($this->issueCachePath);
if ($contents === false) {
throw new Exception('Read unsuccessful');
}
return unserialize($contents);
}
private function loadPlaygroundCache(): PlaygroundCache
{
if (!is_file($this->playgroundCachePath)) {
throw new Exception('Playground cache must exist');
}
$contents = file_get_contents($this->playgroundCachePath);
if ($contents === false) {
throw new Exception('Read unsuccessful');
}
return unserialize($contents);
}
/**
* @return array<string, array<int, list<PlaygroundError>>>
*/
private function loadResults(): array
{
$finder = new Finder();
$tmpResults = [];
foreach ($finder->files()->name('results-*.tmp')->in($this->tmpDir) as $resultFile) {
$contents = file_get_contents($resultFile->getPathname());
if ($contents === false) {
throw new Exception('Result read unsuccessful');
}
$result = unserialize($contents);
$phpVersion = (int) $result['phpVersion'];
foreach ($result['errors'] as $hash => $errors) {
$tmpResults[(string) $hash][$phpVersion] = array_values($errors);
}
}
return $tmpResults;
}
/**
* @param array<int, list<PlaygroundError>> $originalErrors
* @param array<int, list<PlaygroundError>> $newErrors
* @return array<int, list<PlaygroundError>>
*/
private function filterErrors(
array $originalErrors,
array $newErrors,
): array
{
$originalPhpVersions = array_keys($originalErrors);
$filteredNewErrors = [];
foreach ($newErrors as $phpVersion => $errors) {
if (!in_array($phpVersion, $originalPhpVersions, true)) {
continue;
}
$filteredNewErrors[$phpVersion] = $errors;
}
$newTabs = $this->tabCreator->create($newErrors);
$filteredNewTabs = $this->tabCreator->create($filteredNewErrors);
if (count($newTabs) !== count($filteredNewTabs)) {
return $newErrors;
}
$firstFilteredNewTab = $filteredNewTabs[0];
$firstNewTab = $newTabs[0];
if (count($firstFilteredNewTab->getErrors()) !== count($firstNewTab->getErrors())) {
return $newErrors;
}
foreach ($firstFilteredNewTab->getErrors() as $i => $error) {
$otherError = $firstNewTab->getErrors()[$i];
if ($error->getLine() !== $otherError->getLine()) {
return $newErrors;
}
if ($error->getMessage() !== $otherError->getMessage()) {
return $newErrors;
}
}
return $filteredNewErrors;
}
}