Files
KanbanGit/app/Http/Controllers/ProjectTicketController.php
T
Twyco 73cee8178f
linter / quality (pull_request) Successful in 2m28s
tests / ci (8.4) (pull_request) Successful in 1m22s
Enforce request validation style rules
2026-04-11 19:44:21 +02:00

222 lines
8.3 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Actions\Git\BuildSuggestedTicketBranchName;
use App\Actions\Git\ResolveTicketBranchMatches;
use App\Actions\Git\ResolveTicketPullRequestMatches;
use App\Actions\ProjectPermissions\ProjectPermissionService;
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;
use Illuminate\Support\Arr;
use Inertia\Inertia;
use Inertia\Response;
class ProjectTicketController extends Controller
{
public function __construct(
private readonly BuildSuggestedTicketBranchName $buildSuggestedTicketBranchName,
private readonly ProjectPermissionService $projectPermissionService,
private readonly ResolveTicketBranchMatches $resolveTicketBranchMatches,
private readonly ResolveTicketPullRequestMatches $resolveTicketPullRequestMatches,
private readonly UpdateProjectTicket $updateProjectTicket,
) {}
public function show(Request $request, Project $project, Ticket $ticket): Response
{
$user = $request->user();
abort_unless(
$this->projectPermissionService->userCanViewProject($user, $project),
403,
);
$excludedParentIds = [$ticket->id, ...$this->descendantIds($ticket)];
return Inertia::render('projects/Ticket', [
'canEditTicket' => $this->projectPermissionService->userHasPermission($user, $project, 'edit'),
'childTicketTree' => $this->buildChildTicketTree($ticket),
'linkedTickets' => $this->linkedTickets($ticket),
'parentTicketOptions' => Ticket::query()
->where('project_id', $project->id)
->whereNotIn('id', $excludedParentIds)
->whereHas('ticketType', fn ($query) => $query->where('system_key', 'parent'))
->orderByDesc('sequence_number')
->get()
->map(fn (Ticket $parentTicket): array => [
'id' => $parentTicket->id,
'ticket_key' => $parentTicket->ticket_key,
'title' => $parentTicket->title,
]),
'project' => $project,
'repositoryBranches' => $this->resolveTicketBranchMatches->handle(
$user,
$project,
$ticket,
),
'repositoryPullRequests' => $this->resolveTicketPullRequestMatches->handle(
$user,
$project,
$ticket,
),
'status' => session('status'),
'suggestedBranchName' => $this->buildSuggestedTicketBranchName->handle($ticket),
'ticket' => $ticket->load([
'fieldValues.ticketFieldDefinition',
'fieldValues.userValue',
'parentTicket',
'ticketPriority',
'ticketType',
'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()
->orderBy('name')
->get(),
]);
}
public function update(
UpdateProjectTicketRequest $request,
Project $project,
Ticket $ticket,
): RedirectResponse {
$user = $request->user();
abort_unless(
$this->projectPermissionService->userHasPermission($user, $project, 'edit'),
403,
);
$validated = $request->validated();
$fieldValues = Arr::get($validated, 'field_values');
$ticketAttributes = [
'acceptance_criteria' => Arr::get($validated, 'acceptance_criteria'),
'blocked_comment' => Arr::get($validated, 'blocked_comment'),
'description' => Arr::get($validated, 'description'),
'is_blocked' => Arr::get($validated, 'is_blocked', false),
'parent_ticket_id' => Arr::get($validated, 'parent_ticket_id'),
'ticket_priority_id' => Arr::get($validated, 'ticket_priority_id'),
'ticket_type_id' => Arr::get($validated, 'ticket_type_id'),
'title' => Arr::get($validated, 'title'),
];
$wasUpdated = $this->updateProjectTicket->handle(
$ticket,
$user,
$ticketAttributes,
$fieldValues,
);
$statusMessage = $wasUpdated
? "{$ticket->ticket_key} was updated."
: 'No ticket changes were detected.';
return to_route('projects.tickets.show', [$project, $ticket])
->with('status', $statusMessage);
}
/**
* @return array<int, array{
* children: array<int, mixed>,
* id: int,
* ticket_key: string,
* ticket_type_name: string|null,
* title: string
* }>
*/
private function buildChildTicketTree(Ticket $ticket): array
{
return $ticket->childTickets()
->with('ticketType')
->get()
->map(fn (Ticket $childTicket): array => [
'children' => $this->buildChildTicketTree($childTicket),
'id' => $childTicket->id,
'ticket_key' => $childTicket->ticket_key,
'ticket_type_name' => $childTicket->ticketType?->name,
'title' => $childTicket->title,
])
->all();
}
/**
* @return array<int, int>
*/
private function descendantIds(Ticket $ticket): array
{
return $ticket->childTickets()
->get()
->flatMap(function (Ticket $childTicket): array {
return [$childTicket->id, ...$this->descendantIds($childTicket)];
})
->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();
}
}