From 6d1d699595a96f6771358af6389070fc5e646313 Mon Sep 17 00:00:00 2001 From: Andras Bacsai <5845193+andrasbacsai@users.noreply.github.com> Date: Wed, 29 Apr 2026 10:59:32 +0200 Subject: [PATCH] fix(deployments): resolve commit from app git_commit_sha when not explicitly set Change `commit` param from `string 'HEAD'` default to `?string null`, then resolve priority: explicit param > app `git_commit_sha` > `'HEAD'` fallback. Add feature tests covering all four resolution paths. --- bootstrap/helpers/applications.php | 3 +- .../QueueApplicationDeploymentCommitTest.php | 107 ++++++++++++++++++ 2 files changed, 109 insertions(+), 1 deletion(-) create mode 100644 tests/Feature/QueueApplicationDeploymentCommitTest.php diff --git a/bootstrap/helpers/applications.php b/bootstrap/helpers/applications.php index 48e0a8c78..4707b0a07 100644 --- a/bootstrap/helpers/applications.php +++ b/bootstrap/helpers/applications.php @@ -12,8 +12,9 @@ use Spatie\Url\Url; use Visus\Cuid2\Cuid2; -function queue_application_deployment(Application $application, string $deployment_uuid, ?int $pull_request_id = 0, string $commit = 'HEAD', bool $force_rebuild = false, bool $is_webhook = false, bool $is_api = false, bool $restart_only = false, ?string $git_type = null, bool $no_questions_asked = false, ?Server $server = null, ?StandaloneDocker $destination = null, bool $only_this_server = false, bool $rollback = false, ?string $docker_registry_image_tag = null) +function queue_application_deployment(Application $application, string $deployment_uuid, ?int $pull_request_id = 0, ?string $commit = null, bool $force_rebuild = false, bool $is_webhook = false, bool $is_api = false, bool $restart_only = false, ?string $git_type = null, bool $no_questions_asked = false, ?Server $server = null, ?StandaloneDocker $destination = null, bool $only_this_server = false, bool $rollback = false, ?string $docker_registry_image_tag = null) { + $commit = $commit ?: ($application->git_commit_sha ?: 'HEAD'); $application_id = $application->id; $deployment_link = Url::fromString($application->link()."/deployment/{$deployment_uuid}"); $deployment_url = $deployment_link->getPath(); diff --git a/tests/Feature/QueueApplicationDeploymentCommitTest.php b/tests/Feature/QueueApplicationDeploymentCommitTest.php new file mode 100644 index 000000000..ac6be5c9e --- /dev/null +++ b/tests/Feature/QueueApplicationDeploymentCommitTest.php @@ -0,0 +1,107 @@ +team = Team::factory()->create(); + $this->server = Server::factory()->create(['team_id' => $this->team->id]); + $this->destination = StandaloneDocker::factory()->create([ + 'server_id' => $this->server->id, + 'network' => 'test-network-'.fake()->unique()->word(), + ]); + $this->project = Project::factory()->create(['team_id' => $this->team->id]); + $this->environment = Environment::factory()->create(['project_id' => $this->project->id]); +}); + +function makeApplication(int $environmentId, int $destinationId, ?string $gitCommitSha): Application +{ + $attributes = [ + 'environment_id' => $environmentId, + 'destination_id' => $destinationId, + 'destination_type' => StandaloneDocker::class, + ]; + + if ($gitCommitSha !== null) { + $attributes['git_commit_sha'] = $gitCommitSha; + } + + return Application::factory()->create($attributes); +} + +describe('queue_application_deployment commit resolution', function () { + test('uses application git_commit_sha when commit parameter omitted', function () { + $pinnedSha = 'abc123def456abc123def456abc123def456abc1'; + $application = makeApplication($this->environment->id, $this->destination->id, $pinnedSha); + + $result = queue_application_deployment( + application: $application, + deployment_uuid: 'test-deploy-uuid-1', + ); + + expect($result['status'])->toBe('queued'); + + $deployment = ApplicationDeploymentQueue::where('deployment_uuid', 'test-deploy-uuid-1')->first(); + expect($deployment)->not->toBeNull(); + expect($deployment->commit)->toBe($pinnedSha); + }); + + test('falls back to HEAD when both commit parameter and git_commit_sha are unset', function () { + $application = makeApplication($this->environment->id, $this->destination->id, 'HEAD'); + + $result = queue_application_deployment( + application: $application, + deployment_uuid: 'test-deploy-uuid-2', + ); + + expect($result['status'])->toBe('queued'); + + $deployment = ApplicationDeploymentQueue::where('deployment_uuid', 'test-deploy-uuid-2')->first(); + expect($deployment->commit)->toBe('HEAD'); + }); + + test('explicit commit parameter overrides application git_commit_sha', function () { + $pinnedSha = 'abc123def456abc123def456abc123def456abc1'; + $webhookSha = '111222333444555666777888999000aaabbbccc1'; + $application = makeApplication($this->environment->id, $this->destination->id, $pinnedSha); + + $result = queue_application_deployment( + application: $application, + deployment_uuid: 'test-deploy-uuid-3', + commit: $webhookSha, + ); + + expect($result['status'])->toBe('queued'); + + $deployment = ApplicationDeploymentQueue::where('deployment_uuid', 'test-deploy-uuid-3')->first(); + expect($deployment->commit)->toBe($webhookSha); + }); + + test('treats empty string commit parameter as unset and uses git_commit_sha', function () { + $pinnedSha = 'abc123def456abc123def456abc123def456abc1'; + $application = makeApplication($this->environment->id, $this->destination->id, $pinnedSha); + + $result = queue_application_deployment( + application: $application, + deployment_uuid: 'test-deploy-uuid-4', + commit: '', + ); + + expect($result['status'])->toBe('queued'); + + $deployment = ApplicationDeploymentQueue::where('deployment_uuid', 'test-deploy-uuid-4')->first(); + expect($deployment->commit)->toBe($pinnedSha); + }); +});