forked from shopware/shopware
-
Notifications
You must be signed in to change notification settings - Fork 0
/
.danger.php
357 lines (309 loc) · 14.2 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
<?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;
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');
}
},
]
))
->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/Storefront/Migration/')->count() > 0) {
$labels[] = 'core__migration';
}
if ($files->matches('src/Core/Migration/')->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(function (Context $context): void {
// The title is not important here as we import the pull requests and prefix them
if ($context->platform->pullRequest->projectIdentifier === 'shopware/platform') {
return;
}
if (!preg_match('/(?m)^((WIP:\s)|^(Draft:\s)|^(DRAFT:\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(new Condition(
function (Context $context) {
return $context->platform instanceof Github && $context->platform->pullRequest->projectIdentifier === 'shopwareBoostDay/platform';
},
[
new CommitRegex(
'/(?m)(?mi)^NEXT-\d*\s-\s[A-Z].*,\s*fixes\s*shopwareBoostday\/platform#\d*$/m',
'The commit title `###MESSAGE###` does not match our requirements. Example: "NEXT-00000 - My Title, fixes shopwareBoostday/platform#1234"'
),
]
))
->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)
);
}
})
->after(function (Context $context): void {
if ($context->platform instanceof Github && $context->hasFailures()) {
$context->platform->addLabels('Incomplete');
}
})
;