Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 34 additions & 14 deletions packages/cli/src/commands/cli/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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();
}

Expand All @@ -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({
Expand Down
14 changes: 9 additions & 5 deletions packages/cli/test/commands/cli/setup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Expand Down Expand Up @@ -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);
Expand All @@ -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();
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand Down Expand Up @@ -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();
Expand Down
Loading