forked from shopware/shopware
-
Notifications
You must be signed in to change notification settings - Fork 0
/
.danger.php
539 lines (461 loc) · 21.8 KB
/
.danger.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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
<?php declare(strict_types=1);
use Danger\Config;
use Danger\Context;
use Danger\Platform\Github\Github;
use Danger\Platform\Gitlab\Gitlab;
use Danger\Rule\CommitRegex;
use Danger\Rule\Condition;
use Danger\Rule\DisallowRepeatedCommits;
use Danger\Struct\File;
use Danger\Struct\Gitlab\File as GitlabFile;
const COMPOSER_PACKAGE_EXCEPTIONS = [
'~' => [
'^symfony\/.*$' => 'We are too tightly coupled to symfony, therefore minor updates often cause breaks',
'^php$' => 'PHP does not follow semantic versioning, therefore minor updates include breaks',
],
'strict' => [
'^phpstan\/phpstan.*$' => 'Even patch updates for PHPStan may lead to a red CI pipeline, because of new static analysis errors',
'^symplify\/phpstan-rules$' => 'Even patch updates for PHPStan may lead to a red CI pipeline, because of new static analysis errors',
'^rector\/type-perfect$' => 'Even patch updates for PHPStan may lead to a red CI pipeline, because of new static analysis errors',
'^dompdf\/dompdf$' => 'Patch updates of dompdf have let to a lot of issues in the past, therefore it is pinned.',
'^scssphp\/scssphp$' => 'Patch updates of scssphp might lead to UI breaks, therefore it is pinned.',
'^shopware\/conflicts$' => 'The shopware conflicts packages should be required in any version, so use `*` constraint',
'^shopware\/core$' => 'The shopware core packages should be required in any version, so use `*` constraint, the version constraint will be automatically synced during the release process',
'^ext-.*$' => 'PHP extension version ranges should be required in any version, so use `*` constraint',
],
];
const BaseTestClasses = [
'RuleTestCase',
'TestCase'
];
return (new Config())
->useThreadOn(Config::REPORT_LEVEL_WARNING)
->useRule(new DisallowRepeatedCommits())
->useRule(function (Context $context): void {
$files = $context->platform->pullRequest->getFiles();
if ($files->matches('changelog/_unreleased/*.md')->count() === 0) {
$context->warning('The Pull Request doesn\'t contain any changelog file');
}
})
->useRule(new Condition(
function (Context $context) {
return $context->platform instanceof Gitlab;
},
[
function (Context $context): void {
$labels = array_map('strtolower', $context->platform->pullRequest->labels);
if ($context->platform->raw['squash'] === true && in_array('github', $labels, true)) {
$context->failure('GitHub PRs are not allowed to be squashed');
}
},
]
))
/**
* MRs that target a release branch that is not trunk should have a thread with link to a trunk MR
* to disable this rule you can add the no-trunk label
*/
->useRule(new Condition(
function (Context $context) {
$labels = array_map('strtolower', $context->platform->pullRequest->labels);
return $context->platform instanceof Gitlab
&& !\in_array('no-trunk', $labels, true)
&& preg_match('#^6\.\d+.*|saas/\d{4}/\d{1,2}$#', $context->platform->raw['target_branch']);
},
[
function (Context $context): void {
if (str_contains($context->platform->pullRequest->body, '/shopware/6/product/platform/-/merge_requests/')) {
return;
}
$found = false;
foreach ($context->platform->pullRequest->getComments() as $comment) {
if (str_contains($comment->body, '/shopware/6/product/platform/-/merge_requests/')) {
$found = true;
break;
}
}
if (!$found) {
$context->failure('This MR should have a dependency on a trunk MR. Please add a thread with a link');
}
}
]
))
->useRule(new Condition(
function (Context $context) {
$labels = array_map('strtolower', $context->platform->pullRequest->labels);
return $context->platform instanceof Gitlab && !\in_array('github', $labels, true);
},
[
function (Context $context): void {
$files = $context->platform->pullRequest->getFiles();
/** @var Gitlab $gitlab */
$gitlab = $context->platform;
$phpstanBaseline = new GitlabFile(
$gitlab->client,
$_SERVER['CI_PROJECT_ID'],
'phpstan-baseline.neon',
$gitlab->raw['sha']
);
$fileNames = $files->map(fn (File $f) => $f->name);
$filesWithIgnoredErrors = [];
foreach ($fileNames as $fileName) {
if (str_contains($phpstanBaseline->getContent(), 'path: ' . $fileName)) {
$filesWithIgnoredErrors[] = $fileName;
}
}
if ($filesWithIgnoredErrors) {
$context->failure(
'Some files you touched in your MR contain ignored PHPStan errors. Please be nice and fix all ignored errors for the following files:<br>'
. implode('<br>', $filesWithIgnoredErrors)
);
}
},
function (Context $context): void {
$files = $context->platform->pullRequest->getFiles();
$newRepoUseInFrontend = array_merge(
$files->filterStatus(File::STATUS_MODIFIED)->matches('src/Storefront/Controller/*')
->matchesContent('/EntityRepository/')
->matchesContent('/^((?!@deprecated).)*$/')->getElements(),
$files->filterStatus(File::STATUS_MODIFIED)->matches('src/Storefront/Page/*')
->matchesContent('/EntityRepository/')
->matchesContent('/^((?!@deprecated).)*$/')->getElements(),
$files->filterStatus(File::STATUS_MODIFIED)->matches('src/Storefront/Pagelet/*')
->matchesContent('/EntityRepository/')
->matchesContent('/^((?!@deprecated).)*$/')->getElements(),
);
if (count($newRepoUseInFrontend) > 0) {
$errorFiles = [];
foreach ($newRepoUseInFrontend as $file) {
if ($file->name !== '.danger.php') {
$errorFiles[] = $file->name . '<br/>';
}
}
if (count($errorFiles) === 0) {
return;
}
$context->failure(
'Do not use direct repository calls in the Frontend Layer (Controller, Page, Pagelet).'
. ' Use Store-Api Routes instead.<br/>'
. print_r($errorFiles, true)
);
}
},
]
))
->useRule(function (Context $context): void {
$files = $context->platform->pullRequest->getFiles();
if ($files->matches('*/shopware.yaml')->count() > 0) {
$context->warning('You updated the shopware.yaml, please consider to update the config-schema.json');
}
})
->useRule(new Condition(
function (Context $context) {
return $context->platform instanceof Gitlab;
},
[
function (Context $context): void {
$files = $context->platform->pullRequest->getFiles();
$relevant = $files->matches('src/Core/*.php')->count() > 0
|| $files->matches('src/Elasticsearch/*.php')->count() > 0
|| $files->matches('src/Storefront/Migration/')->count() > 0;
if (!$relevant) {
return;
}
$labels = ['core__component'];
if ($files->matches('src/**/Cart/')->count() > 0) {
$labels[] = 'core__cart';
}
if ($files->matches('src/**/*Definition.php')->count() > 0) {
$labels[] = 'core__definition';
}
if ($files->matches('src/**/*Route.php')->count() > 0) {
$labels[] = 'core__store-api';
}
if ($files->matches('src/**/Migration/**/Migration*.php')->count() > 0) {
$labels[] = 'core__migration';
}
if ($files->matches('src/Elasticsearch/')->count() > 0) {
$labels[] = 'core__elasticsearch';
}
if ($files->matches('src/**/DataAbstractionLayer/')->count() > 0) {
$labels[] = 'core__dal';
}
$context->platform->addLabels(...$labels);
},
]
))->useRule(new Condition(
function (Context $context) {
return $context->platform instanceof Gitlab;
},
[
function (Context $context): void {
$files = $context->platform->pullRequest->getFiles();
$bcChange = $files->matches('.bc-exclude.php')->count() > 0;
if (!$bcChange) {
return;
}
$context->platform->addLabels('bc_exclude_php');
},
]
))
->useRule(new Condition(
function (Context $context) {
return $context->platform instanceof Gitlab;
},
[
function (Context $context): void {
if (!preg_match('/(?m)^((WIP:\s)|^(Draft:\s)|^(DRAFT:\s))?(\[[\w.]+]\s)?NEXT-\d*\s-\s\w/', $context->platform->pullRequest->title)) {
$context->failure(sprintf('The title `%s` does not match our requirements. Example: NEXT-00000 - My Title', $context->platform->pullRequest->title));
}
}
]))
->useRule(new Condition(
function (Context $context) {
return $context->platform instanceof Gitlab;
},
[
function (Context $context): void {
$labels = $context->platform->pullRequest->labels;
if (in_array('E2E:skip', $labels, true) || in_array('unit:skip', $labels, true)) {
$context->notice('You skipped some tests. Reviewers be carefully with this');
}
},
function (Context $context): void {
$files = $context->platform->pullRequest->getFiles();
$hasStoreApiModified = false;
/** @var File $file */
foreach ($files->getElements() as $file) {
if (str_contains($file->name, 'SalesChannel') && str_contains($file->name, 'Route.php') && !str_contains($file->name, '/Test/')) {
$hasStoreApiModified = true;
}
}
if ($hasStoreApiModified) {
$context->warning('Store-API Route has been modified. @Reviewers please review carefully!');
$context->platform->addLabels('Security-Audit Required');
}
},
]
))
->useRule(function (Context $context): void {
function checkMigrationForBundle(string $bundle, Context $context): void
{
$files = $context->platform->pullRequest->getFiles();
$migrationFiles = $files->filterStatus(File::STATUS_ADDED)->matches('src/Core/Migration/V*/Migration*.php');
$migrationTestFiles = $files->filterStatus(File::STATUS_ADDED)->matches('tests/migration/Core/V*/*.php');
if ($migrationFiles->count() && !$migrationTestFiles->count()) {
$context->failure('Please add tests for your new Migration file');
}
}
checkMigrationForBundle('Core', $context);
checkMigrationForBundle('Administration', $context);
checkMigrationForBundle('Storefront', $context);
})
->useRule(function (Context $context): void {
$files = $context->platform->pullRequest->getFiles();
$newSqlHeredocs = $files->filterStatus(File::STATUS_MODIFIED)->matchesContent('/<<<SQL/');
if ($newSqlHeredocs->count() > 0) {
$errorFiles = [];
foreach ($newSqlHeredocs as $file) {
if ($file->name !== '.danger.php') {
$errorFiles[] = $file->name . '<br/>';
}
}
if (count($errorFiles) === 0) {
return;
}
$context->failure(
'Please use [Nowdoc](https://www.php.net/manual/de/language.types.string.php#language.types.string.syntax.nowdoc)'
. ' for SQL (<<<\'SQL\') instead of Heredoc (<<<SQL)<br/>'
. print_r($errorFiles, true)
);
}
})
->useRule(function (Context $context): void {
$files = $context->platform->pullRequest->getFiles();
$changedTemplates = $files->filterStatus(File::STATUS_MODIFIED)->matches('src/Storefront/Resources/views/*.twig')
->getElements();
if (count($changedTemplates) > 0) {
$patched = [];
foreach ($changedTemplates as $file) {
preg_match_all('/\- .*? (\{% block (.*?) %\})+/', $file->patch, $removedBlocks);
preg_match_all('/\+ .*? (\{% block (.*?) %\})+/', $file->patch, $addedBlocks);
if (!isset($removedBlocks[2]) || !is_array($removedBlocks[2])) {
$removedBlocks[2] = [];
}
if (!isset($addedBlocks[2]) || !is_array($addedBlocks[2])) {
$addedBlocks[2] = [];
}
$remaining = array_diff_assoc($removedBlocks[2], $addedBlocks[2]);
if (count($remaining) > 0) {
$patched[] = print_r($remaining, true) . '<br/>';
}
}
if (count($patched) === 0) {
return;
}
$context->warning(
'You probably moved or deleted a twig block. This is likely a hard break. Please check your template'
. ' changes and make sure that deleted blocks are already deprecated. <br/>'
. 'If you are sure everything is fine with your changes, you can resolve this warning.<br/>'
. 'Moved or deleted block: <br/>'
. print_r($patched, true)
);
}
})
->useRule(function (Context $context): void {
$files = $context->platform->pullRequest->getFiles();
$invalidFiles = [];
foreach ($files as $file) {
if (str_starts_with($file->name, '.run/')) {
continue;
}
if ($file->status !== File::STATUS_REMOVED && preg_match('/^([-+\.\w\/]+)$/', $file->name) === 0) {
$invalidFiles[] = $file->name;
}
}
if (count($invalidFiles) > 0) {
$context->failure(
'The following filenames contain invalid special characters, please use only alphanumeric characters, dots, dashes and underscores: <br/>'
. print_r($invalidFiles, true)
);
}
})
->useRule(function (Context $context): void {
$addedFiles = $context->platform->pullRequest->getFiles()->filterStatus(File::STATUS_ADDED);
$addedLegacyTests = [];
foreach ($addedFiles->matches('src/**/*Test.php') as $file) {
if (str_contains($file->name, 'src/WebInstaller/')) {
continue;
}
$content = $file->getContent();
if (str_contains($content, 'extends TestCase')) {
$addedLegacyTests[] = $file->name;
}
}
if (count($addedLegacyTests) > 0) {
$context->failure(
'Don\'t add new testcases in the `/src` folder, for new tests write "real" unit tests under `tests/unit` and if needed a few meaningful integration tests under `tests/integration`: <br/>'
. print_r($addedLegacyTests, true)
);
}
})
->useRule(function (Context $context): void {
$addedUnitTests = $context->platform->pullRequest->getFiles()->filterStatus(File::STATUS_ADDED)->matches('tests/unit/**/*Test.php');
$addedSrcFiles = $context->platform->pullRequest->getFiles()->filterStatus(File::STATUS_ADDED)->matches('src/**/*.php');
$missingUnitTests = [];
$unitTestsName = [];
foreach ($addedUnitTests as $file) {
$content = $file->getContent();
preg_match('/\s+extends\s+(?<class>\w+)/', $content, $matches);
if (isset($matches['class']) && in_array($matches['class'], BaseTestClasses)) {
$fqcn = str_replace('.php', '', $file->name);
$className = explode('/', $fqcn);
$testClass = end($className);
$unitTestsName[] = $testClass;
}
}
foreach ($addedSrcFiles as $file) {
$content = $file->getContent();
$fqcn = str_replace('.php', '', $file->name);
$className = explode('/', $fqcn);
$class = end($className);
if (\str_contains($content, '* @codeCoverageIgnore')) {
continue;
}
if (\str_contains($content, 'abstract class ' . $class)) {
continue;
}
if (\str_contains($content, 'interface ' . $class)) {
continue;
}
if (\str_contains($content, 'trait ' . $class)) {
continue;
}
if (\str_starts_with($class, 'Migration1')) {
continue;
}
$ignoreSuffixes = [
'Entity',
'Collection',
'Struct',
'Field',
'Test',
'Definition',
'Event',
];
$ignored = false;
foreach ($ignoreSuffixes as $ignoreSuffix) {
if (\str_ends_with($class, $ignoreSuffix)) {
$ignored = true;
break;
}
}
if (!$ignored && !\in_array($class . 'Test', $unitTestsName, true)) {
$missingUnitTests[] = $file->name;
}
}
if (\count($missingUnitTests) > 0) {
$context->warning(
'Please be kind and add unit tests for your new code in these files: <br/>'
. implode('<br/>', $missingUnitTests)
. '<br/>' . 'If you are sure everything is fine with your changes, you can resolve this warning. <br /> You can run `composer make:coverage` to generate dummy unit tests for files that are not covered'
);
}
})
// check for composer version operators
->useRule(function (Context $context): void {
$composerFiles = $context->platform->pullRequest->getFiles()->matches('**/composer.json');
if ($root = $context->platform->pullRequest->getFiles()->matches('composer.json')->first()) {
$composerFiles->add($root);
}
foreach ($composerFiles as $composerFile) {
if ($composerFile->status === File::STATUS_REMOVED || str_contains((string)$composerFile->name, 'src/WebInstaller')) {
continue;
}
$composerContent = json_decode($composerFile->getContent(), true);
/** @var array<string, string> $requirements */
$requirements = array_merge(
$composerContent['require'] ?? [],
$composerContent['require-dev'] ?? []
);
foreach ($requirements as $package => $constraint) {
foreach (COMPOSER_PACKAGE_EXCEPTIONS['~'] as $exceptionPackage => $exceptionMessage) {
if (preg_match('/' . $exceptionPackage . '/', $package)) {
if (!str_contains($constraint, '~')) {
$context->failure(
sprintf(
'The package `%s` from composer file `%s` should use the [tilde version range](https://getcomposer.org/doc/articles/versions.md#tilde-version-range-) to only allow patch version updates. ',
$package,
$composerFile->name
) . $exceptionMessage
);
}
continue 2;
}
}
foreach (COMPOSER_PACKAGE_EXCEPTIONS['strict'] as $exceptionPackage => $exceptionMessage) {
if (preg_match('/' . $exceptionPackage . '/', $package)) {
if (str_contains($constraint, '~') || str_contains($constraint, '^')) {
$context->failure(
sprintf(
'The package `%s` from composer file `%s` should be pinned to a specific version. ',
$package,
$composerFile->name
) . $exceptionMessage
);
}
continue 2;
}
}
if (!str_contains($constraint, '^')) {
$context->failure(
sprintf(
'The package `%s` from composer file `%s` should use the [caret version range](https://getcomposer.org/doc/articles/versions.md#caret-version-range-), to automatically allow minor updates.',
$package,
$composerFile->name
)
);
}
}
}
})
;