Model ticket link counterparts explicitly
linter / quality (pull_request) Successful in 3m5s
tests / ci (8.4) (pull_request) Failing after 1m41s

This commit is contained in:
2026-05-23 10:29:22 +00:00
parent d60c33fa7c
commit 486994e441
22 changed files with 312 additions and 66 deletions
@@ -12,16 +12,18 @@ class StoreTicketLinkType
use ReordersProjectMetadata;
/**
* @param array{name: string, position?: int|null} $validated
* @param array{name: string, counterpart_name: string, position?: int|null} $validated
*/
public function handle(Project $project, array $validated): TicketLinkType
{
$name = Arr::get($validated, 'name');
$counterpartName = Arr::get($validated, 'counterpart_name');
$position = Arr::get($validated, 'position');
return DB::transaction(function () use ($project, $position, $name): TicketLinkType {
return DB::transaction(function () use ($project, $position, $name, $counterpartName): TicketLinkType {
$ticketLinkType = $project->ticketLinkTypes()->create([
'label' => $name,
'counterpart_label' => $counterpartName,
'position' => $project->ticketLinkTypes()->count() + 1,
]);
@@ -12,16 +12,18 @@ class UpdateTicketLinkType
use ReordersProjectMetadata;
/**
* @param array{name: string, position: int} $validated
* @param array{name: string, counterpart_name: string, position: int} $validated
*/
public function handle(Project $project, TicketLinkType $ticketLinkType, array $validated): TicketLinkType
{
$name = Arr::get($validated, 'name');
$counterpartName = Arr::get($validated, 'counterpart_name');
$position = Arr::get($validated, 'position');
DB::transaction(function () use ($project, $name, $position, $ticketLinkType): void {
DB::transaction(function () use ($project, $name, $counterpartName, $position, $ticketLinkType): void {
$ticketLinkType->update([
'label' => $name,
'counterpart_label' => $counterpartName,
]);
$this->reorderModels(
@@ -23,13 +23,15 @@ class DeleteProjectTicketLink
404,
);
$linkedTicket = $ticketLink->source_ticket_id === $ticket->id
$isSourceTicket = $ticketLink->source_ticket_id === $ticket->id;
$linkedTicket = $isSourceTicket
? $ticketLink->targetTicket
: $ticketLink->sourceTicket;
$label = $ticketLink->ticketLinkType->label;
$label = $ticketLink->ticketLinkType->labelForSource($isSourceTicket);
$counterpartLabel = $ticketLink->ticketLinkType->labelForSource(! $isSourceTicket);
DB::transaction(function () use ($label, $linkedTicket, $user, $ticket, $ticketLink): void {
DB::transaction(function () use ($label, $counterpartLabel, $linkedTicket, $user, $ticket, $ticketLink): void {
$ticketLink->delete();
$this->createTicketWorklogEntry->handle(
@@ -47,10 +49,10 @@ class DeleteProjectTicketLink
$linkedTicket,
$user,
TicketWorklogEntryType::LinkRemoved,
"Removed {$label} link to {$ticket->ticket_key}.",
"Removed {$counterpartLabel} link to {$ticket->ticket_key}.",
[
'linked_ticket_key' => $ticket->ticket_key,
'ticket_link_type' => $label,
'ticket_link_type' => $counterpartLabel,
],
);
});
+32 -25
View File
@@ -28,29 +28,47 @@ class StoreProjectTicketLink
->where('project_id', $project->id)
->findOrFail($linkedTicketId);
[$sourceTicketId, $targetTicketId] = $this->normalizeTicketPair($ticket->id, $linkedTicket->id);
return DB::transaction(function () use ($linkedTicket, $user, $ticketLinkTypeId, $ticket): TicketLink {
$ticketLink = TicketLink::query()
->where(function ($query) use ($ticket, $linkedTicket): void {
$query
->where('source_ticket_id', $ticket->id)
->where('target_ticket_id', $linkedTicket->id);
})
->orWhere(function ($query) use ($ticket, $linkedTicket): void {
$query
->where('source_ticket_id', $linkedTicket->id)
->where('target_ticket_id', $ticket->id);
})
->first();
return DB::transaction(function () use ($linkedTicket, $user, $ticketLinkTypeId, $sourceTicketId, $targetTicketId, $ticket): TicketLink {
$ticketLink = TicketLink::query()->updateOrCreate(
[
'source_ticket_id' => $sourceTicketId,
'target_ticket_id' => $targetTicketId,
],
[
if ($ticketLink === null) {
$ticketLink = TicketLink::query()->create([
'source_ticket_id' => $ticket->id,
'target_ticket_id' => $linkedTicket->id,
'ticket_link_type_id' => $ticketLinkTypeId,
],
);
]);
} else {
$ticketLink->update([
'source_ticket_id' => $ticket->id,
'target_ticket_id' => $linkedTicket->id,
'ticket_link_type_id' => $ticketLinkTypeId,
]);
}
$ticketLink->load('ticketLinkType');
$sourceLabel = $ticketLink->ticketLinkType->labelForSource(true);
$targetLabel = $ticketLink->ticketLinkType->labelForSource(false);
$this->createTicketWorklogEntry->handle(
$ticket,
$user,
TicketWorklogEntryType::LinkUpdated,
"Linked {$linkedTicket->ticket_key} as {$ticketLink->ticketLinkType->label}.",
"Linked {$linkedTicket->ticket_key} as {$sourceLabel}.",
[
'linked_ticket_key' => $linkedTicket->ticket_key,
'ticket_link_type' => $ticketLink->ticketLinkType->label,
'ticket_link_type' => $sourceLabel,
],
);
@@ -58,25 +76,14 @@ class StoreProjectTicketLink
$linkedTicket,
$user,
TicketWorklogEntryType::LinkUpdated,
"Linked {$ticket->ticket_key} as {$ticketLink->ticketLinkType->label}.",
"Linked {$ticket->ticket_key} as {$targetLabel}.",
[
'linked_ticket_key' => $ticket->ticket_key,
'ticket_link_type' => $ticketLink->ticketLinkType->label,
'ticket_link_type' => $targetLabel,
],
);
return $ticketLink;
});
}
/**
* @return array{0: int, 1: int}
*/
private function normalizeTicketPair(int $firstTicketId, int $secondTicketId): array
{
return [
min($firstTicketId, $secondTicketId),
max($firstTicketId, $secondTicketId),
];
}
}