fix(webhooks): show GitLab webhook setup for GitLab App sources

Closes #11602
This commit is contained in:
Andras Bacsai
2026-10-16 21:21:22 +02:00
parent 420692c020
commit 47535d2cab
3 changed files with 126 additions and 0 deletions
+13
View File
@@ -2,11 +2,14 @@
namespace App\Livewire\Project\Shared;
use App\Models\Application;
use App\Models\GitlabApp;
use App\Rules\ManualWebhookSecret;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Support\Str;
use Illuminate\Validation\ValidationException;
use Livewire\Component;
use Spatie\Url\Url;
// Refactored ✅
class Webhooks extends Component
@@ -25,6 +28,12 @@ class Webhooks extends Component
public ?string $giteaManualWebhook;
/**
* Source-level endpoint for GitLab App sources. GitLab has no app-level webhooks,
* so users add this URL to each GitLab project themselves.
*/
public ?string $gitlabAppWebhook = null;
public ?string $githubManualWebhookSecret = null;
public ?string $gitlabManualWebhookSecret = null;
@@ -60,6 +69,10 @@ class Webhooks extends Component
$this->gitlabManualWebhook = generateGitManualWebhook($this->resource, 'gitlab');
$this->bitbucketManualWebhook = generateGitManualWebhook($this->resource, 'bitbucket');
$this->giteaManualWebhook = generateGitManualWebhook($this->resource, 'gitea');
if ($this->resource instanceof Application && $this->resource->source instanceof GitlabApp) {
$this->gitlabAppWebhook = Url::fromString(base_url()).'/webhooks/source/gitlab/events';
}
}
public function canViewSecrets(): bool
@@ -99,6 +99,37 @@
</div>
</x-application.settings-section>
</form>
@elseif ($gitlabAppWebhook)
<x-application.settings-section id="manual-git-webhooks-section" title="Manual Git webhooks"
helper="GitLab Apps cannot register repository webhooks, so add this webhook to the GitLab project yourself." flush>
<x-slot:actions>
@can('view', $resource->source)
<a class="button" href="{{ route('source.gitlab.show', ['gitlab_app_uuid' => $resource->source->uuid]) }}"
{{ wireNavigate() }}>
Source settings
</a>
@endcan
@if (filled($resource?->gitWebhook))
<a class="button" href="{{ $resource->gitWebhook }}" target="_blank"
rel="noopener noreferrer">
Repository settings
<x-external-link />
</a>
@endif
</x-slot:actions>
<section class="px-4 py-5">
<div class="mb-4">
<h4 class="text-sm font-semibold text-black dark:text-fg">GitLab</h4>
<p class="mt-1 text-[13px] leading-5 text-neutral-500 dark:text-fg-dim">
Add this URL in the GitLab project under Settings → Webhooks. Set the Secret token to
the Webhook secret token from the source settings, and enable Push events and Merge
request events.
</p>
</div>
<x-forms.copy-input label="Webhook URL" :text="$gitlabAppWebhook" />
</section>
</x-application.settings-section>
@else
<x-application.settings-section id="manual-git-webhooks-section" title="Manual Git webhooks"
helper="Manual repository webhooks are only required when a repository is not connected through an official Git App.">
@@ -0,0 +1,82 @@
<?php
use App\Livewire\Project\Shared\Webhooks;
use App\Models\Application;
use App\Models\Environment;
use App\Models\GithubApp;
use App\Models\GitlabApp;
use App\Models\InstanceSettings;
use App\Models\Project;
use App\Models\Server;
use App\Models\StandaloneDocker;
use App\Models\Team;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Livewire;
uses(RefreshDatabase::class);
beforeEach(function () {
Server::flushIdentityMap();
InstanceSettings::forceCreate(['id' => 0]);
$this->team = Team::factory()->create();
$this->admin = User::factory()->create();
$this->team->members()->attach($this->admin->id, ['role' => 'admin']);
$server = Server::factory()->create(['team_id' => $this->team->id]);
$destination = StandaloneDocker::query()->where('server_id', $server->id)->firstOrFail();
$project = Project::factory()->create(['team_id' => $this->team->id]);
$environment = Environment::factory()->create(['project_id' => $project->id]);
$this->application = Application::factory()->create([
'environment_id' => $environment->id,
'destination_id' => $destination->id,
'destination_type' => $destination->getMorphClass(),
'git_repository' => 'test-org/test-repo',
'git_branch' => 'main',
]);
$this->actingAs($this->admin);
session(['currentTeam' => $this->team]);
});
afterEach(function () {
Server::flushIdentityMap();
});
test('GitLab App sources show the source webhook instead of claiming automatic setup', function () {
$gitlabApp = GitlabApp::create([
'name' => 'gitlab-app-webhook-setup-test',
'api_url' => 'https://gitlab.example.com/api/v4',
'html_url' => 'https://gitlab.example.com',
'webhook_token' => 'test-webhook-token',
'team_id' => $this->team->id,
]);
$this->application->update(['source_id' => $gitlabApp->id, 'source_type' => GitlabApp::class]);
Livewire::test(Webhooks::class, ['resource' => $this->application->fresh()])
->assertSet('gitlabAppWebhook', url('/webhooks/source/gitlab/events'))
->assertSee(url('/webhooks/source/gitlab/events'))
->assertSee(route('source.gitlab.show', ['gitlab_app_uuid' => $gitlabApp->uuid]))
->assertDontSee('test-webhook-token')
->assertDontSee('Managed by your Git App');
});
test('GitHub App sources still show that webhooks are managed by the app', function () {
$githubApp = GithubApp::create([
'name' => 'github-app-webhook-setup-test',
'api_url' => 'https://api.github.com',
'html_url' => 'https://github.com',
'app_id' => 1234567890,
'webhook_secret' => 'test-secret',
'is_public' => false,
'team_id' => $this->team->id,
]);
$this->application->update(['source_id' => $githubApp->id, 'source_type' => GithubApp::class]);
Livewire::test(Webhooks::class, ['resource' => $this->application->fresh()])
->assertSet('gitlabAppWebhook', null)
->assertSee('Managed by your Git App')
->assertDontSee('/webhooks/source/gitlab/events');
});