diff --git a/Modules/Helpdesk/Http/Controllers/GitWebhookController.php b/Modules/Helpdesk/Http/Controllers/GitWebhookController.php index fab7f218..bbb04baf 100644 --- a/Modules/Helpdesk/Http/Controllers/GitWebhookController.php +++ b/Modules/Helpdesk/Http/Controllers/GitWebhookController.php @@ -15,6 +15,9 @@ use Modules\Helpdesk\Services\GitWebhookIngestionService; 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. */ @@ -26,14 +29,14 @@ final class GitWebhookController $rawPayload = $request->getContent(); 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); return response()->json($result, 403); } 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); return response()->json($result, 403); @@ -43,7 +46,7 @@ final class GitWebhookController /** @var array $payload */ $payload = json_decode($rawPayload, true, 512, JSON_THROW_ON_ERROR); } 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); return response()->json($result, 400); @@ -68,29 +71,20 @@ final class GitWebhookController /** @var array $payload */ $payload = json_decode($rawPayload, true, 512, JSON_THROW_ON_ERROR); } catch (JsonException) { - return response()->json([ - 'status' => 'rejected', - 'message' => 'Invalid JSON webhook payload.', - 'commit_count' => 0, - 'linked_count' => 0, - 'ignored_count' => 1, - ], 400); + return response()->json($this->rejectedResult('Invalid JSON webhook payload.'), 400); } $repository = $this->repositoryForGiteaPayload($payload); if (! $repository instanceof GitRepository) { - return response()->json([ - 'status' => 'rejected', - 'message' => 'No active Helpdesk Git repository matched the Gitea webhook payload.', - 'commit_count' => 0, - 'linked_count' => 0, - 'ignored_count' => 1, - ], 404); + return response()->json( + $this->rejectedResult('No active Helpdesk Git repository matched the Gitea webhook payload.'), + 404, + ); } 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); return response()->json($result, 403); @@ -325,6 +319,18 @@ final class GitWebhookController 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. */