Skip to content

Commit 008bea0

Browse files
committed
feat: add payload validation in WebhookService and refactor WebhookAction
1 parent ef93526 commit 008bea0

File tree

2 files changed

+33
-22
lines changed

2 files changed

+33
-22
lines changed

src/Actions/WebhookAction.php

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -26,34 +26,17 @@ public function __invoke(): JsonResponse
2626
/** @var array<string, mixed> $payload */
2727
$payload = json_decode($request->getContent(), true);
2828

29-
if (!isset($payload['action'])) {
30-
return response()->json(
31-
['message' => __('github-project::github-project.error.event.action_not_found')],
32-
400
33-
);
29+
$validationResponse = $this->webhookService->validatePayload($payload);
30+
if ($validationResponse !== null) {
31+
return $validationResponse;
3432
}
3533

36-
$nodeId = $payload['projects_v2_item']['content_node_id'] ?? null;
37-
$fieldData = $payload['changes']['field_value'] ?? null;
38-
$editor = $payload['sender']['login'] ?? 'Unknown';
39-
40-
if (!$nodeId || !$fieldData) {
41-
return response()->json(
42-
['message' => __('github-project::github-project.error.event.missing_fields')],
43-
400
44-
);
45-
}
46-
47-
$fieldName = $fieldData['field_name'] ?? 'Unknown Field';
48-
$fromValue = $fieldData['from']['name'] ?? 'Unknown';
49-
$toValue = $fieldData['to']['name'] ?? 'Unknown';
50-
5134
$message = view(
5235
'github-project::md.comment',
53-
compact('editor', 'fieldName', 'fromValue', 'toValue')
36+
compact('payload')
5437
)->render();
5538

56-
$this->webhookService->commentOnNode((string) $nodeId, $message);
39+
$this->webhookService->commentOnNode((string) $payload['projects_v2_item']['content_node_id'], $message);
5740

5841
return response()->json(['message' => __('github-project::github-project.success.message')]);
5942
}

src/Services/WebhookService.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace CSlant\GitHubProject\Services;
44

55
use Github\Client;
6+
use Illuminate\Http\JsonResponse;
67
use Symfony\Component\HttpFoundation\Request;
78

89
class WebhookService
@@ -26,6 +27,33 @@ public function eventRequestApproved(Request $request): bool
2627
return $this->eventApproved((string) $event);
2728
}
2829

30+
/**
31+
* @param array<string, mixed> $payload
32+
*
33+
* @return JsonResponse|null
34+
*/
35+
public function validatePayload(array $payload): ?JsonResponse
36+
{
37+
if (!isset($payload['action'])) {
38+
return response()->json(
39+
['message' => __('github-project::github-project.error.event.action_not_found')],
40+
400
41+
);
42+
}
43+
44+
$nodeId = $payload['projects_v2_item']['content_node_id'] ?? null;
45+
$fieldData = $payload['changes']['field_value'] ?? null;
46+
47+
if (!$nodeId || !$fieldData) {
48+
return response()->json(
49+
['message' => __('github-project::github-project.error.event.missing_fields')],
50+
400
51+
);
52+
}
53+
54+
return null;
55+
}
56+
2957
/**
3058
* @param string $contentNodeId
3159
* @param string $message

0 commit comments

Comments
 (0)