feat: shared environments

This commit is contained in:
Andras Bacsai
2024-01-23 17:13:23 +01:00
parent abcc004953
commit fb478c79b3
42 changed files with 495 additions and 78 deletions

View File

@@ -15,6 +15,7 @@ class EnvironmentVariable extends Model
'value' => 'encrypted',
'is_build_time' => 'boolean',
];
protected $appends = ['real_value', 'is_shared'];
protected static function booted()
{
@@ -48,24 +49,94 @@ class EnvironmentVariable extends Model
set: fn (?string $value = null) => $this->set_environment_variables($value),
);
}
private function get_environment_variables(?string $environment_variable = null): string|null
protected function realValue(): Attribute
{
return Attribute::make(
get: fn () => $this->get_real_environment_variables($this->value),
);
}
protected function isShared(): Attribute
{
return Attribute::make(
get: function () {
$type = str($this->value)->after("{{")->before(".")->value;
if (str($this->value)->startsWith('{{' . $type) && str($this->value)->endsWith('}}')) {
return true;
}
return false;
}
);
}
private function team()
{
if ($this->application_id) {
$application = Application::find($this->application_id);
if ($application) {
return $application->team();
}
}
if ($this->service_id) {
$service = Service::find($this->service_id);
if ($service) {
return $service->team();
}
}
if ($this->standalone_postgresql_id) {
$standalone_postgresql = StandalonePostgresql::find($this->standalone_postgresql_id);
if ($standalone_postgresql) {
return $standalone_postgresql->team();
}
}
if ($this->standalone_mysql_id) {
$standalone_mysql = StandaloneMysql::find($this->standalone_mysql_id);
if ($standalone_mysql) {
return $standalone_mysql->team();
}
}
if ($this->standalone_redis_id) {
$standalone_redis = StandaloneRedis::find($this->standalone_redis_id);
if ($standalone_redis) {
return $standalone_redis->team();
}
}
if ($this->standalone_mongodb_id) {
$standalone_mongodb = StandaloneMongodb::find($this->standalone_mongodb_id);
if ($standalone_mongodb) {
return $standalone_mongodb->team();
}
}
if ($this->standalone_mariadb_id) {
$standalone_mariadb = StandaloneMariadb::find($this->standalone_mariadb_id);
if ($standalone_mariadb) {
return $standalone_mariadb->team();
}
}
}
private function get_real_environment_variables(?string $environment_variable = null): string|null
{
// $team_id = currentTeam()->id;
if (!$environment_variable) {
return null;
}
$environment_variable = trim(decrypt($environment_variable));
if (Str::startsWith($environment_variable, '{{') && Str::endsWith($environment_variable, '}}') && Str::contains($environment_variable, 'global.')) {
$variable = Str::after($environment_variable, 'global.');
$environment_variable = trim($environment_variable);
$type = str($environment_variable)->after("{{")->before(".")->value;
if (str($environment_variable)->startsWith("{{" . $type) && str($environment_variable)->endsWith('}}')) {
$variable = Str::after($environment_variable, "{$type}.");
$variable = Str::before($variable, '}}');
$variable = Str::of($variable)->trim()->value;
// $environment_variable = GlobalEnvironmentVariable::where('name', $environment_variable)->where('team_id', $team_id)->first()?->value;
ray('global env variable');
return $environment_variable;
$environment_variable_found = SharedEnvironmentVariable::where("type", $type)->where('key', $variable)->where('team_id', $this->team()->id)->first();
if ($environment_variable_found) {
return $environment_variable_found->value;
}
}
return $environment_variable;
}
private function get_environment_variables(?string $environment_variable = null): string|null
{
if (!$environment_variable) {
return null;
}
return trim(decrypt($environment_variable));
}
private function set_environment_variables(?string $environment_variable = null): string|null
{
@@ -73,6 +144,10 @@ class EnvironmentVariable extends Model
return null;
}
$environment_variable = trim($environment_variable);
$type = str($environment_variable)->after("{{")->before(".")->value;
if (str($environment_variable)->startsWith("{{" . $type) && str($environment_variable)->endsWith('}}')) {
return encrypt((string) str($environment_variable)->replace(' ', ''));
}
return encrypt($environment_variable);
}