An open-source, self-hostable PaaS alternative to Vercel, Heroku & Netlify that lets you easily deploy static sites, databases, full-stack applications and 360+ one-click services on your own servers.
PHP coolifydatabasesdeploymentdockerdocker-composeinertiajslaravelmariadbmysqlnextjsnodejsphppostgresredisself-hostedself-hostingserverstatic-sitesveltesvelte5
fix(webhooks): clean up closed Gitea PRs after base branch changes (#12013)
johandenhollanderd0dff5b1297e
diff --git a/app/Http/Controllers/Webhook/Gitea.php b/app/Http/Controllers/Webhook/Gitea.phpindex b3c0b22a48..6868ae8fac 100644--- a/app/Http/Controllers/Webhook/Gitea.php+++ b/app/Http/Controllers/Webhook/Gitea.php@@ -84,7 +84,10 @@ class Gitea extends Controller}}if ($x_gitea_event === 'pull_request') {- $applications = $this->manualWebhookApplications($applications->where('git_branch', $base_branch), $full_name);+ if ($action !== 'closed') {+ $applications->where('git_branch', $base_branch);+ }+ $applications = $this->manualWebhookApplications($applications, $full_name);if ($applications->isEmpty()) {return $this->unauthenticatedManualWebhookResponse($failure_key, $failure_attempt);}diff --git a/tests/Feature/GiteaPullRequestWebhookRoutingTest.php b/tests/Feature/GiteaPullRequestWebhookRoutingTest.phpnew file mode 100644index 0000000000..841b5d577c--- /dev/null+++ b/tests/Feature/GiteaPullRequestWebhookRoutingTest.php@@ -0,0 +1,106 @@+<?php++use App\Actions\Application\CleanupPreviewDeployment;+use App\Models\Application;+use App\Models\ApplicationPreview;+use App\Models\Environment;+use App\Models\Project;+use App\Models\Server;+use App\Models\Team;+use Illuminate\Foundation\Testing\RefreshDatabase;+use Illuminate\Support\Facades\Queue;++uses(RefreshDatabase::class);++function createGiteaPullRequestWebhookApplication(): Application+{+ $team = Team::factory()->create();+ $project = Project::factory()->create(['team_id' => $team->id]);+ $environment = Environment::factory()->create(['project_id' => $project->id]);+ $server = Server::factory()->create(['team_id' => $team->id]);+ $server->settings->update([+ 'is_reachable' => true,+ 'is_usable' => true,+ 'force_disabled' => false,+ ]);+ $destination = $server->standaloneDockers()->firstOrFail();++ return Application::create([+ 'name' => 'gitea-pr-webhook-app',+ 'git_repository' => 'https://gitea.example.com/test-org/test-repo',+ 'git_branch' => 'main',+ 'build_pack' => 'nixpacks',+ 'ports_exposes' => '3000',+ 'environment_id' => $environment->id,+ 'destination_id' => $destination->id,+ 'destination_type' => $destination->getMorphClass(),+ ]);+}++function sendGiteaPullRequestWebhook(Application $application, string $action, string $baseBranch)+{+ $body = json_encode([+ 'action' => $action,+ 'number' => 42,+ 'repository' => [+ 'id' => 987654321,+ 'full_name' => 'test-org/test-repo',+ ],+ 'pull_request' => [+ 'html_url' => 'https://gitea.example.com/test-org/test-repo/pulls/42',+ 'title' => 'Stacked change',+ 'head' => [+ 'ref' => 'feature/child',+ 'sha' => 'abc1234def5678',+ 'repo' => ['id' => 987654321],+ ],+ 'base' => [+ 'ref' => $baseBranch,+ 'repo' => ['id' => 987654321],+ ],+ ],+ ], JSON_THROW_ON_ERROR);++ return test()->call('POST', '/webhooks/source/gitea/events/manual', [], [], [], [+ 'HTTP_X-Gitea-Event' => 'pull_request',+ 'HTTP_X-Hub-Signature-256' => 'sha256='.hash_hmac('sha256', $body, $application->manual_webhook_secret_gitea),+ 'CONTENT_TYPE' => 'application/json',+ ], $body);+}++it('cleans up the preview of a closed pull request after the base branch changes', function () {+ $application = createGiteaPullRequestWebhookApplication();+ $preview = ApplicationPreview::create([+ 'application_id' => $application->id,+ 'pull_request_id' => 42,+ 'pull_request_html_url' => 'https://gitea.example.com/test-org/test-repo/pulls/42',+ 'git_type' => 'gitea',+ ]);++ CleanupPreviewDeployment::shouldRun()+ ->once()+ ->withArgs(fn (Application $cleanedApplication, int $pullRequestId, ApplicationPreview $cleanedPreview): bool => $cleanedApplication->is($application)+ && $pullRequestId === 42+ && $cleanedPreview->is($preview))+ ->andReturn([+ 'cancelled_deployments' => 0,+ 'killed_containers' => 0,+ 'status' => 'success',+ ]);++ $response = sendGiteaPullRequestWebhook($application, 'closed', 'feature/parent');++ $response->assertOk();+ expect($response->getContent())->toContain('Preview deployment closed.');+});++it('continues to filter opened pull requests by base branch', function () {+ Queue::fake();+ $application = createGiteaPullRequestWebhookApplication();+ $application->settings->update(['is_preview_deployments_enabled' => true]);++ $response = sendGiteaPullRequestWebhook($application, 'opened', 'feature/parent');++ $response->assertOk();+ expect(ApplicationPreview::where('application_id', $application->id)->exists())->toBeFalse();+});