new deployer command
This commit is contained in:
@@ -15,6 +15,9 @@ use Modules\Helpdesk\Services\GitWebhookIngestionService;
|
|||||||
|
|
||||||
final class GitWebhookController
|
final class GitWebhookController
|
||||||
{
|
{
|
||||||
|
private const REJECTED = 'rejected';
|
||||||
|
|
||||||
|
private const SIGNATURE_FAILED = 'Git webhook signature or token validation failed.';
|
||||||
/**
|
/**
|
||||||
* Ingest a signed Git provider webhook for a registered Helpdesk repository.
|
* Ingest a signed Git provider webhook for a registered Helpdesk repository.
|
||||||
*/
|
*/
|
||||||
@@ -26,14 +29,14 @@ final class GitWebhookController
|
|||||||
$rawPayload = $request->getContent();
|
$rawPayload = $request->getContent();
|
||||||
|
|
||||||
if (! $repository->is_active) {
|
if (! $repository->is_active) {
|
||||||
$result = ['status' => 'rejected', 'message' => 'Repository is inactive.', 'commit_count' => 0, 'linked_count' => 0, 'ignored_count' => 1];
|
$result = $this->rejectedResult('Repository is inactive.');
|
||||||
$service->logWebhook($repository, $provider, $deliveryId, $event, $result);
|
$service->logWebhook($repository, $provider, $deliveryId, $event, $result);
|
||||||
|
|
||||||
return response()->json($result, 403);
|
return response()->json($result, 403);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (! $this->hasValidSignature($request, $repository, $provider, $rawPayload)) {
|
if (! $this->hasValidSignature($request, $repository, $provider, $rawPayload)) {
|
||||||
$result = ['status' => 'rejected', 'message' => 'Git webhook signature or token validation failed.', 'commit_count' => 0, 'linked_count' => 0, 'ignored_count' => 1];
|
$result = $this->rejectedResult(self::SIGNATURE_FAILED);
|
||||||
$service->logWebhook($repository, $provider, $deliveryId, $event, $result);
|
$service->logWebhook($repository, $provider, $deliveryId, $event, $result);
|
||||||
|
|
||||||
return response()->json($result, 403);
|
return response()->json($result, 403);
|
||||||
@@ -43,7 +46,7 @@ final class GitWebhookController
|
|||||||
/** @var array<string, mixed> $payload */
|
/** @var array<string, mixed> $payload */
|
||||||
$payload = json_decode($rawPayload, true, 512, JSON_THROW_ON_ERROR);
|
$payload = json_decode($rawPayload, true, 512, JSON_THROW_ON_ERROR);
|
||||||
} catch (JsonException) {
|
} catch (JsonException) {
|
||||||
$result = ['status' => 'rejected', 'message' => 'Invalid JSON webhook payload.', 'commit_count' => 0, 'linked_count' => 0, 'ignored_count' => 1];
|
$result = $this->rejectedResult('Invalid JSON webhook payload.');
|
||||||
$service->logWebhook($repository, $provider, $deliveryId, $event, $result);
|
$service->logWebhook($repository, $provider, $deliveryId, $event, $result);
|
||||||
|
|
||||||
return response()->json($result, 400);
|
return response()->json($result, 400);
|
||||||
@@ -68,29 +71,20 @@ final class GitWebhookController
|
|||||||
/** @var array<string, mixed> $payload */
|
/** @var array<string, mixed> $payload */
|
||||||
$payload = json_decode($rawPayload, true, 512, JSON_THROW_ON_ERROR);
|
$payload = json_decode($rawPayload, true, 512, JSON_THROW_ON_ERROR);
|
||||||
} catch (JsonException) {
|
} catch (JsonException) {
|
||||||
return response()->json([
|
return response()->json($this->rejectedResult('Invalid JSON webhook payload.'), 400);
|
||||||
'status' => 'rejected',
|
|
||||||
'message' => 'Invalid JSON webhook payload.',
|
|
||||||
'commit_count' => 0,
|
|
||||||
'linked_count' => 0,
|
|
||||||
'ignored_count' => 1,
|
|
||||||
], 400);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$repository = $this->repositoryForGiteaPayload($payload);
|
$repository = $this->repositoryForGiteaPayload($payload);
|
||||||
|
|
||||||
if (! $repository instanceof GitRepository) {
|
if (! $repository instanceof GitRepository) {
|
||||||
return response()->json([
|
return response()->json(
|
||||||
'status' => 'rejected',
|
$this->rejectedResult('No active Helpdesk Git repository matched the Gitea webhook payload.'),
|
||||||
'message' => 'No active Helpdesk Git repository matched the Gitea webhook payload.',
|
404,
|
||||||
'commit_count' => 0,
|
);
|
||||||
'linked_count' => 0,
|
|
||||||
'ignored_count' => 1,
|
|
||||||
], 404);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (! $this->hasValidGiteaSecurity($request, $repository, $provider, $rawPayload)) {
|
if (! $this->hasValidGiteaSecurity($request, $repository, $provider, $rawPayload)) {
|
||||||
$result = ['status' => 'rejected', 'message' => 'Git webhook signature or token validation failed.', 'commit_count' => 0, 'linked_count' => 0, 'ignored_count' => 1];
|
$result = $this->rejectedResult(self::SIGNATURE_FAILED);
|
||||||
$service->logWebhook($repository, $provider, $deliveryId, $event, $result, $payload);
|
$service->logWebhook($repository, $provider, $deliveryId, $event, $result, $payload);
|
||||||
|
|
||||||
return response()->json($result, 403);
|
return response()->json($result, 403);
|
||||||
@@ -325,6 +319,18 @@ final class GitWebhookController
|
|||||||
return implode('/', array_slice($segments, -2));
|
return implode('/', array_slice($segments, -2));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @return array{status: string, message: string, commit_count: int, linked_count: int, ignored_count: int} */
|
||||||
|
private function rejectedResult(string $message): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'status' => self::REJECTED,
|
||||||
|
'message' => $message,
|
||||||
|
'commit_count' => 0,
|
||||||
|
'linked_count' => 0,
|
||||||
|
'ignored_count' => 1,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Read the event name from provider-specific headers.
|
* Read the event name from provider-specific headers.
|
||||||
*/
|
*/
|
||||||
|
|||||||
Reference in New Issue
Block a user