From f6cb39732af4da388e2fea0b7b311abca5efe5dd Mon Sep 17 00:00:00 2001 From: ayntk-ai <122374094+ayntk-ai@users.noreply.github.com> Date: Mon, 12 Aug 2024 13:14:38 +0200 Subject: [PATCH 01/10] merge env save button --- .../shared/environment-variable/all.blade.php | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/resources/views/livewire/project/shared/environment-variable/all.blade.php b/resources/views/livewire/project/shared/environment-variable/all.blade.php index 649d59a20..d4e703499 100644 --- a/resources/views/livewire/project/shared/environment-variable/all.blade.php +++ b/resources/views/livewire/project/shared/environment-variable/all.blade.php @@ -48,16 +48,15 @@ @endforeach @endif @else -
+ - Save -
- @if ($showPreview) -
+ + @if ($showPreview) - Save -
- @endif + @endif + + Save Environment Variables + @endif - + \ No newline at end of file From d13c321c3dca154430310e3520cd565ac8189c60 Mon Sep 17 00:00:00 2001 From: ayntk-ai <122374094+ayntk-ai@users.noreply.github.com> Date: Mon, 12 Aug 2024 13:23:09 +0200 Subject: [PATCH 02/10] new submit metode --- .../livewire/project/shared/environment-variable/all.blade.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/resources/views/livewire/project/shared/environment-variable/all.blade.php b/resources/views/livewire/project/shared/environment-variable/all.blade.php index d4e703499..5226199c6 100644 --- a/resources/views/livewire/project/shared/environment-variable/all.blade.php +++ b/resources/views/livewire/project/shared/environment-variable/all.blade.php @@ -48,7 +48,7 @@ @endforeach @endif @else -
+ @if ($showPreview) From c5aba34a6f0232e342f678f5be4a21a139e548f3 Mon Sep 17 00:00:00 2001 From: ayntk-ai <122374094+ayntk-ai@users.noreply.github.com> Date: Mon, 12 Aug 2024 13:23:36 +0200 Subject: [PATCH 03/10] new submit logic --- .../Shared/EnvironmentVariable/All.php | 206 ++++++------------ 1 file changed, 69 insertions(+), 137 deletions(-) diff --git a/app/Livewire/Project/Shared/EnvironmentVariable/All.php b/app/Livewire/Project/Shared/EnvironmentVariable/All.php index 9e6760293..dbd5c2b9b 100644 --- a/app/Livewire/Project/Shared/EnvironmentVariable/All.php +++ b/app/Livewire/Project/Shared/EnvironmentVariable/All.php @@ -105,93 +105,82 @@ class All extends Component $this->sortMe(); } - public function saveVariables($isPreview) + public function submit() { - if ($isPreview) { - $variables = parseEnvFormatToArray($this->variablesPreview); - $this->resource->environment_variables_preview()->whereNotIn('key', array_keys($variables))->delete(); - } else { - $variables = parseEnvFormatToArray($this->variables); - $this->resource->environment_variables()->whereNotIn('key', array_keys($variables))->delete(); - } - foreach ($variables as $key => $variable) { + try { + $isPreview = $this->showPreview; + $variables = $isPreview ? parseEnvFormatToArray($this->variablesPreview) : parseEnvFormatToArray($this->variables); + if ($isPreview) { - $found = $this->resource->environment_variables_preview()->where('key', $key)->first(); + $this->resource->environment_variables_preview()->whereNotIn('key', array_keys($variables))->delete(); } else { - $found = $this->resource->environment_variables()->where('key', $key)->first(); + $this->resource->environment_variables()->whereNotIn('key', array_keys($variables))->delete(); } - if ($found) { - if ($found->is_shown_once || $found->is_multiline) { - continue; + + foreach ($variables as $key => $value) { + $found = $isPreview + ? $this->resource->environment_variables_preview()->where('key', $key)->first() + : $this->resource->environment_variables()->where('key', $key)->first(); + + if ($found) { + if (!$found->is_shown_once && !$found->is_multiline) { + $found->value = $value; + $found->save(); + } + } else { + $environment = new EnvironmentVariable; + $environment->key = $key; + $environment->value = $value; + $environment->is_build_time = false; + $environment->is_multiline = false; + $environment->is_preview = $isPreview; + + $this->setEnvironmentResourceId($environment); + $environment->save(); } - $found->value = $variable; - // if (str($found->value)->startsWith('{{') && str($found->value)->endsWith('}}')) { - // $type = str($found->value)->after('{{')->before('.')->value; - // if (! collect(SHARED_VARIABLE_TYPES)->contains($type)) { - // $this->dispatch('error', 'Invalid shared variable type.', 'Valid types are: team, project, environment.'); - - // return; - // } - // } - $found->save(); - - continue; - } else { - $environment = new EnvironmentVariable; - $environment->key = $key; - $environment->value = $variable; - // if (str($environment->value)->startsWith('{{') && str($environment->value)->endsWith('}}')) { - // $type = str($environment->value)->after('{{')->before('.')->value; - // if (! collect(SHARED_VARIABLE_TYPES)->contains($type)) { - // $this->dispatch('error', 'Invalid shared variable type.', 'Valid types are: team, project, environment.'); - - // return; - // } - // } - $environment->is_build_time = false; - $environment->is_multiline = false; - $environment->is_preview = $isPreview ? true : false; - switch ($this->resource->type()) { - case 'application': - $environment->application_id = $this->resource->id; - break; - case 'standalone-postgresql': - $environment->standalone_postgresql_id = $this->resource->id; - break; - case 'standalone-redis': - $environment->standalone_redis_id = $this->resource->id; - break; - case 'standalone-mongodb': - $environment->standalone_mongodb_id = $this->resource->id; - break; - case 'standalone-mysql': - $environment->standalone_mysql_id = $this->resource->id; - break; - case 'standalone-mariadb': - $environment->standalone_mariadb_id = $this->resource->id; - break; - case 'standalone-keydb': - $environment->standalone_keydb_id = $this->resource->id; - break; - case 'standalone-dragonfly': - $environment->standalone_dragonfly_id = $this->resource->id; - break; - case 'standalone-clickhouse': - $environment->standalone_clickhouse_id = $this->resource->id; - break; - case 'service': - $environment->service_id = $this->resource->id; - break; - } - $environment->save(); } + + $this->refreshEnvs(); + $this->dispatch('success', 'Environment variables saved successfully.'); + } catch (\Throwable $e) { + return handleError($e, $this); } - if ($isPreview) { - $this->dispatch('success', 'Preview environment variables updated.'); - } else { - $this->dispatch('success', 'Environment variables updated.'); + } + + private function setEnvironmentResourceId($environment) + { + switch ($this->resource->type()) { + case 'application': + $environment->application_id = $this->resource->id; + break; + case 'standalone-postgresql': + $environment->standalone_postgresql_id = $this->resource->id; + break; + case 'standalone-redis': + $environment->standalone_redis_id = $this->resource->id; + break; + case 'standalone-mongodb': + $environment->standalone_mongodb_id = $this->resource->id; + break; + case 'standalone-mysql': + $environment->standalone_mysql_id = $this->resource->id; + break; + case 'standalone-mariadb': + $environment->standalone_mariadb_id = $this->resource->id; + break; + case 'standalone-keydb': + $environment->standalone_keydb_id = $this->resource->id; + break; + case 'standalone-dragonfly': + $environment->standalone_dragonfly_id = $this->resource->id; + break; + case 'standalone-clickhouse': + $environment->standalone_clickhouse_id = $this->resource->id; + break; + case 'service': + $environment->service_id = $this->resource->id; + break; } - $this->refreshEnvs(); } public function refreshEnvs() @@ -199,61 +188,4 @@ class All extends Component $this->resource->refresh(); $this->getDevView(); } - - public function submit($data) - { - try { - $found = $this->resource->environment_variables()->where('key', $data['key'])->first(); - if ($found) { - $this->dispatch('error', 'Environment variable already exists.'); - - return; - } - $environment = new EnvironmentVariable; - $environment->key = $data['key']; - $environment->value = $data['value']; - $environment->is_build_time = $data['is_build_time']; - $environment->is_multiline = $data['is_multiline']; - $environment->is_literal = $data['is_literal']; - $environment->is_preview = $data['is_preview']; - - switch ($this->resource->type()) { - case 'application': - $environment->application_id = $this->resource->id; - break; - case 'standalone-postgresql': - $environment->standalone_postgresql_id = $this->resource->id; - break; - case 'standalone-redis': - $environment->standalone_redis_id = $this->resource->id; - break; - case 'standalone-mongodb': - $environment->standalone_mongodb_id = $this->resource->id; - break; - case 'standalone-mysql': - $environment->standalone_mysql_id = $this->resource->id; - break; - case 'standalone-mariadb': - $environment->standalone_mariadb_id = $this->resource->id; - break; - case 'standalone-keydb': - $environment->standalone_keydb_id = $this->resource->id; - break; - case 'standalone-dragonfly': - $environment->standalone_dragonfly_id = $this->resource->id; - break; - case 'standalone-clickhouse': - $environment->standalone_clickhouse_id = $this->resource->id; - break; - case 'service': - $environment->service_id = $this->resource->id; - break; - } - $environment->save(); - $this->refreshEnvs(); - $this->dispatch('success', 'Environment variable added.'); - } catch (\Throwable $e) { - return handleError($e, $this); - } - } -} +} \ No newline at end of file From 93444ea872ca325763a3949b82c79286d1c9d16b Mon Sep 17 00:00:00 2001 From: ayntk-ai <122374094+ayntk-ai@users.noreply.github.com> Date: Mon, 12 Aug 2024 14:06:00 +0200 Subject: [PATCH 04/10] fix manual safe button and few simplifications --- .../Shared/EnvironmentVariable/All.php | 217 ++++++++++-------- .../shared/environment-variable/all.blade.php | 14 +- 2 files changed, 126 insertions(+), 105 deletions(-) diff --git a/app/Livewire/Project/Shared/EnvironmentVariable/All.php b/app/Livewire/Project/Shared/EnvironmentVariable/All.php index dbd5c2b9b..2c90427c0 100644 --- a/app/Livewire/Project/Shared/EnvironmentVariable/All.php +++ b/app/Livewire/Project/Shared/EnvironmentVariable/All.php @@ -9,17 +9,11 @@ use Visus\Cuid2\Cuid2; class All extends Component { public $resource; - public string $resourceClass; - public bool $showPreview = false; - public ?string $modalId = null; - public ?string $variables = null; - public ?string $variablesPreview = null; - public string $view = 'normal'; protected $listeners = [ @@ -35,25 +29,20 @@ class All extends Component { $this->resourceClass = get_class($this->resource); $resourceWithPreviews = ['App\Models\Application']; - $simpleDockerfile = ! is_null(data_get($this->resource, 'dockerfile')); - if (str($this->resourceClass)->contains($resourceWithPreviews) && ! $simpleDockerfile) { + $simpleDockerfile = !is_null(data_get($this->resource, 'dockerfile')); + if (str($this->resourceClass)->contains($resourceWithPreviews) && !$simpleDockerfile) { $this->showPreview = true; } $this->modalId = new Cuid2; $this->sortMe(); - $this->getDevView(); } public function sortMe() { if ($this->resourceClass === 'App\Models\Application' && data_get($this->resource, 'build_pack') !== 'dockercompose') { - if ($this->resource->settings->is_env_sorting_enabled) { - $this->resource->environment_variables = $this->resource->environment_variables->sortBy('key'); - $this->resource->environment_variables_preview = $this->resource->environment_variables_preview->sortBy('key'); - } else { - $this->resource->environment_variables = $this->resource->environment_variables->sortBy('id'); - $this->resource->environment_variables_preview = $this->resource->environment_variables_preview->sortBy('id'); - } + $sortBy = $this->resource->settings->is_env_sorting_enabled ? 'key' : 'id'; + $this->resource->environment_variables = $this->resource->environment_variables->sortBy($sortBy); + $this->resource->environment_variables_preview = $this->resource->environment_variables_preview->sortBy($sortBy); } $this->getDevView(); } @@ -69,117 +58,147 @@ class All extends Component public function getDevView() { - $this->variables = $this->resource->environment_variables->map(function ($item) { + $this->variables = $this->formatEnvironmentVariables($this->resource->environment_variables); + if ($this->showPreview) { + $this->variablesPreview = $this->formatEnvironmentVariables($this->resource->environment_variables_preview); + } + } + + private function formatEnvironmentVariables($variables) + { + return $variables->map(function ($item) { if ($item->is_shown_once) { return "$item->key=(locked secret)"; } if ($item->is_multiline) { return "$item->key=(multiline, edit in normal view)"; } - return "$item->key=$item->value"; - })->join(' -'); - if ($this->showPreview) { - $this->variablesPreview = $this->resource->environment_variables_preview->map(function ($item) { - if ($item->is_shown_once) { - return "$item->key=(locked secret)"; - } - if ($item->is_multiline) { - return "$item->key=(multiline, edit in normal view)"; - } - - return "$item->key=$item->value"; - })->join(' -'); - } + })->join("\n"); } public function switch() { - if ($this->view === 'normal') { - $this->view = 'dev'; - } else { - $this->view = 'normal'; - } + $this->view = $this->view === 'normal' ? 'dev' : 'normal'; $this->sortMe(); } - public function submit() + public function submit($data = null) { try { - $isPreview = $this->showPreview; - $variables = $isPreview ? parseEnvFormatToArray($this->variablesPreview) : parseEnvFormatToArray($this->variables); + if ($data === null) { + // Handle saving in developer view + $variables = parseEnvFormatToArray($this->variables); + $this->deleteRemovedVariables(false, $variables); + $this->updateOrCreateVariables(false, $variables); - if ($isPreview) { - $this->resource->environment_variables_preview()->whereNotIn('key', array_keys($variables))->delete(); - } else { - $this->resource->environment_variables()->whereNotIn('key', array_keys($variables))->delete(); - } - - foreach ($variables as $key => $value) { - $found = $isPreview - ? $this->resource->environment_variables_preview()->where('key', $key)->first() - : $this->resource->environment_variables()->where('key', $key)->first(); - - if ($found) { - if (!$found->is_shown_once && !$found->is_multiline) { - $found->value = $value; - $found->save(); - } - } else { - $environment = new EnvironmentVariable; - $environment->key = $key; - $environment->value = $value; - $environment->is_build_time = false; - $environment->is_multiline = false; - $environment->is_preview = $isPreview; - - $this->setEnvironmentResourceId($environment); - $environment->save(); + if ($this->showPreview) { + $previewVariables = parseEnvFormatToArray($this->variablesPreview); + $this->deleteRemovedVariables(true, $previewVariables); + $this->updateOrCreateVariables(true, $previewVariables); } + + $this->dispatch('success', 'Environment variables updated.'); + } else { + // Handle the case when adding a single variable + $found = $this->resource->environment_variables()->where('key', $data['key'])->first(); + if ($found) { + $this->dispatch('error', 'Environment variable already exists.'); + return; + } + + $environment = new EnvironmentVariable; + $environment->key = $data['key']; + $environment->value = $data['value']; + $environment->is_build_time = $data['is_build_time'] ?? false; + $environment->is_multiline = $data['is_multiline'] ?? false; + $environment->is_literal = $data['is_literal'] ?? false; + $environment->is_preview = $data['is_preview'] ?? false; + + $resourceType = $this->resource->type(); + $resourceIdField = $this->getResourceIdField($resourceType); + + if ($resourceIdField) { + $environment->$resourceIdField = $this->resource->id; + } + + $environment->save(); } $this->refreshEnvs(); - $this->dispatch('success', 'Environment variables saved successfully.'); + $this->sortMe(); } catch (\Throwable $e) { return handleError($e, $this); } } + private function getResourceIdField($resourceType) + { + $resourceTypes = [ + 'application' => 'application_id', + 'standalone-postgresql' => 'standalone_postgresql_id', + 'standalone-redis' => 'standalone_redis_id', + 'standalone-mongodb' => 'standalone_mongodb_id', + 'standalone-mysql' => 'standalone_mysql_id', + 'standalone-mariadb' => 'standalone_mariadb_id', + 'standalone-keydb' => 'standalone_keydb_id', + 'standalone-dragonfly' => 'standalone_dragonfly_id', + 'standalone-clickhouse' => 'standalone_clickhouse_id', + 'service' => 'service_id', + ]; + + return $resourceTypes[$resourceType] ?? null; + } + + private function deleteRemovedVariables($isPreview, $variables) + { + $method = $isPreview ? 'environment_variables_preview' : 'environment_variables'; + $this->resource->$method()->whereNotIn('key', array_keys($variables))->delete(); + } + + private function updateOrCreateVariables($isPreview, $variables) + { + foreach ($variables as $key => $value) { + $method = $isPreview ? 'environment_variables_preview' : 'environment_variables'; + $found = $this->resource->$method()->where('key', $key)->first(); + + if ($found) { + if (!$found->is_shown_once && !$found->is_multiline) { + $found->value = $value; + $found->save(); + } + } else { + $environment = new EnvironmentVariable; + $environment->key = $key; + $environment->value = $value; + $environment->is_build_time = false; + $environment->is_multiline = false; + $environment->is_preview = $isPreview; + + $this->setEnvironmentResourceId($environment); + $environment->save(); + } + } + } + private function setEnvironmentResourceId($environment) { - switch ($this->resource->type()) { - case 'application': - $environment->application_id = $this->resource->id; - break; - case 'standalone-postgresql': - $environment->standalone_postgresql_id = $this->resource->id; - break; - case 'standalone-redis': - $environment->standalone_redis_id = $this->resource->id; - break; - case 'standalone-mongodb': - $environment->standalone_mongodb_id = $this->resource->id; - break; - case 'standalone-mysql': - $environment->standalone_mysql_id = $this->resource->id; - break; - case 'standalone-mariadb': - $environment->standalone_mariadb_id = $this->resource->id; - break; - case 'standalone-keydb': - $environment->standalone_keydb_id = $this->resource->id; - break; - case 'standalone-dragonfly': - $environment->standalone_dragonfly_id = $this->resource->id; - break; - case 'standalone-clickhouse': - $environment->standalone_clickhouse_id = $this->resource->id; - break; - case 'service': - $environment->service_id = $this->resource->id; - break; + $resourceTypes = [ + 'application' => 'application_id', + 'standalone-postgresql' => 'standalone_postgresql_id', + 'standalone-redis' => 'standalone_redis_id', + 'standalone-mongodb' => 'standalone_mongodb_id', + 'standalone-mysql' => 'standalone_mysql_id', + 'standalone-mariadb' => 'standalone_mariadb_id', + 'standalone-keydb' => 'standalone_keydb_id', + 'standalone-dragonfly' => 'standalone_dragonfly_id', + 'standalone-clickhouse' => 'standalone_clickhouse_id', + 'service' => 'service_id', + ]; + + $resourceType = $this->resource->type(); + if (isset($resourceTypes[$resourceType])) { + $environment->{$resourceTypes[$resourceType]} = $this->resource->id; } } diff --git a/resources/views/livewire/project/shared/environment-variable/all.blade.php b/resources/views/livewire/project/shared/environment-variable/all.blade.php index 5226199c6..1fd4160ae 100644 --- a/resources/views/livewire/project/shared/environment-variable/all.blade.php +++ b/resources/views/livewire/project/shared/environment-variable/all.blade.php @@ -2,9 +2,11 @@

Environment Variables

- - - +
+ + + +
{{ $view === 'normal' ? 'Developer view' : 'Normal view' }}
@@ -49,14 +51,14 @@ @endif @else - + @if ($showPreview) + id="variablesPreview" wire:model="variablesPreview"> @endif - Save Environment Variables + Save All Environment Variables @endif
\ No newline at end of file From 1d7c833b7c9be47d3bfd48824760fc27c7ae861d Mon Sep 17 00:00:00 2001 From: ayntk-ai <122374094+ayntk-ai@users.noreply.github.com> Date: Mon, 12 Aug 2024 14:13:03 +0200 Subject: [PATCH 05/10] fix sort alphabetically --- app/Livewire/Project/Shared/EnvironmentVariable/All.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/Livewire/Project/Shared/EnvironmentVariable/All.php b/app/Livewire/Project/Shared/EnvironmentVariable/All.php index 2c90427c0..10497bd0c 100644 --- a/app/Livewire/Project/Shared/EnvironmentVariable/All.php +++ b/app/Livewire/Project/Shared/EnvironmentVariable/All.php @@ -41,8 +41,8 @@ class All extends Component { if ($this->resourceClass === 'App\Models\Application' && data_get($this->resource, 'build_pack') !== 'dockercompose') { $sortBy = $this->resource->settings->is_env_sorting_enabled ? 'key' : 'id'; - $this->resource->environment_variables = $this->resource->environment_variables->sortBy($sortBy); - $this->resource->environment_variables_preview = $this->resource->environment_variables_preview->sortBy($sortBy); + $this->resource->environment_variables = $this->resource->environment_variables->sortBy($sortBy, SORT_NATURAL|SORT_FLAG_CASE); + $this->resource->environment_variables_preview = $this->resource->environment_variables_preview->sortBy($sortBy, SORT_NATURAL|SORT_FLAG_CASE); } $this->getDevView(); } From 122491808caeb6d5bb3c9660d15c84fb233ac6b1 Mon Sep 17 00:00:00 2001 From: ayntk-ai <122374094+ayntk-ai@users.noreply.github.com> Date: Mon, 12 Aug 2024 14:44:18 +0200 Subject: [PATCH 06/10] fix env deletion and sorting of locked envs --- .../Shared/EnvironmentVariable/All.php | 26 ++++++++++--------- .../Shared/EnvironmentVariable/Show.php | 5 ++-- 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/app/Livewire/Project/Shared/EnvironmentVariable/All.php b/app/Livewire/Project/Shared/EnvironmentVariable/All.php index 10497bd0c..61f6aa7c6 100644 --- a/app/Livewire/Project/Shared/EnvironmentVariable/All.php +++ b/app/Livewire/Project/Shared/EnvironmentVariable/All.php @@ -17,8 +17,8 @@ class All extends Component public string $view = 'normal'; protected $listeners = [ - 'refreshEnvs', 'saveKey' => 'submit', + //'environmentVariableDeleted' => 'refreshEnvs', ]; protected $rules = [ @@ -39,20 +39,24 @@ class All extends Component public function sortMe() { - if ($this->resourceClass === 'App\Models\Application' && data_get($this->resource, 'build_pack') !== 'dockercompose') { - $sortBy = $this->resource->settings->is_env_sorting_enabled ? 'key' : 'id'; - $this->resource->environment_variables = $this->resource->environment_variables->sortBy($sortBy, SORT_NATURAL|SORT_FLAG_CASE); - $this->resource->environment_variables_preview = $this->resource->environment_variables_preview->sortBy($sortBy, SORT_NATURAL|SORT_FLAG_CASE); - } + $sortBy = 'key'; // Always sort by key + $this->resource->load(['environment_variables', 'environment_variables_preview']); + $this->resource->environment_variables = $this->resource->environment_variables->sortBy(function ($item) use ($sortBy) { + return strtolower($item->key); + }, SORT_NATURAL | SORT_FLAG_CASE)->values(); + $this->resource->environment_variables_preview = $this->resource->environment_variables_preview->sortBy(function ($item) use ($sortBy) { + return strtolower($item->key); + }, SORT_NATURAL | SORT_FLAG_CASE)->values(); $this->getDevView(); } + public function instantSave() { if ($this->resourceClass === 'App\Models\Application' && data_get($this->resource, 'build_pack') !== 'dockercompose') { $this->resource->settings->save(); - $this->dispatch('success', 'Environment variable settings updated.'); $this->sortMe(); + $this->dispatch('success', 'Environment variable settings updated.'); } } @@ -87,7 +91,6 @@ class All extends Component { try { if ($data === null) { - // Handle saving in developer view $variables = parseEnvFormatToArray($this->variables); $this->deleteRemovedVariables(false, $variables); $this->updateOrCreateVariables(false, $variables); @@ -98,9 +101,9 @@ class All extends Component $this->updateOrCreateVariables(true, $previewVariables); } + $this->sortMe(); $this->dispatch('success', 'Environment variables updated.'); } else { - // Handle the case when adding a single variable $found = $this->resource->environment_variables()->where('key', $data['key'])->first(); if ($found) { $this->dispatch('error', 'Environment variable already exists.'); @@ -117,7 +120,7 @@ class All extends Component $resourceType = $this->resource->type(); $resourceIdField = $this->getResourceIdField($resourceType); - + if ($resourceIdField) { $environment->$resourceIdField = $this->resource->id; } @@ -125,7 +128,6 @@ class All extends Component $environment->save(); } - $this->refreshEnvs(); $this->sortMe(); } catch (\Throwable $e) { return handleError($e, $this); @@ -207,4 +209,4 @@ class All extends Component $this->resource->refresh(); $this->getDevView(); } -} \ No newline at end of file +} diff --git a/app/Livewire/Project/Shared/EnvironmentVariable/Show.php b/app/Livewire/Project/Shared/EnvironmentVariable/Show.php index e63871602..9b95dc7d9 100644 --- a/app/Livewire/Project/Shared/EnvironmentVariable/Show.php +++ b/app/Livewire/Project/Shared/EnvironmentVariable/Show.php @@ -129,9 +129,10 @@ class Show extends Component { try { $this->env->delete(); - $this->dispatch('refreshEnvs'); + $this->dispatch('environmentVariableDeleted'); + $this->dispatch('success', 'Environment variable deleted successfully.'); } catch (\Exception $e) { return handleError($e); } } -} +} \ No newline at end of file From e28289ce1e9c6ce27fdd20ce03ca0e459e300782 Mon Sep 17 00:00:00 2001 From: ayntk-ai <122374094+ayntk-ai@users.noreply.github.com> Date: Mon, 12 Aug 2024 14:46:30 +0200 Subject: [PATCH 07/10] cleanup all.php --- .../Shared/EnvironmentVariable/All.php | 107 ++++++++++-------- 1 file changed, 61 insertions(+), 46 deletions(-) diff --git a/app/Livewire/Project/Shared/EnvironmentVariable/All.php b/app/Livewire/Project/Shared/EnvironmentVariable/All.php index 61f6aa7c6..f5d90a33c 100644 --- a/app/Livewire/Project/Shared/EnvironmentVariable/All.php +++ b/app/Livewire/Project/Shared/EnvironmentVariable/All.php @@ -18,7 +18,7 @@ class All extends Component protected $listeners = [ 'saveKey' => 'submit', - //'environmentVariableDeleted' => 'refreshEnvs', + 'environmentVariableDeleted' => 'refreshEnvs', ]; protected $rules = [ @@ -41,23 +41,23 @@ class All extends Component { $sortBy = 'key'; // Always sort by key $this->resource->load(['environment_variables', 'environment_variables_preview']); - $this->resource->environment_variables = $this->resource->environment_variables->sortBy(function ($item) use ($sortBy) { - return strtolower($item->key); - }, SORT_NATURAL | SORT_FLAG_CASE)->values(); - $this->resource->environment_variables_preview = $this->resource->environment_variables_preview->sortBy(function ($item) use ($sortBy) { - return strtolower($item->key); - }, SORT_NATURAL | SORT_FLAG_CASE)->values(); + $this->resource->environment_variables = $this->sortEnvironmentVariables($this->resource->environment_variables, $sortBy); + $this->resource->environment_variables_preview = $this->sortEnvironmentVariables($this->resource->environment_variables_preview, $sortBy); $this->getDevView(); } + private function sortEnvironmentVariables($variables, $sortBy) + { + return $variables->sortBy(function ($item) use ($sortBy) { + return strtolower($item->key); + }, SORT_NATURAL | SORT_FLAG_CASE)->values(); + } public function instantSave() { - if ($this->resourceClass === 'App\Models\Application' && data_get($this->resource, 'build_pack') !== 'dockercompose') { - $this->resource->settings->save(); - $this->sortMe(); - $this->dispatch('success', 'Environment variable settings updated.'); - } + $this->resource->settings->save(); + $this->sortMe(); + $this->dispatch('success', 'Environment variable settings updated.'); } public function getDevView() @@ -91,41 +91,9 @@ class All extends Component { try { if ($data === null) { - $variables = parseEnvFormatToArray($this->variables); - $this->deleteRemovedVariables(false, $variables); - $this->updateOrCreateVariables(false, $variables); - - if ($this->showPreview) { - $previewVariables = parseEnvFormatToArray($this->variablesPreview); - $this->deleteRemovedVariables(true, $previewVariables); - $this->updateOrCreateVariables(true, $previewVariables); - } - - $this->sortMe(); - $this->dispatch('success', 'Environment variables updated.'); + $this->handleBulkSubmit(); } else { - $found = $this->resource->environment_variables()->where('key', $data['key'])->first(); - if ($found) { - $this->dispatch('error', 'Environment variable already exists.'); - return; - } - - $environment = new EnvironmentVariable; - $environment->key = $data['key']; - $environment->value = $data['value']; - $environment->is_build_time = $data['is_build_time'] ?? false; - $environment->is_multiline = $data['is_multiline'] ?? false; - $environment->is_literal = $data['is_literal'] ?? false; - $environment->is_preview = $data['is_preview'] ?? false; - - $resourceType = $this->resource->type(); - $resourceIdField = $this->getResourceIdField($resourceType); - - if ($resourceIdField) { - $environment->$resourceIdField = $this->resource->id; - } - - $environment->save(); + $this->handleSingleSubmit($data); } $this->sortMe(); @@ -134,6 +102,53 @@ class All extends Component } } + private function handleBulkSubmit() + { + $variables = parseEnvFormatToArray($this->variables); + $this->deleteRemovedVariables(false, $variables); + $this->updateOrCreateVariables(false, $variables); + + if ($this->showPreview) { + $previewVariables = parseEnvFormatToArray($this->variablesPreview); + $this->deleteRemovedVariables(true, $previewVariables); + $this->updateOrCreateVariables(true, $previewVariables); + } + + $this->dispatch('success', 'Environment variables updated.'); + } + + private function handleSingleSubmit($data) + { + $found = $this->resource->environment_variables()->where('key', $data['key'])->first(); + if ($found) { + $this->dispatch('error', 'Environment variable already exists.'); + return; + } + + $environment = $this->createEnvironmentVariable($data); + $environment->save(); + } + + private function createEnvironmentVariable($data) + { + $environment = new EnvironmentVariable; + $environment->key = $data['key']; + $environment->value = $data['value']; + $environment->is_build_time = $data['is_build_time'] ?? false; + $environment->is_multiline = $data['is_multiline'] ?? false; + $environment->is_literal = $data['is_literal'] ?? false; + $environment->is_preview = $data['is_preview'] ?? false; + + $resourceType = $this->resource->type(); + $resourceIdField = $this->getResourceIdField($resourceType); + + if ($resourceIdField) { + $environment->$resourceIdField = $this->resource->id; + } + + return $environment; + } + private function getResourceIdField($resourceType) { $resourceTypes = [ From fbde257166e9ee438da45e85ba6000773d910946 Mon Sep 17 00:00:00 2001 From: ayntk-ai <122374094+ayntk-ai@users.noreply.github.com> Date: Mon, 12 Aug 2024 23:00:08 +0200 Subject: [PATCH 08/10] fix disable/enable environment variabel sorting --- .../Shared/EnvironmentVariable/All.php | 51 +++++++++++-------- 1 file changed, 29 insertions(+), 22 deletions(-) diff --git a/app/Livewire/Project/Shared/EnvironmentVariable/All.php b/app/Livewire/Project/Shared/EnvironmentVariable/All.php index f5d90a33c..880d3e705 100644 --- a/app/Livewire/Project/Shared/EnvironmentVariable/All.php +++ b/app/Livewire/Project/Shared/EnvironmentVariable/All.php @@ -34,32 +34,38 @@ class All extends Component $this->showPreview = true; } $this->modalId = new Cuid2; - $this->sortMe(); - } - - public function sortMe() - { - $sortBy = 'key'; // Always sort by key - $this->resource->load(['environment_variables', 'environment_variables_preview']); - $this->resource->environment_variables = $this->sortEnvironmentVariables($this->resource->environment_variables, $sortBy); - $this->resource->environment_variables_preview = $this->sortEnvironmentVariables($this->resource->environment_variables_preview, $sortBy); - $this->getDevView(); - } - - private function sortEnvironmentVariables($variables, $sortBy) - { - return $variables->sortBy(function ($item) use ($sortBy) { - return strtolower($item->key); - }, SORT_NATURAL | SORT_FLAG_CASE)->values(); + $this->sortEnvironmentVariables(); } public function instantSave() { $this->resource->settings->save(); - $this->sortMe(); + $this->sortEnvironmentVariables(); $this->dispatch('success', 'Environment variable settings updated.'); } + public function sortEnvironmentVariables() + { + $this->resource->load(['environment_variables', 'environment_variables_preview']); + + $sortBy = $this->resource->settings->is_env_sorting_enabled ? 'key' : 'id'; + + $sortFunction = function ($variables) use ($sortBy) { + if ($sortBy === 'key') { + return $variables->sortBy(function ($item) { + return strtolower($item->key); + }, SORT_NATURAL | SORT_FLAG_CASE)->values(); + } else { + return $variables->sortBy('id')->values(); + } + }; + + $this->resource->environment_variables = $sortFunction($this->resource->environment_variables); + $this->resource->environment_variables_preview = $sortFunction($this->resource->environment_variables_preview); + + $this->getDevView(); + } + public function getDevView() { $this->variables = $this->formatEnvironmentVariables($this->resource->environment_variables); @@ -72,10 +78,10 @@ class All extends Component { return $variables->map(function ($item) { if ($item->is_shown_once) { - return "$item->key=(locked secret)"; + return "$item->key=(Locked Secret, delete and add again to change)"; } if ($item->is_multiline) { - return "$item->key=(multiline, edit in normal view)"; + return "$item->key=(Multiline environment variable, edit in normal view)"; } return "$item->key=$item->value"; })->join("\n"); @@ -84,7 +90,7 @@ class All extends Component public function switch() { $this->view = $this->view === 'normal' ? 'dev' : 'normal'; - $this->sortMe(); + $this->sortEnvironmentVariables(); } public function submit($data = null) @@ -96,7 +102,7 @@ class All extends Component $this->handleSingleSubmit($data); } - $this->sortMe(); + $this->sortEnvironmentVariables(); } catch (\Throwable $e) { return handleError($e, $this); } @@ -222,6 +228,7 @@ class All extends Component public function refreshEnvs() { $this->resource->refresh(); + $this->sortEnvironmentVariables(); $this->getDevView(); } } From 77044d51c7e3c4934a35e5fffe8aed83edab9705 Mon Sep 17 00:00:00 2001 From: ayntk-ai <122374094+ayntk-ai@users.noreply.github.com> Date: Mon, 12 Aug 2024 23:12:54 +0200 Subject: [PATCH 09/10] added missing heading and UI fix --- .../project/shared/environment-variable/all.blade.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/resources/views/livewire/project/shared/environment-variable/all.blade.php b/resources/views/livewire/project/shared/environment-variable/all.blade.php index 1fd4160ae..813ee5c52 100644 --- a/resources/views/livewire/project/shared/environment-variable/all.blade.php +++ b/resources/views/livewire/project/shared/environment-variable/all.blade.php @@ -14,7 +14,7 @@ @if ($this->resourceClass === 'App\Models\Application' && data_get($this->resource, 'build_pack') !== 'dockercompose')
@endif @@ -33,6 +33,10 @@ @endif @if ($view === 'normal') +
+

Production Environment Variables

+
Environment (secrets) variables for Production.
+
@forelse ($resource->environment_variables as $env) @@ -41,7 +45,7 @@ @endforelse @if ($resource->type() === 'application' && $resource->environment_variables_preview->count() > 0 && $showPreview)
-

Preview Deployments

+

Preview Deployments Environment Variables

Environment (secrets) variables for Preview Deployments.
@foreach ($resource->environment_variables_preview as $env) @@ -54,7 +58,7 @@ @if ($showPreview) - @endif From 4d124477155198d983816ee41e1bff7c8a9493c7 Mon Sep 17 00:00:00 2001 From: ayntk-ai <122374094+ayntk-ai@users.noreply.github.com> Date: Wed, 14 Aug 2024 20:35:08 +0200 Subject: [PATCH 10/10] order column added to track order of env in the UI --- .../Shared/EnvironmentVariable/All.php | 36 +++++++++++++++++-- ...d_order_to_environment_variables_table.php | 28 +++++++++++++++ 2 files changed, 61 insertions(+), 3 deletions(-) create mode 100644 database/migrations/2024_08_14_183120_add_order_to_environment_variables_table.php diff --git a/app/Livewire/Project/Shared/EnvironmentVariable/All.php b/app/Livewire/Project/Shared/EnvironmentVariable/All.php index 880d3e705..95e2bfbbd 100644 --- a/app/Livewire/Project/Shared/EnvironmentVariable/All.php +++ b/app/Livewire/Project/Shared/EnvironmentVariable/All.php @@ -48,7 +48,7 @@ class All extends Component { $this->resource->load(['environment_variables', 'environment_variables_preview']); - $sortBy = $this->resource->settings->is_env_sorting_enabled ? 'key' : 'id'; + $sortBy = $this->resource->settings->is_env_sorting_enabled ? 'key' : 'order'; $sortFunction = function ($variables) use ($sortBy) { if ($sortBy === 'key') { @@ -56,7 +56,7 @@ class All extends Component return strtolower($item->key); }, SORT_NATURAL | SORT_FLAG_CASE)->values(); } else { - return $variables->sortBy('id')->values(); + return $variables->sortBy('order')->values(); } }; @@ -102,12 +102,40 @@ class All extends Component $this->handleSingleSubmit($data); } + $this->updateOrder(); $this->sortEnvironmentVariables(); } catch (\Throwable $e) { return handleError($e, $this); } } + private function updateOrder() + { + $variables = parseEnvFormatToArray($this->variables); + $order = 1; + foreach ($variables as $key => $value) { + $env = $this->resource->environment_variables()->where('key', $key)->first(); + if ($env) { + $env->order = $order; + $env->save(); + } + $order++; + } + + if ($this->showPreview) { + $previewVariables = parseEnvFormatToArray($this->variablesPreview); + $order = 1; + foreach ($previewVariables as $key => $value) { + $env = $this->resource->environment_variables_preview()->where('key', $key)->first(); + if ($env) { + $env->order = $order; + $env->save(); + } + $order++; + } + } + } + private function handleBulkSubmit() { $variables = parseEnvFormatToArray($this->variables); @@ -131,7 +159,9 @@ class All extends Component return; } + $maxOrder = $this->resource->environment_variables()->max('order') ?? 0; $environment = $this->createEnvironmentVariable($data); + $environment->order = $maxOrder + 1; $environment->save(); } @@ -231,4 +261,4 @@ class All extends Component $this->sortEnvironmentVariables(); $this->getDevView(); } -} +} \ No newline at end of file diff --git a/database/migrations/2024_08_14_183120_add_order_to_environment_variables_table.php b/database/migrations/2024_08_14_183120_add_order_to_environment_variables_table.php new file mode 100644 index 000000000..527535827 --- /dev/null +++ b/database/migrations/2024_08_14_183120_add_order_to_environment_variables_table.php @@ -0,0 +1,28 @@ +integer('order')->nullable(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('environment_variables', function (Blueprint $table) { + $table->dropColumn('order'); + }); + } +};