From 7d47a05a00ea58b3a85803c7fc3e161d4a05dbce Mon Sep 17 00:00:00 2001 From: Evan Purkhiser Date: Wed, 18 Nov 2026 12:49:03 -0500 Subject: [PATCH] feat(cli): Show account context in handoff setup --- packages/cli/src/commands/cli/setup.ts | 48 ++++++++++++++------ packages/cli/test/commands/cli/setup.test.ts | 14 ++++-- 2 files changed, 43 insertions(+), 19 deletions(-) diff --git a/packages/cli/src/commands/cli/setup.ts b/packages/cli/src/commands/cli/setup.ts index ec608732b..07f537989 100644 --- a/packages/cli/src/commands/cli/setup.ts +++ b/packages/cli/src/commands/cli/setup.ts @@ -35,7 +35,11 @@ import { warmCachesAfterLogin, } from "../../lib/completion-warmup.js"; import { CLI_VERSION } from "../../lib/constants.js"; -import { getAuthConfig, hasStoredAuthCredentials } from "../../lib/db/auth.js"; +import { + getAuthConfig, + getStoredAuthHost, + hasStoredAuthCredentials, +} from "../../lib/db/auth.js"; import { getOnOffPreference, setOnOffPreference, @@ -52,10 +56,12 @@ import { detectAgentFromProcessTree, } from "../../lib/detect-agent.js"; import { getEnv } from "../../lib/env.js"; +import { formatUserIdentity } from "../../lib/formatters/human.js"; import { CommandOutput } from "../../lib/formatters/output.js"; import { runInteractiveLogin } from "../../lib/interactive-login.js"; import { interactivePromptsAllowed } from "../../lib/interactive-prompts.js"; import { logger } from "../../lib/logger.js"; +import { normalizeHttpOrigin } from "../../lib/sentry-urls.js"; import { bootstrapAuthentication } from "../../lib/setup-code.js"; import { addToFpath, @@ -798,18 +804,15 @@ export const setupCommand = buildCommand({ const setupCode = SENTRY_SETUP_CODE?.trim(); const shouldUseSetupCode = !handoffCode && Boolean(setupCode) && !hasStoredAuthCredentials(); - let afterSetupInstructions: string | undefined; - - if (handoffCode) { - const handoff = await completeAgentHandoff(handoffCode); - afterSetupInstructions = handoff.flowInstructions; - emit("Authenticated with Sentry using the agent handoff."); - } else if (shouldUseSetupCode) { - await bootstrapAuthentication(); - emit("Authenticated with Sentry using the setup code."); - } + const handoff = handoffCode + ? await completeAgentHandoff(handoffCode) + : undefined; + + const bootstrap = shouldUseSetupCode + ? await bootstrapAuthentication() + : undefined; - if ((handoffCode || shouldUseSetupCode) && !flags["no-completions"]) { + if ((handoff || bootstrap) && !flags["no-completions"]) { await warmCachesAfterLogin(); } @@ -831,8 +834,25 @@ export const setupCommand = buildCommand({ printWelcomeMessage(emit, CLI_VERSION, binaryPath); } - if (afterSetupInstructions) { - emit(afterSetupInstructions); + if (handoff || bootstrap) { + const user = bootstrap?.user; + const identity = + handoff?.userEmail || + (user && (user.email || formatUserIdentity(user))); + const account = identity ? `as ${identity}` : "with Sentry"; + const organization = handoff + ? ` for ${handoff.organizationName} (slug: ${handoff.organizationSlug})` + : ""; + const host = handoff + ? normalizeHttpOrigin(handoff.sentryUrl) + : getStoredAuthHost(); + emit( + `Authenticated ${account}${organization}${host ? ` on ${host}` : ""}.`, + ); + } + + if (handoff?.flowInstructions) { + emit(handoff.flowInstructions); } yield new CommandOutput({ diff --git a/packages/cli/test/commands/cli/setup.test.ts b/packages/cli/test/commands/cli/setup.test.ts index 1f937631e..e3004a0e7 100644 --- a/packages/cli/test/commands/cli/setup.test.ts +++ b/packages/cli/test/commands/cli/setup.test.ts @@ -1056,6 +1056,8 @@ describe("sentry cli setup", () => { email: "new@example.com", }, }; + const handoffSummary = + "Authenticated as new@example.com for New Organization (slug: new-org) on https://sentry.io."; function stubHandoffFetch({ flowInstructions = "Continue the requested setup.", @@ -1101,6 +1103,7 @@ describe("sentry cli setup", () => { refresh_token: "refresh-token", expires_in: 3600, token_type: "bearer", + user: { id: "setup-user", name: null, email: "setup@example.com" }, }), ); vi.stubGlobal("fetch", fetch); @@ -1115,7 +1118,7 @@ describe("sentry cli setup", () => { completions ? 1 : 0, ); expect(fixture.getOutput()).toContain( - "Authenticated with Sentry using the setup code", + "Authenticated as setup@example.com on https://sentry.io.", ); expect(fixture.getOutput()).not.toContain(code); expect(interactiveLogin.runInteractiveLogin).not.toHaveBeenCalled(); @@ -1156,7 +1159,9 @@ describe("sentry cli setup", () => { expect(warmCachesAfterLogin).toHaveBeenCalledTimes( completions ? 1 : 0, ); - expect(fixture.getOutput()).toContain(instructions); + expect(fixture.getOutput()).toContain( + `${handoffSummary}\n${instructions}`, + ); expect(fixture.getOutput()).not.toContain(handoffCode); expect(fixture.getOutput()).not.toContain(handoffSetupCode); expect(interactiveLogin.runInteractiveLogin).not.toHaveBeenCalled(); @@ -1179,9 +1184,7 @@ describe("sentry cli setup", () => { await fixture.run(); const { messages } = JSON.parse(fixture.getOutput()); - expect(messages).toContain( - "Authenticated with Sentry using the agent handoff.", - ); + expect(messages).toContain(handoffSummary); expect(messages).not.toContain(""); } finally { vi.unstubAllGlobals(); @@ -1250,6 +1253,7 @@ describe("sentry cli setup", () => { expect(fixture.context.process.exitCode).toBeGreaterThan(0); expect(fixture.getOutput()).not.toContain(instructions); + expect(fixture.getOutput()).not.toContain(handoffSummary); expect(getAuthConfig()).toBeUndefined(); } finally { vi.unstubAllGlobals();