diff --git a/app/Actions/Notifications/DeleteProjectNotificationAction.php b/app/Actions/Notifications/DeleteProjectNotificationAction.php new file mode 100644 index 0000000..2c8d349 --- /dev/null +++ b/app/Actions/Notifications/DeleteProjectNotificationAction.php @@ -0,0 +1,17 @@ +notificationActions() + ->whereKey($notificationAction->id) + ->firstOrFail() + ->delete(); + } +} diff --git a/app/Actions/Notifications/ListProjectNotificationActions.php b/app/Actions/Notifications/ListProjectNotificationActions.php new file mode 100644 index 0000000..6462a9b --- /dev/null +++ b/app/Actions/Notifications/ListProjectNotificationActions.php @@ -0,0 +1,18 @@ + + */ + public function handle(Project $project): Collection + { + return $project->notificationActions()->get(); + } +} diff --git a/app/Actions/Notifications/StoreProjectNotificationAction.php b/app/Actions/Notifications/StoreProjectNotificationAction.php new file mode 100644 index 0000000..17d3270 --- /dev/null +++ b/app/Actions/Notifications/StoreProjectNotificationAction.php @@ -0,0 +1,45 @@ +|null, + * delivery_url: string, + * enabled: bool, + * event_type: mixed, + * message_template?: string|null, + * name: string + * } $validated + */ + public function handle(Project $project, array $validated): NotificationAction + { + return $project->notificationActions()->create([ + 'channel' => Arr::get($validated, 'channel'), + 'channel_configuration' => Arr::get($validated, 'channel_configuration') ?? [], + 'delivery_url' => trim((string) Arr::get($validated, 'delivery_url')), + 'enabled' => (bool) Arr::get($validated, 'enabled'), + 'event_type' => Arr::get($validated, 'event_type'), + 'message_template' => $this->normalizeMessageTemplate(Arr::get($validated, 'message_template')), + 'name' => trim((string) Arr::get($validated, 'name')), + ]); + } + + private function normalizeMessageTemplate(mixed $messageTemplate): ?string + { + if (! is_string($messageTemplate)) { + return null; + } + + $messageTemplate = trim($messageTemplate); + + return $messageTemplate === '' ? null : $messageTemplate; + } +} diff --git a/app/Actions/Notifications/UpdateProjectNotificationAction.php b/app/Actions/Notifications/UpdateProjectNotificationAction.php new file mode 100644 index 0000000..59faabd --- /dev/null +++ b/app/Actions/Notifications/UpdateProjectNotificationAction.php @@ -0,0 +1,87 @@ +|null, + * delivery_url?: string|null, + * enabled?: bool, + * event_type?: mixed, + * message_template?: string|null, + * name?: string + * } $validated + */ + public function handle( + Project $project, + NotificationAction $notificationAction, + array $validated, + ): NotificationAction { + $notificationAction = $this->resolveProjectNotificationAction($project, $notificationAction); + + $attributes = []; + + if (array_key_exists('name', $validated)) { + $attributes['name'] = trim((string) Arr::get($validated, 'name')); + } + + if (array_key_exists('enabled', $validated)) { + $attributes['enabled'] = (bool) Arr::get($validated, 'enabled'); + } + + if (array_key_exists('event_type', $validated)) { + $attributes['event_type'] = Arr::get($validated, 'event_type'); + } + + if (array_key_exists('channel', $validated)) { + $attributes['channel'] = Arr::get($validated, 'channel'); + } + + if (array_key_exists('channel_configuration', $validated)) { + $attributes['channel_configuration'] = Arr::get($validated, 'channel_configuration') ?? []; + } + + if (array_key_exists('message_template', $validated)) { + $attributes['message_template'] = $this->normalizeMessageTemplate( + Arr::get($validated, 'message_template'), + ); + } + + $deliveryUrl = Arr::get($validated, 'delivery_url'); + + if (is_string($deliveryUrl) && trim($deliveryUrl) !== '') { + $attributes['delivery_url'] = trim($deliveryUrl); + } + + $notificationAction->update($attributes); + + return $notificationAction->fresh(); + } + + private function normalizeMessageTemplate(mixed $messageTemplate): ?string + { + if (! is_string($messageTemplate)) { + return null; + } + + $messageTemplate = trim($messageTemplate); + + return $messageTemplate === '' ? null : $messageTemplate; + } + + private function resolveProjectNotificationAction( + Project $project, + NotificationAction $notificationAction, + ): NotificationAction { + return $project->notificationActions() + ->whereKey($notificationAction->id) + ->firstOrFail(); + } +} diff --git a/app/Enums/NotificationChannel.php b/app/Enums/NotificationChannel.php new file mode 100644 index 0000000..a869ce7 --- /dev/null +++ b/app/Enums/NotificationChannel.php @@ -0,0 +1,31 @@ + 'Discord', + self::Webhook => 'Webhook', + }; + } + + /** + * @return array + */ + public static function options(): array + { + return array_map( + fn (self $channel): array => [ + 'label' => $channel->label(), + 'value' => $channel->value, + ], + self::cases(), + ); + } +} diff --git a/app/Enums/NotificationDeliveryStatus.php b/app/Enums/NotificationDeliveryStatus.php new file mode 100644 index 0000000..cd9ef60 --- /dev/null +++ b/app/Enums/NotificationDeliveryStatus.php @@ -0,0 +1,21 @@ + 'Failed', + self::Pending => 'Pending', + self::Retryable => 'Retryable', + self::Succeeded => 'Succeeded', + }; + } +} diff --git a/app/Enums/NotificationEventType.php b/app/Enums/NotificationEventType.php new file mode 100644 index 0000000..c9e120b --- /dev/null +++ b/app/Enums/NotificationEventType.php @@ -0,0 +1,29 @@ + 'Ticket status changed', + }; + } + + /** + * @return array + */ + public static function options(): array + { + return array_map( + fn (self $eventType): array => [ + 'label' => $eventType->label(), + 'value' => $eventType->value, + ], + self::cases(), + ); + } +} diff --git a/app/Http/Resources/NotificationActionResource.php b/app/Http/Resources/NotificationActionResource.php new file mode 100644 index 0000000..d510ae2 --- /dev/null +++ b/app/Http/Resources/NotificationActionResource.php @@ -0,0 +1,34 @@ + + */ + public function toArray(Request $request): array + { + return [ + 'id' => $this->id, + 'project_id' => $this->project_id, + 'name' => $this->name, + 'enabled' => $this->enabled, + 'event_type' => $this->event_type->value, + 'event_type_label' => $this->event_type->label(), + 'channel' => $this->channel->value, + 'channel_label' => $this->channel->label(), + 'channel_configuration' => $this->channel_configuration ?? [], + 'has_delivery_url' => $this->resource instanceof NotificationAction && filled($this->delivery_url), + 'message_template' => $this->message_template, + 'created_at' => $this->created_at?->toISOString(), + 'updated_at' => $this->updated_at?->toISOString(), + ]; + } +} diff --git a/app/Http/Resources/NotificationDeliveryLogResource.php b/app/Http/Resources/NotificationDeliveryLogResource.php new file mode 100644 index 0000000..f485129 --- /dev/null +++ b/app/Http/Resources/NotificationDeliveryLogResource.php @@ -0,0 +1,37 @@ + + */ + public function toArray(Request $request): array + { + return [ + 'id' => $this->id, + 'notification_action_id' => $this->notification_action_id, + 'event_type' => $this->event_type->value, + 'event_type_label' => $this->event_type->label(), + 'channel' => $this->channel->value, + 'channel_label' => $this->channel->label(), + 'delivery_status' => $this->delivery_status->value, + 'delivery_status_label' => $this->delivery_status->label(), + 'attempt_count' => $this->attempt_count, + 'http_status' => $this->http_status, + 'response_excerpt' => $this->response_excerpt, + 'error_excerpt' => $this->error_excerpt, + 'payload_metadata' => $this->payload_metadata, + 'last_attempted_at' => $this->last_attempted_at?->toISOString(), + 'delivered_at' => $this->delivered_at?->toISOString(), + 'created_at' => $this->created_at?->toISOString(), + 'updated_at' => $this->updated_at?->toISOString(), + ]; + } +} diff --git a/app/Models/NotificationAction.php b/app/Models/NotificationAction.php new file mode 100644 index 0000000..3e42278 --- /dev/null +++ b/app/Models/NotificationAction.php @@ -0,0 +1,62 @@ + */ + use HasFactory; + + /** + * @return array + */ + protected function casts(): array + { + return [ + 'channel' => NotificationChannel::class, + 'channel_configuration' => 'array', + 'delivery_url' => 'encrypted', + 'enabled' => 'boolean', + 'event_type' => NotificationEventType::class, + ]; + } + + public function scopeEnabled(Builder $query): Builder + { + return $query->where('enabled', true); + } + + public function deliveryLogs(): HasMany + { + return $this->hasMany(NotificationDeliveryLog::class) + ->orderByDesc('created_at') + ->orderByDesc('id'); + } + + public function project(): BelongsTo + { + return $this->belongsTo(Project::class); + } +} diff --git a/app/Models/NotificationDeliveryLog.php b/app/Models/NotificationDeliveryLog.php new file mode 100644 index 0000000..0a24637 --- /dev/null +++ b/app/Models/NotificationDeliveryLog.php @@ -0,0 +1,53 @@ + */ + use HasFactory; + + /** + * @return array + */ + protected function casts(): array + { + return [ + 'attempt_count' => 'integer', + 'channel' => NotificationChannel::class, + 'delivered_at' => 'datetime', + 'delivery_status' => NotificationDeliveryStatus::class, + 'event_type' => NotificationEventType::class, + 'http_status' => 'integer', + 'last_attempted_at' => 'datetime', + 'payload_metadata' => 'array', + ]; + } + + public function notificationAction(): BelongsTo + { + return $this->belongsTo(NotificationAction::class); + } +} diff --git a/app/Models/Project.php b/app/Models/Project.php index 5ee7809..51cd5f5 100644 --- a/app/Models/Project.php +++ b/app/Models/Project.php @@ -88,6 +88,13 @@ class Project extends Model ->orderBy('repository_full_name'); } + public function notificationActions(): HasMany + { + return $this->hasMany(NotificationAction::class) + ->orderBy('name') + ->orderBy('id'); + } + public function tickets(): HasMany { return $this->hasMany(Ticket::class) diff --git a/database/factories/NotificationActionFactory.php b/database/factories/NotificationActionFactory.php new file mode 100644 index 0000000..59daa68 --- /dev/null +++ b/database/factories/NotificationActionFactory.php @@ -0,0 +1,58 @@ + + */ +class NotificationActionFactory extends Factory +{ + /** + * Define the model's default state. + * + * @return array + */ + public function definition(): array + { + return [ + 'channel' => NotificationChannel::Webhook, + 'channel_configuration' => [], + 'delivery_url' => 'https://hooks.'.fake()->domainName().'/kanbangit/'.fake()->uuid(), + 'enabled' => true, + 'event_type' => NotificationEventType::TicketStatusChanged, + 'message_template' => null, + 'name' => fake()->unique()->sentence(3), + 'project_id' => Project::factory(), + ]; + } + + public function discord(): static + { + return $this->state(fn (): array => [ + 'channel' => NotificationChannel::Discord, + 'delivery_url' => 'https://discord.com/api/webhooks/'.fake()->numberBetween(100_000, 999_999).'/'.fake()->uuid(), + 'message_template' => '$ticket_key moved from $status_from to $status_to by $actor_name: $url', + ]); + } + + public function disabled(): static + { + return $this->state(fn (): array => [ + 'enabled' => false, + ]); + } + + public function webhook(): static + { + return $this->state(fn (): array => [ + 'channel' => NotificationChannel::Webhook, + 'message_template' => null, + ]); + } +} diff --git a/database/factories/NotificationDeliveryLogFactory.php b/database/factories/NotificationDeliveryLogFactory.php new file mode 100644 index 0000000..23a33eb --- /dev/null +++ b/database/factories/NotificationDeliveryLogFactory.php @@ -0,0 +1,98 @@ + + */ +class NotificationDeliveryLogFactory extends Factory +{ + /** + * Define the model's default state. + * + * @return array + */ + public function definition(): array + { + return [ + 'attempt_count' => 0, + 'channel' => NotificationChannel::Webhook, + 'delivered_at' => null, + 'delivery_status' => NotificationDeliveryStatus::Pending, + 'error_excerpt' => null, + 'event_type' => NotificationEventType::TicketStatusChanged, + 'http_status' => null, + 'last_attempted_at' => null, + 'notification_action_id' => NotificationAction::factory()->webhook(), + 'payload_metadata' => [ + 'project_key' => fake()->regexify('[A-Z]{4}'), + 'ticket_key' => fake()->regexify('[A-Z]{4}-[0-9]{1,3}'), + ], + 'response_excerpt' => null, + ]; + } + + public function forNotificationAction(NotificationAction $notificationAction): static + { + return $this->for($notificationAction, 'notificationAction')->state(fn (): array => [ + 'channel' => $notificationAction->channel, + 'event_type' => $notificationAction->event_type, + ]); + } + + public function failed(): static + { + return $this->state(fn (): array => [ + 'attempt_count' => 3, + 'delivery_status' => NotificationDeliveryStatus::Failed, + 'error_excerpt' => 'Webhook returned a permanent validation error.', + 'http_status' => 422, + 'last_attempted_at' => Carbon::now(), + ]); + } + + public function pending(): static + { + return $this->state(fn (): array => [ + 'attempt_count' => 0, + 'delivered_at' => null, + 'delivery_status' => NotificationDeliveryStatus::Pending, + 'error_excerpt' => null, + 'http_status' => null, + 'last_attempted_at' => null, + 'response_excerpt' => null, + ]); + } + + public function retryable(): static + { + return $this->state(fn (): array => [ + 'attempt_count' => 1, + 'delivery_status' => NotificationDeliveryStatus::Retryable, + 'error_excerpt' => 'Connection timed out while contacting the remote endpoint.', + 'http_status' => null, + 'last_attempted_at' => Carbon::now(), + ]); + } + + public function succeeded(): static + { + return $this->state(fn (): array => [ + 'attempt_count' => 1, + 'delivered_at' => Carbon::now(), + 'delivery_status' => NotificationDeliveryStatus::Succeeded, + 'error_excerpt' => null, + 'http_status' => 204, + 'last_attempted_at' => Carbon::now(), + 'response_excerpt' => 'No Content', + ]); + } +} diff --git a/database/migrations/2026_04_20_194646_create_notification_actions_table.php b/database/migrations/2026_04_20_194646_create_notification_actions_table.php new file mode 100644 index 0000000..ad4e055 --- /dev/null +++ b/database/migrations/2026_04_20_194646_create_notification_actions_table.php @@ -0,0 +1,38 @@ +id(); + $table->foreignId('project_id')->constrained()->cascadeOnDelete(); + $table->string('name'); + $table->boolean('enabled')->default(true); + $table->string('event_type'); + $table->string('channel'); + $table->json('channel_configuration'); + $table->text('message_template')->nullable(); + $table->text('delivery_url'); + $table->timestamps(); + + $table->index(['project_id', 'event_type', 'enabled']); + $table->index(['project_id', 'channel']); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('notification_actions'); + } +}; diff --git a/database/migrations/2026_04_20_194646_create_notification_delivery_logs_table.php b/database/migrations/2026_04_20_194646_create_notification_delivery_logs_table.php new file mode 100644 index 0000000..8dd33b8 --- /dev/null +++ b/database/migrations/2026_04_20_194646_create_notification_delivery_logs_table.php @@ -0,0 +1,41 @@ +id(); + $table->foreignId('notification_action_id')->constrained()->cascadeOnDelete(); + $table->string('event_type'); + $table->string('channel'); + $table->string('delivery_status'); + $table->unsignedInteger('attempt_count')->default(0); + $table->unsignedSmallInteger('http_status')->nullable(); + $table->text('response_excerpt')->nullable(); + $table->text('error_excerpt')->nullable(); + $table->json('payload_metadata')->nullable(); + $table->timestamp('last_attempted_at')->nullable(); + $table->timestamp('delivered_at')->nullable(); + $table->timestamps(); + + $table->index(['notification_action_id', 'delivery_status']); + $table->index(['notification_action_id', 'created_at']); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('notification_delivery_logs'); + } +}; diff --git a/database/seeders/DatabaseSeeder.php b/database/seeders/DatabaseSeeder.php index 33909c8..3ef6473 100644 --- a/database/seeders/DatabaseSeeder.php +++ b/database/seeders/DatabaseSeeder.php @@ -16,6 +16,8 @@ class DatabaseSeeder extends Seeder UserSeeder::class, UserGitConnectionSeeder::class, ProjectSeeder::class, + NotificationActionSeeder::class, + NotificationDeliveryLogSeeder::class, RepositoryConnectionSeeder::class, BoardColumnSeeder::class, ProjectTicketMetadataSeeder::class, diff --git a/database/seeders/NotificationActionSeeder.php b/database/seeders/NotificationActionSeeder.php new file mode 100644 index 0000000..8abcee3 --- /dev/null +++ b/database/seeders/NotificationActionSeeder.php @@ -0,0 +1,52 @@ +where('key', 'CORE')->first() + ?? Project::query()->orderBy('id')->first(); + + if ($project === null) { + return; + } + + $project->notificationActions()->updateOrCreate( + [ + 'name' => 'Core Status Webhook', + ], + [ + 'channel' => NotificationChannel::Webhook, + 'channel_configuration' => [], + 'delivery_url' => 'https://hooks.example.test/kanbangit/core-status', + 'enabled' => true, + 'event_type' => NotificationEventType::TicketStatusChanged, + 'message_template' => null, + ], + ); + + $project->notificationActions()->updateOrCreate( + [ + 'name' => 'Core Status Discord', + ], + [ + 'channel' => NotificationChannel::Discord, + 'channel_configuration' => [], + 'delivery_url' => 'https://discord.com/api/webhooks/123456/core-status', + 'enabled' => false, + 'event_type' => NotificationEventType::TicketStatusChanged, + 'message_template' => '$ticket_key moved from $status_from to $status_to: $url', + ], + ); + } +} diff --git a/database/seeders/NotificationDeliveryLogSeeder.php b/database/seeders/NotificationDeliveryLogSeeder.php new file mode 100644 index 0000000..b21354a --- /dev/null +++ b/database/seeders/NotificationDeliveryLogSeeder.php @@ -0,0 +1,42 @@ +orderBy('id')->first(); + + if ($notificationAction === null || $notificationAction->deliveryLogs()->exists()) { + return; + } + + NotificationDeliveryLog::factory() + ->forNotificationAction($notificationAction) + ->succeeded() + ->create([ + 'payload_metadata' => [ + 'project_key' => 'CORE', + 'ticket_key' => 'CORE-1', + ], + ]); + + NotificationDeliveryLog::factory() + ->forNotificationAction($notificationAction) + ->retryable() + ->create([ + 'payload_metadata' => [ + 'project_key' => 'CORE', + 'ticket_key' => 'CORE-2', + ], + ]); + } +} diff --git a/tests/Feature/Models/NotificationActionTest.php b/tests/Feature/Models/NotificationActionTest.php new file mode 100644 index 0000000..014aeac --- /dev/null +++ b/tests/Feature/Models/NotificationActionTest.php @@ -0,0 +1,65 @@ +webhook()->create([ + 'delivery_url' => 'https://hooks.example.test/kanbangit/core-status', + ]); + + $storedDeliveryUrl = DB::table('notification_actions') + ->where('id', $notificationAction->id) + ->value('delivery_url'); + + $this->assertNotSame('https://hooks.example.test/kanbangit/core-status', $storedDeliveryUrl); + $this->assertSame('https://hooks.example.test/kanbangit/core-status', $notificationAction->fresh()->delivery_url); + $this->assertSame(NotificationEventType::TicketStatusChanged, $notificationAction->fresh()->event_type); + $this->assertSame(NotificationChannel::Webhook, $notificationAction->fresh()->channel); + $this->assertSame([], $notificationAction->fresh()->channel_configuration); + } + + /** + * Ensure the Discord factory state creates message-driven Discord actions. + */ + public function test_factory_can_create_discord_notification_actions(): void + { + $notificationAction = NotificationAction::factory()->discord()->create(); + + $this->assertSame(NotificationChannel::Discord, $notificationAction->fresh()->channel); + $this->assertNotNull($notificationAction->fresh()->message_template); + $this->assertStringStartsWith('https://discord.com/api/webhooks/', $notificationAction->fresh()->delivery_url); + } + + /** + * Ensure notification actions belong to a project and keep secret URLs out of serialized output. + */ + public function test_notification_actions_belong_to_projects_and_hide_delivery_urls(): void + { + $notificationAction = NotificationAction::factory()->create(); + $resource = (new NotificationActionResource($notificationAction->fresh())) + ->toArray(Request::create('/')); + + $this->assertTrue($notificationAction->project()->exists()); + $this->assertTrue($notificationAction->project->is($notificationAction->fresh()->project)); + $this->assertArrayNotHasKey('delivery_url', $notificationAction->fresh()->toArray()); + $this->assertArrayNotHasKey('delivery_url', $resource); + $this->assertTrue($resource['has_delivery_url']); + } +} diff --git a/tests/Feature/Models/NotificationDeliveryLogTest.php b/tests/Feature/Models/NotificationDeliveryLogTest.php new file mode 100644 index 0000000..2c4b7ad --- /dev/null +++ b/tests/Feature/Models/NotificationDeliveryLogTest.php @@ -0,0 +1,66 @@ +create(); + + $this->assertTrue($deliveryLog->notificationAction()->exists()); + $this->assertTrue($deliveryLog->notificationAction->is($deliveryLog->fresh()->notificationAction)); + $this->assertSame(NotificationChannel::Webhook, $deliveryLog->fresh()->channel); + $this->assertSame(NotificationDeliveryStatus::Pending, $deliveryLog->fresh()->delivery_status); + } + + /** + * Ensure delivery logs can represent every supported delivery state. + */ + public function test_delivery_logs_support_pending_succeeded_failed_and_retryable_statuses(): void + { + NotificationDeliveryLog::factory()->pending()->create(); + NotificationDeliveryLog::factory()->succeeded()->create(); + NotificationDeliveryLog::factory()->failed()->create(); + NotificationDeliveryLog::factory()->retryable()->create(); + + $this->assertSame( + [ + NotificationDeliveryStatus::Pending, + NotificationDeliveryStatus::Succeeded, + NotificationDeliveryStatus::Failed, + NotificationDeliveryStatus::Retryable, + ], + NotificationDeliveryLog::query()->orderBy('id')->get()->pluck('delivery_status')->all(), + ); + } + + /** + * Ensure delivery logs cast metadata and attempt information for later delivery tracking. + */ + public function test_delivery_logs_cast_metadata_and_attempt_information(): void + { + $deliveryLog = NotificationDeliveryLog::factory()->succeeded()->create([ + 'payload_metadata' => [ + 'project_key' => 'CORE', + 'ticket_key' => 'CORE-27', + ], + ])->fresh(); + + $this->assertSame('CORE-27', $deliveryLog->payload_metadata['ticket_key']); + $this->assertSame(204, $deliveryLog->http_status); + $this->assertNotNull($deliveryLog->last_attempted_at); + $this->assertNotNull($deliveryLog->delivered_at); + } +} diff --git a/tests/Feature/Notifications/ProjectNotificationActionManagementTest.php b/tests/Feature/Notifications/ProjectNotificationActionManagementTest.php new file mode 100644 index 0000000..fa4c978 --- /dev/null +++ b/tests/Feature/Notifications/ProjectNotificationActionManagementTest.php @@ -0,0 +1,144 @@ +create(); + $otherProject = Project::factory()->create(); + + $storeAction->handle($project, [ + 'name' => 'Discord alerts', + 'enabled' => false, + 'event_type' => NotificationEventType::TicketStatusChanged, + 'channel' => NotificationChannel::Discord, + 'channel_configuration' => ['mentions' => ['ops']], + 'message_template' => '$ticket_key changed to $status_to', + 'delivery_url' => 'https://discord.com/api/webhooks/123456/discord-alerts', + ]); + + $webhookAction = $storeAction->handle($project, [ + 'name' => 'Webhook alerts', + 'enabled' => true, + 'event_type' => NotificationEventType::TicketStatusChanged, + 'channel' => NotificationChannel::Webhook, + 'channel_configuration' => ['source' => 'n8n'], + 'message_template' => null, + 'delivery_url' => 'https://hooks.example.test/kanbangit/webhook-alerts', + ]); + + NotificationAction::factory()->for($otherProject)->create([ + 'name' => 'Foreign project webhook', + ]); + + $listedActions = $listAction->handle($project); + + $this->assertModelExists($webhookAction); + $this->assertCount(2, $listedActions); + $this->assertSame(['Discord alerts', 'Webhook alerts'], $listedActions->pluck('name')->all()); + $this->assertSame(['mentions' => ['ops']], $listedActions->first()->channel_configuration); + } + + /** + * Ensure notification actions can be updated without overwriting their saved secret URL. + */ + public function test_notification_actions_can_be_updated_without_replacing_the_existing_delivery_url(): void + { + $updateAction = app(UpdateProjectNotificationAction::class); + $project = Project::factory()->create(); + $notificationAction = NotificationAction::factory()->discord()->for($project)->create([ + 'delivery_url' => 'https://discord.com/api/webhooks/123456/original-secret', + 'message_template' => '$ticket_key moved to $status_to', + ]); + + $updatedAction = $updateAction->handle($project, $notificationAction, [ + 'enabled' => false, + 'message_template' => '$ticket_key moved from $status_from to $status_to', + 'channel_configuration' => ['mentions' => ['support']], + 'delivery_url' => '', + ]); + + $this->assertFalse($updatedAction->enabled); + $this->assertSame( + '$ticket_key moved from $status_from to $status_to', + $updatedAction->message_template, + ); + $this->assertSame(['mentions' => ['support']], $updatedAction->channel_configuration); + $this->assertSame( + 'https://discord.com/api/webhooks/123456/original-secret', + $updatedAction->delivery_url, + ); + } + + /** + * Ensure notification actions can replace their delivery URL when a new secret is submitted. + */ + public function test_notification_actions_can_replace_delivery_urls(): void + { + $updateAction = app(UpdateProjectNotificationAction::class); + $project = Project::factory()->create(); + $notificationAction = NotificationAction::factory()->for($project)->create([ + 'delivery_url' => 'https://hooks.example.test/kanbangit/original-secret', + ]); + + $updatedAction = $updateAction->handle($project, $notificationAction, [ + 'delivery_url' => 'https://hooks.example.test/kanbangit/replaced-secret', + 'name' => 'Primary webhook', + ]); + + $this->assertSame('Primary webhook', $updatedAction->name); + $this->assertSame('https://hooks.example.test/kanbangit/replaced-secret', $updatedAction->delivery_url); + } + + /** + * Ensure notification actions can be deleted from their owning project. + */ + public function test_notification_actions_can_be_deleted_from_their_project(): void + { + $deleteAction = app(DeleteProjectNotificationAction::class); + $project = Project::factory()->create(); + $notificationAction = NotificationAction::factory()->for($project)->create(); + + $deleteAction->handle($project, $notificationAction); + + $this->assertModelMissing($notificationAction); + } + + /** + * Ensure project-scoped actions reject notification actions from other projects. + */ + public function test_project_scoped_management_rejects_foreign_notification_actions(): void + { + $updateAction = app(UpdateProjectNotificationAction::class); + $project = Project::factory()->create(); + $otherProject = Project::factory()->create(); + $notificationAction = NotificationAction::factory()->for($otherProject)->create(); + + $this->expectException(ModelNotFoundException::class); + + $updateAction->handle($project, $notificationAction, [ + 'name' => 'Should not update', + ]); + } +}