forked from shopware/shopware
-
Notifications
You must be signed in to change notification settings - Fork 0
/
.danger.php
243 lines (214 loc) · 9.3 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
<?php declare(strict_types=1);
use Danger\Config;
use Danger\Context;
use Danger\Struct\File;
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\Gitlab\File as GitlabFile;
return (new Config())
->useThreadOnFails()
->useRule(new DisallowRepeatedCommits)
->useRule(function (Context $context) {
$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) {
$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) {
$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)
);
}
}
]
))
->useRule(function (Context $context) {
$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) {
$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) {
$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) {
// 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)?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) {
$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) {
$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) {
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) {
$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)
);
}
})
->after(function (Context $context) {
if ($context->platform instanceof Github && $context->hasFailures()) {
$context->platform->addLabels('Incomplete');
}
})
;