Implement ticket links and blocker state

This commit is contained in:
2026-03-25 09:12:17 +01:00
parent 3b31e6bb7d
commit 70b90f32d4
34 changed files with 1722 additions and 10 deletions
@@ -18,9 +18,12 @@ class UpdateProjectTicket
/**
* @param array{
* acceptance_criteria: string|null,
* blocked_comment: string|null,
* description: string|null,
* is_blocked: bool,
* ticket_priority_id: int,
* ticket_type_id: int,
* parent_ticket_id: int|null,
* title: string
* } $ticketAttributes
* @param array<int, array{
@@ -36,6 +39,10 @@ class UpdateProjectTicket
array $fieldValueAttributes,
): bool {
return DB::transaction(function () use ($actor, $fieldValueAttributes, $ticket, $ticketAttributes): bool {
if (! $ticketAttributes['is_blocked']) {
$ticketAttributes['blocked_comment'] = null;
}
$ticket->loadMissing([
'fieldValues.ticketFieldDefinition',
'fieldValues.userValue',
@@ -88,6 +95,14 @@ class UpdateProjectTicket
$ticket->acceptance_criteria,
$ticketAttributes['acceptance_criteria'],
);
$this->trackScalarChange(
$changes,
$summaryLabels,
'blocked_comment',
'blocker comment',
$ticket->blocked_comment,
$ticketAttributes['blocked_comment'],
);
if ($ticket->ticket_type_id !== $ticketAttributes['ticket_type_id']) {
$changes['ticket_type'] = [
@@ -105,6 +120,14 @@ class UpdateProjectTicket
$summaryLabels[] = 'priority';
}
if ($ticket->is_blocked !== $ticketAttributes['is_blocked']) {
$changes['is_blocked'] = [
'from' => $ticket->is_blocked,
'to' => $ticketAttributes['is_blocked'],
];
$summaryLabels[] = 'blocked state';
}
if ($ticket->parent_ticket_id !== $ticketAttributes['parent_ticket_id']) {
$newParentTicket = $ticketAttributes['parent_ticket_id'] === null
? null
@@ -7,6 +7,7 @@ use App\Actions\Tickets\UpdateProjectTicket;
use App\Http\Requests\UpdateProjectTicketRequest;
use App\Models\Project;
use App\Models\Ticket;
use App\Models\TicketLink;
use App\Models\User;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
@@ -32,6 +33,7 @@ class ProjectTicketController extends Controller
return Inertia::render('projects/Ticket', [
'canEditTicket' => $this->projectPermissionService->userHasPermission($request->user(), $project, 'edit'),
'childTicketTree' => $this->buildChildTicketTree($ticket),
'linkedTickets' => $this->linkedTickets($ticket),
'parentTicketOptions' => Ticket::query()
->where('project_id', $project->id)
->whereNotIn('id', $excludedParentIds)
@@ -54,6 +56,17 @@ class ProjectTicketController extends Controller
'worklogEntries.actor',
]),
'ticketFieldDefinitions' => $project->ticketFieldDefinitions()->get(),
'ticketLinkOptions' => Ticket::query()
->where('project_id', $project->id)
->whereKeyNot($ticket->id)
->orderByDesc('sequence_number')
->get()
->map(fn (Ticket $linkedTicket): array => [
'id' => $linkedTicket->id,
'ticket_key' => $linkedTicket->ticket_key,
'title' => $linkedTicket->title,
]),
'ticketLinkTypes' => $project->ticketLinkTypes()->get(),
'ticketPriorities' => $project->ticketPriorities()->get(),
'ticketTypes' => $project->ticketTypes()->get(),
'users' => User::query()
@@ -124,4 +137,50 @@ class ProjectTicketController extends Controller
->values()
->all();
}
/**
* @return array<int, array{
* created_at: string|null,
* id: int,
* linked_ticket: array{id: int, ticket_key: string, title: string},
* source_ticket_id: int,
* target_ticket_id: int,
* ticket_link_type: mixed,
* ticket_link_type_id: int,
* updated_at: string|null
* }>
*/
private function linkedTickets(Ticket $ticket): array
{
return TicketLink::query()
->with(['sourceTicket', 'targetTicket', 'ticketLinkType'])
->where(function ($query) use ($ticket): void {
$query
->where('source_ticket_id', $ticket->id)
->orWhere('target_ticket_id', $ticket->id);
})
->orderBy('id')
->get()
->map(function (TicketLink $ticketLink) use ($ticket): array {
$linkedTicket = $ticketLink->source_ticket_id === $ticket->id
? $ticketLink->targetTicket
: $ticketLink->sourceTicket;
return [
'created_at' => $ticketLink->created_at?->toISOString(),
'id' => $ticketLink->id,
'linked_ticket' => [
'id' => $linkedTicket->id,
'ticket_key' => $linkedTicket->ticket_key,
'title' => $linkedTicket->title,
],
'source_ticket_id' => $ticketLink->source_ticket_id,
'target_ticket_id' => $ticketLink->target_ticket_id,
'ticket_link_type' => $ticketLink->ticketLinkType,
'ticket_link_type_id' => $ticketLink->ticket_link_type_id,
'updated_at' => $ticketLink->updated_at?->toISOString(),
];
})
->all();
}
}
@@ -0,0 +1,122 @@
<?php
namespace App\Http\Controllers;
use App\Actions\ProjectPermissions\ProjectPermissionService;
use App\Actions\Tickets\CreateTicketWorklogEntry;
use App\Http\Requests\StoreTicketLinkRequest;
use App\Models\Project;
use App\Models\Ticket;
use App\Models\TicketLink;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class ProjectTicketLinkController extends Controller
{
public function __construct(
private readonly CreateTicketWorklogEntry $createTicketWorklogEntry,
private readonly ProjectPermissionService $projectPermissionService,
) {}
public function store(
StoreTicketLinkRequest $request,
Project $project,
Ticket $ticket,
): RedirectResponse {
abort_unless(
$this->projectPermissionService->userHasPermission($request->user(), $project, 'edit'),
403,
);
$linkedTicket = Ticket::query()
->where('project_id', $project->id)
->findOrFail($request->linkedTicketId());
[$sourceTicketId, $targetTicketId] = $this->normalizeTicketPair($ticket->id, $linkedTicket->id);
$ticketLink = DB::transaction(function () use ($linkedTicket, $request, $sourceTicketId, $targetTicketId, $ticket): TicketLink {
$ticketLink = TicketLink::query()->updateOrCreate(
[
'source_ticket_id' => $sourceTicketId,
'target_ticket_id' => $targetTicketId,
],
[
'ticket_link_type_id' => $request->ticketLinkTypeId(),
],
);
$ticketLink->load('ticketLinkType');
$this->createTicketWorklogEntry->handle(
$ticket,
$request->user(),
'link_updated',
"Linked {$linkedTicket->ticket_key} as {$ticketLink->ticketLinkType->label}.",
[
'linked_ticket_key' => $linkedTicket->ticket_key,
'ticket_link_type' => $ticketLink->ticketLinkType->label,
],
);
return $ticketLink;
});
return to_route('projects.tickets.show', [$project, $ticket])
->with('status', "{$ticketLink->ticketLinkType->label} link was saved.");
}
public function destroy(
Request $request,
Project $project,
Ticket $ticket,
TicketLink $ticketLink,
): RedirectResponse {
abort_unless(
$this->projectPermissionService->userHasPermission($request->user(), $project, 'edit'),
403,
);
$ticketLink->load(['sourceTicket', 'targetTicket', 'ticketLinkType']);
abort_unless(
$ticketLink->source_ticket_id === $ticket->id || $ticketLink->target_ticket_id === $ticket->id,
404,
);
$linkedTicket = $ticketLink->source_ticket_id === $ticket->id
? $ticketLink->targetTicket
: $ticketLink->sourceTicket;
$label = $ticketLink->ticketLinkType->label;
DB::transaction(function () use ($label, $linkedTicket, $request, $ticket, $ticketLink): void {
$ticketLink->delete();
$this->createTicketWorklogEntry->handle(
$ticket,
$request->user(),
'link_removed',
"Removed {$label} link to {$linkedTicket->ticket_key}.",
[
'linked_ticket_key' => $linkedTicket->ticket_key,
'ticket_link_type' => $label,
],
);
});
return to_route('projects.tickets.show', [$project, $ticket])
->with('status', "{$label} link was removed.");
}
/**
* @return array{0: int, 1: int}
*/
private function normalizeTicketPair(int $firstTicketId, int $secondTicketId): array
{
return [
min($firstTicketId, $secondTicketId),
max($firstTicketId, $secondTicketId),
];
}
}
@@ -10,6 +10,7 @@ use App\Http\Requests\UpdateProjectNamedSettingRequest;
use App\Http\Requests\UpdateTicketFieldDefinitionRequest;
use App\Models\Project;
use App\Models\TicketFieldDefinition;
use App\Models\TicketLinkType;
use App\Models\TicketPriority;
use App\Models\TicketType;
use Illuminate\Database\Eloquent\Model;
@@ -42,6 +43,7 @@ class ProjectTicketMetadataSettingsController extends Controller
'project' => $project,
'status' => session('status'),
'ticketFieldDefinitions' => $project->ticketFieldDefinitions()->get(),
'ticketLinkTypes' => $project->ticketLinkTypes()->get(),
'ticketPriorities' => $project->ticketPriorities()->get(),
'ticketTypes' => $project->ticketTypes()->get(),
]);
@@ -249,6 +251,71 @@ class ProjectTicketMetadataSettingsController extends Controller
->with('status', "{$deletedLabel} field was removed.");
}
public function storeLinkType(StoreProjectNamedSettingRequest $request, Project $project): RedirectResponse
{
$this->abortUnlessCanManage($request, $project);
$ticketLinkType = DB::transaction(function () use ($project, $request): TicketLinkType {
$ticketLinkType = $project->ticketLinkTypes()->create([
'label' => $request->validated('name'),
'position' => $project->ticketLinkTypes()->count() + 1,
]);
$this->reorderModels(
$project->ticketLinkTypes()->whereKeyNot($ticketLinkType->id)->get()->values(),
$ticketLinkType,
$request->position() ?? $project->ticketLinkTypes()->count(),
);
return $ticketLinkType;
});
return to_route('projects.settings.metadata', $project)
->with('status', "{$ticketLinkType->label} link type was added.");
}
public function updateLinkType(
UpdateProjectNamedSettingRequest $request,
Project $project,
TicketLinkType $ticketLinkType,
): RedirectResponse {
$this->abortUnlessCanManage($request, $project);
DB::transaction(function () use ($project, $request, $ticketLinkType): void {
$ticketLinkType->update([
'label' => $request->validated('name'),
]);
$this->reorderModels(
$project->ticketLinkTypes()->whereKeyNot($ticketLinkType->id)->get()->values(),
$ticketLinkType,
$request->position(),
);
});
return to_route('projects.settings.metadata', $project)
->with('status', "{$ticketLinkType->fresh()->label} link type was updated.");
}
public function destroyLinkType(
Request $request,
Project $project,
TicketLinkType $ticketLinkType,
): RedirectResponse {
$this->abortUnlessCanManage($request, $project);
$deletedLabel = $ticketLinkType->label;
DB::transaction(function () use ($project, $ticketLinkType): void {
$ticketLinkType->delete();
$this->persistOrderedModels($project->ticketLinkTypes()->get()->values());
});
return to_route('projects.settings.metadata', $project)
->with('status', "{$deletedLabel} link type was removed.");
}
private function abortUnlessCanManage(Request $request, Project $project): void
{
abort_unless(
@@ -0,0 +1,74 @@
<?php
namespace App\Http\Requests;
use App\Models\Project;
use App\Models\Ticket;
use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
use Illuminate\Validation\Validator;
class StoreTicketLinkRequest 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
{
/** @var Project $project */
$project = $this->route('project');
return [
'linked_ticket_id' => [
'required',
'integer',
Rule::exists('tickets', 'id')->where('project_id', $project->id),
],
'ticket_link_type_id' => [
'required',
'integer',
Rule::exists('ticket_link_types', 'id')->where('project_id', $project->id),
],
];
}
/**
* @return array<int, callable(Validator): void>
*/
public function after(): array
{
return [
function (Validator $validator): void {
/** @var Ticket $ticket */
$ticket = $this->route('ticket');
$linkedTicketId = $this->input('linked_ticket_id');
if (is_numeric($linkedTicketId) && (int) $linkedTicketId === $ticket->id) {
$validator->errors()->add('linked_ticket_id', 'A ticket cannot be linked to itself.');
}
},
];
}
public function linkedTicketId(): int
{
return (int) $this->validated('linked_ticket_id');
}
public function ticketLinkTypeId(): int
{
return (int) $this->validated('ticket_link_type_id');
}
}
@@ -33,6 +33,7 @@ class UpdateProjectTicketRequest extends FormRequest
return [
'acceptance_criteria' => ['nullable', 'string'],
'blocked_comment' => ['nullable', 'string'],
'description' => ['nullable', 'string'],
'field_values' => ['array'],
'field_values.*' => ['array'],
@@ -43,6 +44,7 @@ class UpdateProjectTicketRequest extends FormRequest
],
'field_values.*.value_number' => ['nullable', 'numeric'],
'field_values.*.value_user_id' => ['nullable', 'integer', Rule::exists('users', 'id')],
'is_blocked' => ['required', 'boolean'],
'parent_ticket_id' => [
'nullable',
'integer',
@@ -116,6 +118,10 @@ class UpdateProjectTicketRequest extends FormRequest
) {
$validator->errors()->add('ticket_type_id', 'Tickets with child tickets must keep a Parent type.');
}
if ($this->boolean('is_blocked') && blank($this->input('blocked_comment'))) {
$validator->errors()->add('blocked_comment', 'Blocked tickets require a blocker comment.');
}
},
];
}
@@ -123,7 +129,9 @@ class UpdateProjectTicketRequest extends FormRequest
/**
* @return array{
* acceptance_criteria: string|null,
* blocked_comment: string|null,
* description: string|null,
* is_blocked: bool,
* parent_ticket_id: int|null,
* ticket_priority_id: int,
* ticket_type_id: int,
@@ -134,7 +142,9 @@ class UpdateProjectTicketRequest extends FormRequest
{
/** @var array{
* acceptance_criteria: string|null,
* blocked_comment: string|null,
* description: string|null,
* is_blocked: bool,
* parent_ticket_id: int|null,
* ticket_priority_id: int,
* ticket_type_id: int,
@@ -144,12 +154,16 @@ class UpdateProjectTicketRequest extends FormRequest
$attributes = array_merge(
[
'acceptance_criteria' => null,
'blocked_comment' => null,
'description' => null,
'is_blocked' => false,
'parent_ticket_id' => null,
],
Arr::only($this->validated(), [
'acceptance_criteria',
'blocked_comment',
'description',
'is_blocked',
'parent_ticket_id',
'ticket_priority_id',
'ticket_type_id',
+6
View File
@@ -41,6 +41,12 @@ class Project extends Model
->orderBy('position');
}
public function ticketLinkTypes(): HasMany
{
return $this->hasMany(TicketLinkType::class)
->orderBy('position');
}
public function ticketTypes(): HasMany
{
return $this->hasMany(TicketType::class)
+24
View File
@@ -12,7 +12,9 @@ use Illuminate\Database\Eloquent\Relations\HasMany;
#[Fillable([
'acceptance_criteria',
'board_column_id',
'blocked_comment',
'description',
'is_blocked',
'parent_ticket_id',
'project_id',
'sequence_number',
@@ -26,6 +28,16 @@ class Ticket extends Model
/** @use HasFactory<TicketFactory> */
use HasFactory;
/**
* @return array<string, string>
*/
protected function casts(): array
{
return [
'is_blocked' => 'boolean',
];
}
public function boardColumn(): BelongsTo
{
return $this->belongsTo(BoardColumn::class);
@@ -48,6 +60,18 @@ class Ticket extends Model
return $this->belongsTo(Ticket::class, 'parent_ticket_id');
}
public function ticketLinksAsSource(): HasMany
{
return $this->hasMany(TicketLink::class, 'source_ticket_id')
->orderBy('id');
}
public function ticketLinksAsTarget(): HasMany
{
return $this->hasMany(TicketLink::class, 'target_ticket_id')
->orderBy('id');
}
public function project(): BelongsTo
{
return $this->belongsTo(Project::class);
+31
View File
@@ -0,0 +1,31 @@
<?php
namespace App\Models;
use Database\Factories\TicketLinkFactory;
use Illuminate\Database\Eloquent\Attributes\Fillable;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
#[Fillable(['source_ticket_id', 'target_ticket_id', 'ticket_link_type_id'])]
class TicketLink extends Model
{
/** @use HasFactory<TicketLinkFactory> */
use HasFactory;
public function sourceTicket(): BelongsTo
{
return $this->belongsTo(Ticket::class, 'source_ticket_id');
}
public function targetTicket(): BelongsTo
{
return $this->belongsTo(Ticket::class, 'target_ticket_id');
}
public function ticketLinkType(): BelongsTo
{
return $this->belongsTo(TicketLinkType::class);
}
}
+28
View File
@@ -0,0 +1,28 @@
<?php
namespace App\Models;
use Database\Factories\TicketLinkTypeFactory;
use Illuminate\Database\Eloquent\Attributes\Fillable;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
#[Fillable(['label', 'position', 'project_id'])]
class TicketLinkType extends Model
{
/** @use HasFactory<TicketLinkTypeFactory> */
use HasFactory;
public function project(): BelongsTo
{
return $this->belongsTo(Project::class);
}
public function ticketLinks(): HasMany
{
return $this->hasMany(TicketLink::class)
->orderBy('id');
}
}