Skip to content

Commit 29c122b

Browse files
committed
fix(api): return deployment UUID strings directly
1 parent 8ce054c commit 29c122b

6 files changed

Lines changed: 118 additions & 10 deletions

File tree

app/Http/Controllers/Api/ApplicationsController.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3609,15 +3609,15 @@ public function action_deploy(Request $request)
36093609
'team_id' => $teamId,
36103610
'application_uuid' => $application->uuid,
36113611
'application_name' => $application->name,
3612-
'deployment_uuid' => $deployment_uuid->toString(),
3612+
'deployment_uuid' => $deployment_uuid,
36133613
'force_rebuild' => $force,
36143614
'instant_deploy' => $instant_deploy,
36153615
]);
36163616

36173617
return response()->json(
36183618
[
36193619
'message' => 'Deployment request queued.',
3620-
'deployment_uuid' => $deployment_uuid->toString(),
3620+
'deployment_uuid' => $deployment_uuid,
36213621
],
36223622
200
36233623
);
@@ -3803,13 +3803,13 @@ public function action_restart(Request $request)
38033803
'team_id' => $teamId,
38043804
'application_uuid' => $application->uuid,
38053805
'application_name' => $application->name,
3806-
'deployment_uuid' => $deployment_uuid->toString(),
3806+
'deployment_uuid' => $deployment_uuid,
38073807
]);
38083808

38093809
return response()->json(
38103810
[
38113811
'message' => 'Restart request queued.',
3812-
'deployment_uuid' => $deployment_uuid->toString(),
3812+
'deployment_uuid' => $deployment_uuid,
38133813
],
38143814
);
38153815
}

app/Http/Controllers/Api/DeployController.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ private function by_uuids(string $uuid, int $teamId, bool $force = false, int $p
425425
}
426426
['message' => $return_message, 'deployment_uuid' => $deployment_uuid] = $result;
427427
if ($deployment_uuid) {
428-
$deployments->push(['message' => $return_message, 'resource_uuid' => $uuid, 'deployment_uuid' => $deployment_uuid->toString()]);
428+
$deployments->push(['message' => $return_message, 'resource_uuid' => $uuid, 'deployment_uuid' => $deployment_uuid]);
429429
} else {
430430
$deployments->push(['message' => $return_message, 'resource_uuid' => $uuid]);
431431
}
@@ -471,7 +471,7 @@ public function by_tags(string $tags, int $team_id, bool $force = false)
471471
}
472472
['message' => $return_message, 'deployment_uuid' => $deployment_uuid] = $result;
473473
if ($deployment_uuid) {
474-
$deployments->push(['resource_uuid' => $resource->uuid, 'deployment_uuid' => $deployment_uuid->toString()]);
474+
$deployments->push(['resource_uuid' => $resource->uuid, 'deployment_uuid' => $deployment_uuid]);
475475
}
476476
$message = $message->merge($return_message);
477477
}
@@ -529,7 +529,7 @@ public function deploy_resource($resource, bool $force = false, int $pr = 0, ?st
529529
'resource_type' => 'application',
530530
'application_uuid' => $resource->uuid,
531531
'application_name' => $resource->name,
532-
'deployment_uuid' => $deployment_uuid?->toString(),
532+
'deployment_uuid' => $deployment_uuid,
533533
'force_rebuild' => $force,
534534
'pull_request_id' => $pr,
535535
]);

app/Http/Controllers/Webhook/Bitbucket.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ public function manual(Request $request)
162162
'mode' => 'manual',
163163
'application_uuid' => $application->uuid,
164164
'application_name' => $application->name,
165-
'deployment_uuid' => $deployment_uuid->toString(),
165+
'deployment_uuid' => $deployment_uuid,
166166
'commit' => $commit,
167167
'repository' => $full_name ?? null,
168168
]);

app/Http/Controllers/Webhook/Gitea.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ public function manual(Request $request)
148148
'mode' => 'manual',
149149
'application_uuid' => $application->uuid,
150150
'application_name' => $application->name,
151-
'deployment_uuid' => $deployment_uuid->toString(),
151+
'deployment_uuid' => $deployment_uuid,
152152
'commit' => data_get($payload, 'after'),
153153
'repository' => $full_name ?? null,
154154
]);

app/Http/Controllers/Webhook/Gitlab.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ public function manual(Request $request)
190190
'mode' => 'manual',
191191
'application_uuid' => $application->uuid,
192192
'application_name' => $application->name,
193-
'deployment_uuid' => $deployment_uuid->toString(),
193+
'deployment_uuid' => $deployment_uuid,
194194
'commit' => data_get($payload, 'after'),
195195
'repository' => $full_name ?? null,
196196
]);
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<?php
2+
3+
use App\Jobs\ApplicationDeploymentJob;
4+
use App\Models\Application;
5+
use App\Models\ApplicationDeploymentQueue;
6+
use App\Models\Environment;
7+
use App\Models\InstanceSettings;
8+
use App\Models\Project;
9+
use App\Models\Server;
10+
use App\Models\StandaloneDocker;
11+
use App\Models\Team;
12+
use App\Models\User;
13+
use Illuminate\Foundation\Testing\RefreshDatabase;
14+
use Illuminate\Support\Facades\Bus;
15+
use Illuminate\Testing\Fluent\AssertableJson;
16+
17+
uses(RefreshDatabase::class);
18+
19+
beforeEach(function () {
20+
Bus::fake([ApplicationDeploymentJob::class]);
21+
22+
InstanceSettings::unguarded(fn () => InstanceSettings::query()->updateOrCreate(
23+
['id' => 0],
24+
['is_api_enabled' => true],
25+
));
26+
27+
$this->team = Team::factory()->create();
28+
$this->user = User::factory()->create();
29+
$this->team->members()->attach($this->user->id, ['role' => 'owner']);
30+
session(['currentTeam' => $this->team]);
31+
32+
$this->token = $this->user->createToken('deployment-actions-test', ['deploy'])->plainTextToken;
33+
34+
$this->server = Server::factory()->create(['team_id' => $this->team->id]);
35+
$this->destination = StandaloneDocker::query()->where('server_id', $this->server->id)->firstOrFail();
36+
$this->project = Project::factory()->create(['team_id' => $this->team->id]);
37+
$this->environment = Environment::factory()->create(['project_id' => $this->project->id]);
38+
});
39+
40+
function deploymentActionHeaders(string $token): array
41+
{
42+
return [
43+
'Authorization' => 'Bearer '.$token,
44+
'Content-Type' => 'application/json',
45+
];
46+
}
47+
48+
function makeDeploymentActionApplication(Environment $environment, StandaloneDocker $destination): Application
49+
{
50+
return Application::factory()->create([
51+
'environment_id' => $environment->id,
52+
'destination_id' => $destination->id,
53+
'destination_type' => $destination->getMorphClass(),
54+
'git_repository' => 'https://github.com/coollabsio/coolify',
55+
'git_branch' => 'main',
56+
'git_commit_sha' => 'HEAD',
57+
]);
58+
}
59+
60+
test('application start API returns queued deployment uuid as a string', function () {
61+
$application = makeDeploymentActionApplication($this->environment, $this->destination);
62+
63+
$response = $this->withHeaders(deploymentActionHeaders($this->token))
64+
->postJson("/api/v1/applications/{$application->uuid}/start");
65+
66+
$response->assertSuccessful()
67+
->assertJson(fn (AssertableJson $json) => $json
68+
->where('message', 'Deployment request queued.')
69+
->whereType('deployment_uuid', 'string')
70+
);
71+
72+
expect(ApplicationDeploymentQueue::query()->where('deployment_uuid', $response->json('deployment_uuid'))->exists())->toBeTrue();
73+
});
74+
75+
test('application restart API returns queued deployment uuid as a string', function () {
76+
$application = makeDeploymentActionApplication($this->environment, $this->destination);
77+
78+
$response = $this->withHeaders(deploymentActionHeaders($this->token))
79+
->postJson("/api/v1/applications/{$application->uuid}/restart");
80+
81+
$response->assertSuccessful()
82+
->assertJson(fn (AssertableJson $json) => $json
83+
->where('message', 'Restart request queued.')
84+
->whereType('deployment_uuid', 'string')
85+
);
86+
87+
$deployment = ApplicationDeploymentQueue::query()
88+
->where('deployment_uuid', $response->json('deployment_uuid'))
89+
->first();
90+
91+
expect($deployment)->not->toBeNull()
92+
->and($deployment->restart_only)->toBeTruthy();
93+
});
94+
95+
test('deployment uuid strings are not converted as objects in API and webhook controllers', function () {
96+
$files = [
97+
app_path('Http/Controllers/Api/DeployController.php'),
98+
app_path('Http/Controllers/Webhook/Bitbucket.php'),
99+
app_path('Http/Controllers/Webhook/Gitea.php'),
100+
app_path('Http/Controllers/Webhook/Gitlab.php'),
101+
];
102+
103+
foreach ($files as $file) {
104+
expect(file_get_contents($file))
105+
->not->toContain('$deployment_uuid->toString()')
106+
->not->toContain('$deployment_uuid?->toString()');
107+
}
108+
});

0 commit comments

Comments
 (0)