Merge pull request 'Add assignee changed notification event support' (#70) from koda/recreate-68-assignee-changed-notification into main
Reviewed-on: OpenSource/KanbanGit#70
This commit was merged in pull request #70.
This commit is contained in:
@@ -60,7 +60,7 @@ class BuildProjectNotificationSettingsPayload
|
||||
'value' => '$project_name',
|
||||
],
|
||||
[
|
||||
'description' => 'Origin status label for status moves; blank for ticket create.',
|
||||
'description' => 'Origin status label for status moves; blank for ticket create and assignee changes.',
|
||||
'value' => '$status_from',
|
||||
],
|
||||
[
|
||||
@@ -71,6 +71,14 @@ class BuildProjectNotificationSettingsPayload
|
||||
'description' => 'Actor name when the event has a known user.',
|
||||
'value' => '$actor_name',
|
||||
],
|
||||
[
|
||||
'description' => 'Previous assignee name when the event updates assignment; blank otherwise.',
|
||||
'value' => '$assignee_from',
|
||||
],
|
||||
[
|
||||
'description' => 'New assignee name when the event updates assignment; blank when unassigned.',
|
||||
'value' => '$assignee_to',
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
namespace App\Actions\Notifications;
|
||||
|
||||
use App\Models\Project;
|
||||
use App\Models\Ticket;
|
||||
use App\Models\TicketStatus;
|
||||
use App\Models\User;
|
||||
use Carbon\CarbonImmutable;
|
||||
|
||||
class BuildTicketAssigneeChangedPayload
|
||||
{
|
||||
public function handle(
|
||||
Project $project,
|
||||
Ticket $ticket,
|
||||
?User $actor,
|
||||
?int $fromAssigneeId,
|
||||
?string $fromAssigneeName,
|
||||
?int $toAssigneeId,
|
||||
?string $toAssigneeName,
|
||||
CarbonImmutable $occurredAt,
|
||||
): TicketAssigneeChangedPayload {
|
||||
$ticket->loadMissing('ticketStatus.boardColumn');
|
||||
|
||||
return new TicketAssigneeChangedPayload(
|
||||
projectId: $project->id,
|
||||
projectKey: $project->key,
|
||||
projectName: $project->name,
|
||||
ticketId: $ticket->id,
|
||||
ticketKey: $ticket->ticket_key,
|
||||
ticketTitle: $ticket->title,
|
||||
ticketUrl: route('projects.tickets.show', [$project, $ticket]),
|
||||
actorId: $actor?->id,
|
||||
actorName: $actor?->name,
|
||||
fromStatusId: null,
|
||||
fromStatusLabel: null,
|
||||
toStatusId: $ticket->ticket_status_id,
|
||||
toStatusLabel: $this->statusLabel($ticket->ticketStatus),
|
||||
occurredAt: $occurredAt,
|
||||
fromAssigneeId: $fromAssigneeId,
|
||||
fromAssigneeName: $fromAssigneeName,
|
||||
toAssigneeId: $toAssigneeId,
|
||||
toAssigneeName: $toAssigneeName,
|
||||
);
|
||||
}
|
||||
|
||||
private function statusLabel(?TicketStatus $ticketStatus): string
|
||||
{
|
||||
if ($ticketStatus === null) {
|
||||
return 'Unknown status';
|
||||
}
|
||||
|
||||
$ticketStatus->loadMissing('boardColumn');
|
||||
|
||||
if ($ticketStatus->boardColumn === null || $ticketStatus->boardColumn->name === $ticketStatus->name) {
|
||||
return $ticketStatus->name;
|
||||
}
|
||||
|
||||
return "{$ticketStatus->boardColumn->name} / {$ticketStatus->name}";
|
||||
}
|
||||
}
|
||||
@@ -190,6 +190,10 @@ class MatchNotificationFilters
|
||||
$fromStatusLabel = property_exists($payload, 'fromStatusLabel') ? $payload->fromStatusLabel : null;
|
||||
$toStatusId = property_exists($payload, 'toStatusId') ? $payload->toStatusId : null;
|
||||
$toStatusLabel = property_exists($payload, 'toStatusLabel') ? $payload->toStatusLabel : null;
|
||||
$fromAssigneeId = property_exists($payload, 'fromAssigneeId') ? $payload->fromAssigneeId : null;
|
||||
$fromAssigneeName = property_exists($payload, 'fromAssigneeName') ? $payload->fromAssigneeName : null;
|
||||
$toAssigneeId = property_exists($payload, 'toAssigneeId') ? $payload->toAssigneeId : null;
|
||||
$toAssigneeName = property_exists($payload, 'toAssigneeName') ? $payload->toAssigneeName : null;
|
||||
|
||||
return [
|
||||
'project' => [
|
||||
@@ -207,6 +211,16 @@ class MatchNotificationFilters
|
||||
'id' => $actorId,
|
||||
'name' => $actorName,
|
||||
],
|
||||
'assignee' => [
|
||||
'from' => [
|
||||
'id' => $fromAssigneeId,
|
||||
'name' => $fromAssigneeName,
|
||||
],
|
||||
'to' => [
|
||||
'id' => $toAssigneeId,
|
||||
'name' => $toAssigneeName,
|
||||
],
|
||||
],
|
||||
'status' => [
|
||||
'from' => [
|
||||
'id' => $fromStatusId,
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
namespace App\Actions\Notifications;
|
||||
|
||||
use App\Enums\NotificationEventType;
|
||||
use Illuminate\Contracts\Events\ShouldDispatchAfterCommit;
|
||||
use Illuminate\Foundation\Events\Dispatchable;
|
||||
|
||||
final readonly class TicketAssigneeChanged implements NotificationEvent, ShouldDispatchAfterCommit
|
||||
{
|
||||
use Dispatchable;
|
||||
|
||||
public function __construct(
|
||||
public TicketAssigneeChangedPayload $payload,
|
||||
) {}
|
||||
|
||||
public function eventType(): NotificationEventType
|
||||
{
|
||||
return NotificationEventType::TicketAssigneeChanged;
|
||||
}
|
||||
|
||||
public function payload(): NotificationEventPayload
|
||||
{
|
||||
return $this->payload;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,129 @@
|
||||
<?php
|
||||
|
||||
namespace App\Actions\Notifications;
|
||||
|
||||
use App\Enums\NotificationEventType;
|
||||
use Carbon\CarbonImmutable;
|
||||
|
||||
final readonly class TicketAssigneeChangedPayload implements NotificationEventPayload
|
||||
{
|
||||
public function __construct(
|
||||
public int $projectId,
|
||||
public string $projectKey,
|
||||
public string $projectName,
|
||||
public int $ticketId,
|
||||
public string $ticketKey,
|
||||
public string $ticketTitle,
|
||||
public string $ticketUrl,
|
||||
public ?int $actorId,
|
||||
public ?string $actorName,
|
||||
public ?int $fromStatusId,
|
||||
public ?string $fromStatusLabel,
|
||||
public int $toStatusId,
|
||||
public string $toStatusLabel,
|
||||
public CarbonImmutable $occurredAt,
|
||||
public ?int $fromAssigneeId,
|
||||
public ?string $fromAssigneeName,
|
||||
public ?int $toAssigneeId,
|
||||
public ?string $toAssigneeName,
|
||||
) {}
|
||||
|
||||
public function projectId(): int
|
||||
{
|
||||
return $this->projectId;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, scalar|null>
|
||||
*/
|
||||
public function deliveryMetadata(): array
|
||||
{
|
||||
return [
|
||||
'project_key' => $this->projectKey,
|
||||
'ticket_key' => $this->ticketKey,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
public function discordTemplateVariables(): array
|
||||
{
|
||||
return [
|
||||
'$url' => $this->ticketUrl,
|
||||
'$ticket_key' => $this->ticketKey,
|
||||
'$ticket_title' => $this->ticketTitle,
|
||||
'$project_name' => $this->projectName,
|
||||
'$status_from' => $this->fromStatusLabel ?? '',
|
||||
'$status_to' => $this->toStatusLabel,
|
||||
'$actor_name' => $this->actorName ?? '',
|
||||
'$assignee_from' => $this->fromAssigneeName ?? '',
|
||||
'$assignee_to' => $this->toAssigneeName ?? '',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{
|
||||
* event_type: string,
|
||||
* project: array{id: int, key: string, name: string},
|
||||
* ticket: array{id: int, key: string, title: string},
|
||||
* actor: array{id: int, name: string}|null,
|
||||
* assignee: array{
|
||||
* from: array{id: int, name: string}|null,
|
||||
* to: array{id: int, name: string}|null
|
||||
* },
|
||||
* from: array{id: int, label: string}|null,
|
||||
* to: array{id: int, label: string},
|
||||
* ticket_url: string,
|
||||
* occurred_at: string
|
||||
* }
|
||||
*/
|
||||
public function toArray(): array
|
||||
{
|
||||
return [
|
||||
'event_type' => NotificationEventType::TicketAssigneeChanged->value,
|
||||
'project' => [
|
||||
'id' => $this->projectId,
|
||||
'key' => $this->projectKey,
|
||||
'name' => $this->projectName,
|
||||
],
|
||||
'ticket' => [
|
||||
'id' => $this->ticketId,
|
||||
'key' => $this->ticketKey,
|
||||
'title' => $this->ticketTitle,
|
||||
],
|
||||
'actor' => $this->actorId === null || $this->actorName === null
|
||||
? null
|
||||
: [
|
||||
'id' => $this->actorId,
|
||||
'name' => $this->actorName,
|
||||
],
|
||||
'assignee' => [
|
||||
'from' => $this->fromAssigneeId === null || $this->fromAssigneeName === null
|
||||
? null
|
||||
: [
|
||||
'id' => $this->fromAssigneeId,
|
||||
'name' => $this->fromAssigneeName,
|
||||
],
|
||||
'to' => $this->toAssigneeId === null || $this->toAssigneeName === null
|
||||
? null
|
||||
: [
|
||||
'id' => $this->toAssigneeId,
|
||||
'name' => $this->toAssigneeName,
|
||||
],
|
||||
],
|
||||
'from' => $this->fromStatusId === null || $this->fromStatusLabel === null
|
||||
? null
|
||||
: [
|
||||
'id' => $this->fromStatusId,
|
||||
'label' => $this->fromStatusLabel,
|
||||
],
|
||||
'to' => [
|
||||
'id' => $this->toStatusId,
|
||||
'label' => $this->toStatusLabel,
|
||||
],
|
||||
'ticket_url' => $this->ticketUrl,
|
||||
'occurred_at' => $this->occurredAt->toISOString(),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -2,12 +2,15 @@
|
||||
|
||||
namespace App\Actions\Tickets;
|
||||
|
||||
use App\Actions\Notifications\BuildTicketAssigneeChangedPayload;
|
||||
use App\Actions\Notifications\TicketAssigneeChanged;
|
||||
use App\Actions\Tickets\Support\AcceptanceCriteriaChecklistMapper;
|
||||
use App\Enums\TicketWorklogEntryType;
|
||||
use App\Models\Ticket;
|
||||
use App\Models\TicketFieldDefinition;
|
||||
use App\Models\TicketFieldValue;
|
||||
use App\Models\User;
|
||||
use Carbon\CarbonImmutable;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
@@ -15,6 +18,7 @@ use Illuminate\Support\Facades\DB;
|
||||
class UpdateProjectTicket
|
||||
{
|
||||
public function __construct(
|
||||
private readonly BuildTicketAssigneeChangedPayload $buildTicketAssigneeChangedPayload,
|
||||
private readonly CreateTicketWorklogEntry $createTicketWorklogEntry,
|
||||
private readonly SyncProjectTicketFieldValues $syncProjectTicketFieldValues,
|
||||
) {}
|
||||
@@ -68,6 +72,7 @@ class UpdateProjectTicket
|
||||
'parentTicket',
|
||||
'project',
|
||||
'ticketPriority',
|
||||
'ticketStatus.boardColumn',
|
||||
'ticketType',
|
||||
]);
|
||||
|
||||
@@ -90,6 +95,7 @@ class UpdateProjectTicket
|
||||
|
||||
$changes = [];
|
||||
$summaryLabels = [];
|
||||
$assigneeChange = null;
|
||||
|
||||
$this->trackScalarChange(
|
||||
$changes,
|
||||
@@ -167,13 +173,23 @@ class UpdateProjectTicket
|
||||
}
|
||||
|
||||
if ($ticket->assignee_user_id !== $ticketAttributes['assignee_user_id']) {
|
||||
$toAssigneeId = $ticketAttributes['assignee_user_id'];
|
||||
$toAssigneeName = $toAssigneeId === null
|
||||
? null
|
||||
: User::query()->find($toAssigneeId)?->name;
|
||||
|
||||
$changes['assignee'] = [
|
||||
'from' => $ticket->assignee?->name,
|
||||
'to' => $ticketAttributes['assignee_user_id'] === null
|
||||
? null
|
||||
: User::query()->find($ticketAttributes['assignee_user_id'])?->name,
|
||||
'to' => $toAssigneeName,
|
||||
];
|
||||
$summaryLabels[] = 'assignee';
|
||||
|
||||
$assigneeChange = [
|
||||
'from_id' => $ticket->assignee_user_id,
|
||||
'from_name' => $ticket->assignee?->name,
|
||||
'to_id' => $toAssigneeId,
|
||||
'to_name' => $toAssigneeName,
|
||||
];
|
||||
}
|
||||
|
||||
if ($ticket->author_user_id !== $ticketAttributes['author_user_id']) {
|
||||
@@ -223,6 +239,21 @@ class UpdateProjectTicket
|
||||
$changes,
|
||||
);
|
||||
|
||||
if ($assigneeChange !== null) {
|
||||
TicketAssigneeChanged::dispatch(
|
||||
$this->buildTicketAssigneeChangedPayload->handle(
|
||||
$ticket->project,
|
||||
$ticket,
|
||||
$actor,
|
||||
$assigneeChange['from_id'],
|
||||
$assigneeChange['from_name'],
|
||||
$assigneeChange['to_id'],
|
||||
$assigneeChange['to_name'],
|
||||
CarbonImmutable::now(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user