fix: several shared environment variables in one value, parsed correctly

This commit is contained in:
Andras Bacsai
2024-07-26 13:22:44 +02:00
parent dcfb716711
commit d7350fad76
5 changed files with 44 additions and 41 deletions

View File

@@ -48,14 +48,14 @@ class Add extends Component
public function submit() public function submit()
{ {
$this->validate(); $this->validate();
if (str($this->value)->startsWith('{{') && str($this->value)->endsWith('}}')) { // if (str($this->value)->startsWith('{{') && str($this->value)->endsWith('}}')) {
$type = str($this->value)->after('{{')->before('.')->value; // $type = str($this->value)->after('{{')->before('.')->value;
if (! collect(SHARED_VARIABLE_TYPES)->contains($type)) { // if (! collect(SHARED_VARIABLE_TYPES)->contains($type)) {
$this->dispatch('error', 'Invalid shared variable type.', 'Valid types are: team, project, environment.'); // $this->dispatch('error', 'Invalid shared variable type.', 'Valid types are: team, project, environment.');
return; // return;
} // }
} // }
$this->dispatch('saveKey', [ $this->dispatch('saveKey', [
'key' => $this->key, 'key' => $this->key,
'value' => $this->value, 'value' => $this->value,

View File

@@ -125,14 +125,14 @@ class All extends Component
continue; continue;
} }
$found->value = $variable; $found->value = $variable;
if (str($found->value)->startsWith('{{') && str($found->value)->endsWith('}}')) { // if (str($found->value)->startsWith('{{') && str($found->value)->endsWith('}}')) {
$type = str($found->value)->after('{{')->before('.')->value; // $type = str($found->value)->after('{{')->before('.')->value;
if (! collect(SHARED_VARIABLE_TYPES)->contains($type)) { // if (! collect(SHARED_VARIABLE_TYPES)->contains($type)) {
$this->dispatch('error', 'Invalid shared variable type.', 'Valid types are: team, project, environment.'); // $this->dispatch('error', 'Invalid shared variable type.', 'Valid types are: team, project, environment.');
return; // return;
} // }
} // }
$found->save(); $found->save();
continue; continue;
@@ -140,14 +140,14 @@ class All extends Component
$environment = new EnvironmentVariable; $environment = new EnvironmentVariable;
$environment->key = $key; $environment->key = $key;
$environment->value = $variable; $environment->value = $variable;
if (str($environment->value)->startsWith('{{') && str($environment->value)->endsWith('}}')) { // if (str($environment->value)->startsWith('{{') && str($environment->value)->endsWith('}}')) {
$type = str($environment->value)->after('{{')->before('.')->value; // $type = str($environment->value)->after('{{')->before('.')->value;
if (! collect(SHARED_VARIABLE_TYPES)->contains($type)) { // if (! collect(SHARED_VARIABLE_TYPES)->contains($type)) {
$this->dispatch('error', 'Invalid shared variable type.', 'Valid types are: team, project, environment.'); // $this->dispatch('error', 'Invalid shared variable type.', 'Valid types are: team, project, environment.');
return; // 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 ? true : false;

View File

@@ -108,14 +108,14 @@ class Show extends Component
} else { } else {
$this->validate(); $this->validate();
} }
if (str($this->env->value)->startsWith('{{') && str($this->env->value)->endsWith('}}')) { // if (str($this->env->value)->startsWith('{{') && str($this->env->value)->endsWith('}}')) {
$type = str($this->env->value)->after('{{')->before('.')->value; // $type = str($this->env->value)->after('{{')->before('.')->value;
if (! collect(SHARED_VARIABLE_TYPES)->contains($type)) { // if (! collect(SHARED_VARIABLE_TYPES)->contains($type)) {
$this->dispatch('error', 'Invalid shared variable type.', 'Valid types are: team, project, environment.'); // $this->dispatch('error', 'Invalid shared variable type.', 'Valid types are: team, project, environment.');
return; // return;
} // }
} // }
$this->serialize(); $this->serialize();
$this->env->save(); $this->env->save();
$this->dispatch('success', 'Environment variable updated.'); $this->dispatch('success', 'Environment variable updated.');

View File

@@ -5,7 +5,6 @@ namespace App\Models;
use App\Models\EnvironmentVariable as ModelsEnvironmentVariable; use App\Models\EnvironmentVariable as ModelsEnvironmentVariable;
use Illuminate\Database\Eloquent\Casts\Attribute; use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
use OpenApi\Attributes as OA; use OpenApi\Attributes as OA;
use Symfony\Component\Yaml\Yaml; use Symfony\Component\Yaml\Yaml;
use Visus\Cuid2\Cuid2; use Visus\Cuid2\Cuid2;
@@ -200,28 +199,33 @@ class EnvironmentVariable extends Model
return null; return null;
} }
$environment_variable = trim($environment_variable); $environment_variable = trim($environment_variable);
$type = str($environment_variable)->after('{{')->before('.')->value; $sharedEnvsFound = str($environment_variable)->matchAll('/{{(.*?)}}/');
if (str($environment_variable)->startsWith('{{'.$type) && str($environment_variable)->endsWith('}}')) { if ($sharedEnvsFound->isEmpty()) {
$variable = Str::after($environment_variable, "{$type}."); return $environment_variable;
$variable = Str::before($variable, '}}'); }
$variable = str($variable)->trim()->value; foreach ($sharedEnvsFound as $sharedEnv) {
$type = str($sharedEnv)->match('/(.*?)\./');
if (! collect(SHARED_VARIABLE_TYPES)->contains($type)) { if (! collect(SHARED_VARIABLE_TYPES)->contains($type)) {
return $variable; continue;
} }
if ($type === 'environment') { $variable = str($sharedEnv)->match('/\.(.*)/');
if ($type->value() === 'environment') {
$id = $resource->environment->id; $id = $resource->environment->id;
} elseif ($type === 'project') { } elseif ($type->value() === 'project') {
$id = $resource->environment->project->id; $id = $resource->environment->project->id;
} else { } elseif ($type->value() === 'team') {
$id = $resource->team()->id; $id = $resource->team()->id;
} }
if (is_null($id)) {
continue;
}
$environment_variable_found = SharedEnvironmentVariable::where('type', $type)->where('key', $variable)->where('team_id', $resource->team()->id)->where("{$type}_id", $id)->first(); $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) { if ($environment_variable_found) {
return $environment_variable_found; $environment_variable = str($environment_variable)->replace("{{{$sharedEnv}}}", $environment_variable_found->value);
} }
} }
return $environment_variable; return str($environment_variable)->value();
} }
private function get_environment_variables(?string $environment_variable = null): ?string private function get_environment_variables(?string $environment_variable = null): ?string

View File

@@ -128,7 +128,6 @@ function updateCompose(ServiceApplication|ServiceDatabase $resource)
if ($port) { if ($port) {
$variableName = $variableName."_$port"; $variableName = $variableName."_$port";
$generatedEnv = EnvironmentVariable::where('service_id', $resource->service_id)->where('key', $variableName)->first(); $generatedEnv = EnvironmentVariable::where('service_id', $resource->service_id)->where('key', $variableName)->first();
// ray($generatedEnv);
if ($generatedEnv) { if ($generatedEnv) {
$generatedEnv->value = $fqdn.$path; $generatedEnv->value = $fqdn.$path;
$generatedEnv->save(); $generatedEnv->save();