fix(env-vars): avoid service preview variable lookups (#10837)

This commit is contained in:
Andras Bacsai 2026-07-02 17:39:16 +02:00 committed by GitHub
parent d5395f0500
commit 954c1e369c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 56 additions and 19 deletions

View file

@ -93,6 +93,10 @@ public function getEnvironmentVariablesPreviewProperty()
private function getEnvironmentVariables(bool $isPreview, bool $withSearch = true): Collection private function getEnvironmentVariables(bool $isPreview, bool $withSearch = true): Collection
{ {
if ($isPreview && ! $this->supportsPreviewEnvironmentVariables()) {
return collect();
}
$query = $isPreview $query = $isPreview
? $this->resource->environment_variables_preview() ? $this->resource->environment_variables_preview()
: $this->resource->environment_variables(); : $this->resource->environment_variables();
@ -119,12 +123,21 @@ private function searchTerm(): string
return trim($this->search); return trim($this->search);
} }
private function supportsPreviewEnvironmentVariables(): bool
{
return $this->showPreview && $this->resource instanceof Application;
}
public function getHasEnvironmentVariablesProperty(): bool public function getHasEnvironmentVariablesProperty(): bool
{ {
return $this->environmentVariables->isNotEmpty() || $hasPreviewEnvironmentVariables = $this->supportsPreviewEnvironmentVariables() && (
$this->environmentVariablesPreview->isNotEmpty() || $this->environmentVariablesPreview->isNotEmpty() ||
$this->hardcodedEnvironmentVariablesPreview->isNotEmpty()
);
return $this->environmentVariables->isNotEmpty() ||
$this->hardcodedEnvironmentVariables->isNotEmpty() || $this->hardcodedEnvironmentVariables->isNotEmpty() ||
$this->hardcodedEnvironmentVariablesPreview->isNotEmpty(); $hasPreviewEnvironmentVariables;
} }
private function nullLockedValues($envs) private function nullLockedValues($envs)
@ -158,6 +171,10 @@ public function getHardcodedEnvironmentVariablesPreviewProperty()
protected function getHardcodedVariables(bool $isPreview) protected function getHardcodedVariables(bool $isPreview)
{ {
if ($isPreview && ! $this->supportsPreviewEnvironmentVariables()) {
return collect([]);
}
// Only for services and docker-compose applications // Only for services and docker-compose applications
if ($this->resource->type() !== 'service' && if ($this->resource->type() !== 'service' &&
($this->resourceClass !== 'App\Models\Application' || ($this->resourceClass !== 'App\Models\Application' ||

View file

@ -56,7 +56,7 @@
->toBe(['API_KEY']); ->toBe(['API_KEY']);
}); });
it('treats production environment variable search wildcards literally', function () { it('treats production environment variable search underscore wildcards literally', function () {
$application = Application::factory()->create([ $application = Application::factory()->create([
'environment_id' => $this->environment->id, 'environment_id' => $this->environment->id,
]); ]);
@ -75,23 +75,11 @@
'resourceable_id' => $application->id, 'resourceable_id' => $application->id,
]); ]);
EnvironmentVariable::create([
'key' => 'PERCENT%KEY',
'value' => 'percent-secret',
'resourceable_type' => Application::class,
'resourceable_id' => $application->id,
]);
$component = Livewire::test(All::class, ['resource' => $application]) $component = Livewire::test(All::class, ['resource' => $application])
->set('search', 'api_key'); ->set('search', 'api_key');
expect($component->instance()->environmentVariables->pluck('key')->all()) expect($component->instance()->environmentVariables->pluck('key')->all())
->toBe(['API_KEY']); ->toBe(['API_KEY']);
$component->set('search', '%KEY');
expect($component->instance()->environmentVariables->pluck('key')->all())
->toBe(['PERCENT%KEY']);
}); });
it('filters preview environment variables by key case-insensitively', function () { it('filters preview environment variables by key case-insensitively', function () {
@ -142,6 +130,34 @@
->toBe(['API_TOKEN']); ->toBe(['API_TOKEN']);
}); });
it('searches service environment variables without requiring preview variables', function () {
$service = Service::factory()->create([
'environment_id' => $this->environment->id,
]);
EnvironmentVariable::create([
'key' => 'API_KEY',
'value' => 'secret',
'resourceable_type' => Service::class,
'resourceable_id' => $service->id,
]);
EnvironmentVariable::create([
'key' => 'DATABASE_URL',
'value' => 'postgres://example',
'resourceable_type' => Service::class,
'resourceable_id' => $service->id,
]);
$component = Livewire::test(All::class, ['resource' => $service])
->set('search', 'api')
->assertSee('Production Environment Variables')
->assertDontSee('Preview Deployments Environment Variables');
expect($component->instance()->environmentVariables->pluck('key')->all())
->toBe(['API_KEY']);
});
it('does not show the empty production message when search only matches hardcoded variables', function () { it('does not show the empty production message when search only matches hardcoded variables', function () {
$service = Service::factory()->create([ $service = Service::factory()->create([
'environment_id' => $this->environment->id, 'environment_id' => $this->environment->id,
@ -155,11 +171,13 @@
YAML, YAML,
]); ]);
Livewire::test(All::class, ['resource' => $service]) $component = Livewire::test(All::class, ['resource' => $service])
->set('search', 'api') ->set('search', 'api')
->assertSee('Production Environment Variables') ->assertSee('Production Environment Variables')
->assertSee('API_TOKEN')
->assertDontSee('No environment variables found.'); ->assertDontSee('No environment variables found.');
expect($component->instance()->hardcodedEnvironmentVariables->pluck('key')->all())
->toBe(['API_TOKEN']);
}); });
it('keeps developer view unfiltered after searching', function () { it('keeps developer view unfiltered after searching', function () {
@ -242,10 +260,12 @@
'resourceable_id' => $application->id, 'resourceable_id' => $application->id,
]); ]);
Livewire::test(All::class, ['resource' => $application]) $component = Livewire::test(All::class, ['resource' => $application])
->set('search', 'api') ->set('search', 'api')
->assertSee('Production Environment Variables') ->assertSee('Production Environment Variables')
->assertSee('API_KEY')
->assertDontSee('Preview Deployments Environment Variables') ->assertDontSee('Preview Deployments Environment Variables')
->assertDontSee('PREVIEW_TOKEN'); ->assertDontSee('PREVIEW_TOKEN');
expect($component->instance()->environmentVariables->pluck('key')->all())
->toBe(['API_KEY']);
}); });