From 49845f3da743cbc528dd006c09a8de51714a18c4 Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Mon, 29 Jan 2024 11:23:04 +0100 Subject: [PATCH] fix: webhooks for multiple apps --- app/Jobs/ApplicationDeploymentJob.php | 4 +- .../application/deployment-navbar.blade.php | 2 +- .../application/deployment/index.blade.php | 33 ++++-- routes/webhooks.php | 105 ++++++++++++++---- 4 files changed, 111 insertions(+), 33 deletions(-) diff --git a/app/Jobs/ApplicationDeploymentJob.php b/app/Jobs/ApplicationDeploymentJob.php index 9e046fd42..4aa4a898a 100644 --- a/app/Jobs/ApplicationDeploymentJob.php +++ b/app/Jobs/ApplicationDeploymentJob.php @@ -1522,7 +1522,9 @@ COPY ./nginx.conf /etc/nginx/conf.d/default.conf"); public function failed(Throwable $exception): void { $this->application_deployment_queue->addLogEntry("Oops something is not okay, are you okay? 😢", 'stderr'); - $this->application_deployment_queue->addLogEntry($exception->getMessage(), 'stderr'); + if (str($exception->getMessage())->isNotEmpty()) { + $this->application_deployment_queue->addLogEntry($exception->getMessage(), 'stderr'); + } if ($this->application->build_pack !== 'dockercompose') { $this->application_deployment_queue->addLogEntry("Deployment failed. Removing the new version of your application.", 'stderr'); diff --git a/resources/views/livewire/project/application/deployment-navbar.blade.php b/resources/views/livewire/project/application/deployment-navbar.blade.php index 936a3fb6e..49f66dac0 100644 --- a/resources/views/livewire/project/application/deployment-navbar.blade.php +++ b/resources/views/livewire/project/application/deployment-navbar.blade.php @@ -7,6 +7,6 @@ @endif @if (data_get($application_deployment_queue, 'status') === 'in_progress' || data_get($application_deployment_queue, 'status') === 'queued') - Cancel Deployment + Cancel Deployment @endif diff --git a/resources/views/livewire/project/application/deployment/index.blade.php b/resources/views/livewire/project/application/deployment/index.blade.php index 818005a12..9bfcda4b2 100644 --- a/resources/views/livewire/project/application/deployment/index.blade.php +++ b/resources/views/livewire/project/application/deployment/index.blade.php @@ -2,7 +2,8 @@

Deployments

{{-- --}} -
+

Deployments ({{ $deployments_count }})

@if ($show_next) @@ -15,7 +16,7 @@ Filter @forelse ($deployments as $deployment) - data_get($deployment, 'status') === 'queued', 'border-warning hover:bg-warning hover:text-black' => @@ -33,20 +34,28 @@ > {{ $deployment->status }}
- @if (data_get($deployment, 'pull_request_id')) -
- > - Pull Request #{{ data_get($deployment, 'pull_request_id') }} + @if (data_get($deployment, 'is_webhook') || data_get($deployment, 'pull_request_id')) +
@if (data_get($deployment, 'is_webhook')) - (Webhook) + Webhook @endif - Webhook (SHA - @if (data_get($deployment, 'commit')) - {{ data_get($deployment, 'commit') }}) - @else - HEAD) + @if (data_get($deployment, 'pull_request_id')) + @if (data_get($deployment, 'is_webhook')) + | + @endif + Pull Request #{{ data_get($deployment, 'pull_request_id') }} + (SHA + @if (data_get($deployment, 'commit')) + {{ data_get($deployment, 'commit') }}) + @else + HEAD) + @endif @endif
+ @else +
+ Manual +
@endif
diff --git a/routes/webhooks.php b/routes/webhooks.php index 3eab89a3a..515b90d84 100644 --- a/routes/webhooks.php +++ b/routes/webhooks.php @@ -397,6 +397,7 @@ Route::post('/source/bitbucket/events/manual', function () { }); Route::post('/source/github/events/manual', function () { try { + $return_payloads = collect([]); $x_github_event = Str::lower(request()->header('X-GitHub-Event')); $x_hub_signature_256 = Str::after(request()->header('X-Hub-Signature-256'), 'sha256='); $content_type = request()->header('Content-Type'); @@ -445,13 +446,22 @@ Route::post('/source/github/events/manual', function () { foreach ($applications as $application) { $webhook_secret = data_get($application, 'manual_webhook_secret_github'); $hmac = hash_hmac('sha256', request()->getContent(), $webhook_secret); - if (!hash_equals($x_hub_signature_256, $hmac)) { + if (!hash_equals($x_hub_signature_256, $hmac) && !isDev()) { ray('Invalid signature'); + $return_payloads->push([ + 'application' => $application->name, + 'status' => 'failed', + 'message' => 'Invalid token.', + ]); continue; } $isFunctional = $application->destination->server->isFunctional(); if (!$isFunctional) { - ray('Server is not functional: ' . $application->destination->server->name); + $return_payloads->push([ + 'application' => $application->name, + 'status' => 'failed', + 'message' => 'Server is not functional.', + ]); continue; } if ($x_github_event === 'push') { @@ -462,10 +472,19 @@ Route::post('/source/github/events/manual', function () { application: $application, deployment_uuid: $deployment_uuid, force_rebuild: false, - is_webhook: true + is_webhook: true, ); + $return_payloads->push([ + 'application' => $application->name, + 'status' => 'success', + 'message' => 'Deployment queued.', + ]); } else { - ray('Deployments disabled for ' . $application->name); + $return_payloads->push([ + 'application' => $application->name, + 'status' => 'failed', + 'message' => 'Deployments disabled.', + ]); } } if ($x_github_event === 'pull_request') { @@ -489,11 +508,17 @@ Route::post('/source/github/events/manual', function () { is_webhook: true, git_type: 'github' ); - 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 { - ray('Preview deployments disabled for ' . $application->name); - return response('Nothing to do. Preview Deployments disabled.'); + $return_payloads->push([ + 'application' => $application->name, + 'status' => 'failed', + 'message' => 'Preview deployments disabled.', + ]); } } if ($action === 'closed') { @@ -503,12 +528,23 @@ Route::post('/source/github/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.', + ]); + } else { + $return_payloads->push([ + 'application' => $application->name, + 'status' => 'failed', + 'message' => 'No preview deployment found.', + ]); } - return response('Nothing to do. No Preview Deployment found'); } } } + ray($return_payloads); + return response($return_payloads); } catch (Exception $e) { ray($e->getMessage()); return handleError($e); @@ -516,6 +552,7 @@ Route::post('/source/github/events/manual', function () { }); Route::post('/source/github/events', function () { try { + $return_payloads = collect([]); $id = null; $x_github_delivery = request()->header('X-GitHub-Delivery'); $x_github_event = Str::lower(request()->header('X-GitHub-Event')); @@ -539,7 +576,7 @@ Route::post('/source/github/events', function () { $hmac = hash_hmac('sha256', request()->getContent(), $webhook_secret); if (config('app.env') !== 'local') { if (!hash_equals($x_hub_signature_256, $hmac)) { - return response('not cool'); + return response('Invalid signature.'); } } if ($x_github_event === 'push') { @@ -579,7 +616,11 @@ Route::post('/source/github/events', function () { foreach ($applications as $application) { $isFunctional = $application->destination->server->isFunctional(); if (!$isFunctional) { - ray('Server is not functional: ' . $application->destination->server->name); + $return_payloads->push([ + 'application' => $application->name, + 'status' => 'failed', + 'message' => 'Server is not functional.', + ]); continue; } if ($x_github_event === 'push') { @@ -592,8 +633,17 @@ Route::post('/source/github/events', function () { force_rebuild: false, is_webhook: true ); + $return_payloads->push([ + 'application' => $application->name, + 'status' => 'success', + 'message' => 'Deployment queued.', + ]); } else { - ray('Deployments disabled for ' . $application->name); + $return_payloads->push([ + 'application' => $application->name, + 'status' => 'failed', + 'message' => 'Deployments disabled.', + ]); } } if ($x_github_event === 'pull_request') { @@ -617,11 +667,17 @@ Route::post('/source/github/events', function () { is_webhook: true, git_type: 'github' ); - 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 { - ray('Preview deployments disabled for ' . $application->name); - return response('Nothing to do. Preview Deployments disabled.'); + $return_payloads->push([ + 'application' => $application->name, + 'status' => 'failed', + 'message' => 'Preview deployments disabled.', + ]); } } if ($action === 'closed' || $action === 'close') { @@ -632,12 +688,23 @@ Route::post('/source/github/events', 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.', + ]); + } else { + $return_payloads->push([ + 'application' => $application->name, + 'status' => 'failed', + 'message' => 'No preview deployment found.', + ]); } - return response('Nothing to do. No Preview Deployment found'); } } } + ray($return_payloads); + return response($return_payloads); } catch (Exception $e) { ray($e->getMessage()); return handleError($e);