From 3ea3674407ee8b57840b93798f3be143443db9ba Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Fri, 15 Mar 2024 22:02:37 +0100 Subject: [PATCH] fix: multiline env variables --- app/Livewire/Project/Edit.php | 5 ++ app/Livewire/Project/EnvironmentEdit.php | 5 ++ .../Shared/EnvironmentVariable/Add.php | 5 ++ .../Shared/EnvironmentVariable/All.php | 11 ++++- app/Livewire/TeamSharedVariablesIndex.php | 5 ++ app/Models/EnvironmentVariable.php | 18 +++++-- app/View/Components/Forms/Textarea.php | 5 +- .../2024_03_14_214402_add_multiline_envs.php | 6 +++ .../views/components/forms/input.blade.php | 6 +-- .../views/components/forms/textarea.blade.php | 47 ++++++++++++++++--- resources/views/layouts/base.blade.php | 4 +- .../shared/environment-variable/add.blade.php | 6 ++- .../environment-variable/show.blade.php | 8 ++-- 13 files changed, 106 insertions(+), 25 deletions(-) diff --git a/app/Livewire/Project/Edit.php b/app/Livewire/Project/Edit.php index b2cda7a47..a80b1af76 100644 --- a/app/Livewire/Project/Edit.php +++ b/app/Livewire/Project/Edit.php @@ -17,9 +17,14 @@ class Edit extends Component public function saveKey($data) { try { + $found = $this->project->environment_variables()->where('key', $data['key'])->first(); + if ($found) { + throw new \Exception('Variable already exists.'); + } $this->project->environment_variables()->create([ 'key' => $data['key'], 'value' => $data['value'], + 'is_multiline' => $data['is_multiline'], 'type' => 'project', 'team_id' => currentTeam()->id, ]); diff --git a/app/Livewire/Project/EnvironmentEdit.php b/app/Livewire/Project/EnvironmentEdit.php index 276aa58df..c5e4ccf11 100644 --- a/app/Livewire/Project/EnvironmentEdit.php +++ b/app/Livewire/Project/EnvironmentEdit.php @@ -21,9 +21,14 @@ class EnvironmentEdit extends Component public function saveKey($data) { try { + $found = $this->environment->environment_variables()->where('key', $data['key'])->first(); + if ($found) { + throw new \Exception('Variable already exists.'); + } $this->environment->environment_variables()->create([ 'key' => $data['key'], 'value' => $data['value'], + 'is_multiline' => $data['is_multiline'], 'type' => 'environment', 'team_id' => currentTeam()->id, ]); diff --git a/app/Livewire/Project/Shared/EnvironmentVariable/Add.php b/app/Livewire/Project/Shared/EnvironmentVariable/Add.php index 923f0d455..c04242963 100644 --- a/app/Livewire/Project/Shared/EnvironmentVariable/Add.php +++ b/app/Livewire/Project/Shared/EnvironmentVariable/Add.php @@ -11,17 +11,20 @@ class Add extends Component public string $key; public ?string $value = null; public bool $is_build_time = false; + public bool $is_multiline = false; protected $listeners = ['clearAddEnv' => 'clear']; protected $rules = [ 'key' => 'required|string', 'value' => 'nullable', 'is_build_time' => 'required|boolean', + 'is_multiline' => 'required|boolean', ]; protected $validationAttributes = [ 'key' => 'key', 'value' => 'value', 'is_build_time' => 'build', + 'is_multiline' => 'multiline', ]; public function mount() @@ -43,6 +46,7 @@ public function submit() 'key' => $this->key, 'value' => $this->value, 'is_build_time' => $this->is_build_time, + 'is_multiline' => $this->is_multiline, 'is_preview' => $this->is_preview, ]); $this->clear(); @@ -53,5 +57,6 @@ public function clear() $this->key = ''; $this->value = ''; $this->is_build_time = false; + $this->is_multiline = false; } } diff --git a/app/Livewire/Project/Shared/EnvironmentVariable/All.php b/app/Livewire/Project/Shared/EnvironmentVariable/All.php index 3a304d3e9..53dd0ffe3 100644 --- a/app/Livewire/Project/Shared/EnvironmentVariable/All.php +++ b/app/Livewire/Project/Shared/EnvironmentVariable/All.php @@ -11,7 +11,7 @@ class All extends Component { public $resource; public bool $showPreview = false; - public string|null $modalId = null; + public ?string $modalId = null; public ?string $variables = null; public ?string $variablesPreview = null; public string $view = 'normal'; @@ -34,6 +34,9 @@ public function getDevView() 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"; })->sort()->join(' '); @@ -42,6 +45,9 @@ public function getDevView() 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"; })->sort()->join(' '); @@ -67,7 +73,7 @@ public function saveVariables($isPreview) $found = $this->resource->environment_variables()->where('key', $key)->first(); } if ($found) { - if ($found->is_shown_once) { + if ($found->is_shown_once || $found->is_multiline) { continue; } $found->value = $variable; @@ -144,6 +150,7 @@ public function submit($data) $environment->key = $data['key']; $environment->value = $data['value']; $environment->is_build_time = $data['is_build_time']; + $environment->is_multiline = $data['is_multiline']; $environment->is_preview = $data['is_preview']; switch ($this->resource->type()) { diff --git a/app/Livewire/TeamSharedVariablesIndex.php b/app/Livewire/TeamSharedVariablesIndex.php index d2776e8b2..dbb64ab65 100644 --- a/app/Livewire/TeamSharedVariablesIndex.php +++ b/app/Livewire/TeamSharedVariablesIndex.php @@ -13,9 +13,14 @@ class TeamSharedVariablesIndex extends Component public function saveKey($data) { try { + $found = $this->team->environment_variables()->where('key', $data['key'])->first(); + if ($found) { + throw new \Exception('Variable already exists.'); + } $this->team->environment_variables()->create([ 'key' => $data['key'], 'value' => $data['value'], + 'is_multiline' => $data['is_multiline'], 'type' => 'team', 'team_id' => currentTeam()->id, ]); diff --git a/app/Models/EnvironmentVariable.php b/app/Models/EnvironmentVariable.php index a3611a0c6..41fe40c58 100644 --- a/app/Models/EnvironmentVariable.php +++ b/app/Models/EnvironmentVariable.php @@ -49,7 +49,7 @@ protected function value(): Attribute set: fn (?string $value = null) => $this->set_environment_variables($value), ); } - public function realValue(): Attribute + public function resource() { $resource = null; if ($this->application_id) { @@ -71,9 +71,19 @@ public function realValue(): Attribute } } } + return $resource; + } + public function realValue(): Attribute + { + $resource = $this->resource(); return Attribute::make( get: function () use ($resource) { - return $this->get_real_environment_variables($this->value, $resource); + $env = $this->get_real_environment_variables($this->value, $resource); + return data_get($env, 'value', $env); + if (is_string($env)) { + return $env; + } + return $env->value; } ); } @@ -89,7 +99,7 @@ protected function isShared(): Attribute } ); } - private function get_real_environment_variables(?string $environment_variable = null, $resource = null): string|null + private function get_real_environment_variables(?string $environment_variable = null, $resource = null) { if (!$environment_variable || !$resource) { return null; @@ -112,7 +122,7 @@ private function get_real_environment_variables(?string $environment_variable = } $environment_variable_found = SharedEnvironmentVariable::where("type", $type)->where('key', $variable)->where('team_id', $resource->team()->id)->where("{$type}_id", $id)->first(); if ($environment_variable_found) { - return $environment_variable_found->value; + return $environment_variable_found; } } return $environment_variable; diff --git a/app/View/Components/Forms/Textarea.php b/app/View/Components/Forms/Textarea.php index aa8d874ad..8c50af533 100644 --- a/app/View/Components/Forms/Textarea.php +++ b/app/View/Components/Forms/Textarea.php @@ -24,8 +24,9 @@ public function __construct( public bool $readonly = false, public ?string $helper = null, public bool $realtimeValidation = false, - // public bool $allowToPeak = true, - public string $defaultClass = "textarea leading-normal bg-coolgray-100 rounded text-white scrollbar disabled:bg-coolgray-200/50 disabled:border-none placeholder:text-coolgray-500 read-only:text-neutral-500 read-only:bg-coolgray-200/50" + public bool $allowToPeak = true, + public string $defaultClass = "textarea leading-normal bg-coolgray-100 rounded text-white w-full scrollbar disabled:bg-coolgray-200/50 disabled:border-none placeholder:text-coolgray-500 read-only:text-neutral-500 read-only:bg-coolgray-200/50", + public string $defaultClassInput = "input input-sm bg-coolgray-100 rounded text-white w-full disabled:bg-coolgray-200/50 disabled:border-none placeholder:text-coolgray-500 read-only:text-neutral-500 read-only:bg-coolgray-200/50" ) { // } diff --git a/database/migrations/2024_03_14_214402_add_multiline_envs.php b/database/migrations/2024_03_14_214402_add_multiline_envs.php index c8d809927..b3e869bc3 100644 --- a/database/migrations/2024_03_14_214402_add_multiline_envs.php +++ b/database/migrations/2024_03_14_214402_add_multiline_envs.php @@ -14,6 +14,9 @@ public function up(): void Schema::table('environment_variables', function (Blueprint $table) { $table->boolean('is_multiline')->default(false); }); + Schema::table('shared_environment_variables', function (Blueprint $table) { + $table->boolean('is_multiline')->default(false); + }); } /** @@ -24,5 +27,8 @@ public function down(): void Schema::table('environment_variables', function (Blueprint $table) { $table->dropColumn('is_multiline'); }); + Schema::table('shared_environment_variables', function (Blueprint $table) { + $table->dropColumn('is_multiline'); + }); } }; diff --git a/resources/views/components/forms/input.blade.php b/resources/views/components/forms/input.blade.php index c13de00c1..5cabb3c70 100644 --- a/resources/views/components/forms/input.blade.php +++ b/resources/views/components/forms/input.blade.php @@ -1,4 +1,4 @@ -
+
@if ($label) @endif @if ($type === 'password') -
+
@if ($allowToPeak)
@@ -22,7 +22,7 @@ class="absolute inset-y-0 right-0 flex items-center pr-2 cursor-pointer hover:te
@endif - merge(['class' => $defaultClass]) }} + merge(['class' => $defaultClass]) }} @required($required) @if ($id !== 'null') wire:model={{ $id }} @endif wire:dirty.class.remove='text-white' wire:dirty.class="input-warning" wire:loading.attr="disabled" type="{{ $type }}" @readonly($readonly) @disabled($disabled) id="{{ $id }}" diff --git a/resources/views/components/forms/textarea.blade.php b/resources/views/components/forms/textarea.blade.php index 4ef36c310..74845e5e0 100644 --- a/resources/views/components/forms/textarea.blade.php +++ b/resources/views/components/forms/textarea.blade.php @@ -1,4 +1,4 @@ -
+
@if ($label) @endif - + +
+ @else + + wire:model={{ $value ?? $id }} + wire:dirty.class="input-warning" @endif + @disabled($disabled) @readonly($readonly) @required($required) id="{{ $id }}" + name="{{ $name }}" name={{ $id }}> + @endif + @error($id)