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),
];
}
}
@@ -22,10 +22,12 @@ use App\Http\Requests\DestroyProjectTicketTypeRequest;
use App\Http\Requests\StoreProjectNamedSettingRequest;
use App\Http\Requests\StoreProjectTicketTypeRequest;
use App\Http\Requests\StoreTicketFieldDefinitionRequest;
use App\Http\Requests\StoreTicketLinkTypeRequest;
use App\Http\Requests\UpdateProjectNamedSettingRequest;
use App\Http\Requests\UpdateProjectTicketMetadataSettings as UpdateProjectTicketMetadataSettingsRequest;
use App\Http\Requests\UpdateProjectTicketTypeRequest;
use App\Http\Requests\UpdateTicketFieldDefinitionRequest;
use App\Http\Requests\UpdateTicketLinkTypeRequest;
use App\Http\Resources\ProjectResource;
use App\Http\Resources\TicketFieldDefinitionResource;
use App\Http\Resources\TicketLinkTypeResource;
@@ -209,7 +211,7 @@ class ProjectTicketMetadataSettingsController extends ApiController
return response()->json(null, 204);
}
public function storeLinkType(StoreProjectNamedSettingRequest $request, Project $project): JsonResponse
public function storeLinkType(StoreTicketLinkTypeRequest $request, Project $project): JsonResponse
{
$this->abortUnlessCanManage($request, $project);
@@ -222,7 +224,7 @@ class ProjectTicketMetadataSettingsController extends ApiController
}
public function updateLinkType(
UpdateProjectNamedSettingRequest $request,
UpdateTicketLinkTypeRequest $request,
Project $project,
TicketLinkType $ticketLinkType,
): JsonResponse {
@@ -22,10 +22,12 @@ use App\Http\Requests\DestroyProjectTicketTypeRequest;
use App\Http\Requests\StoreProjectNamedSettingRequest;
use App\Http\Requests\StoreProjectTicketTypeRequest;
use App\Http\Requests\StoreTicketFieldDefinitionRequest;
use App\Http\Requests\StoreTicketLinkTypeRequest;
use App\Http\Requests\UpdateProjectNamedSettingRequest;
use App\Http\Requests\UpdateProjectTicketMetadataSettings;
use App\Http\Requests\UpdateProjectTicketTypeRequest;
use App\Http\Requests\UpdateTicketFieldDefinitionRequest;
use App\Http\Requests\UpdateTicketLinkTypeRequest;
use App\Models\Project;
use App\Models\TicketFieldDefinition;
use App\Models\TicketLinkType;
@@ -203,7 +205,7 @@ class ProjectTicketMetadataSettingsController extends Controller
->with('status', "{$deletedLabel} field was removed.");
}
public function storeLinkType(StoreProjectNamedSettingRequest $request, Project $project): RedirectResponse
public function storeLinkType(StoreTicketLinkTypeRequest $request, Project $project): RedirectResponse
{
$this->abortUnlessCanManage($request, $project);
@@ -214,7 +216,7 @@ class ProjectTicketMetadataSettingsController extends Controller
}
public function updateLinkType(
UpdateProjectNamedSettingRequest $request,
UpdateTicketLinkTypeRequest $request,
Project $project,
TicketLinkType $ticketLinkType,
): RedirectResponse {
@@ -0,0 +1,31 @@
<?php
namespace App\Http\Requests;
use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Foundation\Http\FormRequest;
class StoreTicketLinkTypeRequest 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'],
'counterpart_name' => ['required', 'string', 'max:255'],
'position' => ['nullable', 'integer', 'min:1'],
];
}
}
@@ -0,0 +1,31 @@
<?php
namespace App\Http\Requests;
use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Foundation\Http\FormRequest;
class UpdateTicketLinkTypeRequest 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'],
'counterpart_name' => ['required', 'string', 'max:255'],
'position' => ['required', 'integer', 'min:1'],
];
}
}
+5 -2
View File
@@ -18,16 +18,19 @@ class TicketLinkResource extends JsonResource
{
if ($this->resource instanceof TicketLink) {
$currentTicketId = (int) ($this->resource->getAttribute('current_ticket_id') ?? 0);
$linkedTicket = $this->source_ticket_id === $currentTicketId
$isSourceTicket = $this->source_ticket_id === $currentTicketId;
$linkedTicket = $isSourceTicket
? $this->targetTicket
: $this->sourceTicket;
$ticketLinkType = TicketLinkTypeResource::make($this->ticketLinkType)->resolve();
$ticketLinkType['label'] = $this->ticketLinkType->labelForSource($isSourceTicket);
return [
'id' => $this->id,
'linked_ticket' => TicketReferenceResource::make($linkedTicket)->resolve(),
'source_ticket_id' => $this->source_ticket_id,
'target_ticket_id' => $this->target_ticket_id,
'ticket_link_type' => TicketLinkTypeResource::make($this->ticketLinkType)->resolve(),
'ticket_link_type' => $ticketLinkType,
'ticket_link_type_id' => $this->ticket_link_type_id,
'created_at' => $this->created_at?->toISOString(),
'updated_at' => $this->updated_at?->toISOString(),
@@ -17,6 +17,7 @@ class TicketLinkTypeResource extends JsonResource
return [
'id' => $this->id,
'label' => $this->label,
'counterpart_label' => $this->counterpart_label ?: $this->label,
'position' => $this->position,
'project_id' => $this->project_id,
'created_at' => $this->created_at?->toISOString(),
+11 -1
View File
@@ -9,12 +9,22 @@ use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
#[Fillable(['label', 'position', 'project_id'])]
#[Fillable(['label', 'counterpart_label', 'position', 'project_id'])]
class TicketLinkType extends Model
{
/** @use HasFactory<TicketLinkTypeFactory> */
use HasFactory;
public function counterpartLabel(): string
{
return $this->counterpart_label ?: $this->label;
}
public function labelForSource(bool $isSourceTicket): string
{
return $isSourceTicket ? $this->label : $this->counterpartLabel();
}
public function project(): BelongsTo
{
return $this->belongsTo(Project::class);