From 708a08fdd624ee45e5586f200639e289d666d432 Mon Sep 17 00:00:00 2001 From: Andras Bacsai <5845193+andrasbacsai@users.noreply.github.com> Date: Thu, 25 Sep 2025 13:19:12 +0200 Subject: [PATCH] fix(application): enhance domain handling by replacing both dots and dashes with underscores for HTML form binding --- app/Livewire/Project/Application/General.php | 12 +-- app/Models/Application.php | 5 +- bootstrap/helpers/parsers.php | 82 ++++++++++++------- .../project/application/general.blade.php | 4 +- 4 files changed, 65 insertions(+), 38 deletions(-) diff --git a/app/Livewire/Project/Application/General.php b/app/Livewire/Project/Application/General.php index 2ade83038..ae9bd314b 100644 --- a/app/Livewire/Project/Application/General.php +++ b/app/Livewire/Project/Application/General.php @@ -210,10 +210,10 @@ public function mount() } } $this->parsedServiceDomains = $this->application->docker_compose_domains ? json_decode($this->application->docker_compose_domains, true) : []; - // Convert service names with dots to use underscores for HTML form binding + // Convert service names with dots and dashes to use underscores for HTML form binding $sanitizedDomains = []; foreach ($this->parsedServiceDomains as $serviceName => $domain) { - $sanitizedKey = str($serviceName)->slug('_')->toString(); + $sanitizedKey = str($serviceName)->replace('-', '_')->replace('.', '_')->toString(); $sanitizedDomains[$sanitizedKey] = $domain; } $this->parsedServiceDomains = $sanitizedDomains; @@ -305,10 +305,10 @@ public function loadComposeFile($isInit = false, $showToast = true) // Refresh parsedServiceDomains to reflect any changes in docker_compose_domains $this->application->refresh(); $this->parsedServiceDomains = $this->application->docker_compose_domains ? json_decode($this->application->docker_compose_domains, true) : []; - // Convert service names with dots to use underscores for HTML form binding + // Convert service names with dots and dashes to use underscores for HTML form binding $sanitizedDomains = []; foreach ($this->parsedServiceDomains as $serviceName => $domain) { - $sanitizedKey = str($serviceName)->slug('_')->toString(); + $sanitizedKey = str($serviceName)->replace('-', '_')->replace('.', '_')->toString(); $sanitizedDomains[$sanitizedKey] = $domain; } $this->parsedServiceDomains = $sanitizedDomains; @@ -334,7 +334,7 @@ public function generateDomain(string $serviceName) $uuid = new Cuid2; $domain = generateUrl(server: $this->application->destination->server, random: $uuid); - $sanitizedKey = str($serviceName)->slug('_')->toString(); + $sanitizedKey = str($serviceName)->replace('-', '_')->replace('.', '_')->toString(); $this->parsedServiceDomains[$sanitizedKey]['domain'] = $domain; // Convert back to original service names for storage @@ -344,7 +344,7 @@ public function generateDomain(string $serviceName) $originalServiceName = $key; if (isset($this->parsedServices['services'])) { foreach ($this->parsedServices['services'] as $originalName => $service) { - if (str($originalName)->slug('_')->toString() === $key) { + if (str($originalName)->replace('-', '_')->replace('.', '_')->toString() === $key) { $originalServiceName = $originalName; break; } diff --git a/app/Models/Application.php b/app/Models/Application.php index 094e5c82b..9f6dcb125 100644 --- a/app/Models/Application.php +++ b/app/Models/Application.php @@ -1479,14 +1479,14 @@ public function loadComposeFile($isInit = false) if ($this->docker_compose_domains) { $json = collect(json_decode($this->docker_compose_domains)); foreach ($json as $key => $value) { - if (str($key)->contains('-')) { + if (str($key)->contains('-') || str($key)->contains('.')) { $key = str($key)->replace('-', '_')->replace('.', '_'); } $json->put((string) $key, $value); } $services = collect(data_get($parsedServices, 'services', [])); foreach ($services as $name => $service) { - if (str($name)->contains('-')) { + if (str($name)->contains('-') || str($name)->contains('.')) { $replacedName = str($name)->replace('-', '_')->replace('.', '_'); $services->put((string) $replacedName, $service); $services->forget((string) $name); @@ -1503,6 +1503,7 @@ public function loadComposeFile($isInit = false) } else { $this->docker_compose_domains = null; } + ray($this->docker_compose_domains); $this->save(); } diff --git a/bootstrap/helpers/parsers.php b/bootstrap/helpers/parsers.php index d4701d251..25cc5d0a6 100644 --- a/bootstrap/helpers/parsers.php +++ b/bootstrap/helpers/parsers.php @@ -385,21 +385,34 @@ function applicationParser(Application $resource, int $pull_request_id = 0, ?int 'is_preview' => false, ]); if ($resource->build_pack === 'dockercompose') { - $domains = collect(json_decode(data_get($resource, 'docker_compose_domains'))) ?? collect([]); - $domainExists = data_get($domains->get($fqdnFor), 'domain'); - $envExists = $resource->environment_variables()->where('key', $key->value())->first(); - if (str($domainExists)->replace('http://', '')->replace('https://', '')->value() !== $envExists->value) { - $envExists->update([ - 'value' => $url, - ]); + // Check if a service with this name actually exists + $serviceExists = false; + foreach ($services as $serviceName => $service) { + $transformedServiceName = str($serviceName)->replace('-', '_')->replace('.', '_')->value(); + if ($transformedServiceName === $fqdnFor) { + $serviceExists = true; + break; + } } - if (is_null($domainExists)) { - // Put URL in the domains array instead of FQDN - $domains->put((string) $fqdnFor, [ - 'domain' => $url, - ]); - $resource->docker_compose_domains = $domains->toJson(); - $resource->save(); + + // Only add domain if the service exists + if ($serviceExists) { + $domains = collect(json_decode(data_get($resource, 'docker_compose_domains'))) ?? collect([]); + $domainExists = data_get($domains->get($fqdnFor), 'domain'); + $envExists = $resource->environment_variables()->where('key', $key->value())->first(); + if (str($domainExists)->replace('http://', '')->replace('https://', '')->value() !== $envExists->value) { + $envExists->update([ + 'value' => $url, + ]); + } + if (is_null($domainExists)) { + // Put URL in the domains array instead of FQDN + $domains->put((string) $fqdnFor, [ + 'domain' => $url, + ]); + $resource->docker_compose_domains = $domains->toJson(); + $resource->save(); + } } } } elseif ($command->value() === 'URL') { @@ -418,20 +431,33 @@ function applicationParser(Application $resource, int $pull_request_id = 0, ?int 'is_preview' => false, ]); if ($resource->build_pack === 'dockercompose') { - $domains = collect(json_decode(data_get($resource, 'docker_compose_domains'))) ?? collect([]); - $domainExists = data_get($domains->get($urlFor), 'domain'); - $envExists = $resource->environment_variables()->where('key', $key->value())->first(); - if ($domainExists !== $envExists->value) { - $envExists->update([ - 'value' => $url, - ]); + // Check if a service with this name actually exists + $serviceExists = false; + foreach ($services as $serviceName => $service) { + $transformedServiceName = str($serviceName)->replace('-', '_')->replace('.', '_')->value(); + if ($transformedServiceName === $urlFor) { + $serviceExists = true; + break; + } } - if (is_null($domainExists)) { - $domains->put((string) $urlFor, [ - 'domain' => $url, - ]); - $resource->docker_compose_domains = $domains->toJson(); - $resource->save(); + + // Only add domain if the service exists + if ($serviceExists) { + $domains = collect(json_decode(data_get($resource, 'docker_compose_domains'))) ?? collect([]); + $domainExists = data_get($domains->get($urlFor), 'domain'); + $envExists = $resource->environment_variables()->where('key', $key->value())->first(); + if ($domainExists !== $envExists->value) { + $envExists->update([ + 'value' => $url, + ]); + } + if (is_null($domainExists)) { + $domains->put((string) $urlFor, [ + 'domain' => $url, + ]); + $resource->docker_compose_domains = $domains->toJson(); + $resource->save(); + } } } } else { @@ -910,7 +936,7 @@ function applicationParser(Application $resource, int $pull_request_id = 0, ?int $preview = $resource->previews()->find($preview_id); $docker_compose_domains = collect(json_decode(data_get($preview, 'docker_compose_domains'))); if ($docker_compose_domains->count() > 0) { - $found_fqdn = data_get($docker_compose_domains, "$serviceName.domain"); + $found_fqdn = data_get($docker_compose_domains, "$changedServiceName.domain"); if ($found_fqdn) { $fqdns = collect($found_fqdn); } else { diff --git a/resources/views/livewire/project/application/general.blade.php b/resources/views/livewire/project/application/general.blade.php index 6fddb549a..41ce78222 100644 --- a/resources/views/livewire/project/application/general.blade.php +++ b/resources/views/livewire/project/application/general.blade.php @@ -50,8 +50,8 @@
@can('update', $application) Generate