From c7f15c42fa33320e333003c67b13b283e06e3f98 Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Thu, 7 Mar 2024 12:27:23 +0100 Subject: [PATCH] feat: add deployments api --- app/Http/Controllers/Api/Deploy.php | 23 +++++++++++++++++++++-- app/Http/Controllers/Api/Domains.php | 2 +- app/Http/Controllers/Api/Project.php | 6 +++--- app/Http/Controllers/Api/Resources.php | 2 +- app/Http/Controllers/Api/Server.php | 4 ++-- app/Http/Controllers/Api/Team.php | 10 +++++----- bootstrap/helpers/api.php | 4 ++++ routes/api.php | 1 + 8 files changed, 38 insertions(+), 14 deletions(-) diff --git a/app/Http/Controllers/Api/Deploy.php b/app/Http/Controllers/Api/Deploy.php index 1173bd5d2..70753f328 100644 --- a/app/Http/Controllers/Api/Deploy.php +++ b/app/Http/Controllers/Api/Deploy.php @@ -9,13 +9,32 @@ use App\Actions\Database\StartRedis; use App\Actions\Service\StartService; use App\Http\Controllers\Controller; +use App\Models\ApplicationDeploymentQueue; +use App\Models\Server; use App\Models\Tag; use Illuminate\Http\Request; -use Illuminate\Support\Collection; use Visus\Cuid2\Cuid2; class Deploy extends Controller { + public function deployments(Request $request) { + $teamId = get_team_id_from_token(); + if (is_null($teamId)) { + return invalid_token(); + } + $servers = Server::whereTeamId($teamId)->get(); + $deployments_per_server = ApplicationDeploymentQueue::whereIn("status", ["in_progress", "queued"])->whereIn("server_id", $servers->pluck("id"))->get([ + "id", + "application_id", + "application_name", + "deployment_url", + "pull_request_id", + "server_name", + "server_id", + "status" + ])->sortBy('id')->toArray(); + return response()->json($deployments_per_server, 200); + } public function deploy(Request $request) { $teamId = get_team_id_from_token(); @@ -27,7 +46,7 @@ public function deploy(Request $request) return response()->json(['error' => 'You can only use uuid or tag, not both.', 'docs' => 'https://coolify.io/docs/api/deploy-webhook'], 400); } if (is_null($teamId)) { - return response()->json(['error' => 'Invalid token.', 'docs' => 'https://coolify.io/docs/api/authentication'], 400); + return invalid_token(); } if ($tags) { return $this->by_tags($tags, $teamId, $force); diff --git a/app/Http/Controllers/Api/Domains.php b/app/Http/Controllers/Api/Domains.php index 4b8a026fb..15db5b6ef 100644 --- a/app/Http/Controllers/Api/Domains.php +++ b/app/Http/Controllers/Api/Domains.php @@ -13,7 +13,7 @@ public function domains(Request $request) { $teamId = get_team_id_from_token(); if (is_null($teamId)) { - return response()->json(['error' => 'Invalid token.', 'docs' => 'https://coolify.io/docs/api/authentication'], 400); + return invalid_token(); } $projects = ModelsProject::where('team_id', $teamId)->get(); $domains = collect(); diff --git a/app/Http/Controllers/Api/Project.php b/app/Http/Controllers/Api/Project.php index fa2ba34bb..45d6b4059 100644 --- a/app/Http/Controllers/Api/Project.php +++ b/app/Http/Controllers/Api/Project.php @@ -12,7 +12,7 @@ public function projects(Request $request) { $teamId = get_team_id_from_token(); if (is_null($teamId)) { - return response()->json(['error' => 'Invalid token.', 'docs' => 'https://coolify.io/docs/api/authentication'], 400); + return invalid_token(); } $projects = ModelsProject::whereTeamId($teamId)->select('id', 'name', 'uuid')->get(); return response()->json($projects); @@ -21,7 +21,7 @@ public function project_by_uuid(Request $request) { $teamId = get_team_id_from_token(); if (is_null($teamId)) { - return response()->json(['error' => 'Invalid token.', 'docs' => 'https://coolify.io/docs/api/authentication'], 400); + return invalid_token(); } $project = ModelsProject::whereTeamId($teamId)->whereUuid(request()->uuid)->first()->load(['environments']); return response()->json($project); @@ -30,7 +30,7 @@ public function environment_details(Request $request) { $teamId = get_team_id_from_token(); if (is_null($teamId)) { - return response()->json(['error' => 'Invalid token.', 'docs' => 'https://coolify.io/docs/api/authentication'], 400); + return invalid_token(); } $project = ModelsProject::whereTeamId($teamId)->whereUuid(request()->uuid)->first(); $environment = $project->environments()->whereName(request()->environment_name)->first()->load(['applications', 'postgresqls', 'redis', 'mongodbs', 'mysqls', 'mariadbs', 'services']); diff --git a/app/Http/Controllers/Api/Resources.php b/app/Http/Controllers/Api/Resources.php index 78ea854c7..d3d313f3a 100644 --- a/app/Http/Controllers/Api/Resources.php +++ b/app/Http/Controllers/Api/Resources.php @@ -12,7 +12,7 @@ public function resources(Request $request) { $teamId = get_team_id_from_token(); if (is_null($teamId)) { - return response()->json(['error' => 'Invalid token.', 'docs' => 'https://coolify.io/docs/api/authentication'], 400); + return invalid_token(); } $projects = Project::where('team_id', $teamId)->get(); $resources = collect(); diff --git a/app/Http/Controllers/Api/Server.php b/app/Http/Controllers/Api/Server.php index 2cfec183e..bb5ef255b 100644 --- a/app/Http/Controllers/Api/Server.php +++ b/app/Http/Controllers/Api/Server.php @@ -12,7 +12,7 @@ public function servers(Request $request) { $teamId = get_team_id_from_token(); if (is_null($teamId)) { - return response()->json(['error' => 'Invalid token.', 'docs' => 'https://coolify.io/docs/api/authentication'], 400); + return invalid_token(); } $servers = ModelsServer::whereTeamId($teamId)->select('id', 'name', 'uuid', 'ip', 'user', 'port')->get()->load(['settings'])->map(function ($server) { $server['is_reachable'] = $server->settings->is_reachable; @@ -26,7 +26,7 @@ public function server_by_uuid(Request $request) $with_resources = $request->query('resources'); $teamId = get_team_id_from_token(); if (is_null($teamId)) { - return response()->json(['error' => 'Invalid token.', 'docs' => 'https://coolify.io/docs/api/authentication'], 400); + return invalid_token(); } $server = ModelsServer::whereTeamId($teamId)->whereUuid(request()->uuid)->first(); if (is_null($server)) { diff --git a/app/Http/Controllers/Api/Team.php b/app/Http/Controllers/Api/Team.php index d99ea8ea7..862d2e185 100644 --- a/app/Http/Controllers/Api/Team.php +++ b/app/Http/Controllers/Api/Team.php @@ -11,7 +11,7 @@ public function teams(Request $request) { $teamId = get_team_id_from_token(); if (is_null($teamId)) { - return response()->json(['error' => 'Invalid token.', 'docs' => 'https://coolify.io/docs/api/authentication'], 400); + return invalid_token(); } $teams = auth()->user()->teams; return response()->json($teams); @@ -21,7 +21,7 @@ public function team_by_id(Request $request) $id = $request->id; $teamId = get_team_id_from_token(); if (is_null($teamId)) { - return response()->json(['error' => 'Invalid token.', 'docs' => 'https://coolify.io/docs/api/authentication'], 400); + return invalid_token(); } $teams = auth()->user()->teams; $team = $teams->where('id', $id)->first(); @@ -35,7 +35,7 @@ public function members_by_id(Request $request) $id = $request->id; $teamId = get_team_id_from_token(); if (is_null($teamId)) { - return response()->json(['error' => 'Invalid token.', 'docs' => 'https://coolify.io/docs/api/authentication'], 400); + return invalid_token(); } $teams = auth()->user()->teams; $team = $teams->where('id', $id)->first(); @@ -48,7 +48,7 @@ public function current_team(Request $request) { $teamId = get_team_id_from_token(); if (is_null($teamId)) { - return response()->json(['error' => 'Invalid token.', 'docs' => 'https://coolify.io/docs/api/authentication'], 400); + return invalid_token(); } $team = auth()->user()->currentTeam(); return response()->json($team); @@ -56,7 +56,7 @@ public function current_team(Request $request) public function current_team_members(Request $request) { $teamId = get_team_id_from_token(); if (is_null($teamId)) { - return response()->json(['error' => 'Invalid token.', 'docs' => 'https://coolify.io/docs/api/authentication'], 400); + return invalid_token(); } $team = auth()->user()->currentTeam(); return response()->json($team->members); diff --git a/bootstrap/helpers/api.php b/bootstrap/helpers/api.php index 94e9242cb..4fcdbac4f 100644 --- a/bootstrap/helpers/api.php +++ b/bootstrap/helpers/api.php @@ -5,3 +5,7 @@ function get_team_id_from_token() $token = auth()->user()->currentAccessToken(); return data_get($token, 'team_id'); } +function invalid_token() +{ + return response()->json(['error' => 'Invalid token.', 'docs' => 'https://coolify.io/docs/api/authentication'], 400); +} diff --git a/routes/api.php b/routes/api.php index 25f4f948f..c0ea836a6 100644 --- a/routes/api.php +++ b/routes/api.php @@ -32,6 +32,7 @@ return response(config('version')); }); Route::get('/deploy', [Deploy::class, 'deploy']); + Route::get('/deployments', [Deploy::class, 'deployments']); Route::get('/servers', [Server::class, 'servers']); Route::get('/server/{uuid}', [Server::class, 'server_by_uuid']);