From 1935403053949da5297818386d3efb4e83e89638 Mon Sep 17 00:00:00 2001 From: Tjeerd Smid Date: Mon, 23 Feb 2026 16:32:54 +0100 Subject: [PATCH] fix: application rollback uses correct commit sha - setGitImportSettings() now accepts optional $commit parameter - Uses passed commit over application's git_commit_sha (typically HEAD) - Fixes rollback deploying latest instead of selected commit - Also fixes shallow clone "bad object" error on rollback Fixes #8445 --- app/Models/Application.php | 18 ++++---- tests/Feature/ApplicationRollbackTest.php | 55 +++++++++++++++++++++++ 2 files changed, 65 insertions(+), 8 deletions(-) create mode 100644 tests/Feature/ApplicationRollbackTest.php diff --git a/app/Models/Application.php b/app/Models/Application.php index 28ef79078..e31cf74e4 100644 --- a/app/Models/Application.php +++ b/app/Models/Application.php @@ -1085,19 +1085,21 @@ public function dirOnServer() return application_configuration_dir()."/{$this->uuid}"; } - public function setGitImportSettings(string $deployment_uuid, string $git_clone_command, bool $public = false) + public function setGitImportSettings(string $deployment_uuid, string $git_clone_command, bool $public = false, ?string $commit = null) { $baseDir = $this->generateBaseDir($deployment_uuid); $escapedBaseDir = escapeshellarg($baseDir); $isShallowCloneEnabled = $this->settings?->is_git_shallow_clone_enabled ?? false; - if ($this->git_commit_sha !== 'HEAD') { + $commitToUse = $commit ?? $this->git_commit_sha; + + if ($commitToUse !== 'HEAD') { // If shallow clone is enabled and we need a specific commit, // we need to fetch that specific commit with depth=1 if ($isShallowCloneEnabled) { - $git_clone_command = "{$git_clone_command} && cd {$escapedBaseDir} && GIT_SSH_COMMAND=\"ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null\" git fetch --depth=1 origin {$this->git_commit_sha} && git -c advice.detachedHead=false checkout {$this->git_commit_sha} >/dev/null 2>&1"; + $git_clone_command = "{$git_clone_command} && cd {$escapedBaseDir} && GIT_SSH_COMMAND=\"ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null\" git fetch --depth=1 origin {$commitToUse} && git -c advice.detachedHead=false checkout {$commitToUse} >/dev/null 2>&1"; } else { - $git_clone_command = "{$git_clone_command} && cd {$escapedBaseDir} && GIT_SSH_COMMAND=\"ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null\" git -c advice.detachedHead=false checkout {$this->git_commit_sha} >/dev/null 2>&1"; + $git_clone_command = "{$git_clone_command} && cd {$escapedBaseDir} && GIT_SSH_COMMAND=\"ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null\" git -c advice.detachedHead=false checkout {$commitToUse} >/dev/null 2>&1"; } } if ($this->settings->is_git_submodules_enabled) { @@ -1285,7 +1287,7 @@ public function generateGitImportCommands(string $deployment_uuid, int $pull_req $escapedRepoUrl = escapeshellarg("{$this->source->html_url}/{$customRepository}"); $git_clone_command = "{$git_clone_command} {$escapedRepoUrl} {$escapedBaseDir}"; if (! $only_checkout) { - $git_clone_command = $this->setGitImportSettings($deployment_uuid, $git_clone_command, public: true); + $git_clone_command = $this->setGitImportSettings($deployment_uuid, $git_clone_command, public: true, commit: $commit); } if ($exec_in_docker) { $commands->push(executeInDocker($deployment_uuid, $git_clone_command)); @@ -1306,7 +1308,7 @@ public function generateGitImportCommands(string $deployment_uuid, int $pull_req $fullRepoUrl = $repoUrl; } if (! $only_checkout) { - $git_clone_command = $this->setGitImportSettings($deployment_uuid, $git_clone_command, public: false); + $git_clone_command = $this->setGitImportSettings($deployment_uuid, $git_clone_command, public: false, commit: $commit); } if ($exec_in_docker) { $commands->push(executeInDocker($deployment_uuid, $git_clone_command)); @@ -1345,7 +1347,7 @@ public function generateGitImportCommands(string $deployment_uuid, int $pull_req if ($only_checkout) { $git_clone_command = $git_clone_command_base; } else { - $git_clone_command = $this->setGitImportSettings($deployment_uuid, $git_clone_command_base); + $git_clone_command = $this->setGitImportSettings($deployment_uuid, $git_clone_command_base, commit: $commit); } if ($exec_in_docker) { $commands = collect([ @@ -1403,7 +1405,7 @@ public function generateGitImportCommands(string $deployment_uuid, int $pull_req $fullRepoUrl = $customRepository; $escapedCustomRepository = escapeshellarg($customRepository); $git_clone_command = "{$git_clone_command} {$escapedCustomRepository} {$escapedBaseDir}"; - $git_clone_command = $this->setGitImportSettings($deployment_uuid, $git_clone_command, public: true); + $git_clone_command = $this->setGitImportSettings($deployment_uuid, $git_clone_command, public: true, commit: $commit); if ($pull_request_id !== 0) { if ($git_type === 'gitlab') { diff --git a/tests/Feature/ApplicationRollbackTest.php b/tests/Feature/ApplicationRollbackTest.php new file mode 100644 index 000000000..dfb8da010 --- /dev/null +++ b/tests/Feature/ApplicationRollbackTest.php @@ -0,0 +1,55 @@ +create(); + $project = Project::create([ + 'team_id' => $team->id, + 'name' => 'Test Project', + 'uuid' => (string) str()->uuid(), + ]); + $environment = Environment::create([ + 'project_id' => $project->id, + 'name' => 'rollback-test-env', + 'uuid' => (string) str()->uuid(), + ]); + $server = Server::factory()->create(['team_id' => $team->id]); + + // Create application with git_commit_sha = 'HEAD' (default - use latest) + $application = Application::factory()->create([ + 'environment_id' => $environment->id, + 'destination_id' => $server->id, + 'git_commit_sha' => 'HEAD', + ]); + + // Create application settings + ApplicationSetting::create([ + 'application_id' => $application->id, + 'is_git_shallow_clone_enabled' => false, + ]); + + // The rollback commit SHA we want to deploy + $rollbackCommit = 'abc123def456'; + + // This should use the passed commit, not the application's git_commit_sha + $result = $application->setGitImportSettings( + deployment_uuid: 'test-uuid', + git_clone_command: 'git clone', + public: true, + commit: $rollbackCommit + ); + + // Assert: The command should checkout the ROLLBACK commit + expect($result)->toContain($rollbackCommit); + }); +});