From 8ed0245b6560075b307e319ecb794e5972e09366 Mon Sep 17 00:00:00 2001 From: Harshi-Shah-CS Date: Thu, 19 Nov 2026 14:36:30 +0530 Subject: [PATCH 1/2] fix(site): drop --deployment from site:open and never treat the preview screenshot as a URL MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A deployment's previewUrl is the screenshot the platform takes after it goes live (a .png on images.contentstack.io), not a site URL. site:open --deployment opened that image, and deploymentUrlOf fell back to it whenever deploymentUrl was empty, so the default site:open, the deploy follower's "Deployment URL:" line and the deployments:view URL row could show it too. - site:open opens the environment's live site only: --deployment is removed (agreed with Dhruv on CL-8789) and is now refused as an unknown flag, exit 2. - deploymentUrlOf reads deploymentUrl alone. When it is empty, site:open and every announcing command (projects:create, environments:create and now deployments:create, which reads the environment only in that case) fall back to the environment's domains; deployments:view shows "-". - One URL normalizer, core/url withScheme, shared by the deployment url, the domain fallback and the domain prompt: it trims, recognises a real scheme (a bare host like "http-api-ab12.contentstackapps.com" now gets https://) and skips whitespace-only values. - previewUrlOf is deleted, Deployment.previewUrl is documented as the screenshot, and the README is regenerated. - .talismanrc: refreshed the checksums of create.test.ts, deployment.create.test.ts, deployment.presenter.test.ts and README.md (false positives on "key", "secrets" and "Password" in test code and the regenerated command docs). #claude_code# 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- .talismanrc | 8 +- README.md | 19 +-- .../launch/deployments/create.test.ts | 2 +- src/commands/launch/site/open.test.ts | 153 +----------------- src/commands/launch/site/open.ts | 30 +--- src/core/url.ts | 12 ++ src/deployments/deployment.create.test.ts | 54 ++++++- src/deployments/deployment.create.ts | 18 ++- src/deployments/deployment.follower.test.ts | 8 +- src/deployments/deployment.presenter.test.ts | 9 +- src/deployments/deployment.presenter.ts | 9 +- src/deployments/types.ts | 1 + src/site/site.prompt.ts | 14 +- src/site/site.url.test.ts | 30 ++-- src/site/site.url.ts | 11 +- test/integration/site-open.test.ts | 96 +---------- 16 files changed, 134 insertions(+), 340 deletions(-) create mode 100644 src/core/url.ts diff --git a/.talismanrc b/.talismanrc index f29926f0..d7461897 100644 --- a/.talismanrc +++ b/.talismanrc @@ -331,7 +331,7 @@ fileignoreconfig: - filename: src/commands/launch/env/update.test.ts checksum: 2da219594e7bdd589adc0eff9ff035252a75aaa51dfa20514b18fd3266d7b752 - filename: README.md - checksum: a3c7d1fc273e7b5b818f0a7f05664fd7ddbb05c2f2eb44c28b5d496aad828508 + checksum: fde09d802a9252ef675fe34d0865317631649ff3ad0858b606cbac31bea7ed6a allowed_patterns: - "password-protection" - "-p, --password" @@ -433,7 +433,7 @@ fileignoreconfig: - filename: src/commands/launch/deployments/list.test.ts checksum: e493cd51e7c65bbf5b4f71704478b7a669bca4342efc865a1363bc508fe6f9f0 - filename: src/deployments/deployment.presenter.test.ts - checksum: e393d6bb6ce3fd92920f5003603b161186085ea8939fae704e6f71bd9e1778bc + checksum: f5a8d7ccd1118e79abd5f2717d6535ca13c576b92bd686ee429ae72aaafa955a - filename: test/integration/deployments-create.test.ts checksum: fef71ae5b4bed7bcca111e90870238a26518e7a376475aa55768f5ffbe07ba36 - filename: src/deployments/deployment.watcher.test.ts @@ -441,9 +441,9 @@ fileignoreconfig: - filename: src/deployments/deployment.create.ts checksum: ca64f3b29a68c4a2755a88a141e2cd9dbd6fa851a2776876d78c40f3168492e3 - filename: src/commands/launch/deployments/create.test.ts - checksum: bcbb2f85e0a78d38036af4723a0ce691f2692de05f5c7c9c3f97a5cca13497a6 + checksum: 9f5c60d5f6367250aa8f932acf0e5a7ece933f3fc1ca173fa908e80724d1d579 - filename: src/deployments/deployment.create.test.ts - checksum: 6545afa21a23b7e5de18ad8ff417f88a184577e7042713cfda90758ccbcd9869 + checksum: b880edba719b71f388ae04bab474d85e1a81af6346fb9a96a25352d50805366f - filename: test/integration/deployments-rollback.test.ts checksum: 98a9f7001eb5bf01e0e2f46677eeea61ad5d38c595d17ef27d366c93e603f0d5 - filename: test/integration/deployments-cancel.test.ts diff --git a/README.md b/README.md index 9c953341..7404a4b2 100644 --- a/README.md +++ b/README.md @@ -853,31 +853,26 @@ _See code: [src/commands/launch/secrets/unset.ts](https://github.com/contentstac ## `csdx launch:site:open` -Open an environment's live site in your browser, or print its URL when there is no terminal. On a terminal, when the environment has more than one domain, you choose which one to open; --deployment opens that deployment's preview URL without asking. +Open an environment's live site in your browser, or print its URL when there is no terminal. On a terminal, when the environment has more than one domain, you choose which one to open. ``` USAGE $ csdx launch:site:open [--config ] [-d ] [--org ] [--project ] [--env ] - [--deployment ] FLAGS - -d, --data-dir= Current working directory - --config= Path to the local '.cs-launch.json' file - --deployment= Deployment UID - --env= Environment name or UID - --org= Organization UID - --project= Project name or UID + -d, --data-dir= Current working directory + --config= Path to the local '.cs-launch.json' file + --env= Environment name or UID + --org= Organization UID + --project= Project name or UID DESCRIPTION Open an environment's live site in your browser, or print its URL when there is no terminal. On a terminal, when the - environment has more than one domain, you choose which one to open; --deployment opens that deployment's preview URL - without asking. + environment has more than one domain, you choose which one to open. EXAMPLES $ csdx launch:site:open --org --project --env - $ csdx launch:site:open --org --project --env --deployment - $ url=$(csdx launch:site:open --org --project --env ) ``` diff --git a/src/commands/launch/deployments/create.test.ts b/src/commands/launch/deployments/create.test.ts index a6d33ca8..fd0b0e8e 100644 --- a/src/commands/launch/deployments/create.test.ts +++ b/src/commands/launch/deployments/create.test.ts @@ -31,7 +31,7 @@ function commandUnderTest(resolved: Record, projectType = 'GITP created.push(params); return { uid: 'd1', status: 'QUEUED' }; }, - get: async () => ({ uid: 'd1', status: 'LIVE' }), + get: async () => ({ uid: 'd1', status: 'LIVE', deploymentUrl: 'my-site.example.test' }), }, deploymentLogs: { after: async () => [] }, }, diff --git a/src/commands/launch/site/open.test.ts b/src/commands/launch/site/open.test.ts index 4c778113..eb2479d6 100644 --- a/src/commands/launch/site/open.test.ts +++ b/src/commands/launch/site/open.test.ts @@ -1,16 +1,10 @@ -import { randomBytes } from 'node:crypto'; - import { CancelledError, UsageError } from '../../../core/errors'; import type { UxLike } from '../../../core/prompt'; -import { DEPLOYMENT_ERROR_MESSAGES } from '../../../deployments/deployment.errors'; import { ENVIRONMENT_ERROR_MESSAGES } from '../../../environments/environment.errors'; import { LaunchApiError } from '../../../transport/errors'; import SiteOpen from './open'; const SCOPE = { org: 'org1', project: 'proj1', environment: 'env1' }; -const DEPLOYMENT_UID = randomBytes(12).toString('hex'); -const DEPLOYMENT_SCOPE = { ...SCOPE, deployment: DEPLOYMENT_UID }; -const PREVIEW_URL = 'https://preview-1a2b.contentstackapps.com'; const LIVE_URL = 'https://my-site.contentstackapps.com'; const DOMAIN_URL = 'https://www.example.com'; const OTHER_DOMAIN_URL = 'https://shop.example.com'; @@ -25,9 +19,6 @@ interface Harness { environmentFails?: Error; latest?: unknown; latestFails?: Error; - deployment?: string; - fetched?: unknown; - fetchFails?: Error; } function commandUnderTest(harness: Harness = {}) { @@ -55,7 +46,7 @@ function commandUnderTest(harness: Harness = {}) { Object.assign(command, { ux, - resolved: { org: 'org1', project: 'proj1', env: 'env1', deployment: harness.deployment }, + resolved: { org: 'org1', project: 'proj1', env: 'env1' }, services: { ux, isTTY: harness.isTTY ?? true, @@ -75,16 +66,6 @@ function commandUnderTest(harness: Harness = {}) { }, }, deployments: { - get: async (params: unknown) => { - calls.push('deployments.get'); - got.push(params); - - if (harness.fetchFails) { - throw harness.fetchFails; - } - - return harness.fetched; - }, latest: async (params: unknown) => { calls.push('deployments.latest'); got.push(params); @@ -227,115 +208,6 @@ describe('launch:site:open', () => { expect(opened).toEqual([]); }); - it("opens the named deployment's preview url, not its deployment url, reading the environment first and never the latest", async () => { - const { command, stdout, calls, got, opened } = commandUnderTest({ - deployment: DEPLOYMENT_UID, - fetched: { uid: DEPLOYMENT_UID, deploymentUrl: LIVE_URL, previewUrl: 'preview-1a2b.contentstackapps.com' }, - environment: { uid: 'env1', domains: [{ url: 'https://www.example.com' }] }, - }); - - await command.run(); - - expect(calls).toEqual(['environments.get', 'deployments.get']); - expect(got).toEqual([SCOPE, DEPLOYMENT_SCOPE]); - expect(stdout).toEqual([`\u001b[32m✔ Opening ${PREVIEW_URL} in your browser...\u001b[39m`]); - expect(opened).toEqual([PREVIEW_URL]); - }); - - it('prints only the preview url and starts no browser for a named deployment when stdout is not a terminal', async () => { - const { command, stdout, stderr, opened } = commandUnderTest({ - outputIsTTY: false, - deployment: DEPLOYMENT_UID, - fetched: { uid: DEPLOYMENT_UID, previewUrl: PREVIEW_URL }, - }); - - await command.run(); - - expect(stdout).toEqual([PREVIEW_URL]); - expect(stderr).toEqual([]); - expect(opened).toEqual([]); - }); - - it.each([ - ['absent', undefined], - ['empty', ''], - ['whitespace only', ' '], - ])( - 'says the website url was not found in yellow on stdout and exits 2 when the named deployment has a %s preview url', - async (_label, previewUrl) => { - const { command, stdout, stderr, calls, got, opened } = commandUnderTest({ - deployment: DEPLOYMENT_UID, - fetched: { uid: DEPLOYMENT_UID, deploymentUrl: LIVE_URL, ...(previewUrl === undefined ? {} : { previewUrl }) }, - environment: { uid: 'env1', domains: [{ url: 'https://www.example.com' }] }, - }); - - const code = await exitCodeOf(command.run()); - - expect(code).toBe(2); - expect(stdout).toEqual(['\u001b[33mWebsite URL not found.\u001b[39m']); - expect(stderr).toEqual([]); - expect(opened).toEqual([]); - expect(calls).toEqual(['environments.get', 'deployments.get']); - expect(got).toEqual([SCOPE, DEPLOYMENT_SCOPE]); - }, - ); - - it('writes the plain message to stderr, nothing to stdout, and exits 2 for a named deployment without a preview url and no terminal', async () => { - const { command, stdout, stderr, opened } = commandUnderTest({ - outputIsTTY: false, - deployment: DEPLOYMENT_UID, - fetched: { uid: DEPLOYMENT_UID, deploymentUrl: LIVE_URL }, - }); - - const code = await exitCodeOf(command.run()); - - expect(code).toBe(2); - expect(stdout).toEqual([]); - expect(stderr).toEqual(['Website URL not found.\n']); - expect(opened).toEqual([]); - }); - - it('reports a deployment the environment does not have as a usage error in the api wording, printing and opening nothing', async () => { - const notFound = new LaunchApiError(404, [{ code: 'launch.DEPLOYMENT.NOT_FOUND' }], DEPLOYMENT_ERROR_MESSAGES); - const { command, calls, stdout, stderr, opened } = commandUnderTest({ - deployment: DEPLOYMENT_UID, - fetchFails: notFound, - }); - - const error = (await command.run().catch((thrown: unknown) => thrown)) as UsageError; - - expect(error).toBeInstanceOf(UsageError); - expect(error.exitCode).toBe(2); - expect(error.message).toBe('No deployment found with that UID.'); - expect(calls).toEqual(['environments.get', 'deployments.get']); - expect(stdout).toEqual([]); - expect(stderr).toEqual([]); - expect(opened).toEqual([]); - }); - - it('propagates any other failure reading the named deployment untouched, printing and opening nothing', async () => { - const failure = new LaunchApiError(500, []); - const { command, calls, stdout, stderr, opened } = commandUnderTest({ - deployment: DEPLOYMENT_UID, - fetchFails: failure, - }); - - await expect(command.run()).rejects.toBe(failure); - expect(failure.exitCode).toBe(1); - expect(calls).toEqual(['environments.get', 'deployments.get']); - expect(stdout).toEqual([]); - expect(stderr).toEqual([]); - expect(opened).toEqual([]); - }); - - it('reads no deployment when the environment is missing, even with --deployment', async () => { - const failure = new LaunchApiError(500, []); - const { command, calls } = commandUnderTest({ deployment: DEPLOYMENT_UID, environmentFails: failure }); - - await expect(command.run()).rejects.toBe(failure); - expect(calls).toEqual(['environments.get']); - }); - it('asks which domain to open, as a single-select list with the default preselected, and opens only the one picked', async () => { const { command, stdout, calls, opened, asked } = commandUnderTest({ environment: { uid: 'env1', domains: [{ url: DOMAIN_URL }, { url: 'shop.example.com' }] }, @@ -452,20 +324,6 @@ describe('launch:site:open', () => { expect(opened).toEqual([LIVE_URL]); }); - it('asks nothing for a named deployment even when the environment has domains', async () => { - const { command, calls, opened, asked } = commandUnderTest({ - deployment: DEPLOYMENT_UID, - fetched: { uid: DEPLOYMENT_UID, previewUrl: PREVIEW_URL }, - environment: { uid: 'env1', domains: [{ url: DOMAIN_URL }, { url: OTHER_DOMAIN_URL }] }, - }); - - await command.run(); - - expect(asked).toEqual([]); - expect(opened).toEqual([PREVIEW_URL]); - expect(calls).toEqual(['environments.get', 'deployments.get']); - }); - it.each([[[]], [[{ url: DOMAIN_URL }, { url: OTHER_DOMAIN_URL }]]])( 'exits 2 with the website url not found line and asks nothing when there is no deployment and domains %j', async (domains) => { @@ -483,24 +341,23 @@ describe('launch:site:open', () => { }, ); - it('declares no --domain flag', () => { - expect(Object.keys(SiteOpen.flags)).not.toContain('domain'); + it.each([['domain'], ['deployment']])('declares no --%s flag', (flag) => { + expect(Object.keys(SiteOpen.flags)).not.toContain(flag); }); it('describes the domain prompt in its help', () => { expect(SiteOpen.description).toContain('choose which one to open'); - expect(SiteOpen.description).toContain('--deployment'); + expect(SiteOpen.description).not.toContain('--deployment'); expect(SiteOpen.examples).toContain( '$ url=$(csdx launch:site:open --org --project --env )', ); }); - it('declares org, project and env as required inputs and --deployment as optional', () => { + it('declares org, project and env as its only inputs, all required', () => { expect(SiteOpen.inputs).toEqual({ org: { required: true }, project: { required: true }, env: { required: true }, - deployment: {}, }); }); }); diff --git a/src/commands/launch/site/open.ts b/src/commands/launch/site/open.ts index 510e795e..45df3de5 100644 --- a/src/commands/launch/site/open.ts +++ b/src/commands/launch/site/open.ts @@ -3,18 +3,16 @@ import { UsageError } from '../../../core/errors'; import { writeToStderr } from '../../../core/failure'; import { flagsFor, inputs } from '../../../core/inputs'; import { LaunchCommand } from '../../../core/launch-command'; -import type { GetDeploymentParams } from '../../../deployments/deployments.api'; import { domainChoices, askForDomain } from '../../../site/site.prompt'; import { siteOpeningLine, siteUrlNotFoundLine } from '../../../site/site.presenter'; import type { SiteDomain } from '../../../site/site.url'; -import { previewUrlOf, siteUrlOf } from '../../../site/site.url'; +import { siteUrlOf } from '../../../site/site.url'; import { LaunchApiError } from '../../../transport/errors'; const openInputs = inputs({ org: { required: true }, project: { required: true }, env: { required: true }, - deployment: {}, }); function notFoundAsUsage(error: unknown): never { @@ -28,12 +26,10 @@ function notFoundAsUsage(error: unknown): never { export default class SiteOpen extends LaunchCommand { static description = "Open an environment's live site in your browser, or print its URL when there is no terminal. " + - 'On a terminal, when the environment has more than one domain, you choose which one to open; ' + - "--deployment opens that deployment's preview URL without asking."; + 'On a terminal, when the environment has more than one domain, you choose which one to open.'; static examples = [ '$ csdx launch:site:open --org --project --env ', - '$ csdx launch:site:open --org --project --env --deployment ', '$ url=$(csdx launch:site:open --org --project --env )', ]; @@ -42,26 +38,20 @@ export default class SiteOpen extends LaunchCommand { static flags = flagsFor(openInputs); async run(): Promise { - const { org, project, env, deployment: deploymentUid } = this.resolved; + const { org, project, env } = this.resolved; const scope = { org, project, environment: env }; const outputIsTTY = this.services.outputIsTTY === true; const environment = await this.services.api.environments.get(scope).catch(notFoundAsUsage); - let url: string | undefined; + const defaultUrl = siteUrlOf(await this.services.api.deployments.latest(scope), environment.domains); - if (deploymentUid === undefined) { - url = siteUrlOf(await this.services.api.deployments.latest(scope), environment.domains); - - url = url === undefined ? url : await this.chosenUrl(url, environment.domains, outputIsTTY); - } else { - url = await this.previewUrlOfDeployment({ ...scope, deployment: deploymentUid }); - } - - if (url === undefined) { + if (defaultUrl === undefined) { this.reportMissingUrl(outputIsTTY); this.exit(EXIT_USAGE); } + const url = await this.chosenUrl(defaultUrl, environment.domains, outputIsTTY); + if (!outputIsTTY) { this.ux.print(url); return; @@ -81,12 +71,6 @@ export default class SiteOpen extends LaunchCommand { return choices.length > 1 ? askForDomain(this.ux, choices) : url; } - private async previewUrlOfDeployment(params: GetDeploymentParams): Promise { - const deployment = await this.services.api.deployments.get(params).catch(notFoundAsUsage); - - return previewUrlOf(deployment); - } - private reportMissingUrl(outputIsTTY: boolean): void { const line = siteUrlNotFoundLine(outputIsTTY); diff --git a/src/core/url.ts b/src/core/url.ts new file mode 100644 index 00000000..10b2d0db --- /dev/null +++ b/src/core/url.ts @@ -0,0 +1,12 @@ +const SCHEME = /^[a-z][a-z\d+.-]*:\/\//i; +const TRAILING_SLASHES = /\/+$/; + +export function bareHost(url: string): string { + return url.trim().replace(SCHEME, '').replace(TRAILING_SLASHES, ''); +} + +export function withScheme(url: string): string { + const trimmed = url.trim().replace(TRAILING_SLASHES, ''); + + return SCHEME.test(trimmed) ? trimmed : `https://${trimmed}`; +} diff --git a/src/deployments/deployment.create.test.ts b/src/deployments/deployment.create.test.ts index 52c10de8..b2972183 100644 --- a/src/deployments/deployment.create.test.ts +++ b/src/deployments/deployment.create.test.ts @@ -7,6 +7,7 @@ import type { LaunchError } from '../core/errors'; import { UsageError } from '../core/errors'; import type { ServiceContext } from '../core/service-context'; import type { ApiSurface } from '../resources'; +import type { SiteDomain } from '../site/site.url'; import { LaunchApiError } from '../transport/errors'; import type { CreateDeploymentRequest } from './deployment.create'; import { DeploymentCreator } from './deployment.create'; @@ -20,6 +21,7 @@ const PROJECT = randomBytes(12).toString('hex'); const ENVIRONMENT = randomBytes(12).toString('hex'); const DEPLOYMENT = randomBytes(12).toString('hex'); const SHA = randomBytes(20).toString('hex'); +const LIVE_URL = 'https://my-site.example.test'; const MISSING_DIR = join(tmpdir(), `launch-no-such-dir-${randomBytes(6).toString('hex')}`); const BASE: CreateDeploymentRequest = { @@ -64,6 +66,7 @@ interface Harness { fetched: unknown[]; watched: { scope: unknown; stop?: AbortSignal }[]; announced: Deployment[]; + announcedDomains: (readonly SiteDomain[] | undefined)[]; picked: unknown[]; printed: string[]; } @@ -76,6 +79,7 @@ function harness( createFails?: Error; projectFails?: Error; announceFails?: Error; + domains?: SiteDomain[]; pick?: () => Promise; } = {}, ): Harness { @@ -85,6 +89,7 @@ function harness( const fetched: unknown[] = []; const watched: { scope: unknown; stop?: AbortSignal }[] = []; const announced: Deployment[] = []; + const announcedDomains: (readonly SiteDomain[] | undefined)[] = []; const picked: unknown[] = []; const printed: string[] = []; const signals = new FakeSignals(); @@ -101,6 +106,14 @@ function harness( return { uid: PROJECT, projectType: options.projectType ?? 'GITPROVIDER' }; }, }, + environments: { + get: async (params: unknown) => { + methods.push('environment'); + fetched.push(params); + + return { uid: ENVIRONMENT, domains: options.domains ?? [] }; + }, + }, deployments: { create: async (params: unknown) => { methods.push('create'); @@ -144,16 +157,21 @@ function harness( return options.watch(scope, stop); } - const outcome = options.outcome ?? { kind: 'success', status: 'LIVE', deployment: { uid: DEPLOYMENT, status: 'LIVE' } }; + const outcome = options.outcome ?? { + kind: 'success', + status: 'LIVE', + deployment: { uid: DEPLOYMENT, status: 'LIVE', deploymentUrl: LIVE_URL }, + }; return typeof outcome === 'function' ? outcome() : outcome; }, - announceLive: async (deployment: Deployment) => { + announceLive: async (deployment: Deployment, domains?: readonly SiteDomain[]) => { if (options.announceFails !== undefined) { throw options.announceFails; } announced.push(deployment); + announcedDomains.push(domains); }, }; @@ -167,7 +185,19 @@ function harness( follower, }); - return { creator, signals, created, methods, signedUrlCalls, fetched, watched, announced, picked, printed }; + return { + creator, + signals, + created, + methods, + signedUrlCalls, + fetched, + watched, + announced, + announcedDomains, + picked, + printed, + }; } async function failureOf(run: Promise): Promise { @@ -192,7 +222,7 @@ describe('DeploymentCreator on a GitHub project', () => { expect(watched.map((entry) => entry.scope)).toEqual([ { org: ORG, project: PROJECT, environment: ENVIRONMENT, deployment: DEPLOYMENT }, ]); - expect(announced).toEqual([{ uid: DEPLOYMENT, status: 'LIVE' }]); + expect(announced).toEqual([{ uid: DEPLOYMENT, status: 'LIVE', deploymentUrl: LIVE_URL }]); expect(picked).toEqual([]); expect(signedUrlCalls).toEqual([]); expect(signals.listeners).toEqual([]); @@ -321,6 +351,22 @@ describe('DeploymentCreator waiting for the outcome', () => { expect(announced).toEqual([deployment]); }); + it('reads the environment and announces its domains only when the live deployment carries no url', async () => { + const domains = [{ url: 'www.example.com' }]; + const { creator, fetched, announcedDomains } = harness({ + outcome: { kind: 'success', status: 'LIVE', deployment }, + domains, + }); + + await creator.create(BASE); + + expect(fetched).toEqual([ + { org: ORG, project: PROJECT }, + { org: ORG, project: PROJECT, environment: ENVIRONMENT }, + ]); + expect(announcedDomains).toEqual([domains]); + }); + it.each([['ARCHIVED'], ['SKIPPED'], ['CANCELLED'], ['FAILED']])( 'raises DeploymentUnsuccessfulError naming %s and the log command with the full scope, announcing nothing', async (status) => { diff --git a/src/deployments/deployment.create.ts b/src/deployments/deployment.create.ts index 866f4cfe..5800b30e 100644 --- a/src/deployments/deployment.create.ts +++ b/src/deployments/deployment.create.ts @@ -2,13 +2,14 @@ import { UsageError } from '../core/errors'; import type { InterruptSource } from '../core/interruptible-ux'; import type { ServiceContext } from '../core/service-context'; import { asSentence, messageOf } from '../core/values'; +import type { SiteDomain } from '../site/site.url'; import { NoUploadDirectoryError } from '../uploads/upload.errors'; import { FolderUploader } from '../uploads/upload.folder'; import { DeploymentUnsuccessfulError, DeploymentWaitInterruptedError } from './deployment.errors'; import type { DeploymentScope as FollowedDeployment } from './deployment.follower'; import { DeploymentFollower } from './deployment.follower'; import { PICK_COMMIT } from './deployment.inputs'; -import { firstDeploymentFailureMessage } from './deployment.presenter'; +import { deploymentUrlOf, firstDeploymentFailureMessage } from './deployment.presenter'; import type { DeploymentOutcome, WatchTiming } from './deployment.watcher'; import type { DeploymentScope } from './deployments.api'; import type { Deployment, IdentifiedDeployment } from './types'; @@ -24,7 +25,7 @@ export interface CreateDeploymentRequest extends DeploymentScope { export interface DeploymentFollowing { watch(scope: FollowedDeployment, stop?: AbortSignal): Promise; - announceLive(deployment: Deployment): Promise; + announceLive(deployment: Deployment, domains?: readonly SiteDomain[]): Promise; } export interface DeploymentCreatorDeps { @@ -118,13 +119,24 @@ export class DeploymentCreator { const outcome = await this.waitFor(followed); if (outcome.kind === 'success') { - await this.follower.announceLive(outcome.deployment); + await this.follower.announceLive(outcome.deployment, await this.fallbackDomains(scope, outcome.deployment)); return; } throw this.unsuccessful(followed, { status: outcome.status, timedOut: outcome.kind === 'timed-out' }); } + private async fallbackDomains( + scope: DeploymentScope, + deployment: Deployment, + ): Promise { + if (deploymentUrlOf(deployment) !== undefined) { + return undefined; + } + + return (await this.services.api.environments.get(scope)).domains; + } + private async waitFor(followed: FollowedDeployment): Promise { const stop = new AbortController(); const interrupt = (): void => stop.abort(new DeploymentWaitInterruptedError(this.interruptedMessage(followed))); diff --git a/src/deployments/deployment.follower.test.ts b/src/deployments/deployment.follower.test.ts index 65e208e8..fb1ae754 100644 --- a/src/deployments/deployment.follower.test.ts +++ b/src/deployments/deployment.follower.test.ts @@ -117,17 +117,17 @@ describe('DeploymentFollower.announceLive', () => { expect(timeline).toEqual(['print Deployment URL: https://custom.example.test']); }); - it('announces the preview url, with a scheme, when the deployment url is empty and a domain also exists', async () => { + it('never announces the preview url, a screenshot image, and uses the domain when the deployment url is empty', async () => { const { follower, timeline } = harness(); - await follower.announceLive({ uid: 'd1', deploymentUrl: '', previewUrl: 'preview.example.test' }, [ + await follower.announceLive({ uid: 'd1', deploymentUrl: '', previewUrl: 'images.example.test/preview.png' }, [ { url: 'https://www.example.com' }, ]); expect(timeline).toEqual([ - 'print Deployment URL: https://preview.example.test', + 'print Deployment URL: https://www.example.com', `sleep ${SITE_OPEN_DELAY_MS}`, - 'open https://preview.example.test', + 'open https://www.example.com', ]); }); diff --git a/src/deployments/deployment.presenter.test.ts b/src/deployments/deployment.presenter.test.ts index 771b4ced..9aea2ef2 100644 --- a/src/deployments/deployment.presenter.test.ts +++ b/src/deployments/deployment.presenter.test.ts @@ -37,10 +37,13 @@ describe('deployment presenter', () => { it('leaves an absolute deployment url alone and gives a bare host a scheme', () => { expect(deploymentUrlOf({ uid: UID, deploymentUrl: 'https://site.example.test' })).toBe('https://site.example.test'); expect(deploymentUrlOf({ uid: UID, deploymentUrl: 'site.example.test' })).toBe('https://site.example.test'); + expect(deploymentUrlOf({ uid: UID, deploymentUrl: 'http-api-ab12.contentstackapps.com' })).toBe( + 'https://http-api-ab12.contentstackapps.com', + ); }); - it('falls back to the preview url and reports nothing when neither is usable', () => { - expect(deploymentUrlOf({ uid: UID, previewUrl: 'preview.example.test' })).toBe('https://preview.example.test'); + it('reports nothing without a deployment url, never the preview url, which is a screenshot image', () => { + expect(deploymentUrlOf({ uid: UID, previewUrl: 'images.example.test/preview.png' })).toBeUndefined(); expect(deploymentUrlOf({ uid: UID })).toBeUndefined(); expect(deploymentUrlOf({ uid: UID, deploymentUrl: '', previewUrl: '' })).toBeUndefined(); }); @@ -366,7 +369,7 @@ describe('deployment table presenter', () => { } }); - it('falls back to a dash for every absent field and prefers the deployment url over the preview url', () => { + it('falls back to a dash for every absent field and shows the deployment url, never the preview url', () => { const rows = deploymentDetailRows({ uid: UID }, true, SCOPE); for (const field of ['BRANCH', 'COMMIT', 'COMMIT MESSAGE', 'URL', 'CREATED AT', 'UPDATED AT']) { diff --git a/src/deployments/deployment.presenter.ts b/src/deployments/deployment.presenter.ts index 1171e358..40772b1f 100644 --- a/src/deployments/deployment.presenter.ts +++ b/src/deployments/deployment.presenter.ts @@ -2,6 +2,7 @@ import { stripVTControlCharacters } from 'node:util'; import type { TableColumn } from '../core/render'; import { styled } from '../core/style'; +import { withScheme } from '../core/url'; import { isAbsent, withoutControlCharacters } from '../core/values'; import type { DeploymentScope } from './deployments.api'; import type { Deployment, DeploymentLog } from './types'; @@ -20,13 +21,9 @@ const COLOUR_SEQUENCE = /[\u001b\u009b]\[[0-9;:]*m/g; /* eslint-enable no-control-regex */ export function deploymentUrlOf(deployment: Deployment): string | undefined { - const url = deployment.deploymentUrl || deployment.previewUrl; + const url = deployment.deploymentUrl?.trim(); - if (!url) { - return undefined; - } - - return url.startsWith('http') ? url : `https://${url}`; + return url ? withScheme(url) : undefined; } export function deploymentUrlLine(url: string, outputIsTTY: boolean): string { diff --git a/src/deployments/types.ts b/src/deployments/types.ts index 0c3564c2..760e022c 100644 --- a/src/deployments/types.ts +++ b/src/deployments/types.ts @@ -24,6 +24,7 @@ export interface Deployment { status?: string; deploymentNumber?: number; deploymentUrl?: string; + /** The screenshot image taken once the deployment is live, never a URL for the site itself. */ previewUrl?: string; gitBranch?: string; commitHash?: string; diff --git a/src/site/site.prompt.ts b/src/site/site.prompt.ts index 6b314719..ebf69cfd 100644 --- a/src/site/site.prompt.ts +++ b/src/site/site.prompt.ts @@ -1,22 +1,10 @@ import type { Choice, UxLike } from '../core/prompt'; import { askOption } from '../core/prompt'; +import { bareHost, withScheme } from '../core/url'; import type { SiteDomain } from './site.url'; export const DOMAIN_QUESTION = 'Which domain do you want to open?'; -const SCHEME = /^[a-z][a-z\d+.-]*:\/\//i; -const TRAILING_SLASHES = /\/+$/; - -function bareHost(url: string): string { - return url.trim().replace(SCHEME, '').replace(TRAILING_SLASHES, ''); -} - -function withScheme(url: string): string { - const trimmed = url.trim().replace(TRAILING_SLASHES, ''); - - return SCHEME.test(trimmed) ? trimmed : `https://${trimmed}`; -} - export function domainChoices(defaultUrl: string, domains: readonly SiteDomain[] = []): Choice[] { const defaultHost = bareHost(defaultUrl); const seen = new Set([defaultHost.toLowerCase()]); diff --git a/src/site/site.url.test.ts b/src/site/site.url.test.ts index 907b096d..d9c936b5 100644 --- a/src/site/site.url.test.ts +++ b/src/site/site.url.test.ts @@ -1,4 +1,4 @@ -import { previewUrlOf, siteUrlOf } from './site.url'; +import { siteUrlOf } from './site.url'; describe('siteUrlOf', () => { it("chooses the deployment's own url over any domain", () => { @@ -17,6 +17,12 @@ describe('siteUrlOf', () => { expect(url).toBe('https://www.example.com'); }); + it('treats a whitespace-only deployment url or domain as absent', () => { + const url = siteUrlOf({ uid: 'd1', deploymentUrl: ' ' }, [{ url: ' ' }, { url: 'www.example.com' }]); + + expect(url).toBe('https://www.example.com'); + }); + it('adds https:// to a deployment url without a scheme and leaves an http:// one alone', () => { const bare = siteUrlOf({ uid: 'd1', deploymentUrl: 'my-site.contentstackapps.com' }); const plain = siteUrlOf({ uid: 'd1', deploymentUrl: 'http://my-site.example.test' }); @@ -25,10 +31,10 @@ describe('siteUrlOf', () => { expect(plain).toBe('http://my-site.example.test'); }); - it('uses the preview url when the deployment has no deployment url, before any domain', () => { - const url = siteUrlOf({ uid: 'd1', previewUrl: 'preview.example.test' }, [{ url: 'https://www.example.com' }]); + it('skips the preview url, a screenshot image, and falls back to a domain when the deployment has no deployment url', () => { + const url = siteUrlOf({ uid: 'd1', previewUrl: 'images.example.test/preview.png' }, [{ url: 'https://www.example.com' }]); - expect(url).toBe('https://preview.example.test'); + expect(url).toBe('https://www.example.com'); }); it('returns nothing without a deployment, even when the environment has domains', () => { @@ -49,19 +55,3 @@ describe('siteUrlOf', () => { expect(url).toBeUndefined(); }); }); - -describe('previewUrlOf', () => { - it('reads the preview url alone, adding the scheme, and ignores the deployment url', () => { - const bare = previewUrlOf({ uid: 'd1', deploymentUrl: 'https://my-site.contentstackapps.com', previewUrl: 'preview.example.test' }); - const plain = previewUrlOf({ uid: 'd1', previewUrl: 'http://preview.example.test' }); - - expect(bare).toBe('https://preview.example.test'); - expect(plain).toBe('http://preview.example.test'); - }); - - it.each([[undefined], [''], [' ']])('returns nothing for a preview url of %p even with a deployment url', (previewUrl) => { - const url = previewUrlOf({ uid: 'd1', deploymentUrl: 'https://my-site.contentstackapps.com', previewUrl }); - - expect(url).toBeUndefined(); - }); -}); diff --git a/src/site/site.url.ts b/src/site/site.url.ts index 2694ae87..2211f7a0 100644 --- a/src/site/site.url.ts +++ b/src/site/site.url.ts @@ -1,3 +1,4 @@ +import { withScheme } from '../core/url'; import { deploymentUrlOf } from '../deployments/deployment.presenter'; import type { Deployment } from '../deployments/types'; @@ -16,13 +17,7 @@ export function siteUrlOf(deployment: Deployment | undefined, domains: readonly return fromDeployment; } - const domain = domains.find((entry) => Boolean(entry.url)); + const domainUrl = domains.map((entry) => entry.url?.trim()).find(Boolean); - return domain === undefined ? undefined : deploymentUrlOf({ uid: deployment.uid, deploymentUrl: domain.url }); -} - -export function previewUrlOf(deployment: Deployment): string | undefined { - const previewUrl = deployment.previewUrl?.trim(); - - return previewUrl ? deploymentUrlOf({ uid: deployment.uid, previewUrl }) : undefined; + return domainUrl ? withScheme(domainUrl) : undefined; } diff --git a/test/integration/site-open.test.ts b/test/integration/site-open.test.ts index 7dc42419..0469c286 100644 --- a/test/integration/site-open.test.ts +++ b/test/integration/site-open.test.ts @@ -30,8 +30,6 @@ const ENVIRONMENTS_PATH = `/manage/projects/${PROJECT_UID}/environments`; const ENV_PATH = `${ENVIRONMENTS_PATH}/${ENV_UID}`; const DEPLOYMENTS_PATH = `${ENV_PATH}/deployments`; const DEPLOYMENT_UID = randomBytes(12).toString('hex'); -const DEPLOYMENT_PATH = `${DEPLOYMENTS_PATH}/${DEPLOYMENT_UID}`; -const PREVIEW_URL = 'https://preview-1a2b.contentstackapps.com'; const LIVE_URL = 'https://sample-project.contentstackapps.com'; const DOMAIN_URL = 'https://production-sample-project.contentstackapps.com'; const QUESTION = 'Which domain do you want to open?'; @@ -250,39 +248,6 @@ describe('integration: launch:site:open on the wire', () => { expect(nock.pendingMocks()).toEqual([]); }); - it("opens the named deployment's preview url, not its deployment url, reading the environment first and never the list", async () => { - withTerminal(true); - const ownership = projectOwnership(LAUNCH_HUB_URL, ORG_UID, PROJECT_UID); - const environment = nock(LAUNCH_HUB_URL) - .matchHeader('x-organization-uid', ORG_UID) - .matchHeader('x-project-uid', PROJECT_UID) - .get(ENV_PATH) - .reply(200, () => { - sent.push(`GET ${ENV_PATH}`); - return getFixture; - }); - const deployment = nock(LAUNCH_HUB_URL) - .matchHeader('x-organization-uid', ORG_UID) - .matchHeader('x-project-uid', PROJECT_UID) - .get(DEPLOYMENT_PATH) - .reply(200, () => { - sent.push(`GET ${DEPLOYMENT_PATH}`); - return { deployment: { uid: DEPLOYMENT_UID, deploymentUrl: LIVE_URL, previewUrl: PREVIEW_URL } }; - }); - const list = nock(LAUNCH_HUB_URL).get(DEPLOYMENTS_PATH).query(true).reply(200, { deployments: [] }); - - const { error, stdout } = await runOpen(openArgs('--env', ENV_UID, '--deployment', DEPLOYMENT_UID)); - - expect(error).toBeUndefined(); - expect(stripVTControlCharacters(stdout)).toContain(`✔ Opening ${PREVIEW_URL} in your browser...`); - expect(open).toHaveBeenCalledTimes(1); - expect(open).toHaveBeenCalledWith(PREVIEW_URL); - expect(sent).toEqual([`GET ${ENV_PATH}`, `GET ${DEPLOYMENT_PATH}`]); - expect([ownership, environment, deployment].map((scope) => scope.isDone())).toEqual([true, true, true]); - expect(list.isDone()).toBe(false); - expect(onWire).toEqual([]); - }); - it('asks which domain to open as a single-select list with the default first and preselected, and opens only the one picked', async () => { withTerminal(true); const prompts = answerPrompts({ [QUESTION]: DOMAIN_URL }); @@ -322,12 +287,15 @@ describe('integration: launch:site:open on the wire', () => { expect(onWire).toEqual([]); }); - it('has no --domain flag: it is refused as unknown with exit 2 before any request or browser', async () => { + it.each([ + ['--domain', 'www.example.com'], + ['--deployment', DEPLOYMENT_UID], + ])('has no %s flag: it is refused as unknown with exit 2 before any request or browser', async (flag, value) => { withTerminal(true); forbidPrompts(); reads([{ uid: 'd1', deploymentUrl: LIVE_URL }]); - const { error } = await runOpen(openArgs('--env', ENV_UID, '--domain', 'www.example.com')); + const { error } = await runOpen(openArgs('--env', ENV_UID, flag, value)); expect(error?.oclif?.exit).toBe(2); expect(error?.message).toContain('Nonexistent flag'); @@ -366,31 +334,6 @@ describe('integration: launch:site:open on the wire', () => { expect(stripVTControlCharacters(stdout)).toContain(`✔ Opening ${LIVE_URL} in your browser...`); }); - it('asks nothing for a named deployment on a terminal and opens its preview url', async () => { - withTerminal(true); - const prompts = forbidPrompts(); - projectOwnership(LAUNCH_HUB_URL, ORG_UID, PROJECT_UID); - nock(LAUNCH_HUB_URL) - .get(ENV_PATH) - .reply(200, () => { - sent.push(`GET ${ENV_PATH}`); - return getFixture; - }); - nock(LAUNCH_HUB_URL) - .get(DEPLOYMENT_PATH) - .reply(200, () => { - sent.push(`GET ${DEPLOYMENT_PATH}`); - return { deployment: { uid: DEPLOYMENT_UID, deploymentUrl: LIVE_URL, previewUrl: PREVIEW_URL } }; - }); - - const { error } = await runOpen(openArgs('--env', ENV_UID, '--deployment', DEPLOYMENT_UID)); - - expect(error).toBeUndefined(); - expect(prompts.messages).toEqual([]); - expect(open).toHaveBeenCalledWith(PREVIEW_URL); - expect(sent).toEqual([`GET ${ENV_PATH}`, `GET ${DEPLOYMENT_PATH}`]); - }); - it('exits 2 with the yellow not-found line and asks nothing when there is no deployment but two domains', async () => { withTerminal(true); const prompts = forbidPrompts(); @@ -416,35 +359,6 @@ describe('integration: launch:site:open on the wire', () => { expect(open).not.toHaveBeenCalled(); }); - it('refuses a deployment that is not a 24-hex uid before any request reaches the wire', async () => { - withTerminal(true); - - const { error, stdout } = await runOpen(openArgs('--env', ENV_UID, '--deployment', 'not-a-uid')); - - expect(error?.oclif?.exit).toBe(2); - expect(error?.message).toContain('--deployment'); - expect(stdout).toBe(''); - expect(onWire).toEqual([]); - expect(open).not.toHaveBeenCalled(); - }); - - it('exits 2 with the not-found wording for a deployment the environment does not have', async () => { - withTerminal(true); - projectOwnership(LAUNCH_HUB_URL, ORG_UID, PROJECT_UID); - nock(LAUNCH_HUB_URL).get(ENV_PATH).reply(200, getFixture); - const deployment = nock(LAUNCH_HUB_URL) - .get(DEPLOYMENT_PATH) - .reply(404, { errors: [{ code: 'launch.DEPLOYMENT.NOT_FOUND', message: 'Deployment not found' }] }); - - const { error, stdout } = await runOpen(openArgs('--env', ENV_UID, '--deployment', DEPLOYMENT_UID)); - - expect(error?.oclif?.exit).toBe(2); - expect(error?.message).toBe('No deployment found with that UID.'); - expect(deployment.isDone()).toBe(true); - expect(stdout).toBe(''); - expect(open).not.toHaveBeenCalled(); - }); - it('refuses a project that belongs to another organization before reading the environment', async () => { withTerminal(true); const ownership = projectOwnership(LAUNCH_HUB_URL, OTHER_ORG_UID, PROJECT_UID); From 2467b7bc3415c995e7245d682ad54741f909b89a Mon Sep 17 00:00:00 2001 From: Harshi-Shah-CS Date: Thu, 19 Nov 2026 16:49:36 +0530 Subject: [PATCH 2/2] fix(site): keep the post-LIVE domain lookup best-effort and open only web URLs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Review follow-ups on #297: - deployments:create reads the environment's domains only when the LIVE deployment carries no url; a failure of that read no longer fails the command, it just announces no url. A live deployment is never reported as a failed create. - core/url webUrlOf replaces withScheme. It keeps http(s) URLs as given and gives a bare host https://, keeps a path or query exactly (no trailing slash stripped from "https://host/app/" or "?next=/"), drops trailing slashes only from a bare host, and returns nothing for an empty, slash-only, scheme-only or non-web (file://, ftp://) value. The deployment url, the domain fallback and the domain prompt all use it. - .talismanrc: refreshed the checksum of deployment.create.test.ts (false positive on "key" in existing test code). #claude_code# 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- .talismanrc | 2 +- src/core/url.test.ts | 35 +++++++++++++++++++++++ src/core/url.ts | 15 ++++++++-- src/deployments/deployment.create.test.ts | 17 +++++++++++ src/deployments/deployment.create.ts | 6 +++- src/deployments/deployment.presenter.ts | 6 ++-- src/site/site.prompt.ts | 7 +++-- src/site/site.url.ts | 6 ++-- 8 files changed, 78 insertions(+), 16 deletions(-) create mode 100644 src/core/url.test.ts diff --git a/.talismanrc b/.talismanrc index d427f448..ce2a0e53 100644 --- a/.talismanrc +++ b/.talismanrc @@ -443,7 +443,7 @@ fileignoreconfig: - filename: src/commands/launch/deployments/create.test.ts checksum: 9f5c60d5f6367250aa8f932acf0e5a7ece933f3fc1ca173fa908e80724d1d579 - filename: src/deployments/deployment.create.test.ts - checksum: b880edba719b71f388ae04bab474d85e1a81af6346fb9a96a25352d50805366f + checksum: aceeba27a10ffe7bcf7f78af4c88cb19e017a46c898f02a07e314f949775841a - filename: test/integration/deployments-rollback.test.ts checksum: 98a9f7001eb5bf01e0e2f46677eeea61ad5d38c595d17ef27d366c93e603f0d5 - filename: test/integration/deployments-cancel.test.ts diff --git a/src/core/url.test.ts b/src/core/url.test.ts new file mode 100644 index 00000000..11a0faa0 --- /dev/null +++ b/src/core/url.test.ts @@ -0,0 +1,35 @@ +import { bareHost, webUrlOf } from './url'; + +describe('webUrlOf', () => { + it.each([ + ['http://plain.example.test', 'http://plain.example.test'], + [' https://secure.example.test ', 'https://secure.example.test'], + ['bare.example.test', 'https://bare.example.test'], + ['http-api-ab12.contentstackapps.com', 'https://http-api-ab12.contentstackapps.com'], + ])('keeps a web scheme as given and gives a bare host https://: %p', (raw, url) => { + expect(webUrlOf(raw)).toBe(url); + }); + + it.each([ + ['https://host.example.test/app/', 'https://host.example.test/app/'], + ['https://host.example.test/?next=/', 'https://host.example.test/?next=/'], + ['host.example.test/app/', 'https://host.example.test/app/'], + ['https://host.example.test//', 'https://host.example.test'], + ['host.example.test/', 'https://host.example.test'], + ])('keeps a path or query exactly and drops trailing slashes only from a bare host: %p', (raw, url) => { + expect(webUrlOf(raw)).toBe(url); + }); + + it.each([[undefined], [''], [' '], ['/'], ['//'], ['https://'], ['file:///etc/hosts'], ['ftp://files.example.test']])( + 'gives no url for %p, which has no host or is not a web url', + (raw) => { + expect(webUrlOf(raw)).toBeUndefined(); + }, + ); +}); + +describe('bareHost', () => { + it('drops the scheme and trailing slashes', () => { + expect(bareHost(' HTTPS://www.example.com// ')).toBe('www.example.com'); + }); +}); diff --git a/src/core/url.ts b/src/core/url.ts index 10b2d0db..b5460652 100644 --- a/src/core/url.ts +++ b/src/core/url.ts @@ -1,12 +1,21 @@ const SCHEME = /^[a-z][a-z\d+.-]*:\/\//i; +const WEB_SCHEME = /^https?:\/\//i; const TRAILING_SLASHES = /\/+$/; export function bareHost(url: string): string { return url.trim().replace(SCHEME, '').replace(TRAILING_SLASHES, ''); } -export function withScheme(url: string): string { - const trimmed = url.trim().replace(TRAILING_SLASHES, ''); +export function webUrlOf(raw: string | undefined): string | undefined { + const url = raw?.trim() ?? ''; + const host = bareHost(url); + const scheme = SCHEME.exec(url)?.[0]; - return SCHEME.test(trimmed) ? trimmed : `https://${trimmed}`; + if (host === '' || (scheme !== undefined && !WEB_SCHEME.test(scheme))) { + return undefined; + } + + const rest = host.includes('/') ? url.slice(scheme?.length ?? 0) : host; + + return `${scheme ?? 'https://'}${rest}`; } diff --git a/src/deployments/deployment.create.test.ts b/src/deployments/deployment.create.test.ts index b2972183..c00a4f33 100644 --- a/src/deployments/deployment.create.test.ts +++ b/src/deployments/deployment.create.test.ts @@ -80,6 +80,7 @@ function harness( projectFails?: Error; announceFails?: Error; domains?: SiteDomain[]; + environmentFails?: Error; pick?: () => Promise; } = {}, ): Harness { @@ -111,6 +112,10 @@ function harness( methods.push('environment'); fetched.push(params); + if (options.environmentFails !== undefined) { + throw options.environmentFails; + } + return { uid: ENVIRONMENT, domains: options.domains ?? [] }; }, }, @@ -367,6 +372,18 @@ describe('DeploymentCreator waiting for the outcome', () => { expect(announcedDomains).toEqual([domains]); }); + it('still succeeds, announcing without domains, when that environment read fails after the deployment is live', async () => { + const { creator, announced, announcedDomains } = harness({ + outcome: { kind: 'success', status: 'LIVE', deployment }, + environmentFails: new LaunchApiError(503, [{ code: 'x', message: 'Service unavailable.' }]), + }); + + await creator.create(BASE); + + expect(announced).toEqual([deployment]); + expect(announcedDomains).toEqual([undefined]); + }); + it.each([['ARCHIVED'], ['SKIPPED'], ['CANCELLED'], ['FAILED']])( 'raises DeploymentUnsuccessfulError naming %s and the log command with the full scope, announcing nothing', async (status) => { diff --git a/src/deployments/deployment.create.ts b/src/deployments/deployment.create.ts index 5800b30e..d38338e4 100644 --- a/src/deployments/deployment.create.ts +++ b/src/deployments/deployment.create.ts @@ -134,7 +134,11 @@ export class DeploymentCreator { return undefined; } - return (await this.services.api.environments.get(scope)).domains; + try { + return (await this.services.api.environments.get(scope)).domains; + } catch { + return undefined; + } } private async waitFor(followed: FollowedDeployment): Promise { diff --git a/src/deployments/deployment.presenter.ts b/src/deployments/deployment.presenter.ts index 40772b1f..995eb0a9 100644 --- a/src/deployments/deployment.presenter.ts +++ b/src/deployments/deployment.presenter.ts @@ -2,7 +2,7 @@ import { stripVTControlCharacters } from 'node:util'; import type { TableColumn } from '../core/render'; import { styled } from '../core/style'; -import { withScheme } from '../core/url'; +import { webUrlOf } from '../core/url'; import { isAbsent, withoutControlCharacters } from '../core/values'; import type { DeploymentScope } from './deployments.api'; import type { Deployment, DeploymentLog } from './types'; @@ -21,9 +21,7 @@ const COLOUR_SEQUENCE = /[\u001b\u009b]\[[0-9;:]*m/g; /* eslint-enable no-control-regex */ export function deploymentUrlOf(deployment: Deployment): string | undefined { - const url = deployment.deploymentUrl?.trim(); - - return url ? withScheme(url) : undefined; + return webUrlOf(deployment.deploymentUrl); } export function deploymentUrlLine(url: string, outputIsTTY: boolean): string { diff --git a/src/site/site.prompt.ts b/src/site/site.prompt.ts index ebf69cfd..5b61457f 100644 --- a/src/site/site.prompt.ts +++ b/src/site/site.prompt.ts @@ -1,6 +1,6 @@ import type { Choice, UxLike } from '../core/prompt'; import { askOption } from '../core/prompt'; -import { bareHost, withScheme } from '../core/url'; +import { bareHost, webUrlOf } from '../core/url'; import type { SiteDomain } from './site.url'; export const DOMAIN_QUESTION = 'Which domain do you want to open?'; @@ -12,10 +12,11 @@ export function domainChoices(defaultUrl: string, domains: readonly SiteDomain[] for (const domain of domains) { const host = bareHost(domain.url ?? ''); + const url = webUrlOf(domain.url); - if (host !== '' && !seen.has(host.toLowerCase())) { + if (url !== undefined && !seen.has(host.toLowerCase())) { seen.add(host.toLowerCase()); - choices.push({ name: host, value: withScheme(domain.url as string) }); + choices.push({ name: host, value: url }); } } diff --git a/src/site/site.url.ts b/src/site/site.url.ts index 2211f7a0..7d9fbe29 100644 --- a/src/site/site.url.ts +++ b/src/site/site.url.ts @@ -1,4 +1,4 @@ -import { withScheme } from '../core/url'; +import { webUrlOf } from '../core/url'; import { deploymentUrlOf } from '../deployments/deployment.presenter'; import type { Deployment } from '../deployments/types'; @@ -17,7 +17,5 @@ export function siteUrlOf(deployment: Deployment | undefined, domains: readonly return fromDeployment; } - const domainUrl = domains.map((entry) => entry.url?.trim()).find(Boolean); - - return domainUrl ? withScheme(domainUrl) : undefined; + return domains.map((entry) => webUrlOf(entry.url)).find((url) => url !== undefined); }