diff --git a/app/Livewire/Project/Service/Configuration.php b/app/Livewire/Project/Service/Configuration.php index caa19042b..f3ec3ba4d 100644 --- a/app/Livewire/Project/Service/Configuration.php +++ b/app/Livewire/Project/Service/Configuration.php @@ -26,10 +26,17 @@ class Configuration extends Component public array $parameters; - protected $listeners = [ - 'refreshServices' => 'refreshServices', - 'refresh' => 'refreshServices', - ]; + public function getListeners(): array + { + $teamId = auth()->user()->currentTeam()->id; + + return [ + 'refreshServices' => 'refreshServices', + 'refresh' => 'refreshServices', + 'configurationChanged' => 'refreshServices', + "echo-private:team.{$teamId},ApplicationConfigurationChanged" => 'refreshServices', + ]; + } public function render() { diff --git a/app/Livewire/Project/Shared/EnvironmentVariable/Show.php b/app/Livewire/Project/Shared/EnvironmentVariable/Show.php index 7f37b1fc4..cd86d8670 100644 --- a/app/Livewire/Project/Shared/EnvironmentVariable/Show.php +++ b/app/Livewire/Project/Shared/EnvironmentVariable/Show.php @@ -2,6 +2,7 @@ namespace App\Livewire\Project\Shared\EnvironmentVariable; +use App\Events\ApplicationConfigurationChanged; use App\Models\Application; use App\Models\Environment; use App\Models\EnvironmentVariable as ModelsEnvironmentVariable; @@ -298,6 +299,10 @@ public function submit() $this->dispatch('success', 'Environment variable updated.'); $this->dispatch('envsUpdated'); $this->dispatch('configurationChanged'); + + if ($this->is_required && $this->resource instanceof Service) { + event(new ApplicationConfigurationChanged($this->resource->team()->id)); + } } catch (\Exception $e) { return handleError($e); } diff --git a/tests/Feature/Livewire/ConfigurationCheckerTest.php b/tests/Feature/Livewire/ConfigurationCheckerTest.php index ff1400847..3b80ed209 100644 --- a/tests/Feature/Livewire/ConfigurationCheckerTest.php +++ b/tests/Feature/Livewire/ConfigurationCheckerTest.php @@ -1,22 +1,29 @@ 0]); $this->team = Team::factory()->create(); $this->user = User::factory()->create(); $this->team->members()->attach($this->user->id, ['role' => 'owner']); @@ -121,6 +128,39 @@ function markConfigurationCheckerApplicationDeployed(Application $application): }); +it('broadcasts a configuration update after a required service variable is set', function () { + Event::fake([ApplicationConfigurationChanged::class]); + + $server = Server::factory()->create(['team_id' => $this->team->id]); + $service = Service::factory()->create([ + 'environment_id' => $this->environment->id, + 'server_id' => $server->id, + ]); + $environmentVariable = $service->environment_variables()->create([ + 'key' => 'PLUNK_API_KEY', + 'value' => '', + 'is_required' => true, + ]); + + Livewire::test(Show::class, ['env' => $environmentVariable, 'type' => 'service']) + ->call('loadValues') + ->set('value', 'secret') + ->call('submit'); + + Event::assertDispatched( + ApplicationConfigurationChanged::class, + fn (ApplicationConfigurationChanged $event): bool => $event->teamId === $this->team->id, + ); +}); + +it('refreshes the service configuration when a websocket configuration event arrives', function () { + $listeners = app(Configuration::class)->getListeners(); + + expect($listeners) + ->toHaveKey("echo-private:team.{$this->team->id},ApplicationConfigurationChanged", 'refreshServices') + ->toHaveKey('configurationChanged', 'refreshServices'); +}); + it('marks the service environment variables menu when required values are missing', function () { $configuration = file_get_contents(resource_path('views/livewire/project/service/configuration.blade.php')); $sidebar = file_get_contents(resource_path('views/components/service/configuration-sidebar.blade.php'));