From 468ad7d904361d7db42186e7ac95ec8292055d31 Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Fri, 15 Dec 2023 14:09:14 +0100 Subject: [PATCH 1/3] fix: no action in webhooks --- config/sentry.php | 2 +- config/version.php | 2 +- routes/webhooks.php | 7 +++---- versions.json | 2 +- 4 files changed, 6 insertions(+), 7 deletions(-) diff --git a/config/sentry.php b/config/sentry.php index 1f1748039..23c9af8bb 100644 --- a/config/sentry.php +++ b/config/sentry.php @@ -7,7 +7,7 @@ return [ // The release version of your application // Example with dynamic git hash: trim(exec('git --git-dir ' . base_path('.git') . ' log --pretty="%h" -n1 HEAD')) - 'release' => '4.0.0-beta.163', + 'release' => '4.0.0-beta.164', // When left empty or `null` the Laravel environment will be used 'environment' => config('app.env'), diff --git a/config/version.php b/config/version.php index df172e68a..d9e28384f 100644 --- a/config/version.php +++ b/config/version.php @@ -1,3 +1,3 @@ collect(); $headers = request()->headers->all(); - ray($payload, $headers); $x_gitlab_token = data_get($headers, 'x-gitlab-token.0'); $x_gitlab_event = data_get($payload, 'object_kind'); if ($x_gitlab_event === 'push') { @@ -83,7 +82,6 @@ Route::post('/source/gitlab/events/manual', function () { } if ($x_gitlab_event === 'merge_request') { $action = data_get($payload, 'object_attributes.action'); - ray($action); $branch = data_get($payload, 'object_attributes.source_branch'); $base_branch = data_get($payload, 'object_attributes.target_branch'); $full_name = data_get($payload, 'project.path_with_namespace'); @@ -159,8 +157,7 @@ Route::post('/source/gitlab/events/manual', function () { ray('Preview deployments disabled for ' . $application->name); return response('Nothing to do. Preview Deployments disabled.'); } - } - if ($action === 'closed') { + } else if ($action === 'closed') { $found = ApplicationPreview::where('application_id', $application->id)->where('pull_request_id', $pull_request_id)->first(); if ($found) { $found->delete(); @@ -170,6 +167,8 @@ Route::post('/source/gitlab/events/manual', function () { return response('Preview Deployment closed.'); } return response('Nothing to do. No Preview Deployment found'); + } else { + return response('No action found. Contact us for debugging.', 500); } } } diff --git a/versions.json b/versions.json index 473e6beef..c57316476 100644 --- a/versions.json +++ b/versions.json @@ -4,7 +4,7 @@ "version": "3.12.36" }, "v4": { - "version": "4.0.0-beta.163" + "version": "4.0.0-beta.164" } } } From b3ee6b714499c6486b01e10a756baa393ed186e9 Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Fri, 15 Dec 2023 14:17:53 +0100 Subject: [PATCH 2/3] fix: add debug output to gitlab webhooks --- routes/webhooks.php | 65 +++++++++++++++++++++++++++++++++++++++------ 1 file changed, 57 insertions(+), 8 deletions(-) diff --git a/routes/webhooks.php b/routes/webhooks.php index ea7f277e7..9f6d60c0a 100644 --- a/routes/webhooks.php +++ b/routes/webhooks.php @@ -65,6 +65,7 @@ Route::get('/source/github/install', function () { }); Route::post('/source/gitlab/events/manual', function () { try { + $return_payloads = collect([]); $payload = request()->collect(); $headers = request()->headers->all(); $x_gitlab_token = data_get($headers, 'x-gitlab-token.0'); @@ -76,7 +77,11 @@ Route::post('/source/gitlab/events/manual', function () { $branch = Str::after($branch, 'refs/heads/'); } if (!$branch) { - return response('Nothing to do. No branch found in the request.'); + $return_payloads->push([ + 'status' => 'failed', + 'message' => 'Nothing to do. No branch found in the request.', + ]); + return response($return_payloads); } ray('Manual Webhook GitLab Push Event with branch: ' . $branch); } @@ -88,7 +93,11 @@ Route::post('/source/gitlab/events/manual', function () { $pull_request_id = data_get($payload, 'object_attributes.iid'); $pull_request_html_url = data_get($payload, 'object_attributes.url'); if (!$branch) { - return response('Nothing to do. No branch found in the request.'); + $return_payloads->push([ + 'status' => 'failed', + 'message' => 'Nothing to do. No branch found in the request.', + ]); + return response($return_payloads); } ray('Webhook GitHub Pull Request Event with branch: ' . $branch . ' and base branch: ' . $base_branch . ' and pull request id: ' . $pull_request_id); } @@ -96,23 +105,41 @@ Route::post('/source/gitlab/events/manual', function () { if ($x_gitlab_event === 'push') { $applications = $applications->where('git_branch', $branch)->get(); if ($applications->isEmpty()) { - return response("Nothing to do. No applications found with deploy key set, branch is '$branch' and Git Repository name has $full_name."); + $return_payloads->push([ + 'status' => 'failed', + 'message' => "Nothing to do. No applications found with deploy key set, branch is '$branch' and Git Repository name has $full_name.", + ]); + return response($return_payloads); } } if ($x_gitlab_event === 'merge_request') { $applications = $applications->where('git_branch', $base_branch)->get(); if ($applications->isEmpty()) { - return response("Nothing to do. No applications found with branch '$base_branch'."); + $return_payloads->push([ + 'status' => 'failed', + 'message' => "Nothing to do. No applications found with branch '$base_branch'.", + ]); + return response($return_payloads); } } foreach ($applications as $application) { $webhook_secret = data_get($application, 'manual_webhook_secret_gitlab'); if ($webhook_secret !== $x_gitlab_token) { + $return_payloads->push([ + 'application' => $application->name, + 'status' => 'failed', + 'message' => 'Invalid token.', + ]); ray('Invalid signature'); continue; } $isFunctional = $application->destination->server->isFunctional(); if (!$isFunctional) { + $return_payloads->push([ + 'application' => $application->name, + 'status' => 'failed', + 'message' => 'Server is not functional', + ]); ray('Server is not functional: ' . $application->destination->server->name); continue; } @@ -127,6 +154,11 @@ Route::post('/source/gitlab/events/manual', function () { is_webhook: true ); } else { + $return_payloads->push([ + 'application' => $application->name, + 'status' => 'failed', + 'message' => 'Deployments disabled', + ]); ray('Deployments disabled for ' . $application->name); } } @@ -152,10 +184,18 @@ Route::post('/source/gitlab/events/manual', function () { git_type: 'gitlab' ); ray('Deploying preview for ' . $application->name . ' with branch ' . $branch . ' and base branch ' . $base_branch . ' and pull request id ' . $pull_request_id); - return response('Preview Deployment queued.'); + $return_payloads->push([ + 'application' => $application->name, + 'status' => 'success', + 'message' => 'Preview Deployment queued', + ]); } else { + $return_payloads->push([ + 'application' => $application->name, + 'status' => 'failed', + 'message' => 'Preview deployments disabled', + ]); ray('Preview deployments disabled for ' . $application->name); - return response('Nothing to do. Preview Deployments disabled.'); } } else if ($action === 'closed') { $found = ApplicationPreview::where('application_id', $application->id)->where('pull_request_id', $pull_request_id)->first(); @@ -166,12 +206,21 @@ Route::post('/source/gitlab/events/manual', function () { instant_remote_process(["docker rm -f $container_name"], $application->destination->server); return response('Preview Deployment closed.'); } - return response('Nothing to do. No Preview Deployment found'); + $return_payloads->push([ + 'application' => $application->name, + 'status' => 'failed', + 'message' => 'No Preview Deployment found', + ]); } else { - return response('No action found. Contact us for debugging.', 500); + $return_payloads->push([ + 'application' => $application->name, + 'status' => 'failed', + 'message' => 'No action found. Contact us for debugging.', + ]); } } } + return response($return_payloads); } catch (Exception $e) { ray($e->getMessage()); return handleError($e); From 99d07981cfdac2b459d4470e5ee0352ac69ed075 Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Fri, 15 Dec 2023 14:19:29 +0100 Subject: [PATCH 3/3] fix --- routes/webhooks.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/routes/webhooks.php b/routes/webhooks.php index 9f6d60c0a..ba4c5b1b5 100644 --- a/routes/webhooks.php +++ b/routes/webhooks.php @@ -204,7 +204,12 @@ Route::post('/source/gitlab/events/manual', function () { $container_name = generateApplicationContainerName($application, $pull_request_id); // ray('Stopping container: ' . $container_name); instant_remote_process(["docker rm -f $container_name"], $application->destination->server); - return response('Preview Deployment closed.'); + $return_payloads->push([ + 'application' => $application->name, + 'status' => 'success', + 'message' => 'Preview Deployment closed', + ]); + return response($return_payloads); } $return_payloads->push([ 'application' => $application->name,