codex/v0.5-webhook-notification/02-project-notification-settings-ui #48
@@ -0,0 +1,70 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Actions\Notifications;
|
||||||
|
|
||||||
|
use App\Enums\NotificationChannel;
|
||||||
|
use App\Enums\NotificationEventType;
|
||||||
|
use App\Http\Resources\NotificationActionResource;
|
||||||
|
use App\Http\Resources\ProjectResource;
|
||||||
|
use App\Models\Project;
|
||||||
|
|
||||||
|
class BuildProjectNotificationSettingsPayload
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
private readonly ListProjectNotificationActions $listProjectNotificationActions,
|
||||||
|
) {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array<string, mixed>
|
||||||
|
*/
|
||||||
|
public function handle(Project $project, mixed $status = null): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'channelOptions' => NotificationChannel::options(),
|
||||||
|
'eventTypeOptions' => NotificationEventType::options(),
|
||||||
|
'notificationActions' => NotificationActionResource::collection(
|
||||||
|
$this->listProjectNotificationActions->handle($project),
|
||||||
|
)->resolve(),
|
||||||
|
'project' => ProjectResource::make($project)->resolve(),
|
||||||
|
'status' => $status,
|
||||||
|
'templateVariables' => $this->templateVariables(),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array<int, array{description: string, value: string}>
|
||||||
|
*/
|
||||||
|
private function templateVariables(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
[
|
||||||
|
'description' => 'Absolute ticket URL for the status change event.',
|
||||||
|
'value' => '$url',
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'description' => 'Project ticket key such as CORE-42.',
|
||||||
|
'value' => '$ticket_key',
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'description' => 'Ticket title from the changed ticket.',
|
||||||
|
'value' => '$ticket_title',
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'description' => 'Project name that owns the ticket.',
|
||||||
|
'value' => '$project_name',
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'description' => 'Origin status label before the move.',
|
||||||
|
'value' => '$status_from',
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'description' => 'Destination status label after the move.',
|
||||||
|
'value' => '$status_to',
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'description' => 'Actor name when the move has a known user.',
|
||||||
|
'value' => '$actor_name',
|
||||||
|
],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Actions\Notifications;
|
||||||
|
|
||||||
|
use App\Models\NotificationAction;
|
||||||
|
use App\Models\Project;
|
||||||
|
|
||||||
|
class DeleteProjectNotificationAction
|
||||||
|
{
|
||||||
|
public function handle(Project $project, NotificationAction $notificationAction): void
|
||||||
|
{
|
||||||
|
$project->notificationActions()
|
||||||
|
->whereKey($notificationAction->id)
|
||||||
|
->firstOrFail()
|
||||||
|
->delete();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Actions\Notifications;
|
||||||
|
|
||||||
|
use App\Models\NotificationAction;
|
||||||
|
use App\Models\Project;
|
||||||
|
use Illuminate\Database\Eloquent\Collection;
|
||||||
|
|
||||||
|
class ListProjectNotificationActions
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @return Collection<int, NotificationAction>
|
||||||
|
*/
|
||||||
|
public function handle(Project $project): Collection
|
||||||
|
{
|
||||||
|
return $project->notificationActions()->get();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,45 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Actions\Notifications;
|
||||||
|
|
||||||
|
use App\Models\NotificationAction;
|
||||||
|
use App\Models\Project;
|
||||||
|
use Illuminate\Support\Arr;
|
||||||
|
|
||||||
|
class StoreProjectNotificationAction
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @param array{
|
||||||
|
* channel: mixed,
|
||||||
|
* channel_configuration?: array<string, mixed>|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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,87 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Actions\Notifications;
|
||||||
|
|
||||||
|
use App\Models\NotificationAction;
|
||||||
|
use App\Models\Project;
|
||||||
|
use Illuminate\Support\Arr;
|
||||||
|
|
||||||
|
class UpdateProjectNotificationAction
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @param array{
|
||||||
|
* channel?: mixed,
|
||||||
|
* channel_configuration?: array<string, mixed>|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();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Enums;
|
||||||
|
|
||||||
|
enum NotificationChannel: string
|
||||||
|
{
|
||||||
|
case Discord = 'discord';
|
||||||
|
case Webhook = 'webhook';
|
||||||
|
|
||||||
|
public function label(): string
|
||||||
|
{
|
||||||
|
return match ($this) {
|
||||||
|
self::Discord => 'Discord',
|
||||||
|
self::Webhook => 'Webhook',
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array<int, array{label: string, value: string}>
|
||||||
|
*/
|
||||||
|
public static function options(): array
|
||||||
|
{
|
||||||
|
return array_map(
|
||||||
|
fn (self $channel): array => [
|
||||||
|
'label' => $channel->label(),
|
||||||
|
'value' => $channel->value,
|
||||||
|
],
|
||||||
|
self::cases(),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Enums;
|
||||||
|
|
||||||
|
enum NotificationDeliveryStatus: string
|
||||||
|
{
|
||||||
|
case Failed = 'failed';
|
||||||
|
case Pending = 'pending';
|
||||||
|
case Retryable = 'retryable';
|
||||||
|
case Succeeded = 'succeeded';
|
||||||
|
|
||||||
|
public function label(): string
|
||||||
|
{
|
||||||
|
return match ($this) {
|
||||||
|
self::Failed => 'Failed',
|
||||||
|
self::Pending => 'Pending',
|
||||||
|
self::Retryable => 'Retryable',
|
||||||
|
self::Succeeded => 'Succeeded',
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Enums;
|
||||||
|
|
||||||
|
enum NotificationEventType: string
|
||||||
|
{
|
||||||
|
case TicketStatusChanged = 'ticket_status_changed';
|
||||||
|
|
||||||
|
public function label(): string
|
||||||
|
{
|
||||||
|
return match ($this) {
|
||||||
|
self::TicketStatusChanged => 'Ticket status changed',
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array<int, array{label: string, value: string}>
|
||||||
|
*/
|
||||||
|
public static function options(): array
|
||||||
|
{
|
||||||
|
return array_map(
|
||||||
|
fn (self $eventType): array => [
|
||||||
|
'label' => $eventType->label(),
|
||||||
|
'value' => $eventType->value,
|
||||||
|
],
|
||||||
|
self::cases(),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,94 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Controllers;
|
||||||
|
|
||||||
|
use App\Actions\Notifications\BuildProjectNotificationSettingsPayload;
|
||||||
|
use App\Actions\Notifications\DeleteProjectNotificationAction;
|
||||||
|
use App\Actions\Notifications\StoreProjectNotificationAction;
|
||||||
|
use App\Actions\Notifications\UpdateProjectNotificationAction;
|
||||||
|
use App\Actions\Permissions\PermissionService;
|
||||||
|
use App\Enums\Permission;
|
||||||
|
use App\Http\Requests\StoreProjectNotificationActionRequest;
|
||||||
|
use App\Http\Requests\UpdateProjectNotificationActionRequest;
|
||||||
|
use App\Models\NotificationAction;
|
||||||
|
use App\Models\Project;
|
||||||
|
use Illuminate\Http\RedirectResponse;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use Inertia\Inertia;
|
||||||
|
use Inertia\Response;
|
||||||
|
|
||||||
|
class ProjectNotificationSettingsController extends Controller
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
private readonly BuildProjectNotificationSettingsPayload $buildProjectNotificationSettingsPayload,
|
||||||
|
private readonly DeleteProjectNotificationAction $deleteProjectNotificationAction,
|
||||||
|
private readonly PermissionService $permissionService,
|
||||||
|
private readonly StoreProjectNotificationAction $storeProjectNotificationAction,
|
||||||
|
private readonly UpdateProjectNotificationAction $updateProjectNotificationAction,
|
||||||
|
) {}
|
||||||
|
|
||||||
|
public function show(Request $request, Project $project): Response
|
||||||
|
{
|
||||||
|
$this->abortUnlessCanManage($request, $project);
|
||||||
|
|
||||||
|
return Inertia::render(
|
||||||
|
'projects/settings/Notifications',
|
||||||
|
$this->buildProjectNotificationSettingsPayload->handle($project, session('status')),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function store(
|
||||||
|
StoreProjectNotificationActionRequest $request,
|
||||||
|
Project $project,
|
||||||
|
): RedirectResponse {
|
||||||
|
$this->abortUnlessCanManage($request, $project);
|
||||||
|
|
||||||
|
$notificationAction = $this->storeProjectNotificationAction->handle($project, $request->validated());
|
||||||
|
|
||||||
|
return to_route('projects.settings.notifications', $project)
|
||||||
|
->with('status', "{$notificationAction->name} notification was added.");
|
||||||
|
}
|
||||||
|
|
||||||
|
public function update(
|
||||||
|
UpdateProjectNotificationActionRequest $request,
|
||||||
|
Project $project,
|
||||||
|
NotificationAction $notificationAction,
|
||||||
|
): RedirectResponse {
|
||||||
|
$this->abortUnlessCanManage($request, $project);
|
||||||
|
|
||||||
|
$notificationAction = $this->updateProjectNotificationAction->handle(
|
||||||
|
$project,
|
||||||
|
$notificationAction,
|
||||||
|
$request->validated(),
|
||||||
|
);
|
||||||
|
|
||||||
|
return to_route('projects.settings.notifications', $project)
|
||||||
|
->with('status', "{$notificationAction->name} notification was updated.");
|
||||||
|
}
|
||||||
|
|
||||||
|
public function destroy(
|
||||||
|
Request $request,
|
||||||
|
Project $project,
|
||||||
|
NotificationAction $notificationAction,
|
||||||
|
): RedirectResponse {
|
||||||
|
$this->abortUnlessCanManage($request, $project);
|
||||||
|
|
||||||
|
$notificationAction = $project->notificationActions()
|
||||||
|
->whereKey($notificationAction->id)
|
||||||
|
->firstOrFail();
|
||||||
|
$deletedName = $notificationAction->name;
|
||||||
|
|
||||||
|
$this->deleteProjectNotificationAction->handle($project, $notificationAction);
|
||||||
|
|
||||||
|
return to_route('projects.settings.notifications', $project)
|
||||||
|
->with('status', "{$deletedName} notification was deleted.");
|
||||||
|
}
|
||||||
|
|
||||||
|
private function abortUnlessCanManage(Request $request, Project $project): void
|
||||||
|
{
|
||||||
|
abort_unless(
|
||||||
|
$this->permissionService->allows($request->user(), Permission::ProjectAdmin, $project),
|
||||||
|
403,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,71 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Requests;
|
||||||
|
|
||||||
|
use App\Enums\NotificationChannel;
|
||||||
|
use App\Enums\NotificationEventType;
|
||||||
|
use Illuminate\Contracts\Validation\ValidationRule;
|
||||||
|
use Illuminate\Foundation\Http\FormRequest;
|
||||||
|
use Illuminate\Validation\Rule;
|
||||||
|
|
||||||
|
class StoreProjectNotificationActionRequest extends FormRequest
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Determine if the user is authorized to make this request.
|
||||||
|
*/
|
||||||
|
public function authorize(): bool
|
||||||
|
{
|
||||||
|
return $this->user() !== null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the validation rules that apply to the request.
|
||||||
|
*
|
||||||
|
* @return array<string, ValidationRule|array<mixed>|string>
|
||||||
|
*/
|
||||||
|
public function rules(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'name' => ['required', 'string', 'max:255'],
|
||||||
|
'event_type' => ['required', 'string', Rule::in($this->eventTypes())],
|
||||||
|
'channel' => ['required', 'string', Rule::in($this->channels())],
|
||||||
|
'enabled' => ['required', 'boolean'],
|
||||||
|
'delivery_url' => ['required', 'string', 'max:2048', 'url'],
|
||||||
|
'message_template' => $this->messageTemplateRules(),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array<int, string>
|
||||||
|
*/
|
||||||
|
private function channels(): array
|
||||||
|
{
|
||||||
|
return array_map(
|
||||||
|
static fn (NotificationChannel $channel): string => $channel->value,
|
||||||
|
NotificationChannel::cases(),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array<int, string>
|
||||||
|
*/
|
||||||
|
private function eventTypes(): array
|
||||||
|
{
|
||||||
|
return array_map(
|
||||||
|
static fn (NotificationEventType $eventType): string => $eventType->value,
|
||||||
|
NotificationEventType::cases(),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array<int, ValidationRule|string>
|
||||||
|
*/
|
||||||
|
private function messageTemplateRules(): array
|
||||||
|
{
|
||||||
|
if ($this->input('channel') === NotificationChannel::Discord->value) {
|
||||||
|
return ['required', 'string', 'max:2000'];
|
||||||
|
}
|
||||||
|
|
||||||
|
return ['nullable', 'string', 'max:2000'];
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,71 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Requests;
|
||||||
|
|
||||||
|
use App\Enums\NotificationChannel;
|
||||||
|
use App\Enums\NotificationEventType;
|
||||||
|
use Illuminate\Contracts\Validation\ValidationRule;
|
||||||
|
use Illuminate\Foundation\Http\FormRequest;
|
||||||
|
use Illuminate\Validation\Rule;
|
||||||
|
|
||||||
|
class UpdateProjectNotificationActionRequest extends FormRequest
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Determine if the user is authorized to make this request.
|
||||||
|
*/
|
||||||
|
public function authorize(): bool
|
||||||
|
{
|
||||||
|
return $this->user() !== null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the validation rules that apply to the request.
|
||||||
|
*
|
||||||
|
* @return array<string, ValidationRule|array<mixed>|string>
|
||||||
|
*/
|
||||||
|
public function rules(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'name' => ['required', 'string', 'max:255'],
|
||||||
|
'event_type' => ['required', 'string', Rule::in($this->eventTypes())],
|
||||||
|
'channel' => ['required', 'string', Rule::in($this->channels())],
|
||||||
|
'enabled' => ['required', 'boolean'],
|
||||||
|
'delivery_url' => ['nullable', 'string', 'max:2048', 'url'],
|
||||||
|
'message_template' => $this->messageTemplateRules(),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array<int, string>
|
||||||
|
*/
|
||||||
|
private function channels(): array
|
||||||
|
{
|
||||||
|
return array_map(
|
||||||
|
static fn (NotificationChannel $channel): string => $channel->value,
|
||||||
|
NotificationChannel::cases(),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array<int, string>
|
||||||
|
*/
|
||||||
|
private function eventTypes(): array
|
||||||
|
{
|
||||||
|
return array_map(
|
||||||
|
static fn (NotificationEventType $eventType): string => $eventType->value,
|
||||||
|
NotificationEventType::cases(),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array<int, ValidationRule|string>
|
||||||
|
*/
|
||||||
|
private function messageTemplateRules(): array
|
||||||
|
{
|
||||||
|
if ($this->input('channel') === NotificationChannel::Discord->value) {
|
||||||
|
return ['required', 'string', 'max:2000'];
|
||||||
|
}
|
||||||
|
|
||||||
|
return ['nullable', 'string', 'max:2000'];
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,34 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Resources;
|
||||||
|
|
||||||
|
use App\Models\NotificationAction;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use Illuminate\Http\Resources\Json\JsonResource;
|
||||||
|
|
||||||
|
class NotificationActionResource extends JsonResource
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Transform the resource into an array.
|
||||||
|
*
|
||||||
|
* @return array<string, mixed>
|
||||||
|
*/
|
||||||
|
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(),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Resources;
|
||||||
|
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use Illuminate\Http\Resources\Json\JsonResource;
|
||||||
|
|
||||||
|
class NotificationDeliveryLogResource extends JsonResource
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Transform the resource into an array.
|
||||||
|
*
|
||||||
|
* @return array<string, mixed>
|
||||||
|
*/
|
||||||
|
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(),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,62 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Models;
|
||||||
|
|
||||||
|
use App\Enums\NotificationChannel;
|
||||||
|
use App\Enums\NotificationEventType;
|
||||||
|
use Database\Factories\NotificationActionFactory;
|
||||||
|
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||||
|
use Illuminate\Database\Eloquent\Attributes\Hidden;
|
||||||
|
use Illuminate\Database\Eloquent\Builder;
|
||||||
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||||
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||||
|
|
||||||
|
#[Fillable([
|
||||||
|
'channel',
|
||||||
|
'channel_configuration',
|
||||||
|
'delivery_url',
|
||||||
|
'enabled',
|
||||||
|
'event_type',
|
||||||
|
'message_template',
|
||||||
|
'name',
|
||||||
|
'project_id',
|
||||||
|
])]
|
||||||
|
#[Hidden(['delivery_url'])]
|
||||||
|
class NotificationAction extends Model
|
||||||
|
{
|
||||||
|
/** @use HasFactory<NotificationActionFactory> */
|
||||||
|
use HasFactory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array<string, string>
|
||||||
|
*/
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,53 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Models;
|
||||||
|
|
||||||
|
use App\Enums\NotificationChannel;
|
||||||
|
use App\Enums\NotificationDeliveryStatus;
|
||||||
|
use App\Enums\NotificationEventType;
|
||||||
|
use Database\Factories\NotificationDeliveryLogFactory;
|
||||||
|
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||||
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||||
|
|
||||||
|
#[Fillable([
|
||||||
|
'attempt_count',
|
||||||
|
'channel',
|
||||||
|
'delivered_at',
|
||||||
|
'delivery_status',
|
||||||
|
'error_excerpt',
|
||||||
|
'event_type',
|
||||||
|
'http_status',
|
||||||
|
'last_attempted_at',
|
||||||
|
'notification_action_id',
|
||||||
|
'payload_metadata',
|
||||||
|
'response_excerpt',
|
||||||
|
])]
|
||||||
|
class NotificationDeliveryLog extends Model
|
||||||
|
{
|
||||||
|
/** @use HasFactory<NotificationDeliveryLogFactory> */
|
||||||
|
use HasFactory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array<string, string>
|
||||||
|
*/
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -88,6 +88,13 @@ class Project extends Model
|
|||||||
->orderBy('repository_full_name');
|
->orderBy('repository_full_name');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function notificationActions(): HasMany
|
||||||
|
{
|
||||||
|
return $this->hasMany(NotificationAction::class)
|
||||||
|
->orderBy('name')
|
||||||
|
->orderBy('id');
|
||||||
|
}
|
||||||
|
|
||||||
public function tickets(): HasMany
|
public function tickets(): HasMany
|
||||||
{
|
{
|
||||||
return $this->hasMany(Ticket::class)
|
return $this->hasMany(Ticket::class)
|
||||||
|
|||||||
@@ -0,0 +1,58 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Database\Factories;
|
||||||
|
|
||||||
|
use App\Enums\NotificationChannel;
|
||||||
|
use App\Enums\NotificationEventType;
|
||||||
|
use App\Models\NotificationAction;
|
||||||
|
use App\Models\Project;
|
||||||
|
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @extends Factory<NotificationAction>
|
||||||
|
*/
|
||||||
|
class NotificationActionFactory extends Factory
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Define the model's default state.
|
||||||
|
*
|
||||||
|
* @return array<string, mixed>
|
||||||
|
*/
|
||||||
|
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,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,98 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Database\Factories;
|
||||||
|
|
||||||
|
use App\Enums\NotificationChannel;
|
||||||
|
use App\Enums\NotificationDeliveryStatus;
|
||||||
|
use App\Enums\NotificationEventType;
|
||||||
|
use App\Models\NotificationAction;
|
||||||
|
use App\Models\NotificationDeliveryLog;
|
||||||
|
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||||
|
use Illuminate\Support\Carbon;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @extends Factory<NotificationDeliveryLog>
|
||||||
|
*/
|
||||||
|
class NotificationDeliveryLogFactory extends Factory
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Define the model's default state.
|
||||||
|
*
|
||||||
|
* @return array<string, mixed>
|
||||||
|
*/
|
||||||
|
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',
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
|
return new class extends Migration
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Run the migrations.
|
||||||
|
*/
|
||||||
|
public function up(): void
|
||||||
|
{
|
||||||
|
Schema::create('notification_actions', function (Blueprint $table) {
|
||||||
|
$table->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');
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -0,0 +1,41 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
|
return new class extends Migration
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Run the migrations.
|
||||||
|
*/
|
||||||
|
public function up(): void
|
||||||
|
{
|
||||||
|
Schema::create('notification_delivery_logs', function (Blueprint $table) {
|
||||||
|
$table->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');
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -16,6 +16,8 @@ class DatabaseSeeder extends Seeder
|
|||||||
UserSeeder::class,
|
UserSeeder::class,
|
||||||
UserGitConnectionSeeder::class,
|
UserGitConnectionSeeder::class,
|
||||||
ProjectSeeder::class,
|
ProjectSeeder::class,
|
||||||
|
NotificationActionSeeder::class,
|
||||||
|
NotificationDeliveryLogSeeder::class,
|
||||||
RepositoryConnectionSeeder::class,
|
RepositoryConnectionSeeder::class,
|
||||||
BoardColumnSeeder::class,
|
BoardColumnSeeder::class,
|
||||||
ProjectTicketMetadataSeeder::class,
|
ProjectTicketMetadataSeeder::class,
|
||||||
|
|||||||
@@ -0,0 +1,52 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Database\Seeders;
|
||||||
|
|
||||||
|
use App\Enums\NotificationChannel;
|
||||||
|
use App\Enums\NotificationEventType;
|
||||||
|
use App\Models\Project;
|
||||||
|
use Illuminate\Database\Seeder;
|
||||||
|
|
||||||
|
class NotificationActionSeeder extends Seeder
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Run the database seeds.
|
||||||
|
*/
|
||||||
|
public function run(): void
|
||||||
|
{
|
||||||
|
$project = Project::query()->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',
|
||||||
|
],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,42 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Database\Seeders;
|
||||||
|
|
||||||
|
use App\Models\NotificationAction;
|
||||||
|
use App\Models\NotificationDeliveryLog;
|
||||||
|
use Illuminate\Database\Seeder;
|
||||||
|
|
||||||
|
class NotificationDeliveryLogSeeder extends Seeder
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Run the database seeds.
|
||||||
|
*/
|
||||||
|
public function run(): void
|
||||||
|
{
|
||||||
|
$notificationAction = NotificationAction::query()->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',
|
||||||
|
],
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -19,6 +19,7 @@ import {
|
|||||||
board as boardSettings,
|
board as boardSettings,
|
||||||
general as generalSettings,
|
general as generalSettings,
|
||||||
metadata as metadataSettings,
|
metadata as metadataSettings,
|
||||||
|
notifications as notificationSettings,
|
||||||
repositories as repositorySettings,
|
repositories as repositorySettings,
|
||||||
} from '@/routes/projects/settings';
|
} from '@/routes/projects/settings';
|
||||||
import type { BreadcrumbItem, ProjectShowPageProps } from '@/types';
|
import type { BreadcrumbItem, ProjectShowPageProps } from '@/types';
|
||||||
@@ -128,6 +129,12 @@ const projectAreas = [
|
|||||||
</Link>
|
</Link>
|
||||||
</Button>
|
</Button>
|
||||||
|
|
||||||
|
<Button as-child variant="outline">
|
||||||
|
<Link :href="notificationSettings(project.id)">
|
||||||
|
Notification settings
|
||||||
|
</Link>
|
||||||
|
</Button>
|
||||||
|
|
||||||
<Button as-child variant="outline">
|
<Button as-child variant="outline">
|
||||||
<Link :href="access(project.id)">Manage access</Link>
|
<Link :href="access(project.id)">Manage access</Link>
|
||||||
</Button>
|
</Button>
|
||||||
|
|||||||
@@ -19,6 +19,8 @@ import { index, show } from '@/routes/projects/index';
|
|||||||
import {
|
import {
|
||||||
board as boardSettings,
|
board as boardSettings,
|
||||||
general as generalSettings,
|
general as generalSettings,
|
||||||
|
metadata as metadataSettings,
|
||||||
|
notifications as notificationSettings,
|
||||||
repositories as repositorySettings,
|
repositories as repositorySettings,
|
||||||
} from '@/routes/projects/settings';
|
} from '@/routes/projects/settings';
|
||||||
import type { BreadcrumbItem, ProjectBoardSettingsPageProps } from '@/types';
|
import type { BreadcrumbItem, ProjectBoardSettingsPageProps } from '@/types';
|
||||||
@@ -105,12 +107,24 @@ const statusAllowsTarget = (
|
|||||||
</Link>
|
</Link>
|
||||||
</Button>
|
</Button>
|
||||||
|
|
||||||
|
<Button as-child variant="outline">
|
||||||
|
<Link :href="metadataSettings(project.id)">
|
||||||
|
Ticket settings
|
||||||
|
</Link>
|
||||||
|
</Button>
|
||||||
|
|
||||||
<Button as-child variant="outline">
|
<Button as-child variant="outline">
|
||||||
<Link :href="repositorySettings(project.id)">
|
<Link :href="repositorySettings(project.id)">
|
||||||
Repository settings
|
Repository settings
|
||||||
</Link>
|
</Link>
|
||||||
</Button>
|
</Button>
|
||||||
|
|
||||||
|
<Button as-child variant="outline">
|
||||||
|
<Link :href="notificationSettings(project.id)">
|
||||||
|
Notification settings
|
||||||
|
</Link>
|
||||||
|
</Button>
|
||||||
|
|
||||||
<Button as-child variant="outline">
|
<Button as-child variant="outline">
|
||||||
<Link :href="show(project.id)">Back to project</Link>
|
<Link :href="show(project.id)">Back to project</Link>
|
||||||
</Button>
|
</Button>
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ import {
|
|||||||
board as boardSettings,
|
board as boardSettings,
|
||||||
general as generalSettings,
|
general as generalSettings,
|
||||||
metadata as metadataSettings,
|
metadata as metadataSettings,
|
||||||
|
notifications as notificationSettings,
|
||||||
repositories as repositorySettings,
|
repositories as repositorySettings,
|
||||||
} from '@/routes/projects/settings';
|
} from '@/routes/projects/settings';
|
||||||
import type { BreadcrumbItem, ProjectGeneralSettingsPageProps } from '@/types';
|
import type { BreadcrumbItem, ProjectGeneralSettingsPageProps } from '@/types';
|
||||||
@@ -98,6 +99,12 @@ const submitDelete = (): void => {
|
|||||||
</Link>
|
</Link>
|
||||||
</Button>
|
</Button>
|
||||||
|
|
||||||
|
<Button as-child variant="outline">
|
||||||
|
<Link :href="notificationSettings(project.id)">
|
||||||
|
Notification settings
|
||||||
|
</Link>
|
||||||
|
</Button>
|
||||||
|
|
||||||
<Button as-child variant="outline">
|
<Button as-child variant="outline">
|
||||||
<Link :href="show(project.id)">Back to project</Link>
|
<Link :href="show(project.id)">Back to project</Link>
|
||||||
</Button>
|
</Button>
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ import {
|
|||||||
board as boardSettings,
|
board as boardSettings,
|
||||||
general as generalSettings,
|
general as generalSettings,
|
||||||
metadata as metadataSettings,
|
metadata as metadataSettings,
|
||||||
|
notifications as notificationSettings,
|
||||||
repositories as repositorySettings,
|
repositories as repositorySettings,
|
||||||
} from '@/routes/projects/settings';
|
} from '@/routes/projects/settings';
|
||||||
import type {
|
import type {
|
||||||
@@ -90,6 +91,12 @@ const replacementTicketTypes = (ticketTypeId: number) =>
|
|||||||
</Link>
|
</Link>
|
||||||
</Button>
|
</Button>
|
||||||
|
|
||||||
|
<Button as-child variant="outline">
|
||||||
|
<Link :href="notificationSettings(project.id)">
|
||||||
|
Notification settings
|
||||||
|
</Link>
|
||||||
|
</Button>
|
||||||
|
|
||||||
<Button as-child variant="outline">
|
<Button as-child variant="outline">
|
||||||
<Link :href="show(project.id)">Back to project</Link>
|
<Link :href="show(project.id)">Back to project</Link>
|
||||||
</Button>
|
</Button>
|
||||||
|
|||||||
@@ -0,0 +1,640 @@
|
|||||||
|
<script setup lang="ts">
|
||||||
|
import { Form, Head, Link } from '@inertiajs/vue3';
|
||||||
|
import { reactive } from 'vue';
|
||||||
|
import ProjectNotificationSettingsController from '@/actions/App/Http/Controllers/ProjectNotificationSettingsController';
|
||||||
|
import Heading from '@/components/Heading.vue';
|
||||||
|
import InputError from '@/components/InputError.vue';
|
||||||
|
import { Badge } from '@/components/ui/badge';
|
||||||
|
import { Button } from '@/components/ui/button';
|
||||||
|
import {
|
||||||
|
Card,
|
||||||
|
CardContent,
|
||||||
|
CardDescription,
|
||||||
|
CardHeader,
|
||||||
|
CardTitle,
|
||||||
|
} from '@/components/ui/card';
|
||||||
|
import { Input } from '@/components/ui/input';
|
||||||
|
import { Label } from '@/components/ui/label';
|
||||||
|
import AppLayout from '@/layouts/AppLayout.vue';
|
||||||
|
import { index, show } from '@/routes/projects/index';
|
||||||
|
import {
|
||||||
|
board as boardSettings,
|
||||||
|
general as generalSettings,
|
||||||
|
metadata as metadataSettings,
|
||||||
|
notifications as notificationSettings,
|
||||||
|
repositories as repositorySettings,
|
||||||
|
} from '@/routes/projects/settings';
|
||||||
|
import type {
|
||||||
|
BreadcrumbItem,
|
||||||
|
ProjectNotificationSettingsPageProps,
|
||||||
|
} from '@/types';
|
||||||
|
import type { NotificationActionModel } from '@/types/models/notification-action-model';
|
||||||
|
|
||||||
|
const props = defineProps<ProjectNotificationSettingsPageProps>();
|
||||||
|
|
||||||
|
const defaultCreateChannel =
|
||||||
|
props.channelOptions.find((option) => option.value === 'webhook')?.value ??
|
||||||
|
props.channelOptions[0]?.value ??
|
||||||
|
'';
|
||||||
|
const createChannelState = reactive<{ value: string }>({
|
||||||
|
value: defaultCreateChannel,
|
||||||
|
});
|
||||||
|
const editChannelState = reactive<Record<number, string>>({});
|
||||||
|
const timestampFormatter = new Intl.DateTimeFormat(undefined, {
|
||||||
|
dateStyle: 'medium',
|
||||||
|
timeStyle: 'short',
|
||||||
|
});
|
||||||
|
|
||||||
|
const breadcrumbs: BreadcrumbItem[] = [
|
||||||
|
{
|
||||||
|
title: 'Projects',
|
||||||
|
href: index(),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
title: props.project.name,
|
||||||
|
href: show(props.project.id),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
title: 'Notification settings',
|
||||||
|
href: notificationSettings(props.project.id),
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
props.notificationActions.forEach((notificationAction) => {
|
||||||
|
editChannelState[notificationAction.id] = notificationAction.channel;
|
||||||
|
});
|
||||||
|
|
||||||
|
const selectedChannel = (notificationAction: NotificationActionModel): string =>
|
||||||
|
editChannelState[notificationAction.id] ?? notificationAction.channel;
|
||||||
|
|
||||||
|
const notificationActionTimestamp = (
|
||||||
|
notificationAction: NotificationActionModel,
|
||||||
|
): string => {
|
||||||
|
const createdAt = notificationAction.created_at;
|
||||||
|
const updatedAt = notificationAction.updated_at ?? createdAt;
|
||||||
|
|
||||||
|
if (updatedAt === null) {
|
||||||
|
return 'No saved changes yet';
|
||||||
|
}
|
||||||
|
|
||||||
|
if (createdAt !== null && createdAt !== updatedAt) {
|
||||||
|
return `Updated ${timestampFormatter.format(new Date(updatedAt))}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
return `Created ${timestampFormatter.format(new Date(updatedAt))}`;
|
||||||
|
};
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<template>
|
||||||
|
<AppLayout :breadcrumbs="breadcrumbs">
|
||||||
|
<Head :title="`${project.name} notification settings`" />
|
||||||
|
|
||||||
|
<div class="flex flex-1 flex-col gap-6 p-4">
|
||||||
|
<div
|
||||||
|
class="flex flex-col gap-4 sm:flex-row sm:items-start sm:justify-between"
|
||||||
|
>
|
||||||
|
<div class="space-y-3">
|
||||||
|
<Badge variant="outline" class="px-3 py-1">
|
||||||
|
{{ project.key }}
|
||||||
|
</Badge>
|
||||||
|
<Heading
|
||||||
|
:title="`${project.name} notification settings`"
|
||||||
|
description="Configure project-scoped webhook and Discord notification actions for ticket status changes."
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="flex flex-wrap gap-3">
|
||||||
|
<Button
|
||||||
|
v-if="$page.props.auth.permissions.globalAdmin"
|
||||||
|
as-child
|
||||||
|
variant="outline"
|
||||||
|
>
|
||||||
|
<Link :href="generalSettings(project.id)">
|
||||||
|
General settings
|
||||||
|
</Link>
|
||||||
|
</Button>
|
||||||
|
|
||||||
|
<Button as-child variant="outline">
|
||||||
|
<Link :href="boardSettings(project.id)">
|
||||||
|
Board settings
|
||||||
|
</Link>
|
||||||
|
</Button>
|
||||||
|
|
||||||
|
<Button as-child variant="outline">
|
||||||
|
<Link :href="metadataSettings(project.id)">
|
||||||
|
Ticket settings
|
||||||
|
</Link>
|
||||||
|
</Button>
|
||||||
|
|
||||||
|
<Button as-child variant="outline">
|
||||||
|
<Link :href="repositorySettings(project.id)">
|
||||||
|
Repository settings
|
||||||
|
</Link>
|
||||||
|
</Button>
|
||||||
|
|
||||||
|
<Button as-child variant="outline">
|
||||||
|
<Link :href="show(project.id)">Back to project</Link>
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div
|
||||||
|
v-if="status"
|
||||||
|
class="rounded-xl border border-emerald-200 bg-emerald-50 px-4 py-3 text-sm text-emerald-900 dark:border-emerald-900/70 dark:bg-emerald-950/40 dark:text-emerald-100"
|
||||||
|
>
|
||||||
|
{{ status }}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="grid gap-6 xl:grid-cols-[24rem_minmax(0,1fr)]">
|
||||||
|
<div class="space-y-6">
|
||||||
|
<Card>
|
||||||
|
<CardHeader>
|
||||||
|
<CardTitle>Create notification</CardTitle>
|
||||||
|
<CardDescription>
|
||||||
|
Add a project-scoped notification action for
|
||||||
|
the supported ticket status change event.
|
||||||
|
</CardDescription>
|
||||||
|
</CardHeader>
|
||||||
|
|
||||||
|
<CardContent>
|
||||||
|
<Form
|
||||||
|
v-bind="
|
||||||
|
ProjectNotificationSettingsController.store.form(
|
||||||
|
project.id,
|
||||||
|
)
|
||||||
|
"
|
||||||
|
error-bag="createNotificationAction"
|
||||||
|
reset-on-success
|
||||||
|
@success="
|
||||||
|
createChannelState.value =
|
||||||
|
defaultCreateChannel
|
||||||
|
"
|
||||||
|
class="space-y-4"
|
||||||
|
v-slot="{ errors, processing }"
|
||||||
|
>
|
||||||
|
<div class="grid gap-2">
|
||||||
|
<Label for="create-notification-name">
|
||||||
|
Name
|
||||||
|
</Label>
|
||||||
|
<Input
|
||||||
|
id="create-notification-name"
|
||||||
|
name="name"
|
||||||
|
placeholder="Discord alerts"
|
||||||
|
/>
|
||||||
|
<InputError :message="errors.name" />
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="grid gap-4 md:grid-cols-2">
|
||||||
|
<div class="grid gap-2">
|
||||||
|
<Label
|
||||||
|
for="create-notification-event-type"
|
||||||
|
>
|
||||||
|
Event type
|
||||||
|
</Label>
|
||||||
|
<select
|
||||||
|
id="create-notification-event-type"
|
||||||
|
name="event_type"
|
||||||
|
class="border-input bg-background shadow-xs focus-visible:border-ring focus-visible:ring-ring/50 rounded-md border px-3 py-2 text-sm outline-none transition-[color,box-shadow] focus-visible:ring-[3px]"
|
||||||
|
>
|
||||||
|
<option
|
||||||
|
v-for="eventTypeOption in eventTypeOptions"
|
||||||
|
:key="eventTypeOption.value"
|
||||||
|
:value="eventTypeOption.value"
|
||||||
|
>
|
||||||
|
{{ eventTypeOption.label }}
|
||||||
|
</option>
|
||||||
|
</select>
|
||||||
|
<InputError
|
||||||
|
:message="errors.event_type"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="grid gap-2">
|
||||||
|
<Label for="create-notification-channel">
|
||||||
|
Channel
|
||||||
|
</Label>
|
||||||
|
<select
|
||||||
|
id="create-notification-channel"
|
||||||
|
name="channel"
|
||||||
|
v-model="createChannelState.value"
|
||||||
|
class="border-input bg-background shadow-xs focus-visible:border-ring focus-visible:ring-ring/50 rounded-md border px-3 py-2 text-sm outline-none transition-[color,box-shadow] focus-visible:ring-[3px]"
|
||||||
|
>
|
||||||
|
<option
|
||||||
|
v-for="channelOption in channelOptions"
|
||||||
|
:key="channelOption.value"
|
||||||
|
:value="channelOption.value"
|
||||||
|
>
|
||||||
|
{{ channelOption.label }}
|
||||||
|
</option>
|
||||||
|
</select>
|
||||||
|
<InputError :message="errors.channel" />
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="grid gap-2">
|
||||||
|
<Label for="create-notification-delivery-url">
|
||||||
|
Delivery URL
|
||||||
|
</Label>
|
||||||
|
<Input
|
||||||
|
id="create-notification-delivery-url"
|
||||||
|
name="delivery_url"
|
||||||
|
type="url"
|
||||||
|
placeholder="https://hooks.example.test/kanbangit/secret-token"
|
||||||
|
/>
|
||||||
|
<p class="text-muted-foreground text-sm">
|
||||||
|
Secret URLs are stored encrypted and are
|
||||||
|
never rendered back to the browser after
|
||||||
|
save.
|
||||||
|
</p>
|
||||||
|
<InputError :message="errors.delivery_url" />
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="grid gap-2">
|
||||||
|
<Label
|
||||||
|
for="create-notification-message-template"
|
||||||
|
>
|
||||||
|
Discord message template
|
||||||
|
</Label>
|
||||||
|
<textarea
|
||||||
|
id="create-notification-message-template"
|
||||||
|
name="message_template"
|
||||||
|
rows="4"
|
||||||
|
:disabled="
|
||||||
|
createChannelState.value !==
|
||||||
|
'discord'
|
||||||
|
"
|
||||||
|
:class="[
|
||||||
|
'border-input bg-background shadow-xs focus-visible:border-ring focus-visible:ring-ring/50 min-h-28 rounded-md border px-3 py-2 text-sm outline-none transition-[color,box-shadow] focus-visible:ring-[3px]',
|
||||||
|
createChannelState.value !==
|
||||||
|
'discord'
|
||||||
|
? 'hidden'
|
||||||
|
: 'block',
|
||||||
|
]"
|
||||||
|
placeholder="$ticket_key moved from $status_from to $status_to by $actor_name: $url"
|
||||||
|
/>
|
||||||
|
<p class="text-muted-foreground text-sm">
|
||||||
|
{{
|
||||||
|
createChannelState.value ===
|
||||||
|
'discord'
|
||||||
|
? 'Available for Discord actions only. Use the fixed variables listed below.'
|
||||||
|
: 'Discord message templates are only used when the channel is Discord.'
|
||||||
|
}}
|
||||||
|
</p>
|
||||||
|
<InputError
|
||||||
|
:message="errors.message_template"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="grid gap-2">
|
||||||
|
<Label
|
||||||
|
for="create-notification-enabled"
|
||||||
|
class="flex items-center gap-3"
|
||||||
|
>
|
||||||
|
<input
|
||||||
|
type="hidden"
|
||||||
|
name="enabled"
|
||||||
|
value="0"
|
||||||
|
/>
|
||||||
|
<input
|
||||||
|
id="create-notification-enabled"
|
||||||
|
type="checkbox"
|
||||||
|
name="enabled"
|
||||||
|
value="1"
|
||||||
|
checked
|
||||||
|
class="border-input text-primary focus:ring-ring size-4 rounded"
|
||||||
|
/>
|
||||||
|
<span>Enabled immediately</span>
|
||||||
|
</Label>
|
||||||
|
<InputError :message="errors.enabled" />
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<Button type="submit" :disabled="processing">
|
||||||
|
Save notification
|
||||||
|
</Button>
|
||||||
|
</Form>
|
||||||
|
</CardContent>
|
||||||
|
</Card>
|
||||||
|
|
||||||
|
<Card>
|
||||||
|
<CardHeader>
|
||||||
|
<CardTitle>Template variables</CardTitle>
|
||||||
|
<CardDescription>
|
||||||
|
Discord templates can reference the following
|
||||||
|
fixed variables.
|
||||||
|
</CardDescription>
|
||||||
|
</CardHeader>
|
||||||
|
|
||||||
|
<CardContent class="space-y-3">
|
||||||
|
<div
|
||||||
|
v-for="templateVariable in templateVariables"
|
||||||
|
:key="templateVariable.value"
|
||||||
|
class="border-border/70 rounded-xl border p-4"
|
||||||
|
>
|
||||||
|
<div class="flex flex-wrap items-center gap-2">
|
||||||
|
<Badge variant="secondary">
|
||||||
|
{{ templateVariable.value }}
|
||||||
|
</Badge>
|
||||||
|
</div>
|
||||||
|
<p class="text-muted-foreground mt-2 text-sm">
|
||||||
|
{{ templateVariable.description }}
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
</CardContent>
|
||||||
|
</Card>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="space-y-6">
|
||||||
|
<Card
|
||||||
|
v-if="notificationActions.length === 0"
|
||||||
|
class="border-dashed"
|
||||||
|
>
|
||||||
|
<CardHeader>
|
||||||
|
<CardTitle>No notifications configured</CardTitle>
|
||||||
|
<CardDescription>
|
||||||
|
Create your first webhook or Discord action to
|
||||||
|
start sending project notifications later in
|
||||||
|
this release.
|
||||||
|
</CardDescription>
|
||||||
|
</CardHeader>
|
||||||
|
</Card>
|
||||||
|
|
||||||
|
<Card
|
||||||
|
v-for="notificationAction in notificationActions"
|
||||||
|
:key="notificationAction.id"
|
||||||
|
>
|
||||||
|
<CardHeader
|
||||||
|
class="flex flex-col gap-4 sm:flex-row sm:items-start sm:justify-between"
|
||||||
|
>
|
||||||
|
<div class="space-y-3">
|
||||||
|
<div class="flex flex-wrap items-center gap-2">
|
||||||
|
<CardTitle>
|
||||||
|
{{ notificationAction.name }}
|
||||||
|
</CardTitle>
|
||||||
|
<Badge variant="outline">
|
||||||
|
{{
|
||||||
|
notificationAction.event_type_label
|
||||||
|
}}
|
||||||
|
</Badge>
|
||||||
|
<Badge variant="outline">
|
||||||
|
{{ notificationAction.channel_label }}
|
||||||
|
</Badge>
|
||||||
|
<Badge
|
||||||
|
:variant="
|
||||||
|
notificationAction.enabled
|
||||||
|
? 'default'
|
||||||
|
: 'secondary'
|
||||||
|
"
|
||||||
|
>
|
||||||
|
{{
|
||||||
|
notificationAction.enabled
|
||||||
|
? 'Enabled'
|
||||||
|
: 'Disabled'
|
||||||
|
}}
|
||||||
|
</Badge>
|
||||||
|
</div>
|
||||||
|
<CardDescription>
|
||||||
|
{{ notificationActionTimestamp(notificationAction) }}
|
||||||
|
</CardDescription>
|
||||||
|
</div>
|
||||||
|
</CardHeader>
|
||||||
|
|
||||||
|
<CardContent class="space-y-4">
|
||||||
|
<Form
|
||||||
|
v-bind="
|
||||||
|
ProjectNotificationSettingsController.update.form(
|
||||||
|
[project.id, notificationAction.id],
|
||||||
|
)
|
||||||
|
"
|
||||||
|
:error-bag="
|
||||||
|
`updateNotificationAction-${notificationAction.id}`
|
||||||
|
"
|
||||||
|
class="space-y-4"
|
||||||
|
v-slot="{ errors, processing }"
|
||||||
|
>
|
||||||
|
<div class="grid gap-4 md:grid-cols-2">
|
||||||
|
<div class="grid gap-2">
|
||||||
|
<Label
|
||||||
|
:for="
|
||||||
|
`notification-name-${notificationAction.id}`
|
||||||
|
"
|
||||||
|
>
|
||||||
|
Name
|
||||||
|
</Label>
|
||||||
|
<Input
|
||||||
|
:id="
|
||||||
|
`notification-name-${notificationAction.id}`
|
||||||
|
"
|
||||||
|
name="name"
|
||||||
|
:default-value="
|
||||||
|
notificationAction.name
|
||||||
|
"
|
||||||
|
/>
|
||||||
|
<InputError :message="errors.name" />
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="grid gap-2">
|
||||||
|
<Label
|
||||||
|
:for="
|
||||||
|
`notification-delivery-url-${notificationAction.id}`
|
||||||
|
"
|
||||||
|
>
|
||||||
|
Delivery URL
|
||||||
|
</Label>
|
||||||
|
<Input
|
||||||
|
:id="
|
||||||
|
`notification-delivery-url-${notificationAction.id}`
|
||||||
|
"
|
||||||
|
name="delivery_url"
|
||||||
|
type="url"
|
||||||
|
:placeholder="
|
||||||
|
notificationAction.has_delivery_url
|
||||||
|
? 'Saved URL stays hidden. Enter a new URL to replace it.'
|
||||||
|
: 'https://hooks.example.test/kanbangit/secret-token'
|
||||||
|
"
|
||||||
|
/>
|
||||||
|
<p
|
||||||
|
class="text-muted-foreground text-sm"
|
||||||
|
>
|
||||||
|
{{
|
||||||
|
notificationAction.has_delivery_url
|
||||||
|
? 'A delivery URL is already stored. Leaving this blank keeps the existing secret.'
|
||||||
|
: 'No delivery URL is stored yet. Enter a secret URL to enable delivery later.'
|
||||||
|
}}
|
||||||
|
</p>
|
||||||
|
<InputError
|
||||||
|
:message="errors.delivery_url"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="grid gap-2">
|
||||||
|
<Label
|
||||||
|
:for="
|
||||||
|
`notification-event-type-${notificationAction.id}`
|
||||||
|
"
|
||||||
|
>
|
||||||
|
Event type
|
||||||
|
</Label>
|
||||||
|
<select
|
||||||
|
:id="
|
||||||
|
`notification-event-type-${notificationAction.id}`
|
||||||
|
"
|
||||||
|
name="event_type"
|
||||||
|
:default-value="
|
||||||
|
notificationAction.event_type
|
||||||
|
"
|
||||||
|
class="border-input bg-background shadow-xs focus-visible:border-ring focus-visible:ring-ring/50 rounded-md border px-3 py-2 text-sm outline-none transition-[color,box-shadow] focus-visible:ring-[3px]"
|
||||||
|
>
|
||||||
|
<option
|
||||||
|
v-for="eventTypeOption in eventTypeOptions"
|
||||||
|
:key="eventTypeOption.value"
|
||||||
|
:value="eventTypeOption.value"
|
||||||
|
>
|
||||||
|
{{ eventTypeOption.label }}
|
||||||
|
</option>
|
||||||
|
</select>
|
||||||
|
<InputError
|
||||||
|
:message="errors.event_type"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="grid gap-2">
|
||||||
|
<Label
|
||||||
|
:for="
|
||||||
|
`notification-channel-${notificationAction.id}`
|
||||||
|
"
|
||||||
|
>
|
||||||
|
Channel
|
||||||
|
</Label>
|
||||||
|
<select
|
||||||
|
:id="
|
||||||
|
`notification-channel-${notificationAction.id}`
|
||||||
|
"
|
||||||
|
name="channel"
|
||||||
|
v-model="
|
||||||
|
editChannelState[
|
||||||
|
notificationAction.id
|
||||||
|
]
|
||||||
|
"
|
||||||
|
class="border-input bg-background shadow-xs focus-visible:border-ring focus-visible:ring-ring/50 rounded-md border px-3 py-2 text-sm outline-none transition-[color,box-shadow] focus-visible:ring-[3px]"
|
||||||
|
>
|
||||||
|
<option
|
||||||
|
v-for="channelOption in channelOptions"
|
||||||
|
:key="channelOption.value"
|
||||||
|
:value="channelOption.value"
|
||||||
|
>
|
||||||
|
{{ channelOption.label }}
|
||||||
|
</option>
|
||||||
|
</select>
|
||||||
|
<InputError :message="errors.channel" />
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="grid gap-2">
|
||||||
|
<Label
|
||||||
|
:for="
|
||||||
|
`notification-message-template-${notificationAction.id}`
|
||||||
|
"
|
||||||
|
>
|
||||||
|
Discord message template
|
||||||
|
</Label>
|
||||||
|
<textarea
|
||||||
|
:id="
|
||||||
|
`notification-message-template-${notificationAction.id}`
|
||||||
|
"
|
||||||
|
name="message_template"
|
||||||
|
rows="4"
|
||||||
|
:value="
|
||||||
|
notificationAction.message_template ??
|
||||||
|
''
|
||||||
|
"
|
||||||
|
:disabled="
|
||||||
|
selectedChannel(notificationAction) !==
|
||||||
|
'discord'
|
||||||
|
"
|
||||||
|
:class="[
|
||||||
|
'border-input bg-background shadow-xs focus-visible:border-ring focus-visible:ring-ring/50 min-h-28 rounded-md border px-3 py-2 text-sm outline-none transition-[color,box-shadow] focus-visible:ring-[3px]',
|
||||||
|
selectedChannel(notificationAction) !==
|
||||||
|
'discord'
|
||||||
|
? 'hidden'
|
||||||
|
: 'block',
|
||||||
|
]"
|
||||||
|
placeholder="$ticket_key moved from $status_from to $status_to by $actor_name: $url"
|
||||||
|
/>
|
||||||
|
<p class="text-muted-foreground text-sm">
|
||||||
|
{{
|
||||||
|
selectedChannel(notificationAction) ===
|
||||||
|
'discord'
|
||||||
|
? 'Available for Discord actions only. Use the fixed variables listed in the sidebar.'
|
||||||
|
: 'Discord message templates are only used when the channel is Discord.'
|
||||||
|
}}
|
||||||
|
</p>
|
||||||
|
<InputError
|
||||||
|
:message="errors.message_template"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div
|
||||||
|
class="flex flex-col gap-3 border-t pt-4 sm:flex-row sm:items-center sm:justify-between"
|
||||||
|
>
|
||||||
|
<div class="grid gap-2">
|
||||||
|
<Label
|
||||||
|
:for="
|
||||||
|
`notification-enabled-${notificationAction.id}`
|
||||||
|
"
|
||||||
|
class="flex items-center gap-3"
|
||||||
|
>
|
||||||
|
<input
|
||||||
|
type="hidden"
|
||||||
|
name="enabled"
|
||||||
|
value="0"
|
||||||
|
/>
|
||||||
|
<input
|
||||||
|
:id="
|
||||||
|
`notification-enabled-${notificationAction.id}`
|
||||||
|
"
|
||||||
|
type="checkbox"
|
||||||
|
name="enabled"
|
||||||
|
value="1"
|
||||||
|
:default-checked="
|
||||||
|
notificationAction.enabled
|
||||||
|
"
|
||||||
|
class="border-input text-primary focus:ring-ring size-4 rounded"
|
||||||
|
/>
|
||||||
|
<span>Enabled</span>
|
||||||
|
</Label>
|
||||||
|
<InputError :message="errors.enabled" />
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<Button
|
||||||
|
type="submit"
|
||||||
|
:disabled="processing"
|
||||||
|
>
|
||||||
|
Save changes
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
</Form>
|
||||||
|
|
||||||
|
<Form
|
||||||
|
v-bind="
|
||||||
|
ProjectNotificationSettingsController.destroy.form(
|
||||||
|
[project.id, notificationAction.id],
|
||||||
|
)
|
||||||
|
"
|
||||||
|
class="border-t pt-4"
|
||||||
|
v-slot="{ processing }"
|
||||||
|
>
|
||||||
|
<Button
|
||||||
|
type="submit"
|
||||||
|
variant="destructive"
|
||||||
|
:disabled="processing"
|
||||||
|
>
|
||||||
|
Delete notification
|
||||||
|
</Button>
|
||||||
|
</Form>
|
||||||
|
</CardContent>
|
||||||
|
</Card>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</AppLayout>
|
||||||
|
</template>
|
||||||
@@ -21,6 +21,7 @@ import {
|
|||||||
board as boardSettings,
|
board as boardSettings,
|
||||||
general as generalSettings,
|
general as generalSettings,
|
||||||
metadata as metadataSettings,
|
metadata as metadataSettings,
|
||||||
|
notifications as notificationSettings,
|
||||||
repositories as repositorySettings,
|
repositories as repositorySettings,
|
||||||
} from '@/routes/projects/settings';
|
} from '@/routes/projects/settings';
|
||||||
import type { AvailableGitRepositorySourceModel } from '@/types/models/available-git-repository-source-model';
|
import type { AvailableGitRepositorySourceModel } from '@/types/models/available-git-repository-source-model';
|
||||||
@@ -257,6 +258,12 @@ const submitUpdate = (): void => {
|
|||||||
</Link>
|
</Link>
|
||||||
</Button>
|
</Button>
|
||||||
|
|
||||||
|
<Button as-child variant="outline">
|
||||||
|
<Link :href="notificationSettings(project.id)">
|
||||||
|
Notification settings
|
||||||
|
</Link>
|
||||||
|
</Button>
|
||||||
|
|
||||||
<Button as-child variant="outline">
|
<Button as-child variant="outline">
|
||||||
<Link :href="show(project.id)">Back to project</Link>
|
<Link :href="show(project.id)">Back to project</Link>
|
||||||
</Button>
|
</Button>
|
||||||
|
|||||||
@@ -0,0 +1,15 @@
|
|||||||
|
export type NotificationActionModel = {
|
||||||
|
id: number;
|
||||||
|
project_id: number;
|
||||||
|
name: string;
|
||||||
|
enabled: boolean;
|
||||||
|
event_type: string;
|
||||||
|
event_type_label: string;
|
||||||
|
channel: string;
|
||||||
|
channel_label: string;
|
||||||
|
channel_configuration: Record<string, unknown>;
|
||||||
|
has_delivery_url: boolean;
|
||||||
|
message_template: string | null;
|
||||||
|
created_at: string | null;
|
||||||
|
updated_at: string | null;
|
||||||
|
};
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
export type NotificationActionOptionModel = {
|
||||||
|
label: string;
|
||||||
|
value: string;
|
||||||
|
};
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
export type NotificationActionTemplateVariableModel = {
|
||||||
|
description: string;
|
||||||
|
value: string;
|
||||||
|
};
|
||||||
@@ -1,5 +1,8 @@
|
|||||||
import type { BoardColumnModel } from '@/types/models/board-column-model';
|
import type { BoardColumnModel } from '@/types/models/board-column-model';
|
||||||
import type { AvailableGitRepositorySourceModel } from '@/types/models/available-git-repository-source-model';
|
import type { AvailableGitRepositorySourceModel } from '@/types/models/available-git-repository-source-model';
|
||||||
|
import type { NotificationActionModel } from '@/types/models/notification-action-model';
|
||||||
|
import type { NotificationActionOptionModel } from '@/types/models/notification-action-option-model';
|
||||||
|
import type { NotificationActionTemplateVariableModel } from '@/types/models/notification-action-template-variable-model';
|
||||||
import type { ProjectModel } from '@/types/models/project-model';
|
import type { ProjectModel } from '@/types/models/project-model';
|
||||||
import type { RepositoryConnectionModel } from '@/types/models/repository-connection-model';
|
import type { RepositoryConnectionModel } from '@/types/models/repository-connection-model';
|
||||||
import type { TeamModel } from '@/types/models/team-model';
|
import type { TeamModel } from '@/types/models/team-model';
|
||||||
@@ -103,6 +106,15 @@ export type ProjectRepositorySettingsPageProps = {
|
|||||||
status?: string | null;
|
status?: string | null;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export type ProjectNotificationSettingsPageProps = {
|
||||||
|
channelOptions: NotificationActionOptionModel[];
|
||||||
|
eventTypeOptions: NotificationActionOptionModel[];
|
||||||
|
notificationActions: NotificationActionModel[];
|
||||||
|
project: ProjectModel;
|
||||||
|
status?: string | null;
|
||||||
|
templateVariables: NotificationActionTemplateVariableModel[];
|
||||||
|
};
|
||||||
|
|
||||||
export type ProjectBacklogPageProps = {
|
export type ProjectBacklogPageProps = {
|
||||||
backlogTicketStatuses: TicketStatusModel[];
|
backlogTicketStatuses: TicketStatusModel[];
|
||||||
canCreateTickets: boolean;
|
canCreateTickets: boolean;
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
use App\Http\Controllers\ProjectBoardSettingsController;
|
use App\Http\Controllers\ProjectBoardSettingsController;
|
||||||
use App\Http\Controllers\ProjectController;
|
use App\Http\Controllers\ProjectController;
|
||||||
|
use App\Http\Controllers\ProjectNotificationSettingsController;
|
||||||
use App\Http\Controllers\ProjectRepositorySettingsController;
|
use App\Http\Controllers\ProjectRepositorySettingsController;
|
||||||
use App\Http\Controllers\ProjectTicketMetadataSettingsController;
|
use App\Http\Controllers\ProjectTicketMetadataSettingsController;
|
||||||
use Illuminate\Support\Facades\Route;
|
use Illuminate\Support\Facades\Route;
|
||||||
@@ -35,3 +36,12 @@ Route::prefix('repositories')
|
|||||||
->group(function (): void {
|
->group(function (): void {
|
||||||
require __DIR__.'/settings/repositories.php';
|
require __DIR__.'/settings/repositories.php';
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Route::get('notifications', [ProjectNotificationSettingsController::class, 'show'])->name('notifications');
|
||||||
|
|
||||||
|
Route::prefix('notifications')
|
||||||
|
->name('notifications.')
|
||||||
|
->controller(ProjectNotificationSettingsController::class)
|
||||||
|
->group(function (): void {
|
||||||
|
require __DIR__.'/settings/notifications.php';
|
||||||
|
});
|
||||||
|
|||||||
@@ -0,0 +1,7 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Support\Facades\Route;
|
||||||
|
|
||||||
|
Route::post('/', 'store')->name('store');
|
||||||
|
Route::patch('{notificationAction}', 'update')->name('update');
|
||||||
|
Route::delete('{notificationAction}', 'destroy')->name('destroy');
|
||||||
@@ -0,0 +1,65 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Tests\Feature\Models;
|
||||||
|
|
||||||
|
use App\Enums\NotificationChannel;
|
||||||
|
use App\Enums\NotificationEventType;
|
||||||
|
use App\Http\Resources\NotificationActionResource;
|
||||||
|
use App\Models\NotificationAction;
|
||||||
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use Illuminate\Support\Facades\DB;
|
||||||
|
use Tests\TestCase;
|
||||||
|
|
||||||
|
class NotificationActionTest extends TestCase
|
||||||
|
{
|
||||||
|
use RefreshDatabase;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Ensure webhook notification actions encrypt their delivery URL and cast enum fields.
|
||||||
|
*/
|
||||||
|
public function test_factory_creates_an_encrypted_webhook_notification_action(): void
|
||||||
|
{
|
||||||
|
$notificationAction = NotificationAction::factory()->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']);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,66 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Tests\Feature\Models;
|
||||||
|
|
||||||
|
use App\Enums\NotificationChannel;
|
||||||
|
use App\Enums\NotificationDeliveryStatus;
|
||||||
|
use App\Models\NotificationDeliveryLog;
|
||||||
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||||
|
use Tests\TestCase;
|
||||||
|
|
||||||
|
class NotificationDeliveryLogTest extends TestCase
|
||||||
|
{
|
||||||
|
use RefreshDatabase;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Ensure delivery log factories create related notification actions with consistent casts.
|
||||||
|
*/
|
||||||
|
public function test_delivery_log_factory_creates_a_related_notification_action(): void
|
||||||
|
{
|
||||||
|
$deliveryLog = NotificationDeliveryLog::factory()->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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,144 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Tests\Feature\Notifications;
|
||||||
|
|
||||||
|
use App\Actions\Notifications\DeleteProjectNotificationAction;
|
||||||
|
use App\Actions\Notifications\ListProjectNotificationActions;
|
||||||
|
use App\Actions\Notifications\StoreProjectNotificationAction;
|
||||||
|
use App\Actions\Notifications\UpdateProjectNotificationAction;
|
||||||
|
use App\Enums\NotificationChannel;
|
||||||
|
use App\Enums\NotificationEventType;
|
||||||
|
use App\Models\NotificationAction;
|
||||||
|
use App\Models\Project;
|
||||||
|
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
||||||
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||||
|
use Tests\TestCase;
|
||||||
|
|
||||||
|
class ProjectNotificationActionManagementTest extends TestCase
|
||||||
|
{
|
||||||
|
use RefreshDatabase;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Ensure notification actions can be created and listed for a single project.
|
||||||
|
*/
|
||||||
|
public function test_notification_actions_can_be_created_and_listed_for_a_project(): void
|
||||||
|
{
|
||||||
|
$storeAction = app(StoreProjectNotificationAction::class);
|
||||||
|
$listAction = app(ListProjectNotificationActions::class);
|
||||||
|
$project = Project::factory()->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',
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,334 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Tests\Feature\Projects;
|
||||||
|
|
||||||
|
use App\Enums\NotificationChannel;
|
||||||
|
use App\Enums\NotificationEventType;
|
||||||
|
use App\Models\NotificationAction;
|
||||||
|
use App\Models\Project;
|
||||||
|
use App\Models\User;
|
||||||
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||||
|
use Inertia\Testing\AssertableInertia as Assert;
|
||||||
|
use Tests\TestCase;
|
||||||
|
|
||||||
|
class ProjectNotificationSettingsControllerTest extends TestCase
|
||||||
|
{
|
||||||
|
use RefreshDatabase;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Ensure project admins can view notification settings with notification action metadata.
|
||||||
|
*/
|
||||||
|
public function test_project_admins_can_view_notification_settings(): void
|
||||||
|
{
|
||||||
|
$user = User::factory()->create();
|
||||||
|
$project = Project::factory()->create([
|
||||||
|
'key' => 'CORE',
|
||||||
|
]);
|
||||||
|
$notificationAction = NotificationAction::factory()
|
||||||
|
->discord()
|
||||||
|
->for($project)
|
||||||
|
->create([
|
||||||
|
'name' => 'Discord alerts',
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->syncProjectPermissions($project, $user, ['edit']);
|
||||||
|
|
||||||
|
$this->actingAs($user)
|
||||||
|
->get(route('projects.settings.notifications', $project))
|
||||||
|
->assertOk()
|
||||||
|
->assertInertia(fn (Assert $page) => $page
|
||||||
|
->component('projects/settings/Notifications')
|
||||||
|
->where('project.id', $project->id)
|
||||||
|
->where('notificationActions.0.name', 'Discord alerts')
|
||||||
|
->where('notificationActions.0.channel', NotificationChannel::Discord->value)
|
||||||
|
->where('notificationActions.0.event_type', NotificationEventType::TicketStatusChanged->value)
|
||||||
|
->where('notificationActions.0.enabled', true)
|
||||||
|
->where('notificationActions.0.has_delivery_url', true)
|
||||||
|
->where('notificationActions.0.updated_at', $notificationAction->updated_at?->toISOString())
|
||||||
|
->where('channelOptions.0.value', NotificationChannel::Discord->value)
|
||||||
|
->where('eventTypeOptions.0.value', NotificationEventType::TicketStatusChanged->value)
|
||||||
|
->where('templateVariables.0.value', '$url')
|
||||||
|
->where('templateVariables.6.value', '$actor_name')
|
||||||
|
->missing('notificationActions.0.delivery_url'),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Ensure non-admin project users cannot open notification settings.
|
||||||
|
*/
|
||||||
|
public function test_non_admin_users_cannot_view_notification_settings(): void
|
||||||
|
{
|
||||||
|
$user = User::factory()->create();
|
||||||
|
$project = Project::factory()->create();
|
||||||
|
|
||||||
|
$this->syncProjectPermissions($project, $user, ['view']);
|
||||||
|
|
||||||
|
$this->actingAs($user)
|
||||||
|
->get(route('projects.settings.notifications', $project))
|
||||||
|
->assertForbidden();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Ensure project admins can create notification actions from settings.
|
||||||
|
*/
|
||||||
|
public function test_project_admins_can_create_notification_actions(): void
|
||||||
|
{
|
||||||
|
$user = User::factory()->create();
|
||||||
|
$project = Project::factory()->create();
|
||||||
|
|
||||||
|
$this->syncProjectPermissions($project, $user, ['edit']);
|
||||||
|
|
||||||
|
$this->actingAs($user)
|
||||||
|
->post(route('projects.settings.notifications.store', $project), [
|
||||||
|
'name' => 'Discord alerts',
|
||||||
|
'event_type' => NotificationEventType::TicketStatusChanged->value,
|
||||||
|
'channel' => NotificationChannel::Discord->value,
|
||||||
|
'enabled' => '1',
|
||||||
|
'delivery_url' => 'https://discord.com/api/webhooks/123456/discord-alerts',
|
||||||
|
'message_template' => '$ticket_key moved from $status_from to $status_to by $actor_name: $url',
|
||||||
|
])
|
||||||
|
->assertRedirect(route('projects.settings.notifications', $project))
|
||||||
|
->assertSessionHas('status', 'Discord alerts notification was added.');
|
||||||
|
|
||||||
|
$notificationAction = NotificationAction::query()->firstOrFail();
|
||||||
|
|
||||||
|
$this->assertSame($project->id, $notificationAction->project_id);
|
||||||
|
$this->assertSame(NotificationChannel::Discord, $notificationAction->channel);
|
||||||
|
$this->assertSame(NotificationEventType::TicketStatusChanged, $notificationAction->event_type);
|
||||||
|
$this->assertTrue($notificationAction->enabled);
|
||||||
|
$this->assertSame(
|
||||||
|
'https://discord.com/api/webhooks/123456/discord-alerts',
|
||||||
|
$notificationAction->delivery_url,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Ensure project admins can update notification actions without replacing stored secret URLs.
|
||||||
|
*/
|
||||||
|
public function test_project_admins_can_update_notification_actions_without_replacing_delivery_urls(): void
|
||||||
|
{
|
||||||
|
$user = User::factory()->create();
|
||||||
|
$project = Project::factory()->create();
|
||||||
|
$notificationAction = NotificationAction::factory()
|
||||||
|
->discord()
|
||||||
|
->for($project)
|
||||||
|
->create([
|
||||||
|
'delivery_url' => 'https://discord.com/api/webhooks/123456/original-secret',
|
||||||
|
'name' => 'Ops alerts',
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->syncProjectPermissions($project, $user, ['edit']);
|
||||||
|
|
||||||
|
$this->actingAs($user)
|
||||||
|
->patch(route('projects.settings.notifications.update', [$project, $notificationAction]), [
|
||||||
|
'name' => 'Ops escalation alerts',
|
||||||
|
'event_type' => NotificationEventType::TicketStatusChanged->value,
|
||||||
|
'channel' => NotificationChannel::Discord->value,
|
||||||
|
'enabled' => '1',
|
||||||
|
'delivery_url' => '',
|
||||||
|
'message_template' => '$ticket_key moved to $status_to: $url',
|
||||||
|
])
|
||||||
|
->assertRedirect(route('projects.settings.notifications', $project))
|
||||||
|
->assertSessionHas('status', 'Ops escalation alerts notification was updated.');
|
||||||
|
|
||||||
|
$notificationAction->refresh();
|
||||||
|
|
||||||
|
$this->assertSame('Ops escalation alerts', $notificationAction->name);
|
||||||
|
$this->assertSame(
|
||||||
|
'https://discord.com/api/webhooks/123456/original-secret',
|
||||||
|
$notificationAction->delivery_url,
|
||||||
|
);
|
||||||
|
$this->assertSame('$ticket_key moved to $status_to: $url', $notificationAction->message_template);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Ensure project admins can disable and re-enable notification actions.
|
||||||
|
*/
|
||||||
|
public function test_project_admins_can_toggle_notification_actions(): void
|
||||||
|
{
|
||||||
|
$user = User::factory()->create();
|
||||||
|
$project = Project::factory()->create();
|
||||||
|
$notificationAction = NotificationAction::factory()
|
||||||
|
->for($project)
|
||||||
|
->create([
|
||||||
|
'enabled' => true,
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->syncProjectPermissions($project, $user, ['edit']);
|
||||||
|
|
||||||
|
$this->actingAs($user)
|
||||||
|
->patch(route('projects.settings.notifications.update', [$project, $notificationAction]), [
|
||||||
|
'name' => $notificationAction->name,
|
||||||
|
'event_type' => $notificationAction->event_type->value,
|
||||||
|
'channel' => $notificationAction->channel->value,
|
||||||
|
'enabled' => '0',
|
||||||
|
'delivery_url' => '',
|
||||||
|
'message_template' => '',
|
||||||
|
])
|
||||||
|
->assertRedirect(route('projects.settings.notifications', $project));
|
||||||
|
|
||||||
|
$this->assertFalse($notificationAction->fresh()->enabled);
|
||||||
|
|
||||||
|
$this->actingAs($user)
|
||||||
|
->patch(route('projects.settings.notifications.update', [$project, $notificationAction]), [
|
||||||
|
'name' => $notificationAction->fresh()->name,
|
||||||
|
'event_type' => $notificationAction->fresh()->event_type->value,
|
||||||
|
'channel' => $notificationAction->fresh()->channel->value,
|
||||||
|
'enabled' => '1',
|
||||||
|
'delivery_url' => '',
|
||||||
|
'message_template' => '',
|
||||||
|
])
|
||||||
|
->assertRedirect(route('projects.settings.notifications', $project));
|
||||||
|
|
||||||
|
$this->assertTrue($notificationAction->fresh()->enabled);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Ensure project admins can replace stored delivery URLs when a new secret is submitted.
|
||||||
|
*/
|
||||||
|
public function test_project_admins_can_replace_delivery_urls(): void
|
||||||
|
{
|
||||||
|
$user = User::factory()->create();
|
||||||
|
$project = Project::factory()->create();
|
||||||
|
$notificationAction = NotificationAction::factory()
|
||||||
|
->webhook()
|
||||||
|
->for($project)
|
||||||
|
->create([
|
||||||
|
'delivery_url' => 'https://hooks.example.test/kanbangit/original-secret',
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->syncProjectPermissions($project, $user, ['edit']);
|
||||||
|
|
||||||
|
$this->actingAs($user)
|
||||||
|
->patch(route('projects.settings.notifications.update', [$project, $notificationAction]), [
|
||||||
|
'name' => 'Primary webhook',
|
||||||
|
'event_type' => NotificationEventType::TicketStatusChanged->value,
|
||||||
|
'channel' => NotificationChannel::Webhook->value,
|
||||||
|
'enabled' => '1',
|
||||||
|
'delivery_url' => 'https://hooks.example.test/kanbangit/replaced-secret',
|
||||||
|
'message_template' => '',
|
||||||
|
])
|
||||||
|
->assertRedirect(route('projects.settings.notifications', $project));
|
||||||
|
|
||||||
|
$this->assertSame(
|
||||||
|
'https://hooks.example.test/kanbangit/replaced-secret',
|
||||||
|
$notificationAction->fresh()->delivery_url,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Ensure project admins can delete configured notification actions.
|
||||||
|
*/
|
||||||
|
public function test_project_admins_can_delete_notification_actions(): void
|
||||||
|
{
|
||||||
|
$user = User::factory()->create();
|
||||||
|
$project = Project::factory()->create();
|
||||||
|
$notificationAction = NotificationAction::factory()
|
||||||
|
->for($project)
|
||||||
|
->create([
|
||||||
|
'name' => 'Webhook alerts',
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->syncProjectPermissions($project, $user, ['edit']);
|
||||||
|
|
||||||
|
$this->actingAs($user)
|
||||||
|
->delete(route('projects.settings.notifications.destroy', [$project, $notificationAction]))
|
||||||
|
->assertRedirect(route('projects.settings.notifications', $project))
|
||||||
|
->assertSessionHas('status', 'Webhook alerts notification was deleted.');
|
||||||
|
|
||||||
|
$this->assertModelMissing($notificationAction);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Ensure validation errors are returned for notification action forms.
|
||||||
|
*/
|
||||||
|
public function test_notification_action_validation_errors_are_returned_for_relevant_fields(): void
|
||||||
|
{
|
||||||
|
$user = User::factory()->create();
|
||||||
|
$project = Project::factory()->create();
|
||||||
|
|
||||||
|
$this->syncProjectPermissions($project, $user, ['edit']);
|
||||||
|
|
||||||
|
$this->actingAs($user)
|
||||||
|
->from(route('projects.settings.notifications', $project))
|
||||||
|
->post(route('projects.settings.notifications.store', $project), [
|
||||||
|
'name' => '',
|
||||||
|
'event_type' => 'invalid-event',
|
||||||
|
'channel' => NotificationChannel::Discord->value,
|
||||||
|
'enabled' => 'definitely-not-a-boolean',
|
||||||
|
'delivery_url' => 'not-a-url',
|
||||||
|
'message_template' => '',
|
||||||
|
])
|
||||||
|
->assertRedirect(route('projects.settings.notifications', $project))
|
||||||
|
->assertSessionHasErrors([
|
||||||
|
'name',
|
||||||
|
'event_type',
|
||||||
|
'enabled',
|
||||||
|
'delivery_url',
|
||||||
|
'message_template',
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Ensure non-admin project users cannot mutate notification actions.
|
||||||
|
*/
|
||||||
|
public function test_non_admin_users_cannot_mutate_notification_actions(): void
|
||||||
|
{
|
||||||
|
$user = User::factory()->create();
|
||||||
|
$project = Project::factory()->create();
|
||||||
|
$notificationAction = NotificationAction::factory()->for($project)->create();
|
||||||
|
|
||||||
|
$this->syncProjectPermissions($project, $user, ['view']);
|
||||||
|
|
||||||
|
$this->actingAs($user)
|
||||||
|
->post(route('projects.settings.notifications.store', $project), [
|
||||||
|
'name' => 'Forbidden webhook',
|
||||||
|
'event_type' => NotificationEventType::TicketStatusChanged->value,
|
||||||
|
'channel' => NotificationChannel::Webhook->value,
|
||||||
|
'enabled' => '1',
|
||||||
|
'delivery_url' => 'https://hooks.example.test/kanbangit/forbidden-secret',
|
||||||
|
'message_template' => '',
|
||||||
|
])
|
||||||
|
->assertForbidden();
|
||||||
|
|
||||||
|
$this->actingAs($user)
|
||||||
|
->patch(route('projects.settings.notifications.update', [$project, $notificationAction]), [
|
||||||
|
'name' => 'Forbidden update',
|
||||||
|
'event_type' => $notificationAction->event_type->value,
|
||||||
|
'channel' => $notificationAction->channel->value,
|
||||||
|
'enabled' => '0',
|
||||||
|
'delivery_url' => '',
|
||||||
|
'message_template' => '',
|
||||||
|
])
|
||||||
|
->assertForbidden();
|
||||||
|
|
||||||
|
$this->actingAs($user)
|
||||||
|
->delete(route('projects.settings.notifications.destroy', [$project, $notificationAction]))
|
||||||
|
->assertForbidden();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Ensure notification settings props never render stored secret URLs back to the browser.
|
||||||
|
*/
|
||||||
|
public function test_notification_settings_do_not_render_secret_delivery_urls(): void
|
||||||
|
{
|
||||||
|
$user = User::factory()->create();
|
||||||
|
$project = Project::factory()->create();
|
||||||
|
NotificationAction::factory()
|
||||||
|
->for($project)
|
||||||
|
->create([
|
||||||
|
'delivery_url' => 'https://hooks.example.test/kanbangit/secret-token',
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->syncProjectPermissions($project, $user, ['edit']);
|
||||||
|
|
||||||
|
$this->actingAs($user)
|
||||||
|
->get(route('projects.settings.notifications', $project))
|
||||||
|
->assertOk()
|
||||||
|
->assertInertia(fn (Assert $page) => $page
|
||||||
|
->component('projects/settings/Notifications')
|
||||||
|
->where('notificationActions.0.has_delivery_url', true)
|
||||||
|
->missing('notificationActions.0.delivery_url'),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user