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
@@ -33,6 +33,7 @@ class NotificationActionTest extends TestCase
$this->assertSame(NotificationEventType::TicketStatusChanged, $notificationAction->fresh()->event_type);
$this->assertSame(NotificationChannel::Webhook, $notificationAction->fresh()->channel);
$this->assertSame([], $notificationAction->fresh()->channel_configuration);
$this->assertSame([], $notificationAction->fresh()->filters ?? []);
}
/**
@@ -61,5 +62,6 @@ class NotificationActionTest extends TestCase
$this->assertArrayNotHasKey('delivery_url', $notificationAction->fresh()->toArray());
$this->assertArrayNotHasKey('delivery_url', $resource);
$this->assertTrue($resource['has_delivery_url']);
$this->assertSame([], $resource['filters']);
}
}
@@ -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,
@@ -29,6 +29,13 @@ class ProjectNotificationSettingsControllerTest extends TestCase
->for($project)
->create([
'name' => 'Discord alerts',
'filters' => [
[
'attribute' => 'status.to.label',
'operator' => 'equals',
'value' => 'Doing',
],
],
]);
$this->syncProjectPermissions($project, $user, ['edit']);
@@ -44,10 +51,15 @@ class ProjectNotificationSettingsControllerTest extends TestCase
->where('notificationActions.0.event_type', NotificationEventType::TicketStatusChanged->value)
->where('notificationActions.0.enabled', true)
->where('notificationActions.0.has_delivery_url', true)
->where('notificationActions.0.filters.0.attribute', 'status.to.label')
->where('notificationActions.0.filters.0.operator', 'equals')
->where('notificationActions.0.filters.0.value', 'Doing')
->where('notificationActions.0.updated_at', $notificationAction->updated_at?->toISOString())
->where('channelOptions.0.value', NotificationChannel::Discord->value)
->where('eventTypeOptions.0.value', NotificationEventType::TicketStatusChanged->value)
->where('eventTypeOptions.1.value', NotificationEventType::TicketCreate->value)
->where('filterAttributeOptions.0.value', 'project.id')
->where('filterOperatorOptions.0.value', 'equals')
->where('templateVariables.0.value', '$url')
->where('templateVariables.6.value', '$actor_name')
->missing('notificationActions.0.delivery_url'),
@@ -86,6 +98,13 @@ class ProjectNotificationSettingsControllerTest extends TestCase
'channel' => NotificationChannel::Discord->value,
'enabled' => '1',
'delivery_url' => 'https://discord.com/api/webhooks/123456/discord-alerts',
'filters' => json_encode([
[
'attribute' => 'status.to.label',
'operator' => 'equals',
'value' => 'Doing',
],
], JSON_THROW_ON_ERROR),
'message_template' => '$ticket_key moved from $status_from to $status_to by $actor_name: $url',
])
->assertRedirect(route('projects.settings.notifications', $project))
@@ -101,6 +120,13 @@ class ProjectNotificationSettingsControllerTest extends TestCase
'https://discord.com/api/webhooks/123456/discord-alerts',
$notificationAction->delivery_url,
);
$this->assertSame([
[
'attribute' => 'status.to.label',
'operator' => 'equals',
'value' => 'Doing',
],
], $notificationAction->filters);
}
/**
@@ -127,6 +153,13 @@ class ProjectNotificationSettingsControllerTest extends TestCase
'channel' => NotificationChannel::Discord->value,
'enabled' => '1',
'delivery_url' => '',
'filters' => json_encode([
[
'attribute' => 'ticket.key',
'operator' => 'in',
'value' => ['CORE-1', 'CORE-2'],
],
], JSON_THROW_ON_ERROR),
'message_template' => '$ticket_key moved to $status_to: $url',
])
->assertRedirect(route('projects.settings.notifications', $project))
@@ -139,6 +172,13 @@ class ProjectNotificationSettingsControllerTest extends TestCase
'https://discord.com/api/webhooks/123456/original-secret',
$notificationAction->delivery_url,
);
$this->assertSame([
[
'attribute' => 'ticket.key',
'operator' => 'in',
'value' => ['CORE-1', 'CORE-2'],
],
], $notificationAction->filters);
$this->assertSame('$ticket_key moved to $status_to: $url', $notificationAction->message_template);
}
@@ -258,6 +298,13 @@ class ProjectNotificationSettingsControllerTest extends TestCase
'channel' => NotificationChannel::Discord->value,
'enabled' => 'definitely-not-a-boolean',
'delivery_url' => 'not-a-url',
'filters' => json_encode([
[
'attribute' => 'ticket.key',
'operator' => 'in',
'value' => [],
],
], JSON_THROW_ON_ERROR),
'message_template' => '',
])
->assertRedirect(route('projects.settings.notifications', $project))
@@ -266,6 +313,7 @@ class ProjectNotificationSettingsControllerTest extends TestCase
'event_type',
'enabled',
'delivery_url',
'filters.0.value',
'message_template',
]);
}