Skip to content
This repository was archived by the owner on Oct 23, 2026. It is now read-only.
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
3 changes: 3 additions & 0 deletions packages/cli/src/lib/init/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ export const INIT_REQUEST_CONFLICT_CODE = "init_request_conflict" as const;

export const SENTRY_DOCS_URL = "https://docs.sentry.io/platforms/";

/** Command that installs the Sentry coding-agent plugin (skills) into detected agents. */
export const SENTRY_AGENT_INSTALL_COMMAND = "npx @sentry/ai install";

export const MAX_FILE_BYTES = 262_144; // 256KB per file
export const MAX_OUTPUT_BYTES = 65_536; // 64KB stdout/stderr truncation
export const DEFAULT_COMMAND_TIMEOUT_MS = 120_000; // 2 minutes
Expand Down
156 changes: 144 additions & 12 deletions packages/cli/src/lib/init/formatters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,107 @@
* `ui.log.*` because errors are short enough to live as plain text.
*/

import { basename } from "node:path";
import { terminalLink } from "../formatters/colors.js";
import { stripAnsi } from "../formatters/plain-detect.js";
import {
buildEventSearchUrl,
buildProjectIssuesUrl,
parseOrgProjectFromSettingsUrl,
} from "../sentry-urls.js";
import { featureLabel, sortFeatures } from "./clack-utils.js";
import {
EXIT_DEPENDENCY_INSTALL_FAILED,
EXIT_PLATFORM_NOT_DETECTED,
EXIT_VERIFICATION_FAILED,
SENTRY_AGENT_INSTALL_COMMAND,
} from "./constants.js";
import type { WizardOutput, WorkflowRunResult } from "./types.js";
import type { WizardSummary, WizardUI } from "./ui/types.js";
import type {
SentryProjectIdentity,
WizardOutput,
WorkflowRunResult,
} from "./types.js";
import type { WizardCompletion, WizardSummary, WizardUI } from "./ui/types.js";
import type { VerifyResult } from "./verify-setup.js";

/** Package managers whose dev script we can name for the "start your app" step. */
const KNOWN_PACKAGE_MANAGERS = ["pnpm", "yarn", "bun", "npm"] as const;

/** A genuine Sentry project id is all digits — anything else (e.g. the
* "(dry-run)" stub) must not be used to scope the Issues stream. */
const NUMERIC_PROJECT_ID = /^\d+$/;

/** Best-effort human name for the project, for the completion header. */
function deriveProjectName(output: WizardOutput): string {
if (output.projectSlug) {
return output.projectSlug;
}
if (output.projectDir) {
const base = basename(output.projectDir);
if (base && base !== "." && base !== "/") {
return base;
}
}
return "your project";
}

/** Derive a "start your app" command from the install command's package manager. */
function deriveStartCommand(
commands: string[] | undefined
): string | undefined {
const first = commands?.[0]?.trim();
if (!first) {
return;
}
const pm = KNOWN_PACKAGE_MANAGERS.find((p) => first.startsWith(`${p} `));
if (!pm) {
return;
}
return pm === "npm" ? "npm run dev" : `${pm} dev`;
}

/** Assemble the structured data the interactive completion screen renders. */
function buildCompletion(
output: WizardOutput,
verify: VerifyResult | undefined,
featureBlurbs: { label: string; blurb: string }[]
): WizardCompletion {
// The server may not hand the org slug back directly (older deploys), so
// fall back to recovering it from the settings URL it did send. This keeps
// the Issues link working regardless of server version.
const parsed = output.sentryProjectUrl
? parseOrgProjectFromSettingsUrl(output.sentryProjectUrl)
: {};
const orgSlug = output.orgSlug ?? parsed.orgSlug;
// Only a genuine numeric project id can scope the Issues stream. Guard against
// non-numeric placeholders — e.g. the "(dry-run)" sentinel from the dry-run
// project stub — leaking into the URL as `?project=(dry-run)`. A missing id
// just yields the org-wide Issues stream.
const projectId = NUMERIC_PROJECT_ID.test(output.projectId ?? "")
? output.projectId
: undefined;
// Prefer the project-scoped Issues stream (where errors land); fall back to
// whatever project URL the server sent if we couldn't resolve the org.
const issuesUrl = orgSlug
? buildProjectIssuesUrl(orgSlug, projectId)
: output.sentryProjectUrl;
const received = verify?.verified ?? false;
const eventUrl =
received && verify?.eventId && orgSlug
? buildEventSearchUrl(orgSlug, verify.eventId)
: undefined;

return {
projectName: deriveProjectName(output),
Comment thread
cursor[bot] marked this conversation as resolved.
Resolved
features: sortFeatures(output.features ?? []).map(featureLabel),
featureBlurbs,
changedFileCount: output.changedFiles?.length ?? 0,
issuesUrl,
verification: { received, eventUrl },
agentInstallCommand: SENTRY_AGENT_INSTALL_COMMAND,
startCommand: deriveStartCommand(output.commands),
};
}

/**
* Build the structured summary handed to `ui.summary()`.
Expand All @@ -33,7 +124,10 @@ import type { WizardSummary, WizardUI } from "./ui/types.js";
* skips the summary call entirely in that case so empty panels don't
* appear.
*/
function buildSummary(output: WizardOutput): WizardSummary | null {
function buildSummary(
output: WizardOutput,
verify: VerifyResult | undefined
): WizardSummary | null {
// Resolve blurbs first so the Features row can check the *resolved* length.
// If the agent returns blurbs with wrong IDs they all drop out here, and
// the Features row falls back to showing correctly.
Expand Down Expand Up @@ -78,25 +172,59 @@ function buildSummary(output: WizardOutput): WizardSummary | null {
}

const changedFiles = output.changedFiles ?? [];
const completion = buildCompletion(output, verify, featureBlurbs);

if (
fields.length === 0 &&
changedFiles.length === 0 &&
featureBlurbs.length === 0
) {
const hasContent =
fields.length > 0 ||
changedFiles.length > 0 ||
featureBlurbs.length > 0 ||
completion.issuesUrl !== undefined ||
completion.features.length > 0;

if (!hasContent) {
return null;
}

return {
fields,
...(changedFiles.length > 0 ? { changedFiles } : {}),
...(featureBlurbs.length > 0 ? { featureBlurbs } : {}),
completion,
};
}

/**
* Overlay the locally-captured Sentry project identity onto the workflow output.
*
* The CLI creates the project itself, so its captured org/project/projectId are
* authoritative and take precedence over anything the server echoes back. The
* server no longer sends these fields (see cli-init-api#239), but keeping the
* `output.*` fallback means the CLI still works against any server version.
*/
function mergeSentryProjectIdentity(
output: WizardOutput,
identity: SentryProjectIdentity | undefined
): WizardOutput {
if (!identity) {
return output;
}
return {
...output,
orgSlug: identity.orgSlug ?? output.orgSlug,
projectSlug: identity.projectSlug ?? output.projectSlug,
projectId: identity.projectId ?? output.projectId,
sentryProjectUrl: output.sentryProjectUrl ?? identity.url,
};
}

export function formatResult(result: WorkflowRunResult, ui: WizardUI): void {
const output: WizardOutput = result.result ?? {};
const summary = buildSummary(output);
export function formatResult(
result: WorkflowRunResult,
ui: WizardUI,
verify?: VerifyResult,
sentryProject?: SentryProjectIdentity
): void {
const output = mergeSentryProjectIdentity(result.result ?? {}, sentryProject);
const summary = buildSummary(output, verify);

if (summary) {
ui.summary(summary);
Expand All @@ -110,7 +238,11 @@ export function formatResult(result: WorkflowRunResult, ui: WizardUI): void {

ui.log.info("Please review the changes above before committing.");

ui.outro("Sentry SDK installed successfully!");
ui.outro(
output.message
? stripAnsi(output.message)
: "Sentry SDK installed successfully!"
);
ui.feedback("success");
}

Expand Down
20 changes: 20 additions & 0 deletions packages/cli/src/lib/init/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,10 +277,30 @@ export type WizardOutput = {
exitCode?: number;
docsUrl?: string;
sentryProjectUrl?: string;
orgSlug?: string;
projectSlug?: string;
projectId?: string;
message?: string;
featureBlurbs?: Array<{ feature: string; blurb: string }>;
};

/**
* Sentry project identity captured locally during the run.
*
* The CLI creates (or resolves) the Sentry project itself via the
* `create-sentry-project` / `ensure-sentry-project` tool, so it already knows
* the org/project identifiers first-hand — the server does not need to echo
* them back in the final output. Captured from the tool result in the wizard
* runner and used to build the Issues / event URLs on the completion screen.
*/
export type SentryProjectIdentity = {
orgSlug: string;
projectSlug: string;
projectId: string;
dsn?: string;
url?: string;
};

// Interactive payloads
export type InteractivePayload =
| SelectPayload
Expand Down
Loading
Loading