*/ public static function copyableAreas(): array { return [ self::COPY_PROJECT_SETTINGS, self::COPY_BOARD_COLUMNS, self::COPY_TICKET_STATUSES, self::COPY_STATUS_TRANSITIONS, self::COPY_TICKET_TYPES, self::COPY_TICKET_PRIORITIES, self::COPY_TICKET_FIELDS, self::COPY_TICKET_LINK_TYPES, self::COPY_REPOSITORY_CONNECTIONS, self::COPY_NOTIFICATION_ACTIONS, ]; } /** @return list */ public static function acceptedCopyAreas(): array { return [...self::copyableAreas(), self::COPY_BOARD_WORKFLOW]; } /** @return list */ public static function copyAreaOptions(): array { return [ ['value' => self::COPY_PROJECT_SETTINGS, 'label' => 'Project settings', 'description' => 'General project preferences like story points and author visibility.'], ['value' => self::COPY_BOARD_COLUMNS, 'label' => 'Board columns', 'description' => 'Columns and their order on the project board.'], ['value' => self::COPY_TICKET_STATUSES, 'label' => 'Ticket statuses', 'description' => 'Statuses and their assignment to board columns.'], ['value' => self::COPY_STATUS_TRANSITIONS, 'label' => 'Status transitions', 'description' => 'Allowed moves between ticket statuses.'], ['value' => self::COPY_TICKET_TYPES, 'label' => 'Ticket types', 'description' => 'Available ticket types, including parent types.'], ['value' => self::COPY_TICKET_PRIORITIES, 'label' => 'Ticket priorities', 'description' => 'Available ticket priorities.'], ['value' => self::COPY_TICKET_FIELDS, 'label' => 'Custom ticket fields', 'description' => 'Custom field definitions without ticket values.'], ['value' => self::COPY_TICKET_LINK_TYPES, 'label' => 'Ticket link types', 'description' => 'Relationship labels between tickets.'], ['value' => self::COPY_REPOSITORY_CONNECTIONS, 'label' => 'Repository connections', 'description' => 'Repositories linked to the project.'], ['value' => self::COPY_NOTIFICATION_ACTIONS, 'label' => 'Notification actions', 'description' => 'Notification rules and delivery settings.'], ]; } /** @param list $copyAreas @return list */ public function normalizeCopyAreas(array $copyAreas): array { if (in_array(self::COPY_BOARD_WORKFLOW, $copyAreas, true)) { $copyAreas = [ ...array_diff($copyAreas, [self::COPY_BOARD_WORKFLOW]), self::COPY_BOARD_COLUMNS, self::COPY_TICKET_STATUSES, self::COPY_STATUS_TRANSITIONS, ]; } return array_values(array_intersect(self::copyableAreas(), array_unique($copyAreas))); } /** @param array $configuration @return array */ public function normalizeConfiguration(array $configuration): array { $workflow = $configuration[self::COPY_BOARD_WORKFLOW] ?? null; if (is_array($workflow)) { $statuses = $workflow['ticket_statuses'] ?? []; $configuration[self::COPY_BOARD_COLUMNS] ??= $workflow['board_columns'] ?? []; $configuration[self::COPY_TICKET_STATUSES] ??= array_map( static fn (array $status): array => array_diff_key($status, ['allowed_target_indexes' => true]), $statuses, ); $configuration[self::COPY_STATUS_TRANSITIONS] ??= array_map( static fn (array $status): array => $status['allowed_target_indexes'] ?? [], $statuses, ); unset($configuration[self::COPY_BOARD_WORKFLOW]); } return $configuration; } /** @param list $copyAreas @return array */ public function export(Project $sourceProject, array $copyAreas): array { $selectedAreas = $this->normalizeCopyAreas($copyAreas); if ($selectedAreas === []) { return []; } $sourceProject->load([ 'boardColumns', 'notificationActions', 'repositoryConnections', 'ticketFieldDefinitions', 'ticketLinkTypes', 'ticketPriorities', 'ticketStatuses.allowedTargetStatuses', 'ticketTypes', ]); $configuration = []; if (in_array(self::COPY_PROJECT_SETTINGS, $selectedAreas, true)) { $configuration[self::COPY_PROJECT_SETTINGS] = [ 'show_author' => $sourceProject->show_author, 'show_story_points' => $sourceProject->show_story_points, ]; } $columnIndexes = $sourceProject->boardColumns->values()->mapWithKeys( static fn ($column, int $index): array => [$column->id => $index], ); $statusIndexes = $sourceProject->ticketStatuses->values()->mapWithKeys( static fn ($status, int $index): array => [$status->id => $index], ); if (in_array(self::COPY_BOARD_COLUMNS, $selectedAreas, true)) { $configuration[self::COPY_BOARD_COLUMNS] = $sourceProject->boardColumns->values()->map( static fn ($column): array => ['name' => $column->name, 'position' => $column->position], )->all(); } if (in_array(self::COPY_TICKET_STATUSES, $selectedAreas, true)) { $configuration[self::COPY_TICKET_STATUSES] = $sourceProject->ticketStatuses->values()->map( static fn ($status): array => [ 'board_column_index' => $status->board_column_id === null ? null : $columnIndexes->get($status->board_column_id), 'name' => $status->name, 'position' => $status->position, ], )->all(); } if (in_array(self::COPY_STATUS_TRANSITIONS, $selectedAreas, true)) { $configuration[self::COPY_STATUS_TRANSITIONS] = $sourceProject->ticketStatuses->values()->map( static fn ($status): array => $status->allowedTargetStatuses->pluck('id')->map( static fn (int $id): ?int => $statusIndexes->get($id), )->filter(static fn (?int $index): bool => $index !== null)->values()->all(), )->all(); } $simpleAreas = [ self::COPY_TICKET_TYPES => fn ($item): array => ['name' => $item->name, 'position' => $item->position, 'is_parent' => $item->is_parent], self::COPY_TICKET_PRIORITIES => fn ($item): array => ['name' => $item->name, 'position' => $item->position], self::COPY_TICKET_FIELDS => fn ($item): array => ['field_type' => $item->field_type, 'label' => $item->label, 'position' => $item->position], self::COPY_TICKET_LINK_TYPES => fn ($item): array => ['label' => $item->label, 'counterpart_label' => $item->counterpart_label, 'position' => $item->position], self::COPY_REPOSITORY_CONNECTIONS => fn ($item): array => [ 'provider' => $item->provider->value, 'base_url' => $item->base_url, 'provider_repository_id' => $item->provider_repository_id, 'owner_name' => $item->owner_name, 'repository_name' => $item->repository_name, 'repository_full_name' => $item->repository_full_name, 'repository_url' => $item->repository_url, ], self::COPY_NOTIFICATION_ACTIONS => fn ($item): array => [ 'name' => $item->name, 'enabled' => $item->enabled, 'event_type' => $item->event_type->value, 'channel' => $item->channel->value, 'channel_configuration' => $item->channel_configuration ?? [], 'message_template' => $item->message_template, 'delivery_url' => $item->delivery_url, 'filters' => $item->filters ?? [], ], ]; $relations = [ self::COPY_TICKET_TYPES => 'ticketTypes', self::COPY_TICKET_PRIORITIES => 'ticketPriorities', self::COPY_TICKET_FIELDS => 'ticketFieldDefinitions', self::COPY_TICKET_LINK_TYPES => 'ticketLinkTypes', self::COPY_REPOSITORY_CONNECTIONS => 'repositoryConnections', self::COPY_NOTIFICATION_ACTIONS => 'notificationActions', ]; foreach ($simpleAreas as $area => $mapper) { if (in_array($area, $selectedAreas, true)) { $configuration[$area] = $sourceProject->{$relations[$area]}->values()->map($mapper)->all(); } } return $configuration; } /** * @param array $configuration * @param array $statusReplacements * @return list areas which retained referenced local values */ public function apply(Project $targetProject, array $configuration, array $statusReplacements = []): array { $configuration = $this->normalizeConfiguration($configuration); $blockedAreas = $this->projectTemplateOverrideManager->withoutRecording(function () use ($targetProject, $configuration, $statusReplacements): array { $blockedAreas = []; if (isset($configuration[self::COPY_PROJECT_SETTINGS])) { $targetProject->forceFill($configuration[self::COPY_PROJECT_SETTINGS])->saveQuietly(); } if (isset($configuration[self::COPY_BOARD_COLUMNS])) { $blocked = $this->syncModels( $targetProject->boardColumns()->get(), $configuration[self::COPY_BOARD_COLUMNS], fn (array $item) => $targetProject->boardColumns()->create($item), static fn (Model $model, array $item) => $model->forceFill($item)->saveQuietly(), static fn (Model $column): bool => ! $column->ticketStatuses()->exists(), ); if ($blocked) { $blockedAreas[] = self::COPY_BOARD_COLUMNS; } } if (isset($configuration[self::COPY_TICKET_STATUSES])) { $columns = $targetProject->boardColumns()->get()->values(); $items = collect($configuration[self::COPY_TICKET_STATUSES])->map(static function (array $item) use ($columns): array { $columnIndex = $item['board_column_index'] ?? null; $item['board_column_id'] = $columnIndex === null ? null : $columns->get($columnIndex)?->id; unset($item['board_column_index']); return $item; })->all(); $blocked = $this->syncModels( $targetProject->ticketStatuses()->get(), $items, fn (array $item) => $targetProject->ticketStatuses()->create($item), static fn (Model $model, array $item) => $model->forceFill($item)->saveQuietly(), function (Model $status) use ($statusReplacements): bool { if (! $status->tickets()->exists()) { return true; } $replacementId = $statusReplacements[$status->id] ?? null; if ($replacementId === null) { return false; } $status->tickets()->update(['ticket_status_id' => $replacementId]); return true; }, ); if ($blocked) { $blockedAreas[] = self::COPY_TICKET_STATUSES; } } if (isset($configuration[self::COPY_STATUS_TRANSITIONS])) { $statuses = $targetProject->ticketStatuses()->get()->values(); foreach ($configuration[self::COPY_STATUS_TRANSITIONS] as $index => $targets) { $status = $statuses->get($index); if ($status !== null) { $status->allowedTargetStatuses()->sync(collect($targets)->map(fn (int $target): ?int => $statuses->get($target)?->id)->filter()->all()); } } } $relations = [ self::COPY_TICKET_TYPES => ['ticketTypes', fn (Model $item): bool => ! $item->tickets()->exists()], self::COPY_TICKET_PRIORITIES => ['ticketPriorities', fn (Model $item): bool => ! $item->tickets()->exists()], self::COPY_TICKET_FIELDS => ['ticketFieldDefinitions', fn (Model $item): bool => ! $item->ticketFieldValues()->exists()], self::COPY_TICKET_LINK_TYPES => ['ticketLinkTypes', fn (Model $item): bool => ! $item->ticketLinks()->exists()], self::COPY_REPOSITORY_CONNECTIONS => ['repositoryConnections', null], self::COPY_NOTIFICATION_ACTIONS => ['notificationActions', null], ]; foreach ($relations as $area => [$relation, $canDelete]) { if (! isset($configuration[$area])) { continue; } $blocked = $this->syncModels( $targetProject->{$relation}()->get(), $configuration[$area], fn (array $item) => $targetProject->{$relation}()->create($item), static fn (Model $model, array $item) => $model->forceFill($item)->saveQuietly(), $canDelete, ); if ($blocked) { $blockedAreas[] = $area; } } foreach ($targetProject->configurationRelations() as $relation) { $targetProject->{$relation}()->update(['project_template_id' => $targetProject->project_template_id]); } return array_values(array_unique($blockedAreas)); }); foreach ($blockedAreas as $area) { $this->projectTemplateOverrideManager->mark($targetProject->fresh(), $area); } return $blockedAreas; } /** @param list $copyAreas */ public function handle(Project $sourceProject, Project $targetProject, array $copyAreas): void { $this->apply($targetProject, $this->export($sourceProject, $copyAreas)); } /** * @param EloquentCollection $models * @param array> $items */ private function syncModels(EloquentCollection $models, array $items, callable $create, callable $update, ?callable $canDelete = null): bool { foreach ($items as $index => $item) { $model = $models->get($index); $model === null ? $create($item) : $update($model, $item); } $blocked = false; foreach ($models->slice(count($items)) as $model) { if ($canDelete !== null && ! $canDelete($model)) { $blocked = true; continue; } $model->deleteQuietly(); } return $blocked; } }