forked from TYPO3GmbH/site-intercept
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRenderDocumentationService.php
More file actions
319 lines (295 loc) · 17.2 KB
/
Copy pathRenderDocumentationService.php
File metadata and controls
319 lines (295 loc) · 17.2 KB
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);
/*
* This file is part of the package t3g/intercept.
*
* For the full copyright and license information, please read the
* LICENSE file that was distributed with this source code.
*/
namespace App\Service;
use App\Dto\HistoryEntryDto;
use App\Entity\DocumentationJar;
use App\Enum\DocsRenderingHistoryStatus;
use App\Enum\DocumentationRenderingTrigger;
use App\Enum\DocumentationStatus;
use App\Enum\HistoryEntryTrigger;
use App\Enum\HistoryEntryType;
use App\Exception\Composer\DocsComposerDependencyException;
use App\Exception\Composer\DocsComposerMissingValueException;
use App\Exception\ComposerJsonInvalidException;
use App\Exception\ComposerJsonNotFoundException;
use App\Exception\DisallowedComposerJsonUrlException;
use App\Exception\DocsPackageDoNotCareBranch;
use App\Exception\DocsPackageRegisteredWithDifferentRepositoryException;
use App\Exception\DocumentationRenderingRequestDeclinedException;
use App\Exception\InvalidComposerJsonUrlException;
use App\Exception\UnknownComposerJsonUrlException;
use App\Extractor\DeploymentInformation;
use App\Extractor\PushEvent;
use App\Repository\RepositoryBlacklistEntryRepository;
use App\Utility\RepositoryUrlUtility;
use Psr\Log\LoggerInterface;
use Symfony\Bundle\SecurityBundle\Security;
use T3G\Bundle\Keycloak\Security\KeyCloakUser;
final readonly class RenderDocumentationService
{
public function __construct(
private DocumentationBuildInformationService $documentationBuildInformationService,
private GithubService $githubService,
private HistoryService $historyService,
private LoggerInterface $logger,
private DocumentationQuarantineService $documentationQuarantineService,
private RepositoryBlacklistEntryRepository $repositoryBlacklistEntryRepository,
private MailService $mailService,
private Security $security,
) {
}
public function requestDocumentationRendering(PushEvent $pushEvent, DocumentationRenderingTrigger $trigger): void
{
$userIdentifier = $this->security->getUser() instanceof KeyCloakUser ? $this->security->getUser()->getDisplayName() : 'Anon.';
try {
$this->documentationBuildInformationService->updateLastHit(RepositoryUrlUtility::getNormalizedDomain($pushEvent->getRepositoryUrl()));
$composerJson = $this->documentationBuildInformationService->fetchRemoteComposerJson($pushEvent->getUrlToComposerFile());
} catch (UnknownComposerJsonUrlException $e) {
if (!$this->documentationQuarantineService->isQuarantined($pushEvent)) {
$documentationQuarantine = $this->documentationQuarantineService->quarantine($pushEvent);
$this->documentationBuildInformationService->notifyAboutUnknownRepositoryDomain($documentationQuarantine);
$this->historyService->writeHistory(new HistoryEntryDto(
type: HistoryEntryType::DOCS_RENDERING,
status: DocsRenderingHistoryStatus::UNKNOWN_REPOSITORY_DOMAIN,
triggeredBy: $trigger->toHistoryEntryTrigger(),
data: [
'repository' => $pushEvent->getRepositoryUrl(),
'composerFile' => $pushEvent->getUrlToComposerFile(),
'payload' => $pushEvent->getPayload(),
'user' => $userIdentifier,
]
));
}
throw new DocumentationRenderingRequestDeclinedException(sprintf('composer.json\'s host domain %s is unknown to the system and needs approval', $e->normalizedHost), 1782293322, $e);
} catch (DisallowedComposerJsonUrlException $e) {
$this->historyService->writeHistory(new HistoryEntryDto(
type: HistoryEntryType::DOCS_RENDERING,
status: DocsRenderingHistoryStatus::UNKNOWN_REPOSITORY_DOMAIN,
triggeredBy: $trigger->toHistoryEntryTrigger(),
data: [
'repository' => $pushEvent->getRepositoryUrl(),
'composerFile' => $pushEvent->getUrlToComposerFile(),
'payload' => $pushEvent->getPayload(),
'user' => $userIdentifier,
]
));
throw new DocumentationRenderingRequestDeclinedException(sprintf('composer.json\'s host domain %s is disallowed for rendering request', $e->normalizedHost), 1782294348, $e);
} catch (InvalidComposerJsonUrlException $e) {
$this->historyService->writeHistory(new HistoryEntryDto(
type: HistoryEntryType::DOCS_RENDERING,
status: DocsRenderingHistoryStatus::INVALID_COMPOSER_JSON_URL,
triggeredBy: $trigger->toHistoryEntryTrigger(),
data: [
'repository' => $pushEvent->getRepositoryUrl(),
'composerFile' => $pushEvent->getUrlToComposerFile(),
'payload' => $pushEvent->getPayload(),
'user' => $userIdentifier,
]
));
throw new DocumentationRenderingRequestDeclinedException(sprintf('composer.json url %s can not be used for a rendering request', $e->composerJsonUrl), 1785810600, $e);
}
$composerAsObject = $this->documentationBuildInformationService->getComposerJsonObject($composerJson);
try {
if ($this->repositoryBlacklistEntryRepository->isBlacklisted($pushEvent->getRepositoryUrl())) {
$this->historyService->writeHistory(new HistoryEntryDto(
type: HistoryEntryType::DOCS_RENDERING,
status: DocsRenderingHistoryStatus::BLACKLISTED,
triggeredBy: $trigger->toHistoryEntryTrigger(),
data: [
'repository' => $pushEvent->getRepositoryUrl(),
'composerFile' => $pushEvent->getUrlToComposerFile(),
'payload' => $pushEvent->getPayload(),
'user' => $userIdentifier,
]
));
return;
}
$buildInformation = $this->documentationBuildInformationService->generateBuildInformation($pushEvent, $composerAsObject);
$this->documentationBuildInformationService->assertBuildWasTriggeredByRepositoryOwner($buildInformation);
$documentationJar = $this->documentationBuildInformationService->registerDocumentationRendering($buildInformation);
// Trigger build only if status is not already "I'm rendering". Else, only set a flag that re-rendering is needed.
// The re-render flag is used and reset by the post build controller if it is set, to trigger a new
// rendering. This suppresses multiple builds for one repo at the same time and prevents conditions where
// an older build finishes after a younger triggered build which would overwrite the result af the later build.
if (DocumentationStatus::STATUS_RENDERING === $documentationJar->getStatus()) {
$this->documentationBuildInformationService->updateReRenderNeeded($documentationJar, true);
$this->historyService->writeHistory(new HistoryEntryDto(
type: HistoryEntryType::DOCS_RENDERING,
status: DocsRenderingHistoryStatus::RE_RENDER_NEEDED,
triggeredBy: $trigger->toHistoryEntryTrigger(),
data: [
'repository' => $buildInformation->repositoryUrl,
'package' => $buildInformation->packageName,
'sourceBranch' => $buildInformation->sourceBranch,
'targetBranch' => $buildInformation->targetBranchDirectory,
'user' => $userIdentifier,
]
));
} elseif (!$documentationJar->isApproved()) {
$this->logger->info('Repository present, but not approved. Do nothing.', [$documentationJar]);
} else {
$buildTriggered = $this->githubService->triggerDocumentationPlan($buildInformation);
$this->documentationBuildInformationService->update($documentationJar, function (DocumentationJar $documentationJar) use ($buildTriggered) {
$documentationJar->setStatus(DocumentationStatus::STATUS_RENDERING);
$documentationJar->setReRenderNeeded(false);
$documentationJar->setBuildKey($buildTriggered->buildResultKey);
});
$this->historyService->writeHistory(new HistoryEntryDto(
type: HistoryEntryType::DOCS_RENDERING,
status: DocsRenderingHistoryStatus::TRIGGERED,
triggeredBy: $trigger->toHistoryEntryTrigger(),
groupEntry: $buildTriggered->buildResultKey,
data: [
'repository' => $buildInformation->repositoryUrl,
'package' => $buildInformation->packageName,
'sourceBranch' => $buildInformation->sourceBranch,
'targetBranch' => $buildInformation->targetBranchDirectory,
'bambooKey' => $buildTriggered->buildResultKey,
'user' => $userIdentifier,
]
));
}
} catch (ComposerJsonNotFoundException $e) {
// Repository did not provide a composer.json, or fetch failed
$this->historyService->writeHistory(new HistoryEntryDto(
type: HistoryEntryType::DOCS_RENDERING,
status: DocsRenderingHistoryStatus::NO_COMPOSER_JSON,
triggeredBy: $trigger->toHistoryEntryTrigger(),
data: [
'exceptionCode' => $e->getCode(),
'exceptionMessage' => $e->getMessage(),
'repository' => $pushEvent->getRepositoryUrl(),
'composerFile' => $pushEvent->getUrlToComposerFile(),
'payload' => $pushEvent->getPayload(),
'user' => $userIdentifier,
]
));
throw new DocumentationRenderingRequestDeclinedException('No composer.json found, invalid or unable to fetch. See https://intercept.typo3.com for more information.', 1782294357, $e);
} catch (ComposerJsonInvalidException $e) {
$this->historyService->writeHistory(new HistoryEntryDto(
type: HistoryEntryType::DOCS_RENDERING,
status: DocsRenderingHistoryStatus::INVALID_COMPOSER_JSON,
triggeredBy: $trigger->toHistoryEntryTrigger(),
data: [
'exceptionCode' => $e->getCode(),
'exceptionMessage' => $e->getMessage(),
'repository' => $pushEvent->getRepositoryUrl(),
'composerFile' => $pushEvent->getUrlToComposerFile(),
'payload' => $pushEvent->getPayload(),
'user' => $userIdentifier,
]
));
throw new DocumentationRenderingRequestDeclinedException('Invalid composer.json. See https://intercept.typo3.com for more information.', 1782294366, $e);
} catch (DocsPackageRegisteredWithDifferentRepositoryException $e) {
$this->historyService->writeHistory(new HistoryEntryDto(
type: HistoryEntryType::DOCS_RENDERING,
status: DocsRenderingHistoryStatus::PACKAGE_REGISTERED_WITH_DIFFERENT_REPOSITORY,
triggeredBy: $trigger->toHistoryEntryTrigger(),
data: [
'exceptionCode' => $e->getCode(),
'exceptionMessage' => $e->getMessage(),
'repository' => $pushEvent->getRepositoryUrl(),
'package' => $e->getPackageName(),
'user' => $userIdentifier,
]
));
throw new DocumentationRenderingRequestDeclinedException('Package already registered for different repository. See https://intercept.typo3.com for more information.', 1782294378, $e);
} catch (DocsPackageDoNotCareBranch $e) {
$this->historyService->writeHistory(new HistoryEntryDto(
type: HistoryEntryType::DOCS_RENDERING,
status: DocsRenderingHistoryStatus::NO_RELEVANT_BRANCH_OR_TAG,
triggeredBy: $trigger->toHistoryEntryTrigger(),
data: [
'exceptionCode' => $e->getCode(),
'exceptionMessage' => $e->getMessage(),
'repository' => $pushEvent->getRepositoryUrl(),
'sourceBranch' => $pushEvent->getVersionString(),
'user' => $userIdentifier,
]
));
throw new DocumentationRenderingRequestDeclinedException('Branch or tag name ignored for documentation rendering. See https://intercept.typo3.com for more information.', 1782294385, $e);
} catch (DocsComposerMissingValueException $e) {
$this->historyService->writeHistory(new HistoryEntryDto(
type: HistoryEntryType::DOCS_RENDERING,
status: DocsRenderingHistoryStatus::MISSING_VALUE_IN_COMPOSER_JSON,
triggeredBy: $trigger->toHistoryEntryTrigger(),
data: [
'exceptionCode' => $e->getCode(),
'exceptionMessage' => $e->getMessage(),
'repository' => $pushEvent->getRepositoryUrl(),
'sourceBranch' => $pushEvent->getVersionString(),
'user' => $userIdentifier,
]
));
throw new DocumentationRenderingRequestDeclinedException('A mandatory value is missing in the composer.json. See https://intercept.typo3.com for more information.', 1782294401, $e);
} catch (DocsComposerDependencyException $e) {
$this->historyService->writeHistory(new HistoryEntryDto(
type: HistoryEntryType::DOCS_RENDERING,
status: DocsRenderingHistoryStatus::CORE_DEPENDENCY_NOT_SET,
triggeredBy: $trigger->toHistoryEntryTrigger(),
data: [
'exceptionCode' => $e->getCode(),
'exceptionMessage' => $e->getMessage(),
'repository' => $pushEvent->getRepositoryUrl(),
'sourceBranch' => $pushEvent->getVersionString(),
'user' => $userIdentifier,
]
));
try {
$author = $composerAsObject->getFirstAuthor();
if (filter_var($author['email'] ?? '', FILTER_VALIDATE_EMAIL)) {
$this->mailService->sendMailToAuthorDueToMissingDependency($pushEvent, $composerAsObject, $e->getMessage());
}
} catch (DocsComposerMissingValueException) {
// Do not send mail if 'authors' is not set in composer.json
}
throw new DocumentationRenderingRequestDeclinedException('Dependencies are not fulfilled. See https://intercept.typo3.com for more information.', 1782294424, $e);
}
}
/**
* @throws DocsPackageDoNotCareBranch
*/
public function renderDocumentationByDocumentationJar(DocumentationJar $documentationJar, HistoryEntryTrigger $scope): DeploymentInformation
{
$buildInformation = $this->documentationBuildInformationService->generateBuildInformationFromDocumentationJar($documentationJar);
$documentationJar = $this->documentationBuildInformationService->registerDocumentationRendering($buildInformation);
$buildTriggered = $this->githubService->triggerDocumentationPlan($buildInformation);
$this->documentationBuildInformationService->update($documentationJar, function (DocumentationJar $documentationJar) use ($buildTriggered) {
$documentationJar->setStatus(DocumentationStatus::STATUS_RENDERING);
$documentationJar->setReRenderNeeded(false);
$documentationJar->setBuildKey($buildTriggered->buildResultKey);
});
$user = $this->security->getUser();
$userIdentifier = 'Anon.';
if ($user instanceof KeyCloakUser) {
$userIdentifier = $user->getDisplayName();
}
$this->historyService->writeHistory(new HistoryEntryDto(
type: HistoryEntryType::DOCS_RENDERING,
status: DocsRenderingHistoryStatus::TRIGGERED,
triggeredBy: $scope,
groupEntry: $buildTriggered->buildResultKey,
data: [
'repository' => $buildInformation->repositoryUrl,
'package' => $buildInformation->packageName,
'sourceBranch' => $buildInformation->sourceBranch,
'targetBranch' => $buildInformation->targetBranchDirectory,
'bambooKey' => $buildTriggered->buildResultKey,
'user' => $userIdentifier,
]
));
return $buildInformation;
}
public function dumpRenderingInformationByDocumentationJar(DocumentationJar $documentationJar): DeploymentInformation
{
$buildInformation = $this->documentationBuildInformationService->generateBuildInformationFromDocumentationJar($documentationJar);
$this->documentationBuildInformationService->dumpDeploymentInformationFile($buildInformation);
return $buildInformation;
}
}