From 431cc796d873718e563706e39fc192c121fed39f Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Fri, 17 May 2024 11:10:57 +0200 Subject: [PATCH] feat: sort envs alphabetically and creation date --- app/Jobs/ApplicationDeploymentJob.php | 70 ++++++++++--------- .../Shared/EnvironmentVariable/All.php | 43 ++++++++++-- ...24_05_17_082012_add_env_sorting_toggle.php | 28 ++++++++ .../shared/environment-variable/all.blade.php | 10 ++- 4 files changed, 109 insertions(+), 42 deletions(-) create mode 100644 database/migrations/2024_05_17_082012_add_env_sorting_toggle.php diff --git a/app/Jobs/ApplicationDeploymentJob.php b/app/Jobs/ApplicationDeploymentJob.php index 9270b648a..d9ccfd49a 100644 --- a/app/Jobs/ApplicationDeploymentJob.php +++ b/app/Jobs/ApplicationDeploymentJob.php @@ -713,10 +713,36 @@ class ApplicationDeploymentJob implements ShouldQueue, ShouldBeEncrypted private function save_environment_variables() { $envs = collect([]); + $sort = $this->application->settings->is_env_sorting_enabled; + if ($sort) { + $sorted_environment_variables = $this->application->environment_variables->sortBy('key'); + $sorted_environment_variables_preview = $this->application->environment_variables_preview->sortBy('key'); + } else { + $sorted_environment_variables = $this->application->environment_variables->sortBy('id'); + $sorted_environment_variables_preview = $this->application->environment_variables_preview->sortBy('id'); + } $ports = $this->application->main_port(); if ($this->pull_request_id !== 0) { $this->env_filename = ".env-pr-$this->pull_request_id"; - foreach ($this->application->environment_variables_preview as $env) { + // Add SOURCE_COMMIT if not exists + if ($this->application->environment_variables_preview->where('key', 'SOURCE_COMMIT')->isEmpty()) { + if (!is_null($this->commit)) { + $envs->push("SOURCE_COMMIT={$this->commit}"); + } else { + $envs->push("SOURCE_COMMIT=unknown"); + } + } + if ($this->application->environment_variables_preview->where('key', 'COOLIFY_FQDN')->isEmpty()) { + $envs->push("COOLIFY_FQDN={$this->preview->fqdn}"); + } + if ($this->application->environment_variables_preview->where('key', 'COOLIFY_URL')->isEmpty()) { + $url = str($this->preview->fqdn)->replace('http://', '')->replace('https://', ''); + $envs->push("COOLIFY_URL={$url}"); + } + if ($this->application->environment_variables_preview->where('key', 'COOLIFY_BRANCH')->isEmpty()) { + $envs->push("COOLIFY_BRANCH={$this->application->git_branch}"); + } + foreach ($sorted_environment_variables_preview as $env) { $real_value = $env->real_value; if ($env->version === '4.0.0-beta.239') { $real_value = $env->real_value; @@ -737,30 +763,27 @@ class ApplicationDeploymentJob implements ShouldQueue, ShouldBeEncrypted if ($this->application->environment_variables_preview->where('key', 'HOST')->isEmpty()) { $envs->push("HOST=0.0.0.0"); } + } else { + $this->env_filename = ".env"; // Add SOURCE_COMMIT if not exists - if ($this->application->environment_variables_preview->where('key', 'SOURCE_COMMIT')->isEmpty()) { + if ($this->application->environment_variables->where('key', 'SOURCE_COMMIT')->isEmpty()) { if (!is_null($this->commit)) { $envs->push("SOURCE_COMMIT={$this->commit}"); } else { $envs->push("SOURCE_COMMIT=unknown"); } } - if ($this->application->environment_variables_preview->where('key', 'COOLIFY_FQDN')->isEmpty()) { - $envs->push("COOLIFY_FQDN={$this->preview->fqdn}"); + if ($this->application->environment_variables->where('key', 'COOLIFY_FQDN')->isEmpty()) { + $envs->push("COOLIFY_FQDN={$this->application->fqdn}"); } - if ($this->application->environment_variables_preview->where('key', 'COOLIFY_URL')->isEmpty()) { - $url = str($this->preview->fqdn)->replace('http://', '')->replace('https://', ''); + if ($this->application->environment_variables->where('key', 'COOLIFY_URL')->isEmpty()) { + $url = str($this->application->fqdn)->replace('http://', '')->replace('https://', ''); $envs->push("COOLIFY_URL={$url}"); } if ($this->application->environment_variables_preview->where('key', 'COOLIFY_BRANCH')->isEmpty()) { $envs->push("COOLIFY_BRANCH={$this->application->git_branch}"); } - $envs = $envs->sort(function ($a, $b) { - return strpos($a, '$') === false ? -1 : 1; - }); - } else { - $this->env_filename = ".env"; - foreach ($this->application->environment_variables as $env) { + foreach ($sorted_environment_variables as $env) { $real_value = $env->real_value; if ($env->version === '4.0.0-beta.239') { $real_value = $env->real_value; @@ -781,27 +804,6 @@ class ApplicationDeploymentJob implements ShouldQueue, ShouldBeEncrypted if ($this->application->environment_variables->where('key', 'HOST')->isEmpty()) { $envs->push("HOST=0.0.0.0"); } - // Add SOURCE_COMMIT if not exists - if ($this->application->environment_variables->where('key', 'SOURCE_COMMIT')->isEmpty()) { - if (!is_null($this->commit)) { - $envs->push("SOURCE_COMMIT={$this->commit}"); - } else { - $envs->push("SOURCE_COMMIT=unknown"); - } - } - if ($this->application->environment_variables->where('key', 'COOLIFY_FQDN')->isEmpty()) { - $envs->push("COOLIFY_FQDN={$this->application->fqdn}"); - } - if ($this->application->environment_variables->where('key', 'COOLIFY_URL')->isEmpty()) { - $url = str($this->application->fqdn)->replace('http://', '')->replace('https://', ''); - $envs->push("COOLIFY_URL={$url}"); - } - if ($this->application->environment_variables_preview->where('key', 'COOLIFY_BRANCH')->isEmpty()) { - $envs->push("COOLIFY_BRANCH={$this->application->git_branch}"); - } - $envs = $envs->sort(function ($a, $b) { - return strpos($a, '$') === false ? -1 : 1; - }); } if ($envs->isEmpty()) { @@ -1272,6 +1274,7 @@ class ApplicationDeploymentJob implements ShouldQueue, ShouldBeEncrypted private function generate_env_variables() { $this->env_args = collect([]); + $this->env_args->put('SOURCE_COMMIT', $this->commit); if ($this->pull_request_id === 0) { foreach ($this->application->build_environment_variables as $env) { if (!is_null($env->real_value)) { @@ -1285,7 +1288,6 @@ class ApplicationDeploymentJob implements ShouldQueue, ShouldBeEncrypted } } } - $this->env_args->put('SOURCE_COMMIT', $this->commit); } private function generate_compose_file() diff --git a/app/Livewire/Project/Shared/EnvironmentVariable/All.php b/app/Livewire/Project/Shared/EnvironmentVariable/All.php index 6a6d94142..9a61f639a 100644 --- a/app/Livewire/Project/Shared/EnvironmentVariable/All.php +++ b/app/Livewire/Project/Shared/EnvironmentVariable/All.php @@ -5,11 +5,11 @@ namespace App\Livewire\Project\Shared\EnvironmentVariable; use App\Models\EnvironmentVariable; use Livewire\Component; use Visus\Cuid2\Cuid2; -use Illuminate\Support\Str; class All extends Component { public $resource; + public string $resourceClass; public bool $showPreview = false; public ?string $modalId = null; public ?string $variables = null; @@ -19,17 +19,42 @@ class All extends Component 'refreshEnvs', 'saveKey' => 'submit', ]; + protected $rules = [ + 'resource.settings.is_env_sorting_enabled' => 'required|boolean', + ]; + public function mount() { - $resourceClass = get_class($this->resource); + $this->resourceClass = get_class($this->resource); $resourceWithPreviews = ['App\Models\Application']; $simpleDockerfile = !is_null(data_get($this->resource, 'dockerfile')); - if (Str::of($resourceClass)->contains($resourceWithPreviews) && !$simpleDockerfile) { + if (str($this->resourceClass)->contains($resourceWithPreviews) && !$simpleDockerfile) { $this->showPreview = true; } $this->modalId = new Cuid2(7); + $this->sortMe(); $this->getDevView(); } + + public function sortMe() + { + if ($this->resourceClass === 'App\Models\Application' && $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'); + } + $this->getDevView(); + } + public function instantSave() + { + if ($this->resourceClass === 'App\Models\Application') { + $this->resource->settings->save(); + $this->dispatch('success', 'Environment variable settings updated.'); + $this->sortMe(); + } + } public function getDevView() { $this->variables = $this->resource->environment_variables->map(function ($item) { @@ -40,7 +65,7 @@ class All extends Component return "$item->key=(multiline, edit in normal view)"; } return "$item->key=$item->value"; - })->sort()->join(' + })->join(' '); if ($this->showPreview) { $this->variablesPreview = $this->resource->environment_variables_preview->map(function ($item) { @@ -51,13 +76,18 @@ class All extends Component return "$item->key=(multiline, edit in normal view)"; } return "$item->key=$item->value"; - })->sort()->join(' + })->join(' '); } } public function switch() { - $this->view = $this->view === 'normal' ? 'dev' : 'normal'; + if ($this->view === 'normal') { + $this->view = 'dev'; + } else { + $this->view = 'normal'; + } + $this->sortMe(); } public function saveVariables($isPreview) { @@ -66,6 +96,7 @@ class All extends Component $this->resource->environment_variables_preview()->whereNotIn('key', array_keys($variables))->delete(); } else { $variables = parseEnvFormatToArray($this->variables); + ray($variables, $this->variables); $this->resource->environment_variables()->whereNotIn('key', array_keys($variables))->delete(); } foreach ($variables as $key => $variable) { diff --git a/database/migrations/2024_05_17_082012_add_env_sorting_toggle.php b/database/migrations/2024_05_17_082012_add_env_sorting_toggle.php new file mode 100644 index 000000000..d4e120e2b --- /dev/null +++ b/database/migrations/2024_05_17_082012_add_env_sorting_toggle.php @@ -0,0 +1,28 @@ +boolean('is_env_sorting_enabled')->default(true); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('application_settings', function (Blueprint $table) { + $table->dropColumn('is_env_sorting_enabled'); + }); + } +}; 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 06df0871c..41a8a6d59 100644 --- a/resources/views/livewire/project/shared/environment-variable/all.blade.php +++ b/resources/views/livewire/project/shared/environment-variable/all.blade.php @@ -11,12 +11,18 @@ wire:click='switch'>{{ $view === 'normal' ? 'Developer view' : 'Normal view' }}
Environment variables (secrets) for this resource.
+ @if ($this->resourceClass === 'App\Models\Application') +
+ +
+ @endif @if ($resource->type() === 'service' || $resource?->build_pack === 'dockercompose')
Hardcoded variables are not shown here.
@endif @if ($view === 'normal') - @forelse ($resource->environment_variables->sort()->sortBy('key') as $env) + @forelse ($resource->environment_variables as $env) @empty @@ -27,7 +33,7 @@

Preview Deployments

Environment (secrets) variables for Preview Deployments.
- @foreach ($resource->environment_variables_preview->sort()->sortBy('key') as $env) + @foreach ($resource->environment_variables_preview as $env) @endforeach