mirror of
https://github.com/coollabsio/coolify.git
synced 2026-11-06 13:10:37 -04:00
fix(previews): build the PR commit on manual Bitbucket preview redeploys
Bitbucket Cloud has no pull request refs over git, so a redeploy without a webhook commit checked out the base branch. Reuse the latest known commit of the pull request, or fail the deployment when none is known. Closes #10806 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
1017a5b307
commit
e822db4e71
@@ -3341,6 +3341,7 @@ class ApplicationDeploymentJob implements ShouldBeEncrypted, ShouldQueue
|
||||
}
|
||||
}
|
||||
}
|
||||
$this->resolve_bitbucket_pull_request_commit();
|
||||
$this->generate_git_import_commands();
|
||||
$local_branch = $this->branch;
|
||||
if ($this->pull_request_id !== 0) {
|
||||
@@ -3423,6 +3424,35 @@ class ApplicationDeploymentJob implements ShouldBeEncrypted, ShouldQueue
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Bitbucket Cloud has no pull request refs over git, so a preview deployment without a webhook
|
||||
* commit (a manual redeploy) builds the commit of the latest earlier deployment of the pull request.
|
||||
* Without such a commit the clone checks out the base branch, so the deployment fails instead.
|
||||
*/
|
||||
private function resolve_bitbucket_pull_request_commit(): void
|
||||
{
|
||||
if ($this->pull_request_id === 0 || $this->git_type !== 'bitbucket' || ! $this->shouldResolveBranchHeadCommit()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$commit = ApplicationDeploymentQueue::query()
|
||||
->where('application_id', $this->application->id)
|
||||
->where('pull_request_id', $this->pull_request_id)
|
||||
->whereKeyNot($this->application_deployment_queue->id)
|
||||
->whereNotIn('commit', ['HEAD', ''])
|
||||
->latest('id')
|
||||
->value('commit');
|
||||
|
||||
if (blank($commit)) {
|
||||
throw new DeploymentException("Coolify cannot find the commit of Bitbucket pull request #{$this->pull_request_id}, because Bitbucket Cloud has no pull request refs over git. Push a commit to the pull request to deploy it with the webhook, or deploy the preview with a commit SHA through the API.");
|
||||
}
|
||||
|
||||
$this->commit = $commit;
|
||||
$this->application_deployment_queue->commit = $commit;
|
||||
$this->application_deployment_queue->save();
|
||||
$this->application_deployment_queue->addLogEntry("Using commit {$commit} of the latest deployment of Bitbucket pull request #{$this->pull_request_id}.");
|
||||
}
|
||||
|
||||
/**
|
||||
* The commit message is read from the cloned repository. Deployments that skip the build or fail
|
||||
* before the clone reuse the message stored by an earlier deployment of the same commit.
|
||||
|
||||
@@ -0,0 +1,101 @@
|
||||
<?php
|
||||
|
||||
use App\Exceptions\DeploymentException;
|
||||
use App\Jobs\ApplicationDeploymentJob;
|
||||
use App\Models\Application;
|
||||
use App\Models\ApplicationDeploymentQueue;
|
||||
use App\Models\Environment;
|
||||
use App\Models\Project;
|
||||
use App\Models\Server;
|
||||
use App\Models\StandaloneDocker;
|
||||
use App\Models\Team;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
|
||||
uses(RefreshDatabase::class);
|
||||
|
||||
beforeEach(function () {
|
||||
$team = Team::factory()->create();
|
||||
$this->server = Server::factory()->create(['team_id' => $team->id]);
|
||||
$destination = StandaloneDocker::where('server_id', $this->server->id)->firstOrFail();
|
||||
$project = Project::factory()->create(['team_id' => $team->id]);
|
||||
$environment = $project->environments()->first() ?? Environment::factory()->create(['project_id' => $project->id]);
|
||||
|
||||
$this->application = Application::factory()->create([
|
||||
'environment_id' => $environment->id,
|
||||
'destination_id' => $destination->id,
|
||||
'destination_type' => $destination->getMorphClass(),
|
||||
]);
|
||||
});
|
||||
|
||||
function createPreviewDeployment(Application $application, Server $server, int $pullRequestId, string $commit): ApplicationDeploymentQueue
|
||||
{
|
||||
return ApplicationDeploymentQueue::create([
|
||||
'application_id' => $application->id,
|
||||
'deployment_uuid' => new_public_id(),
|
||||
'server_id' => $server->id,
|
||||
'pull_request_id' => $pullRequestId,
|
||||
'git_type' => 'bitbucket',
|
||||
'commit' => $commit,
|
||||
]);
|
||||
}
|
||||
|
||||
function resolveBitbucketPullRequestCommit(ApplicationDeploymentQueue $deployment, Application $application, string $gitType = 'bitbucket'): ApplicationDeploymentJob
|
||||
{
|
||||
$reflection = new ReflectionClass(ApplicationDeploymentJob::class);
|
||||
$job = $reflection->newInstanceWithoutConstructor();
|
||||
$reflection->getProperty('application')->setValue($job, $application);
|
||||
$reflection->getProperty('application_deployment_queue')->setValue($job, $deployment);
|
||||
$reflection->getProperty('pull_request_id')->setValue($job, $deployment->pull_request_id);
|
||||
$reflection->getProperty('git_type')->setValue($job, $gitType);
|
||||
$reflection->getProperty('commit')->setValue($job, $deployment->commit);
|
||||
|
||||
$reflection->getMethod('resolve_bitbucket_pull_request_commit')->invoke($job);
|
||||
|
||||
return $job;
|
||||
}
|
||||
|
||||
function deploymentJobCommit(ApplicationDeploymentJob $job): string
|
||||
{
|
||||
return (new ReflectionProperty(ApplicationDeploymentJob::class, 'commit'))->getValue($job);
|
||||
}
|
||||
|
||||
test('a manual bitbucket preview redeploy builds the commit of the latest deployment of the pull request', function () {
|
||||
createPreviewDeployment($this->application, $this->server, 7, str_repeat('a', 40));
|
||||
createPreviewDeployment($this->application, $this->server, 7, str_repeat('b', 40));
|
||||
createPreviewDeployment($this->application, $this->server, 8, str_repeat('c', 40));
|
||||
createPreviewDeployment($this->application, $this->server, 7, 'HEAD');
|
||||
$deployment = createPreviewDeployment($this->application, $this->server, 7, 'HEAD');
|
||||
|
||||
$job = resolveBitbucketPullRequestCommit($deployment, $this->application);
|
||||
|
||||
expect(deploymentJobCommit($job))->toBe(str_repeat('b', 40))
|
||||
->and($deployment->fresh()->commit)->toBe(str_repeat('b', 40));
|
||||
});
|
||||
|
||||
test('a manual bitbucket preview redeploy fails when no commit of the pull request is known', function () {
|
||||
createPreviewDeployment($this->application, $this->server, 8, str_repeat('c', 40));
|
||||
$deployment = createPreviewDeployment($this->application, $this->server, 7, 'HEAD');
|
||||
|
||||
expect(fn () => resolveBitbucketPullRequestCommit($deployment, $this->application))
|
||||
->toThrow(DeploymentException::class, 'Bitbucket pull request #7');
|
||||
|
||||
expect($deployment->fresh()->commit)->toBe('HEAD');
|
||||
});
|
||||
|
||||
test('a bitbucket preview deployment keeps the commit from the webhook', function () {
|
||||
createPreviewDeployment($this->application, $this->server, 7, str_repeat('a', 40));
|
||||
$deployment = createPreviewDeployment($this->application, $this->server, 7, str_repeat('d', 40));
|
||||
|
||||
$job = resolveBitbucketPullRequestCommit($deployment, $this->application);
|
||||
|
||||
expect(deploymentJobCommit($job))->toBe(str_repeat('d', 40));
|
||||
});
|
||||
|
||||
test('other git providers resolve the pull request ref over git', function () {
|
||||
createPreviewDeployment($this->application, $this->server, 7, str_repeat('a', 40));
|
||||
$deployment = createPreviewDeployment($this->application, $this->server, 7, 'HEAD');
|
||||
|
||||
$job = resolveBitbucketPullRequestCommit($deployment, $this->application, 'github');
|
||||
|
||||
expect(deploymentJobCommit($job))->toBe('HEAD');
|
||||
});
|
||||
Reference in New Issue
Block a user