codex ticket rework

This commit is contained in:
2026-04-13 01:04:06 +02:00
parent b3f2cc3afd
commit 182ed018d1
45 changed files with 1208 additions and 420 deletions
@@ -8,26 +8,24 @@ class InitializeProjectTicketMetadata
{
public function handle(Project $project): void
{
$project->ticketTypes()->create(
[
if (! $project->ticketTypes()->exists()) {
$project->ticketTypes()->create([
'name' => 'Task',
'position' => 1,
],
);
]);
$project->ticketTypes()->create(
[
$project->ticketTypes()->create([
'name' => 'Release',
'is_parent' => true,
'position' => 2,
],
);
]);
}
$project->ticketPriorities()->create(
[
if (! $project->ticketPriorities()->exists()) {
$project->ticketPriorities()->create([
'name' => 'Medium',
'position' => 1,
]
);
]);
}
}
}
+10 -6
View File
@@ -5,7 +5,7 @@ namespace App\Actions\Tickets;
use App\Models\Project;
use App\Models\Ticket;
use App\Models\User;
use Arr;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\DB;
class CreateProjectTicket
@@ -17,7 +17,9 @@ class CreateProjectTicket
/**
* @param array{
* acceptance_criteria: string|null,
* assignee_user_id?: int|null,
* description: string|null,
* story_points?: int|null,
* ticket_priority_id: int,
* ticket_type_id: int,
* title: string
@@ -31,15 +33,17 @@ class CreateProjectTicket
->max('sequence_number') + 1;
$ticket = Ticket::query()->create([
'acceptance_criteria' => $attributes['acceptance_criteria'],
'acceptance_criteria' => Arr::get($attributes, 'acceptance_criteria'),
'assignee_user_id' => Arr::get($attributes, 'assignee_user_id'),
'author_user_id' => $actor?->id,
'board_column_id' => null,
'description' => $attributes['description'],
'description' => Arr::get($attributes, 'description'),
'project_id' => $project->id,
'sequence_number' => $nextSequenceNumber,
'ticket_key' => "{$project->key}-{$nextSequenceNumber}",
'ticket_priority_id' => $attributes['ticket_priority_id'],
'ticket_type_id' => $attributes['ticket_type_id'],
'title' => $attributes['title'],
'ticket_priority_id' => Arr::get($attributes, 'ticket_priority_id'),
'ticket_type_id' => Arr::get($attributes, 'ticket_type_id'),
'title' => Arr::get($attributes, 'title'),
'story_points' => Arr::get($attributes, 'story_points'),
]);
@@ -18,9 +18,12 @@ class UpdateProjectTicket
/**
* @param array{
* acceptance_criteria: string|null,
* assignee_user_id: int|null,
* author_user_id: int|null,
* blocked_comment: string|null,
* description: string|null,
* is_blocked: bool,
* story_points: int|null,
* ticket_priority_id: int,
* ticket_type_id: int,
* parent_ticket_id: int|null,
@@ -44,6 +47,8 @@ class UpdateProjectTicket
}
$ticket->loadMissing([
'assignee',
'author',
'fieldValues.ticketFieldDefinition',
'fieldValues.userValue',
'parentTicket',
@@ -103,6 +108,14 @@ class UpdateProjectTicket
$ticket->blocked_comment,
$ticketAttributes['blocked_comment'],
);
$this->trackNumberChange(
$changes,
$summaryLabels,
'story_points',
'story points',
$ticket->story_points,
$ticketAttributes['story_points'],
);
if ($ticket->ticket_type_id !== $ticketAttributes['ticket_type_id']) {
$changes['ticket_type'] = [
@@ -140,6 +153,26 @@ class UpdateProjectTicket
$summaryLabels[] = 'parent ticket';
}
if ($ticket->assignee_user_id !== $ticketAttributes['assignee_user_id']) {
$changes['assignee'] = [
'from' => $ticket->assignee?->name,
'to' => $ticketAttributes['assignee_user_id'] === null
? null
: User::query()->find($ticketAttributes['assignee_user_id'])?->name,
];
$summaryLabels[] = 'assignee';
}
if ($ticket->author_user_id !== $ticketAttributes['author_user_id']) {
$changes['author'] = [
'from' => $ticket->author?->name,
'to' => $ticketAttributes['author_user_id'] === null
? null
: User::query()->find($ticketAttributes['author_user_id'])?->name,
];
$summaryLabels[] = 'author';
}
$ticket->fill($ticketAttributes);
if ($ticket->isDirty()) {
@@ -217,6 +250,29 @@ class UpdateProjectTicket
$summaryLabels[] = $summaryLabel;
}
/**
* @param array<string, mixed> $changes
* @param array<int, string> $summaryLabels
*/
private function trackNumberChange(
array &$changes,
array &$summaryLabels,
string $changeKey,
string $summaryLabel,
?int $currentValue,
?int $newValue,
): void {
if ($currentValue === $newValue) {
return;
}
$changes[$changeKey] = [
'from' => $currentValue,
'to' => $newValue,
];
$summaryLabels[] = $summaryLabel;
}
/**
* @param array<string, mixed> $changes
* @param array{value_user_id?: int|string|null} $fieldValueAttribute
@@ -8,6 +8,7 @@ use App\Enums\Permission;
use App\Http\Requests\StoreProjectTicketRequest;
use App\Models\Project;
use App\Models\Ticket;
use App\Models\User;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Inertia\Inertia;
@@ -43,6 +44,9 @@ class ProjectBacklogController extends Controller
->whereNull('board_column_id')
->orderByDesc('sequence_number')
->get(),
'users' => User::query()
->orderBy('name')
->get(),
]);
}
@@ -38,10 +38,28 @@ class ProjectTicketController extends Controller
403,
);
$canEditTicket = $this->permissionService->allows($user, Permission::ProjectWrite, $project);
$canManageProject = $this->permissionService->allows($user, Permission::ProjectAdmin, $project);
$excludedParentIds = [$ticket->id, ...$this->descendantIds($ticket)];
$displayTicket = $ticket->load([
'assignee',
'author',
'fieldValues.ticketFieldDefinition',
'fieldValues.userValue',
'parentTicket',
'ticketPriority',
'ticketType',
'worklogEntries.actor',
]);
if (! $project->show_author && ! $canManageProject) {
$displayTicket->setRelation('author', null);
$displayTicket->author_user_id = null;
}
return Inertia::render('projects/Ticket', [
'canEditTicket' => $this->permissionService->allows($user, Permission::ProjectWrite, $project),
'canEditTicket' => $canEditTicket,
'canManageProject' => $canManageProject,
'childTicketTree' => $this->buildChildTicketTree($ticket),
'linkedTickets' => $this->linkedTickets($ticket),
'parentTicketOptions' => Ticket::query()
@@ -68,14 +86,7 @@ class ProjectTicketController extends Controller
),
'status' => session('status'),
'suggestedBranchName' => $this->buildSuggestedTicketBranchName->handle($ticket),
'ticket' => $ticket->load([
'fieldValues.ticketFieldDefinition',
'fieldValues.userValue',
'parentTicket',
'ticketPriority',
'ticketType',
'worklogEntries.actor',
]),
'ticket' => $displayTicket,
'ticketFieldDefinitions' => $project->ticketFieldDefinitions()->get(),
'ticketLinkOptions' => Ticket::query()
->where('project_id', $project->id)
@@ -109,17 +120,25 @@ class ProjectTicketController extends Controller
);
$validated = $request->validated();
$fieldValues = Arr::get($validated, 'field_values');
$fieldValues = Arr::get($validated, 'field_values', []);
$ticketAttributes = [
'acceptance_criteria' => Arr::get($validated, 'acceptance_criteria'),
'assignee_user_id' => Arr::exists($validated, 'assignee_user_id')
? Arr::get($validated, 'assignee_user_id')
: $ticket->assignee_user_id,
'author_user_id' => Arr::exists($validated, 'author_user_id')
? Arr::get($validated, 'author_user_id')
: $ticket->author_user_id,
'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'),
'story_points' => Arr::exists($validated, 'story_points')
? Arr::get($validated, 'story_points')
: $ticket->story_points,
'ticket_priority_id' => Arr::get($validated, 'ticket_priority_id'),
'ticket_type_id' => Arr::get($validated, 'ticket_type_id'),
'title' => Arr::get($validated, 'title'),
'story_points' => Arr::get($validated, 'story_points'),
];
$wasUpdated = $this->updateProjectTicket->handle(
@@ -3,14 +3,18 @@
namespace App\Http\Controllers;
use App\Actions\Permissions\PermissionService;
use App\Actions\Tickets\CreateTicketWorklogEntry;
use App\Enums\Permission;
use App\Http\Requests\DestroyProjectTicketTypeRequest;
use App\Http\Requests\StoreProjectNamedSettingRequest;
use App\Http\Requests\StoreProjectTicketTypeRequest;
use App\Http\Requests\StoreTicketFieldDefinitionRequest;
use App\Http\Requests\UpdateProjectNamedSettingRequest;
use App\Http\Requests\UpdateProjectTicketMetadataSettings;
use App\Http\Requests\UpdateProjectTicketTypeRequest;
use App\Http\Requests\UpdateTicketFieldDefinitionRequest;
use App\Models\Project;
use App\Models\Ticket;
use App\Models\TicketFieldDefinition;
use App\Models\TicketLinkType;
use App\Models\TicketPriority;
@@ -27,6 +31,7 @@ use Inertia\Response;
class ProjectTicketMetadataSettingsController extends Controller
{
public function __construct(
private readonly CreateTicketWorklogEntry $createTicketWorklogEntry,
private readonly PermissionService $permissionService,
) {}
@@ -47,15 +52,20 @@ class ProjectTicketMetadataSettingsController extends Controller
'ticketFieldDefinitions' => $project->ticketFieldDefinitions()->get(),
'ticketLinkTypes' => $project->ticketLinkTypes()->get(),
'ticketPriorities' => $project->ticketPriorities()->get(),
'ticketTypes' => $project->ticketTypes()->get(),
'ticketTypes' => $project->ticketTypes()->withCount('tickets')->get(),
]);
}
public function update(UpdateProjectTicketMetadataSettings $request, Project $project): RedirectResponse
{
$this->abortUnlessCanManage($request, $project);
$validated = $request->validated();
$project->update(['show_story_points' => Arr::get($validated, 'show_story_points', false)]);
$project->update([
'show_author' => Arr::get($validated, 'show_author', false),
'show_story_points' => Arr::get($validated, 'show_story_points', false),
]);
return to_route('projects.settings.metadata', $project)
->with('status', 'Settings have been updated.');
@@ -88,7 +98,7 @@ class ProjectTicketMetadataSettingsController extends Controller
}
public function updateType(
UpdateProjectNamedSettingRequest $request,
UpdateProjectTicketTypeRequest $request,
Project $project,
TicketType $ticketType,
): RedirectResponse {
@@ -99,7 +109,6 @@ class ProjectTicketMetadataSettingsController extends Controller
DB::transaction(function () use ($project, $validated, $ticketType): void {
$ticketType->update([
'name' => Arr::get($validated, 'name'),
'is_parent' => Arr::get($validated, 'is_parent', false),
]);
$this->reorderModels(
@@ -113,8 +122,11 @@ class ProjectTicketMetadataSettingsController extends Controller
->with('status', "{$ticketType->fresh()->name} type was updated.");
}
public function destroyType(Request $request, Project $project, TicketType $ticketType): RedirectResponse
{
public function destroyType(
DestroyProjectTicketTypeRequest $request,
Project $project,
TicketType $ticketType,
): RedirectResponse {
$this->abortUnlessCanManage($request, $project);
if ($project->ticketTypes()->count() === 1) {
@@ -124,8 +136,40 @@ class ProjectTicketMetadataSettingsController extends Controller
}
$deletedName = $ticketType->name;
$validated = $request->validated();
DB::transaction(function () use ($deletedName, $project, $request, $ticketType, $validated): void {
$replacementTicketTypeId = Arr::get($validated, 'replacement_ticket_type_id');
if ($replacementTicketTypeId !== null) {
$replacementTicketType = $project->ticketTypes()
->findOrFail($replacementTicketTypeId);
Ticket::query()
->where('project_id', $project->id)
->where('ticket_type_id', $ticketType->id)
->with(['ticketType'])
->get()
->each(function (Ticket $ticket) use ($deletedName, $replacementTicketType, $request): void {
$ticket->update([
'ticket_type_id' => $replacementTicketType->id,
]);
$this->createTicketWorklogEntry->handle(
$ticket->fresh(),
$request->user(),
'updated',
"Updated type because {$deletedName} type was deleted.",
[
'ticket_type' => [
'from' => $deletedName,
'to' => $replacementTicketType->name,
],
],
);
});
}
DB::transaction(function () use ($project, $ticketType): void {
$ticketType->delete();
$this->persistOrderedModels($project->ticketTypes()->get()->values());
@@ -0,0 +1,101 @@
<?php
namespace App\Http\Requests;
use App\Models\Project;
use App\Models\TicketType;
use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Arr;
use Illuminate\Validation\Rule;
use Illuminate\Validation\Validator;
class DestroyProjectTicketTypeRequest 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 [
'replacement_ticket_type_id' => [
'nullable',
'integer',
Rule::exists('ticket_types', 'id')->where('project_id', $project->id),
],
];
}
/**
* @return array<int, callable(Validator): void>
*/
public function after(): array
{
return [
function (Validator $validator): void {
/** @var Project $project */
$project = $this->route('project');
/** @var TicketType $ticketType */
$ticketType = $this->route('ticketType');
$hasReplacementOptions = $project->ticketTypes()
->whereKeyNot($ticketType->id)
->exists();
if (! $hasReplacementOptions || ! $ticketType->tickets()->exists()) {
return;
}
/** @var array<string, mixed> $validated */
$validated = $this->safe()->all();
$replacementTicketTypeId = Arr::get($validated, 'replacement_ticket_type_id');
if ($replacementTicketTypeId === null) {
$validator->errors()->add(
'replacement_ticket_type_id',
'Choose a replacement type before deleting a type that is assigned to tickets.',
);
return;
}
if ((int) $replacementTicketTypeId === $ticketType->id) {
$validator->errors()->add(
'replacement_ticket_type_id',
'The replacement type must be different from the deleted type.',
);
return;
}
$replacementTicketType = $project->ticketTypes()
->find($replacementTicketTypeId);
if (
$replacementTicketType !== null
&& ! $replacementTicketType->is_parent
&& $ticketType->tickets()->has('childTickets')->exists()
) {
$validator->errors()->add(
'replacement_ticket_type_id',
'Choose a parent-enabled replacement type because affected tickets already have child tickets.',
);
}
},
];
}
}
@@ -29,6 +29,7 @@ class StoreProjectTicketRequest extends FormRequest
return [
'acceptance_criteria' => ['nullable', 'string'],
'assignee_user_id' => ['nullable', 'integer', Rule::exists('users', 'id')],
'description' => ['nullable', 'string'],
'ticket_priority_id' => [
'required',
@@ -25,7 +25,6 @@ class UpdateProjectNamedSettingRequest extends FormRequest
return [
'name' => ['required', 'string', 'max:255'],
'position' => ['required', 'integer', 'min:1'],
'is_parent' => ['nullable', 'boolean'],
];
}
}
@@ -23,7 +23,8 @@ class UpdateProjectTicketMetadataSettings extends FormRequest
public function rules(): array
{
return [
'show_story_points' => 'nullable|boolean',
'show_author' => ['nullable', 'boolean'],
'show_story_points' => ['nullable', 'boolean'],
];
}
}
@@ -2,6 +2,8 @@
namespace App\Http\Requests;
use App\Actions\Permissions\PermissionService;
use App\Enums\Permission;
use App\Models\Project;
use App\Models\Ticket;
use App\Models\TicketType;
@@ -33,6 +35,8 @@ class UpdateProjectTicketRequest extends FormRequest
return [
'acceptance_criteria' => ['nullable', 'string'],
'assignee_user_id' => ['nullable', 'integer', Rule::exists('users', 'id')],
'author_user_id' => ['nullable', 'integer', Rule::exists('users', 'id')],
'blocked_comment' => ['nullable', 'string'],
'description' => ['nullable', 'string'],
'field_values' => ['array'],
@@ -61,7 +65,7 @@ class UpdateProjectTicketRequest extends FormRequest
Rule::exists('ticket_types', 'id')->where('project_id', $project->id),
],
'title' => ['required', 'string', 'max:255'],
'story_points' => ['nullable', 'integer', 'gt:0'],
'story_points' => ['nullable', 'integer', 'min:0'],
];
}
@@ -73,7 +77,7 @@ class UpdateProjectTicketRequest extends FormRequest
return [
function (Validator $validator): void {
/** @var array<string, mixed> $validated */
$validated = $this->validated();
$validated = $this->safe()->all();
/** @var Project $project */
$project = $this->route('project');
@@ -81,6 +85,9 @@ class UpdateProjectTicketRequest extends FormRequest
/** @var Ticket $ticket */
$ticket = $this->route('ticket');
$canManageProject = app(PermissionService::class)
->allows($this->user(), Permission::ProjectAdmin, $project);
$parentTicketId = Arr::get($validated, 'parent_ticket_id');
if ($parentTicketId !== null) {
@@ -95,8 +102,8 @@ class UpdateProjectTicketRequest extends FormRequest
->with('ticketType')
->find($parentTicketId);
if ($parentTicket !== null && $parentTicket->ticketType?->system_key !== 'parent') {
$validator->errors()->add('parent_ticket_id', 'Only tickets of type Parent can be selected as a parent.');
if ($parentTicket !== null && ! $parentTicket->ticketType?->is_parent) {
$validator->errors()->add('parent_ticket_id', 'Only tickets with a parent-enabled type can be selected as a parent.');
return;
}
@@ -114,15 +121,23 @@ class UpdateProjectTicketRequest extends FormRequest
->find($requestedTicketTypeId);
if (
$requestedTicketType?->system_key !== 'parent'
! $requestedTicketType?->is_parent
&& $ticket->childTickets()->exists()
) {
$validator->errors()->add('ticket_type_id', 'Tickets with child tickets must keep a Parent type.');
$validator->errors()->add('ticket_type_id', 'Tickets with child tickets must keep a parent-enabled type.');
}
if (Arr::get($validated, 'is_blocked', false) && blank(Arr::get($validated, 'blocked_comment'))) {
$validator->errors()->add('blocked_comment', 'Blocked tickets require a blocker comment.');
}
if (
Arr::exists($validated, 'author_user_id')
&& (int) Arr::get($validated, 'author_user_id') !== (int) $ticket->author_user_id
&& ! $canManageProject
) {
$validator->errors()->add('author_user_id', 'Only project admins can change the author.');
}
},
];
}
@@ -0,0 +1,30 @@
<?php
namespace App\Http\Requests;
use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Foundation\Http\FormRequest;
class UpdateProjectTicketTypeRequest 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'],
'position' => ['required', 'integer', 'min:1'],
];
}
}
+2 -1
View File
@@ -11,7 +11,7 @@ use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Illuminate\Support\Str;
#[Fillable(['name', 'key', 'show_story_points'])]
#[Fillable(['name', 'key', 'show_author', 'show_story_points'])]
class Project extends Model
{
/** @use HasFactory<ProjectFactory> */
@@ -20,6 +20,7 @@ class Project extends Model
protected function casts(): array
{
return [
'show_author' => 'boolean',
'show_story_points' => 'boolean',
];
}
+12
View File
@@ -11,6 +11,8 @@ use Illuminate\Database\Eloquent\Relations\HasMany;
#[Fillable([
'acceptance_criteria',
'assignee_user_id',
'author_user_id',
'board_column_id',
'blocked_comment',
'description',
@@ -44,6 +46,16 @@ class Ticket extends Model
return $this->belongsTo(BoardColumn::class);
}
public function assignee(): BelongsTo
{
return $this->belongsTo(User::class, 'assignee_user_id');
}
public function author(): BelongsTo
{
return $this->belongsTo(User::class, 'author_user_id');
}
public function childTickets(): HasMany
{
return $this->hasMany(Ticket::class, 'parent_ticket_id')
+6
View File
@@ -7,6 +7,7 @@ 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(['name', 'position', 'project_id', 'is_parent'])]
class TicketType extends Model
@@ -25,4 +26,9 @@ class TicketType extends Model
{
return $this->belongsTo(Project::class);
}
public function tickets(): HasMany
{
return $this->hasMany(Ticket::class);
}
}
+10
View File
@@ -59,4 +59,14 @@ class User extends Authenticatable
->orderBy('provider')
->orderBy('base_url');
}
public function assignedTickets(): HasMany
{
return $this->hasMany(Ticket::class, 'assignee_user_id');
}
public function authoredTickets(): HasMany
{
return $this->hasMany(Ticket::class, 'author_user_id');
}
}