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
29 changes: 13 additions & 16 deletions src/commands/project/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@
* 1. Parse name arg → extract org prefix if present (e.g., "acme/my-app")
* 2. Resolve org → CLI flag > env vars > config defaults > DSN auto-detection
* 3. Resolve team → `--team` flag > auto-select single team > auto-create if empty
* 4. Call `createProject` API
* 5. Fetch DSN (best-effort) and display results
* 4. Call `createProjectWithDsn` (creates project, fetches DSN, builds URL)
* 5. Display results
*
* When the team is auto-selected or auto-created, the output includes a note
* so the user knows which team was used and how to change it.
*/

import type { SentryContext } from "../../context.js";
import {
createProject,
type CreatedProjectDetails,
createProjectWithDsn,
listTeams,
tryGetPrimaryDsn,
} from "../../lib/api-client.js";
import { parseOrgProjectArg } from "../../lib/arg-parsing.js";
import { buildCommand } from "../../lib/command.js";
Expand Down Expand Up @@ -52,9 +52,7 @@ import {
type ResolvedTeam,
resolveOrCreateTeam,
} from "../../lib/resolve-team.js";
import { buildProjectUrl } from "../../lib/sentry-urls.js";
import { slugify } from "../../lib/utils.js";
import type { SentryProject } from "../../types/index.js";

const log = logger.withTag("project.create");

Expand Down Expand Up @@ -227,7 +225,7 @@ async function handleCreateProject404(opts: {
}

/**
* Create a project with user-friendly error handling.
* Create a project (with DSN + URL) with user-friendly error handling.
* Wraps API errors with actionable messages instead of raw HTTP status codes.
*/
async function createProjectWithErrors(opts: {
Expand All @@ -236,10 +234,10 @@ async function createProjectWithErrors(opts: {
name: string;
platform: string;
detectedFrom?: string;
}): Promise<SentryProject> {
}): Promise<CreatedProjectDetails> {
const { orgSlug, teamSlug, name, platform } = opts;
try {
return await createProject(orgSlug, teamSlug, { name, platform });
return await createProjectWithDsn(orgSlug, teamSlug, { name, platform });
} catch (error) {
if (error instanceof ApiError) {
if (error.status === 409) {
Expand All @@ -253,7 +251,9 @@ async function createProjectWithErrors(opts: {
throw new CliError(buildPlatformError(`${orgSlug}/${name}`, platform));
}
if (error.status === 404) {
return await handleCreateProject404(opts);
// handleCreateProject404 always throws — cast needed because
// createProjectWithDsn's return type differs from SentryProject
return await (handleCreateProject404(opts) as never);
}
throw new CliError(
`Failed to create project '${name}' in ${orgSlug}.\n\n` +
Expand Down Expand Up @@ -407,26 +407,23 @@ export const createCommand = buildCommand({
return yield new CommandOutput(result);
}

// Create the project
const project = await createProjectWithErrors({
// Create the project, fetch DSN, and build URL
const { project, dsn, url } = await createProjectWithErrors({
orgSlug,
teamSlug: team.slug,
name,
platform,
detectedFrom: resolved.detectedFrom,
});

// Fetch DSN (best-effort)
const dsn = await tryGetPrimaryDsn(orgSlug, project.slug);

const result: ProjectCreatedResult = {
project,
orgSlug,
teamSlug: team.slug,
teamSource: team.source,
requestedPlatform: platform,
dsn,
url: buildProjectUrl(orgSlug, project.slug),
url,
slugDiverged: project.slug !== expectedSlug,
expectedSlug,
};
Expand Down
2 changes: 2 additions & 0 deletions src/lib/api-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ export {
listOrganizationsUncached,
} from "./api/organizations.js";
export {
type CreatedProjectDetails,
createProject,
createProjectWithDsn,
deleteProject,
findProjectByDsnKey,
findProjectsByPattern,
Expand Down
25 changes: 25 additions & 0 deletions src/lib/api/projects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { getCachedOrganizations } from "../db/regions.js";
import { type AuthGuardSuccess, withAuthGuard } from "../errors.js";
import { logger } from "../logger.js";
import { getApiBaseUrl } from "../sentry-client.js";
import { buildProjectUrl } from "../sentry-urls.js";
import { isAllDigits } from "../utils.js";

import {
Expand Down Expand Up @@ -168,6 +169,30 @@ export async function createProject(
return data as unknown as SentryProject;
}

/** Result of creating a project and fetching its DSN + dashboard URL. */
export type CreatedProjectDetails = {
project: SentryProject;
dsn: string | null;
url: string;
};

/**
* Create a project, fetch its DSN, and build its dashboard URL.
*
* Shared core used by both `sentry project create` and `sentry init`.
* Callers handle their own error wrapping and team resolution.
*/
export async function createProjectWithDsn(
orgSlug: string,
teamSlug: string,
body: CreateProjectBody
): Promise<CreatedProjectDetails> {
const project = await createProject(orgSlug, teamSlug, body);
const dsn = await tryGetPrimaryDsn(orgSlug, project.slug);
const url = buildProjectUrl(orgSlug, project.slug);
return { project, dsn, url };
}

/**
* Delete a project from an organization.
*
Expand Down
19 changes: 7 additions & 12 deletions src/lib/init/local-ops.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import fs from "node:fs";
import path from "node:path";
import { isCancel, select } from "@clack/prompts";
import {
createProject,
createProjectWithDsn,
getProject,
listOrganizations,
tryGetPrimaryDsn,
Expand Down Expand Up @@ -919,17 +919,12 @@ async function createSentryProject(
usageHint: "sentry init",
});

// 5. Create project
const project = await createProject(orgSlug, team.slug, {
name,
platform,
});

// 6. Get DSN (best-effort)
const dsn = await tryGetPrimaryDsn(orgSlug, project.slug);

// 7. Build URL
const url = buildProjectUrl(orgSlug, project.slug);
// 5. Create project, fetch DSN, and build URL
const { project, dsn, url } = await createProjectWithDsn(
orgSlug,
team.slug,
{ name, platform }
);

return {
ok: true,
Expand Down
Loading