diff --git a/app/Http/Controllers/NodeController.php b/app/Http/Controllers/NodeController.php index 7d31769..228bc6d 100644 --- a/app/Http/Controllers/NodeController.php +++ b/app/Http/Controllers/NodeController.php @@ -51,7 +51,7 @@ public function show(Node $node) $initTaskGroup = null; } - $lastAgentVersion = AgentRelease::latest()->sole()->tag_name; + $lastAgentVersion = AgentRelease::latest()->first()?->tag_name; $taskGroup = $node->actualTaskGroup(NodeTaskGroupType::SelfUpgrade); diff --git a/app/Models/DeploymentData.php b/app/Models/DeploymentData.php index 62348e1..d06d3cb 100644 --- a/app/Models/DeploymentData.php +++ b/app/Models/DeploymentData.php @@ -16,6 +16,7 @@ use App\Models\NodeTasks\CreateService\CreateServiceMeta; use App\Rules\RequiredIfArrayHas; use App\Util\Arrays; +use Illuminate\Validation\ValidationException; use Spatie\LaravelData\Attributes\DataCollectionOf; use Spatie\LaravelData\Attributes\Validation\Exists; use Spatie\LaravelData\Attributes\Validation\RequiredIf; @@ -75,7 +76,38 @@ public static function make(array $attributes): static public function copyWith(array $attributes): DeploymentData { - return DeploymentData::make(Arrays::niceMerge($this->toArray(), $attributes)); + $result = $this->toArray(); + $errors = []; + + if (isset($attributes['processes'])) { + foreach ($attributes['processes'] as $idx => $process) { + if (!isset($process['name'])) { + $errors["processes.{$idx}.name"] = "Process name is required"; + + continue; + } + + $processExists = false; + + foreach ($result['processes'] as $existingIdx => $existingProcess) { + if ($existingProcess['name'] === $process['name']) { + $result['processes'][$existingIdx] = Arrays::niceMerge($existingProcess, $process); + + $processExists = true; + } + } + + if (!$processExists) { + $errors["processes.{$idx}.name"] = "Process {$process['name']} does not exist"; + } + } + } + + if (!empty($errors)) { + throw ValidationException::withMessages($errors); + } + + return DeploymentData::validateAndCreate($result); } public function findProcess(string $dockerName): ?Process