From 515d40174685901d986d027a673fcaf80bb24bc1 Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Thu, 7 Mar 2024 12:22:18 +0100 Subject: [PATCH] feat: add deployment details to deploy endpoint --- app/Http/Controllers/Api/Deploy.php | 35 ++++++++++++++++++++++------- routes/api.php | 3 +++ 2 files changed, 30 insertions(+), 8 deletions(-) diff --git a/app/Http/Controllers/Api/Deploy.php b/app/Http/Controllers/Api/Deploy.php index 21da51d66..1173bd5d2 100644 --- a/app/Http/Controllers/Api/Deploy.php +++ b/app/Http/Controllers/Api/Deploy.php @@ -45,15 +45,24 @@ private function by_uuids(string $uuid, int $teamId, bool $force = false) return response()->json(['error' => 'No UUIDs provided.', 'docs' => 'https://coolify.io/docs/api/deploy-webhook'], 400); } $message = collect([]); + $deployments = collect(); + $payload = collect(); foreach ($uuids as $uuid) { $resource = getResourceByUuid($uuid, $teamId); if ($resource) { - $return_message = $this->deploy_resource($resource, $force); + ['message' => $return_message, 'deployment_uuid' => $deployment_uuid] = $this->deploy_resource($resource, $force); + if ($deployment_uuid) { + $deployments->push(['resource_uuid' => $uuid, 'deployment_uuid' => $deployment_uuid->toString()]); + } $message = $message->merge($return_message); } } if ($message->count() > 0) { - return response()->json(['message' => $message->toArray()], 200); + $payload->put('message', $message->toArray()); + if ($deployments->count() > 0) { + $payload->put('details', $deployments->toArray()); + } + return response()->json($payload->toArray(), 200); } return response()->json(['error' => "No resources found.", 'docs' => 'https://coolify.io/docs/api/deploy-webhook'], 404); } @@ -66,6 +75,8 @@ public function by_tags(string $tags, int $team_id, bool $force = false) return response()->json(['error' => 'No TAGs provided.', 'docs' => 'https://coolify.io/docs/api/deploy-webhook'], 400); } $message = collect([]); + $deployments = collect(); + $payload = collect(); foreach ($tags as $tag) { $found_tag = Tag::where(['name' => $tag, 'team_id' => $team_id])->first(); if (!$found_tag) { @@ -79,21 +90,28 @@ public function by_tags(string $tags, int $team_id, bool $force = false) continue; } foreach ($applications as $resource) { - $return_message = $this->deploy_resource($resource, $force); + ['message' => $return_message, 'deployment_uuid' => $deployment_uuid] = $this->deploy_resource($resource, $force); + if ($deployment_uuid) { + $deployments->push(['resource_uuid' => $resource->uuid, 'deployment_uuid' => $deployment_uuid->toString()]); + } $message = $message->merge($return_message); } foreach ($services as $resource) { - $return_message = $this->deploy_resource($resource, $force); + ['message' => $return_message] = $this->deploy_resource($resource, $force); $message = $message->merge($return_message); } } if ($message->count() > 0) { - return response()->json(['message' => $message->toArray()], 200); + $payload->put('message', $message->toArray()); + if ($deployments->count() > 0) { + $payload->put('details', $deployments->toArray()); + } + return response()->json($payload->toArray(), 200); } return response()->json(['error' => "No resources found.", 'docs' => 'https://coolify.io/docs/api/deploy-webhook'], 404); } - public function deploy_resource($resource, bool $force = false): Collection + public function deploy_resource($resource, bool $force = false): array { $message = collect([]); if (gettype($resource) !== 'object') { @@ -101,9 +119,10 @@ public function deploy_resource($resource, bool $force = false): Collection } $type = $resource?->getMorphClass(); if ($type === 'App\Models\Application') { + $deployment_uuid = new Cuid2(7); queue_application_deployment( application: $resource, - deployment_uuid: new Cuid2(7), + deployment_uuid: $deployment_uuid, force_rebuild: $force, ); $message->push("Application {$resource->name} deployment queued."); @@ -156,6 +175,6 @@ public function deploy_resource($resource, bool $force = false): Collection StartService::run($resource); $message->push("Service {$resource->name} started. It could take a while, be patient."); } - return $message; + return ['message' => $message, 'deployment_uuid' => $deployment_uuid]; } } diff --git a/routes/api.php b/routes/api.php index b27d1fb28..25f4f948f 100644 --- a/routes/api.php +++ b/routes/api.php @@ -32,10 +32,13 @@ return response(config('version')); }); Route::get('/deploy', [Deploy::class, 'deploy']); + Route::get('/servers', [Server::class, 'servers']); Route::get('/server/{uuid}', [Server::class, 'server_by_uuid']); + Route::get('/resources', [Resources::class, 'resources']); Route::get('/domains', [Domains::class, 'domains']); + Route::get('/teams', [Team::class, 'teams']); Route::get('/team/current', [Team::class, 'current_team']); Route::get('/team/current/members', [Team::class, 'current_team_members']);