Add notification filters

This commit is contained in:
2026-04-24 20:02:22 +00:00
parent 5f74ddd964
commit c54df44ebe
24 changed files with 1288 additions and 7 deletions
@@ -34,6 +34,13 @@ class ProjectNotificationActionManagementTest extends TestCase
'event_type' => NotificationEventType::TicketStatusChanged,
'channel' => NotificationChannel::Discord,
'channel_configuration' => ['mentions' => ['ops']],
'filters' => [
[
'attribute' => 'status.to.label',
'operator' => 'equals',
'value' => 'Doing',
],
],
'message_template' => '$ticket_key changed to $status_to',
'delivery_url' => 'https://discord.com/api/webhooks/123456/discord-alerts',
]);
@@ -58,6 +65,13 @@ class ProjectNotificationActionManagementTest extends TestCase
$this->assertCount(2, $listedActions);
$this->assertSame(['Discord alerts', 'Webhook alerts'], $listedActions->pluck('name')->all());
$this->assertSame(['mentions' => ['ops']], $listedActions->first()->channel_configuration);
$this->assertSame([
[
'attribute' => 'status.to.label',
'operator' => 'equals',
'value' => 'Doing',
],
], $listedActions->first()->filters);
}
/**
@@ -74,6 +88,13 @@ class ProjectNotificationActionManagementTest extends TestCase
$updatedAction = $updateAction->handle($project, $notificationAction, [
'enabled' => false,
'filters' => [
[
'attribute' => 'ticket.key',
'operator' => 'in',
'value' => 'CORE-1, CORE-2',
],
],
'message_template' => '$ticket_key moved from $status_from to $status_to',
'channel_configuration' => ['mentions' => ['support']],
'delivery_url' => '',
@@ -85,6 +106,13 @@ class ProjectNotificationActionManagementTest extends TestCase
$updatedAction->message_template,
);
$this->assertSame(['mentions' => ['support']], $updatedAction->channel_configuration);
$this->assertSame([
[
'attribute' => 'ticket.key',
'operator' => 'in',
'value' => ['CORE-1', 'CORE-2'],
],
], $updatedAction->filters);
$this->assertSame(
'https://discord.com/api/webhooks/123456/original-secret',
$updatedAction->delivery_url,
@@ -71,6 +71,34 @@ class TicketCreatedNotificationDispatchTest extends TestCase
'name' => 'Status changed webhook',
'event_type' => NotificationEventType::TicketStatusChanged,
]);
$filterNotExistsAction = NotificationAction::factory()
->webhook()
->for($project)
->create([
'name' => 'Ticket create without origin status',
'event_type' => NotificationEventType::TicketCreate,
'filters' => [
[
'attribute' => 'status.from.id',
'operator' => 'not_exists',
'value' => null,
],
],
]);
$filterExistsAction = NotificationAction::factory()
->webhook()
->for($project)
->create([
'name' => 'Ticket create with origin status',
'event_type' => NotificationEventType::TicketCreate,
'filters' => [
[
'attribute' => 'status.from.id',
'operator' => 'exists',
'value' => null,
],
],
]);
$foreignProjectAction = NotificationAction::factory()
->for($otherProject)
->create([
@@ -100,7 +128,7 @@ class TicketCreatedNotificationDispatchTest extends TestCase
->assertRedirect(route('projects.tickets.show', [$project, $ticket]))
->assertSessionHas('status', 'CORE-1 was created.');
Queue::assertPushed(DeliverNotificationAction::class, 2);
Queue::assertPushed(DeliverNotificationAction::class, 3);
Queue::assertPushed(DeliverNotificationAction::class, function (DeliverNotificationAction $job) use ($webhookAction, $project, $ticket, $user, $backlogStatus): bool {
return $job->notificationActionId === $webhookAction->id
&& $job->payload->projectId === $project->id
@@ -122,11 +150,15 @@ class TicketCreatedNotificationDispatchTest extends TestCase
Queue::assertPushed(DeliverNotificationAction::class, fn (DeliverNotificationAction $job): bool => $job->notificationActionId === $discordAction->id
&& $job->payload->eventType === NotificationEventType::TicketCreate
&& $job->notificationDeliveryLogId !== null);
Queue::assertPushed(DeliverNotificationAction::class, fn (DeliverNotificationAction $job): bool => $job->notificationActionId === $filterNotExistsAction->id
&& $job->payload->eventType === NotificationEventType::TicketCreate
&& $job->notificationDeliveryLogId !== null);
Queue::assertNotPushed(DeliverNotificationAction::class, fn (DeliverNotificationAction $job): bool => $job->notificationActionId === $disabledAction->id);
Queue::assertNotPushed(DeliverNotificationAction::class, fn (DeliverNotificationAction $job): bool => $job->notificationActionId === $statusChangedAction->id);
Queue::assertNotPushed(DeliverNotificationAction::class, fn (DeliverNotificationAction $job): bool => $job->notificationActionId === $filterExistsAction->id);
Queue::assertNotPushed(DeliverNotificationAction::class, fn (DeliverNotificationAction $job): bool => $job->notificationActionId === $foreignProjectAction->id);
$this->assertDatabaseCount('notification_delivery_logs', 2);
$this->assertDatabaseCount('notification_delivery_logs', 3);
$this->assertDatabaseHas('notification_delivery_logs', [
'notification_action_id' => $webhookAction->id,
'delivery_status' => NotificationDeliveryStatus::Pending->value,
@@ -141,6 +173,13 @@ class TicketCreatedNotificationDispatchTest extends TestCase
'attempt_count' => 0,
'http_status' => null,
]);
$this->assertDatabaseHas('notification_delivery_logs', [
'notification_action_id' => $filterNotExistsAction->id,
'delivery_status' => NotificationDeliveryStatus::Pending->value,
'event_type' => NotificationEventType::TicketCreate->value,
'attempt_count' => 0,
'http_status' => null,
]);
}
private function backlogStatus(Project $project): TicketStatus
@@ -72,6 +72,71 @@ class TicketStatusChangedNotificationDispatchTest extends TestCase
->create([
'name' => 'Disabled webhook',
]);
$filterEqualsAction = NotificationAction::factory()
->webhook()
->for($project)
->create([
'name' => 'Filtered equals webhook',
'filters' => [
[
'attribute' => 'status.to.id',
'operator' => 'equals',
'value' => (string) $doingStatus->id,
],
],
]);
$filterNotEqualsAction = NotificationAction::factory()
->webhook()
->for($project)
->create([
'name' => 'Filtered not equals webhook',
'filters' => [
[
'attribute' => 'status.to.id',
'operator' => 'not_equals',
'value' => (string) $doingStatus->id,
],
],
]);
$filterExistsAction = NotificationAction::factory()
->webhook()
->for($project)
->create([
'name' => 'Filtered exists webhook',
'filters' => [
[
'attribute' => 'actor.id',
'operator' => 'exists',
'value' => null,
],
],
]);
$filterNotExistsAction = NotificationAction::factory()
->webhook()
->for($project)
->create([
'name' => 'Filtered not exists webhook',
'filters' => [
[
'attribute' => 'actor.id',
'operator' => 'not_exists',
'value' => null,
],
],
]);
$filterInAction = NotificationAction::factory()
->webhook()
->for($project)
->create([
'name' => 'Filtered in webhook',
'filters' => [
[
'attribute' => 'ticket.key',
'operator' => 'in',
'value' => ['CORE-1', 'CORE-99'],
],
],
]);
$foreignProjectAction = NotificationAction::factory()
->for($otherProject)
->create([
@@ -101,7 +166,7 @@ class TicketStatusChangedNotificationDispatchTest extends TestCase
->assertRedirect(route('projects.backlog.index', $project))
->assertSessionHas('status', 'CORE-1 moved to Doing.');
Queue::assertPushed(DeliverNotificationAction::class, 2);
Queue::assertPushed(DeliverNotificationAction::class, 5);
Queue::assertPushed(DeliverNotificationAction::class, function (DeliverNotificationAction $job) use ($webhookAction, $project, $ticket, $user, $backlogStatus, $doingStatus): bool {
return $job->notificationActionId === $webhookAction->id
&& $job->payload->projectId === $project->id
@@ -122,10 +187,18 @@ class TicketStatusChangedNotificationDispatchTest extends TestCase
});
Queue::assertPushed(DeliverNotificationAction::class, fn (DeliverNotificationAction $job): bool => $job->notificationActionId === $discordAction->id
&& $job->notificationDeliveryLogId !== null);
Queue::assertPushed(DeliverNotificationAction::class, fn (DeliverNotificationAction $job): bool => $job->notificationActionId === $filterEqualsAction->id
&& $job->notificationDeliveryLogId !== null);
Queue::assertPushed(DeliverNotificationAction::class, fn (DeliverNotificationAction $job): bool => $job->notificationActionId === $filterExistsAction->id
&& $job->notificationDeliveryLogId !== null);
Queue::assertPushed(DeliverNotificationAction::class, fn (DeliverNotificationAction $job): bool => $job->notificationActionId === $filterInAction->id
&& $job->notificationDeliveryLogId !== null);
Queue::assertNotPushed(DeliverNotificationAction::class, fn (DeliverNotificationAction $job): bool => $job->notificationActionId === $disabledAction->id);
Queue::assertNotPushed(DeliverNotificationAction::class, fn (DeliverNotificationAction $job): bool => $job->notificationActionId === $filterNotEqualsAction->id);
Queue::assertNotPushed(DeliverNotificationAction::class, fn (DeliverNotificationAction $job): bool => $job->notificationActionId === $filterNotExistsAction->id);
Queue::assertNotPushed(DeliverNotificationAction::class, fn (DeliverNotificationAction $job): bool => $job->notificationActionId === $foreignProjectAction->id);
$this->assertDatabaseCount('notification_delivery_logs', 2);
$this->assertDatabaseCount('notification_delivery_logs', 5);
$this->assertDatabaseHas('notification_delivery_logs', [
'notification_action_id' => $webhookAction->id,
'delivery_status' => NotificationDeliveryStatus::Pending->value,
@@ -138,6 +211,24 @@ class TicketStatusChangedNotificationDispatchTest extends TestCase
'attempt_count' => 0,
'http_status' => null,
]);
$this->assertDatabaseHas('notification_delivery_logs', [
'notification_action_id' => $filterEqualsAction->id,
'delivery_status' => NotificationDeliveryStatus::Pending->value,
'attempt_count' => 0,
'http_status' => null,
]);
$this->assertDatabaseHas('notification_delivery_logs', [
'notification_action_id' => $filterExistsAction->id,
'delivery_status' => NotificationDeliveryStatus::Pending->value,
'attempt_count' => 0,
'http_status' => null,
]);
$this->assertDatabaseHas('notification_delivery_logs', [
'notification_action_id' => $filterInAction->id,
'delivery_status' => NotificationDeliveryStatus::Pending->value,
'attempt_count' => 0,
'http_status' => null,
]);
$this->assertDatabaseHas('ticket_worklog_entries', [
'ticket_id' => $ticket->id,