Skip to content

Commit 2cb1d3c

Browse files
committed
feat: enhance GenerateCommentAction to improve JSON payload handling and add execution time tracking
1 parent 04d0b1d commit 2cb1d3c

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

src/Actions/GenerateCommentAction.php

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
use Illuminate\Http\Request;
99
use Throwable;
1010

11+
use function Pest\Laravel\json;
12+
1113
class GenerateCommentAction
1214
{
1315
protected WebhookService $webhookService;
@@ -31,9 +33,19 @@ public function __construct(WebhookService $webhookService, GithubService $githu
3133
*/
3234
public function __invoke(Request $request, bool $validate = true): JsonResponse
3335
{
36+
$startTime = microtime(true);
37+
3438
try {
3539
$payload = $request->isJson() ? $request->json()->all() : json_decode($request->getContent(), true);
3640

41+
if (!empty($payload['payload'])) {
42+
$payload = $payload['payload'];
43+
}
44+
45+
if (is_string($payload)) {
46+
$payload = json_decode($payload, true);
47+
}
48+
3749
if ($validate) {
3850
$validationResponse = $this->webhookService->validatePayload($payload);
3951
if ($validationResponse !== null) {
@@ -47,14 +59,20 @@ public function __invoke(Request $request, bool $validate = true): JsonResponse
4759
'success' => true,
4860
'message' => __('github-project::github-project.success.message'),
4961
'comment' => $comment,
50-
'payload' => $payload,
62+
'execution_time' => round((microtime(true) - $startTime) * 1000, 2) . 'ms'
5163
]);
5264
} catch (\Exception $e) {
5365
return response()->json([
5466
'success' => false,
55-
'message' => __('github-project::github-project.error.message', ['error' => $e->getMessage()]),
56-
'comment' => '',
57-
'payload' => $payload,
67+
'message' => 'Error processing request',
68+
'error' => [
69+
'message' => $e->getMessage(),
70+
'type' => get_class($e),
71+
'file' => $e->getFile(),
72+
'line' => $e->getLine(),
73+
'trace' => explode("\n", $e->getTraceAsString())
74+
],
75+
'execution_time' => round((microtime(true) - $startTime) * 1000, 2) . 'ms'
5876
], 500);
5977
}
6078
}

0 commit comments

Comments
 (0)