Implement generic webhook delivery

This commit is contained in:
Eric Peters
2026-04-24 18:13:50 +02:00
parent 71f07a8f7b
commit a1dc615958
4 changed files with 667 additions and 4 deletions
@@ -5,6 +5,7 @@ namespace Tests\Feature\Notifications;
use App\Actions\Notifications\DeliverNotificationAction;
use App\Actions\ProjectTicketMetadata\InitializeProjectTicketMetadata;
use App\Enums\NotificationChannel;
use App\Enums\NotificationDeliveryStatus;
use App\Enums\NotificationEventType;
use App\Models\BoardColumn;
use App\Models\NotificationAction;
@@ -16,6 +17,7 @@ use App\Models\User;
use Carbon\CarbonImmutable;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Queue;
use Tests\TestCase;
@@ -166,6 +168,86 @@ class TicketStatusChangedNotificationDispatchTest extends TestCase
$this->assertDatabaseCount('ticket_worklog_entries', 0);
}
/**
* Ensure ticket moves stay successful even when webhook delivery fails later on a queued attempt.
*/
public function test_ticket_moves_still_succeed_when_webhook_delivery_fails_later(): void
{
Queue::fake();
Http::preventStrayRequests();
$user = User::factory()->create([
'name' => 'Erica Example',
]);
$project = Project::factory()->create([
'key' => 'CORE',
]);
app(InitializeProjectTicketMetadata::class)->handle($project);
$doingColumn = BoardColumn::factory()->for($project)->create([
'name' => 'Doing',
'position' => 1,
]);
$doingStatus = $this->createColumnStatus($doingColumn);
$backlogStatus = $this->backlogStatus($project);
$this->allowMove($backlogStatus, $doingStatus);
$ticket = $this->createTicket($project, $backlogStatus, 'Retry failing webhook');
$webhookAction = NotificationAction::factory()
->webhook()
->for($project)
->create([
'delivery_url' => 'https://hooks.example.test/kanbangit/retry-failing-webhook',
]);
$this->syncProjectPermissions($project, $user, ['edit', 'view']);
$this->actingAs($user)
->patch(route('projects.tickets.status.update', [$project, $ticket]), [
'redirect_to' => 'backlog',
'ticket_status_id' => $doingStatus->id,
])
->assertRedirect(route('projects.backlog.index', $project))
->assertSessionHas('status', 'CORE-1 moved to Doing.');
$queuedJob = null;
Queue::assertPushed(DeliverNotificationAction::class, function (DeliverNotificationAction $job) use ($webhookAction, &$queuedJob): bool {
if ($job->notificationActionId !== $webhookAction->id) {
return false;
}
$queuedJob = $job;
return true;
});
$this->assertInstanceOf(DeliverNotificationAction::class, $queuedJob);
Http::fake([
'https://hooks.example.test/kanbangit/retry-failing-webhook' => Http::failedConnection(
'Connection timed out while contacting the remote endpoint.',
),
]);
$queuedJob->withFakeQueueInteractions()->handle();
$queuedJob->assertReleased($queuedJob->backoff()[0]);
$this->assertDatabaseHas('tickets', [
'id' => $ticket->id,
'ticket_status_id' => $doingStatus->id,
]);
$this->assertDatabaseHas('notification_delivery_logs', [
'notification_action_id' => $webhookAction->id,
'delivery_status' => NotificationDeliveryStatus::Retryable->value,
'attempt_count' => 1,
'http_status' => null,
]);
}
private function backlogStatus(Project $project): TicketStatus
{
return $project->ticketStatuses()