From 006dfca2a781a945472d8074b0b7a553736c8173 Mon Sep 17 00:00:00 2001 From: JonPurvis Date: Mon, 16 Nov 2026 22:04:58 +0000 Subject: [PATCH 1/2] fix browser primary domain --- app/Commands/Browser.php | 8 +-- app/Dto/Environment.php | 21 ++++++++ tests/Feature/BrowserTest.php | 97 +++++++++++++++++++++++++++++++++++ 3 files changed, 123 insertions(+), 3 deletions(-) create mode 100644 tests/Feature/BrowserTest.php diff --git a/app/Commands/Browser.php b/app/Commands/Browser.php index ef5410d8..70a444d7 100644 --- a/app/Commands/Browser.php +++ b/app/Commands/Browser.php @@ -28,14 +28,16 @@ public function handle() $app = $this->resolvers()->application()->from($this->argument('application')); $environment = $this->resolvers()->environment()->withApplication($app)->from($this->argument('environment')); - if (! $environment->url) { + $url = $environment->siteUrl(); + + if (! $url) { warning('No site found for this environment.'); return self::FAILURE; } - openUrl($environment->url); + openUrl($url); - outro($environment->url); + outro($url); } } diff --git a/app/Dto/Environment.php b/app/Dto/Environment.php index 65c48482..15c375f4 100644 --- a/app/Dto/Environment.php +++ b/app/Dto/Environment.php @@ -47,6 +47,7 @@ public function __construct( public readonly ?string $currentDeploymentId = null, public readonly array $domainIds = [], public readonly ?string $primaryDomainId = null, + public readonly ?Domain $primaryDomain = null, public readonly ?string $databaseSchemaId = null, public readonly ?string $cacheId = null, public readonly ?string $websocketApplicationId = null, @@ -134,6 +135,12 @@ public static function createFromResponse(array $response): self if (isset($relationships['primaryDomain']['data']['id'])) { $transformed['primaryDomainId'] = $relationships['primaryDomain']['data']['id']; + + $domain = collect($included)->first(fn ($item) => $item['type'] === 'domains' && $item['id'] === $transformed['primaryDomainId']); + + if ($domain) { + $transformed['primaryDomain'] = Domain::createFromResponse(['data' => $domain]); + } } if (isset($relationships['database']['data']['id'])) { @@ -161,6 +168,20 @@ public static function createFromResponse(array $response): self return self::from($transformed); } + /** + * The URL visitors should use: the primary custom domain once it has been + * verified, otherwise the *.laravel.cloud vanity domain. An unverified + * custom domain may not resolve yet, so it isn't worth sending anyone to. + */ + public function siteUrl(): string + { + if ($this->primaryDomain?->name && $this->primaryDomain->hostnameStatus === 'verified') { + return (string) str($this->primaryDomain->name)->start('https://'); + } + + return $this->url; + } + public function colorEnum(): ?EnvironmentColor { return $this->color ? EnvironmentColor::tryFrom($this->color) : null; diff --git a/tests/Feature/BrowserTest.php b/tests/Feature/BrowserTest.php new file mode 100644 index 00000000..385c2eae --- /dev/null +++ b/tests/Feature/BrowserTest.php @@ -0,0 +1,97 @@ +mockGit = Mockery::mock(Git::class); + $this->mockGit->shouldReceive('isRepo')->andReturn(true)->byDefault(); + $this->mockGit->shouldReceive('hasRemote')->andReturn(true)->byDefault(); + $this->mockGit->shouldReceive('getRoot')->andReturn('/tmp/test-repo')->byDefault(); + $this->mockGit->shouldReceive('currentBranch')->andReturn('main')->byDefault(); + $this->app->instance(Git::class, $this->mockGit); + + $this->mockConfig = Mockery::mock(ConfigRepository::class); + $this->mockConfig->shouldReceive('apiTokens')->andReturn(collect(['test-api-token'])); + $this->app->instance(ConfigRepository::class, $this->mockConfig); + + Process::fake(); +}); + +afterEach(function () { + MockClient::destroyGlobal(); +}); + +function browserEnvironmentResponse(?string $hostnameStatus): array +{ + if ($hostnameStatus === null) { + return ['data' => createEnvironmentResponse(), 'included' => []]; + } + + return [ + 'data' => createEnvironmentResponse([ + 'relationships' => [ + 'primaryDomain' => ['data' => ['id' => 'dom-1', 'type' => 'domains']], + ], + ]), + 'included' => [[ + 'id' => 'dom-1', + 'type' => 'domains', + 'attributes' => [ + 'name' => 'example.com', + 'type' => 'root', + 'hostname_status' => $hostnameStatus, + 'ssl_status' => 'verified', + 'origin_status' => 'verified', + ], + ]], + ]; +} + +function mockBrowserApi(?string $hostnameStatus): void +{ + MockClient::global([ + GetOrganizationRequest::class => MockResponse::make(organizationResponse(), 200), + GetApplicationRequest::class => MockResponse::make(['data' => createApplicationResponse()], 200), + GetEnvironmentRequest::class => MockResponse::make(browserEnvironmentResponse($hostnameStatus), 200), + ]); +} + +function openedUrl(string $url): void +{ + Process::assertRan(fn (PendingProcess $process) => in_array($url, (array) $process->command, true)); +} + +it('opens the verified primary domain', function () { + mockBrowserApi('verified'); + + $this->artisan('browser', ['application' => 'app-123', 'environment' => 'env-1', '--no-interaction' => true]) + ->assertSuccessful(); + + openedUrl('https://example.com'); +}); + +it('falls back to the vanity domain while the primary domain is unverified', function () { + mockBrowserApi('pending'); + + $this->artisan('browser', ['application' => 'app-123', 'environment' => 'env-1', '--no-interaction' => true]) + ->assertSuccessful(); + + openedUrl('https://my-app.cloud.laravel.com'); +}); + +it('opens the vanity domain when there is no custom domain', function () { + mockBrowserApi(null); + + $this->artisan('browser', ['application' => 'app-123', 'environment' => 'env-1', '--no-interaction' => true]) + ->assertSuccessful(); + + openedUrl('https://my-app.cloud.laravel.com'); +}); From 2cebd0a1edf196e77e8ec65e66624be219c1fbcb Mon Sep 17 00:00:00 2001 From: Joe Tannenbaum Date: Mon, 23 Nov 2026 20:05:00 -0500 Subject: [PATCH 2/2] make sure ssl is verified before using primary --- app/Dto/Environment.php | 10 ++++++---- app/Prompts/MonitorDeployments.php | 2 +- tests/Feature/BrowserTest.php | 17 +++++++++++++---- 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/app/Dto/Environment.php b/app/Dto/Environment.php index 15c375f4..42177465 100644 --- a/app/Dto/Environment.php +++ b/app/Dto/Environment.php @@ -169,13 +169,15 @@ public static function createFromResponse(array $response): self } /** - * The URL visitors should use: the primary custom domain once it has been - * verified, otherwise the *.laravel.cloud vanity domain. An unverified - * custom domain may not resolve yet, so it isn't worth sending anyone to. + * Until hostname and SSL both verify, the custom domain may not resolve or may serve a certificate error. */ public function siteUrl(): string { - if ($this->primaryDomain?->name && $this->primaryDomain->hostnameStatus === 'verified') { + if ( + $this->primaryDomain?->name + && $this->primaryDomain->hostnameStatus === 'verified' + && $this->primaryDomain->sslStatus === 'verified' + ) { return (string) str($this->primaryDomain->name)->start('https://'); } diff --git a/app/Prompts/MonitorDeployments.php b/app/Prompts/MonitorDeployments.php index 56aa4e09..c778bc95 100644 --- a/app/Prompts/MonitorDeployments.php +++ b/app/Prompts/MonitorDeployments.php @@ -98,7 +98,7 @@ public function display(): void $keyPressListener = KeyPressListener::for($this)->listenForQuit()->on('o', function () { if ($this->lastDeployment) { - openUrl($this->environment->url); + openUrl($this->environment->siteUrl()); } }); diff --git a/tests/Feature/BrowserTest.php b/tests/Feature/BrowserTest.php index 385c2eae..4fe0515a 100644 --- a/tests/Feature/BrowserTest.php +++ b/tests/Feature/BrowserTest.php @@ -29,7 +29,7 @@ MockClient::destroyGlobal(); }); -function browserEnvironmentResponse(?string $hostnameStatus): array +function browserEnvironmentResponse(?string $hostnameStatus, string $sslStatus = 'verified'): array { if ($hostnameStatus === null) { return ['data' => createEnvironmentResponse(), 'included' => []]; @@ -48,19 +48,19 @@ function browserEnvironmentResponse(?string $hostnameStatus): array 'name' => 'example.com', 'type' => 'root', 'hostname_status' => $hostnameStatus, - 'ssl_status' => 'verified', + 'ssl_status' => $sslStatus, 'origin_status' => 'verified', ], ]], ]; } -function mockBrowserApi(?string $hostnameStatus): void +function mockBrowserApi(?string $hostnameStatus, string $sslStatus = 'verified'): void { MockClient::global([ GetOrganizationRequest::class => MockResponse::make(organizationResponse(), 200), GetApplicationRequest::class => MockResponse::make(['data' => createApplicationResponse()], 200), - GetEnvironmentRequest::class => MockResponse::make(browserEnvironmentResponse($hostnameStatus), 200), + GetEnvironmentRequest::class => MockResponse::make(browserEnvironmentResponse($hostnameStatus, $sslStatus), 200), ]); } @@ -87,6 +87,15 @@ function openedUrl(string $url): void openedUrl('https://my-app.cloud.laravel.com'); }); +it('falls back to the vanity domain while the certificate is pending', function () { + mockBrowserApi('verified', 'pending'); + + $this->artisan('browser', ['application' => 'app-123', 'environment' => 'env-1', '--no-interaction' => true]) + ->assertSuccessful(); + + openedUrl('https://my-app.cloud.laravel.com'); +}); + it('opens the vanity domain when there is no custom domain', function () { mockBrowserApi(null);