Fix: Environment variables are not saving and sorting is not working properly in some cases
This commit is contained in:
Andras Bacsai
2024-08-14 21:13:23 +02:00
committed by GitHub
4 changed files with 242 additions and 203 deletions

View File

@@ -9,22 +9,16 @@ use Visus\Cuid2\Cuid2;
class All extends Component class All extends Component
{ {
public $resource; public $resource;
public string $resourceClass; public string $resourceClass;
public bool $showPreview = false; public bool $showPreview = false;
public ?string $modalId = null; public ?string $modalId = null;
public ?string $variables = null; public ?string $variables = null;
public ?string $variablesPreview = null; public ?string $variablesPreview = null;
public string $view = 'normal'; public string $view = 'normal';
protected $listeners = [ protected $listeners = [
'refreshEnvs',
'saveKey' => 'submit', 'saveKey' => 'submit',
'environmentVariableDeleted' => 'refreshEnvs',
]; ];
protected $rules = [ protected $rules = [
@@ -35,225 +29,236 @@ class All extends Component
{ {
$this->resourceClass = get_class($this->resource); $this->resourceClass = get_class($this->resource);
$resourceWithPreviews = ['App\Models\Application']; $resourceWithPreviews = ['App\Models\Application'];
$simpleDockerfile = ! is_null(data_get($this->resource, 'dockerfile')); $simpleDockerfile = !is_null(data_get($this->resource, 'dockerfile'));
if (str($this->resourceClass)->contains($resourceWithPreviews) && ! $simpleDockerfile) { if (str($this->resourceClass)->contains($resourceWithPreviews) && !$simpleDockerfile) {
$this->showPreview = true; $this->showPreview = true;
} }
$this->modalId = new Cuid2; $this->modalId = new Cuid2;
$this->sortMe(); $this->sortEnvironmentVariables();
$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');
}
}
$this->getDevView();
} }
public function instantSave() public function instantSave()
{ {
if ($this->resourceClass === 'App\Models\Application' && data_get($this->resource, 'build_pack') !== 'dockercompose') { $this->resource->settings->save();
$this->resource->settings->save(); $this->sortEnvironmentVariables();
$this->dispatch('success', 'Environment variable settings updated.'); $this->dispatch('success', 'Environment variable settings updated.');
$this->sortMe(); }
}
public function sortEnvironmentVariables()
{
$this->resource->load(['environment_variables', 'environment_variables_preview']);
$sortBy = $this->resource->settings->is_env_sorting_enabled ? 'key' : 'order';
$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('order')->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() 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) { 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) { 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"; return "$item->key=$item->value";
})->join(' })->join("\n");
');
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('
');
}
} }
public function switch() public function switch()
{ {
if ($this->view === 'normal') { $this->view = $this->view === 'normal' ? 'dev' : 'normal';
$this->view = 'dev'; $this->sortEnvironmentVariables();
} else {
$this->view = 'normal';
}
$this->sortMe();
} }
public function saveVariables($isPreview) public function submit($data = null)
{ {
if ($isPreview) { try {
$variables = parseEnvFormatToArray($this->variablesPreview); if ($data === null) {
$this->resource->environment_variables_preview()->whereNotIn('key', array_keys($variables))->delete(); $this->handleBulkSubmit();
} else {
$variables = parseEnvFormatToArray($this->variables);
$this->resource->environment_variables()->whereNotIn('key', array_keys($variables))->delete();
}
foreach ($variables as $key => $variable) {
if ($isPreview) {
$found = $this->resource->environment_variables_preview()->where('key', $key)->first();
} else { } else {
$found = $this->resource->environment_variables()->where('key', $key)->first(); $this->handleSingleSubmit($data);
} }
if ($found) {
if ($found->is_shown_once || $found->is_multiline) { $this->updateOrder();
continue; $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();
} }
$found->value = $variable; $order++;
// 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; private function handleBulkSubmit()
// } {
// } $variables = parseEnvFormatToArray($this->variables);
$found->save(); $this->deleteRemovedVariables(false, $variables);
$this->updateOrCreateVariables(false, $variables);
continue; 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;
}
$maxOrder = $this->resource->environment_variables()->max('order') ?? 0;
$environment = $this->createEnvironmentVariable($data);
$environment->order = $maxOrder + 1;
$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 = [
'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 { } else {
$environment = new EnvironmentVariable; $environment = new EnvironmentVariable;
$environment->key = $key; $environment->key = $key;
$environment->value = $variable; $environment->value = $value;
// 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_build_time = false;
$environment->is_multiline = false; $environment->is_multiline = false;
$environment->is_preview = $isPreview ? true : false; $environment->is_preview = $isPreview;
switch ($this->resource->type()) {
case 'application': $this->setEnvironmentResourceId($environment);
$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(); $environment->save();
} }
} }
if ($isPreview) { }
$this->dispatch('success', 'Preview environment variables updated.');
} else { private function setEnvironmentResourceId($environment)
$this->dispatch('success', 'Environment variables updated.'); {
$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;
} }
$this->refreshEnvs();
} }
public function refreshEnvs() public function refreshEnvs()
{ {
$this->resource->refresh(); $this->resource->refresh();
$this->sortEnvironmentVariables();
$this->getDevView(); $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);
}
}
}

View File

@@ -130,9 +130,10 @@ class Show extends Component
{ {
try { try {
$this->env->delete(); $this->env->delete();
$this->dispatch('refreshEnvs'); $this->dispatch('environmentVariableDeleted');
$this->dispatch('success', 'Environment variable deleted successfully.');
} catch (\Exception $e) { } catch (\Exception $e) {
return handleError($e); return handleError($e);
} }
} }
} }

View File

@@ -0,0 +1,28 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('environment_variables', function (Blueprint $table) {
$table->integer('order')->nullable();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('environment_variables', function (Blueprint $table) {
$table->dropColumn('order');
});
}
};

View File

@@ -2,9 +2,11 @@
<div> <div>
<div class="flex items-center gap-2"> <div class="flex items-center gap-2">
<h2>Environment Variables</h2> <h2>Environment Variables</h2>
<x-modal-input buttonTitle="+ Add" title="New Environment Variable"> <div class="flex flex-col items-center">
<livewire:project.shared.environment-variable.add /> <x-modal-input buttonTitle="+ Add" title="New Environment Variable">
</x-modal-input> <livewire:project.shared.environment-variable.add />
</x-modal-input>
</div>
<x-forms.button <x-forms.button
wire:click='switch'>{{ $view === 'normal' ? 'Developer view' : 'Normal view' }}</x-forms.button> wire:click='switch'>{{ $view === 'normal' ? 'Developer view' : 'Normal view' }}</x-forms.button>
</div> </div>
@@ -12,7 +14,7 @@
@if ($this->resourceClass === 'App\Models\Application' && data_get($this->resource, 'build_pack') !== 'dockercompose') @if ($this->resourceClass === 'App\Models\Application' && data_get($this->resource, 'build_pack') !== 'dockercompose')
<div class="w-64 pt-2"> <div class="w-64 pt-2">
<x-forms.checkbox id="resource.settings.is_env_sorting_enabled" label="Sort alphabetically" <x-forms.checkbox id="resource.settings.is_env_sorting_enabled" label="Sort alphabetically"
helper="Turn this off if one environment is dependent on an other. It will be sorted by creation order." helper="Turn this off if one environment is dependent on an other. It will be sorted by creation order (like you pasted them or in the order you created them)."
instantSave></x-forms.checkbox> instantSave></x-forms.checkbox>
</div> </div>
@endif @endif
@@ -31,6 +33,10 @@
@endif @endif
</div> </div>
@if ($view === 'normal') @if ($view === 'normal')
<div>
<h3>Production Environment Variables</h3>
<div>Environment (secrets) variables for Production.</div>
</div>
@forelse ($resource->environment_variables as $env) @forelse ($resource->environment_variables as $env)
<livewire:project.shared.environment-variable.show wire:key="environment-{{ $env->id }}" <livewire:project.shared.environment-variable.show wire:key="environment-{{ $env->id }}"
:env="$env" :type="$resource->type()" /> :env="$env" :type="$resource->type()" />
@@ -39,7 +45,7 @@
@endforelse @endforelse
@if ($resource->type() === 'application' && $resource->environment_variables_preview->count() > 0 && $showPreview) @if ($resource->type() === 'application' && $resource->environment_variables_preview->count() > 0 && $showPreview)
<div> <div>
<h3>Preview Deployments</h3> <h3>Preview Deployments Environment Variables</h3>
<div>Environment (secrets) variables for Preview Deployments.</div> <div>Environment (secrets) variables for Preview Deployments.</div>
</div> </div>
@foreach ($resource->environment_variables_preview as $env) @foreach ($resource->environment_variables_preview as $env)
@@ -48,16 +54,15 @@
@endforeach @endforeach
@endif @endif
@else @else
<form wire:submit='saveVariables(false)' class="flex flex-col gap-2"> <form wire:submit.prevent='submit' class="flex flex-col gap-2">
<x-forms.textarea rows="10" class="whitespace-pre-wrap" id="variables"></x-forms.textarea> <x-forms.textarea rows="10" class="whitespace-pre-wrap" id="variables" wire:model="variables" label="Production Environment Variables"></x-forms.textarea>
<x-forms.button type="submit" class="btn btn-primary">Save</x-forms.button>
@if ($showPreview)
<x-forms.textarea rows="10" class="whitespace-pre-wrap" label="Preview Deployments Environment Variables"
id="variablesPreview" wire:model="variablesPreview"></x-forms.textarea>
@endif
<x-forms.button type="submit" class="btn btn-primary">Save All Environment Variables</x-forms.button>
</form> </form>
@if ($showPreview)
<form wire:submit='saveVariables(true)' class="flex flex-col gap-2">
<x-forms.textarea rows="10" class="whitespace-pre-wrap" label="Preview Environment Variables"
id="variablesPreview"></x-forms.textarea>
<x-forms.button type="submit" class="btn btn-primary">Save</x-forms.button>
</form>
@endif
@endif @endif
</div> </div>