-
-
Notifications
You must be signed in to change notification settings - Fork 2
feat(project): add project create command
#237
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
betegon
wants to merge
20
commits into
main
Choose a base branch
from
feat/project-create
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
82d0ad4
feat(api): add SentryTeam type, listTeams, and createProject endpoints
betegon 0ff0135
feat(project): add `project create` command
betegon 62fc00c
chore: regenerate SKILL.md
github-actions[bot] f747f3b
fix(project): show platform list on invalid platform API error
betegon 4f7258f
fix(project): improve platform error message formatting
betegon 6294862
refactor: extract shared helpers for reuse across create commands
betegon 61619e6
Merge branch 'main' into feat/project-create
betegon 1726210
Merge branch 'main' into feat/project-create
betegon 8766e4c
fix(project): disambiguate 404 errors from create endpoint
betegon d4c1670
fix(project): slugify name in 409 conflict hint
betegon 8777a13
fix(project): only diagnose 'org not found' on 404 from listTeams
betegon 968ac1a
fix(resolve-team): only diagnose 'org not found' on 404 from listTeams
betegon 1e68d54
Merge branch 'main' into feat/project-create
betegon 7ac4c5c
refactor(project): extract fetchOrgListHint and clean up create command
betegon 6390271
feat(project): show note when Sentry adjusts the project slug
betegon 2d37cdd
Merge remote-tracking branch 'origin/main' into feat/project-create
betegon 552574f
feat(project): auto-select team based on membership in multi-team orgs
betegon 2aaea2f
fix(project): avoid contradictory 'team not found' error when team ex…
betegon e357d8a
fix(arg-parsing): trim whitespace in parseOrgPrefixedArg for consistency
betegon 80b809d
Merge branch 'main' into feat/project-create
betegon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,368 @@ | ||
| /** | ||
| * sentry project create | ||
| * | ||
| * Create a new Sentry project. | ||
| * Supports org/name positional syntax (like `gh repo create owner/repo`). | ||
| */ | ||
|
|
||
| import type { SentryContext } from "../../context.js"; | ||
| import { | ||
| createProject, | ||
| listTeams, | ||
| tryGetPrimaryDsn, | ||
| } from "../../lib/api-client.js"; | ||
| import { parseOrgPrefixedArg } from "../../lib/arg-parsing.js"; | ||
| import { buildCommand } from "../../lib/command.js"; | ||
| import { ApiError, CliError, ContextError } from "../../lib/errors.js"; | ||
| import { writeFooter, writeJson } from "../../lib/formatters/index.js"; | ||
| import { resolveOrg } from "../../lib/resolve-target.js"; | ||
| import { fetchOrgListHint, resolveTeam } from "../../lib/resolve-team.js"; | ||
| import { buildProjectUrl } from "../../lib/sentry-urls.js"; | ||
| import type { SentryProject, SentryTeam, Writer } from "../../types/index.js"; | ||
|
|
||
| /** Usage hint template — base command without positionals */ | ||
| const USAGE_HINT = "sentry project create <org>/<name> <platform>"; | ||
|
|
||
| type CreateFlags = { | ||
| readonly team?: string; | ||
| readonly json: boolean; | ||
| }; | ||
|
|
||
| /** Common Sentry platform strings, shown when platform arg is missing or invalid */ | ||
| const PLATFORMS = [ | ||
| "javascript", | ||
| "javascript-react", | ||
| "javascript-nextjs", | ||
| "javascript-vue", | ||
| "javascript-angular", | ||
| "javascript-svelte", | ||
| "javascript-remix", | ||
| "javascript-astro", | ||
| "node", | ||
| "node-express", | ||
| "python", | ||
| "python-django", | ||
| "python-flask", | ||
| "python-fastapi", | ||
| "go", | ||
| "ruby", | ||
| "ruby-rails", | ||
| "php", | ||
| "php-laravel", | ||
| "java", | ||
| "android", | ||
| "dotnet", | ||
| "react-native", | ||
| "apple-ios", | ||
| "rust", | ||
| "elixir", | ||
| ] as const; | ||
|
|
||
| /** | ||
| * Convert a project name to its expected Sentry slug. | ||
| * Sentry slugs are lowercase, with non-alphanumeric runs replaced by hyphens. | ||
| * | ||
| * @example slugify("My Cool App") // "my-cool-app" | ||
| * @example slugify("my-app") // "my-app" | ||
| */ | ||
| function slugify(name: string): string { | ||
| return name | ||
| .toLowerCase() | ||
| .replace(/[^a-z0-9]+/g, "-") | ||
| .replace(/^-|-$/g, ""); | ||
| } | ||
|
|
||
| /** | ||
| * Check whether an API error is about an invalid platform value. | ||
| * Relies on Sentry's error message wording — may need updating if the API changes. | ||
| */ | ||
| function isPlatformError(error: ApiError): boolean { | ||
| const detail = error.detail ?? error.message; | ||
| return detail.includes("platform") && detail.includes("Invalid"); | ||
| } | ||
|
|
||
| /** | ||
| * Build a user-friendly error message for missing or invalid platform. | ||
| * | ||
| * @param nameArg - The name arg (used in the usage example) | ||
| * @param platform - The invalid platform string, if provided | ||
| */ | ||
| function buildPlatformError(nameArg: string, platform?: string): string { | ||
| const list = PLATFORMS.map((p) => ` ${p}`).join("\n"); | ||
| const heading = platform | ||
| ? `Invalid platform '${platform}'.` | ||
| : "Platform is required."; | ||
|
|
||
| return ( | ||
| `${heading}\n\n` + | ||
| "Usage:\n" + | ||
| ` sentry project create ${nameArg} <platform>\n\n` + | ||
| `Available platforms:\n\n${list}\n\n` + | ||
| "Full list: https://docs.sentry.io/platforms/" | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Disambiguate a 404 from the create project endpoint. | ||
| * | ||
| * The `/teams/{org}/{team}/projects/` endpoint returns 404 for both | ||
| * a bad org and a bad team. This helper calls `listTeams` to determine | ||
| * which is wrong, then throws an actionable error. | ||
| * | ||
| * Only called on the error path — no cost to the happy path. | ||
| */ | ||
| async function handleCreateProject404( | ||
| orgSlug: string, | ||
| teamSlug: string, | ||
| name: string, | ||
| platform: string | ||
| ): Promise<never> { | ||
| let teams: SentryTeam[] | null = null; | ||
| let listTeamsError: unknown = null; | ||
|
|
||
| try { | ||
| teams = await listTeams(orgSlug); | ||
| } catch (error) { | ||
| listTeamsError = error; | ||
| } | ||
|
|
||
| // listTeams succeeded → org is valid, diagnose the team | ||
| if (teams !== null) { | ||
| const teamExists = teams.some((t) => t.slug === teamSlug); | ||
| if (teamExists) { | ||
| // Team is in the list but the create endpoint still returned 404 — | ||
| // likely a permissions issue (rare; Sentry usually returns 403) | ||
| throw new CliError( | ||
| `Failed to create project '${name}' in ${orgSlug}.\n\n` + | ||
| `Team '${teamSlug}' exists but the request was rejected. ` + | ||
| "You may lack permission to create projects in this team." | ||
| ); | ||
| } | ||
|
|
||
| if (teams.length > 0) { | ||
| const teamList = teams.map((t) => ` ${t.slug}`).join("\n"); | ||
| throw new CliError( | ||
| `Team '${teamSlug}' not found in ${orgSlug}.\n\n` + | ||
| `Available teams:\n\n${teamList}\n\n` + | ||
| "Try:\n" + | ||
| ` sentry project create ${orgSlug}/${name} ${platform} --team <team-slug>` | ||
| ); | ||
| } | ||
cursor[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| throw new CliError( | ||
| `No teams found in ${orgSlug}.\n\n` + | ||
| "Create a team first, then try again." | ||
| ); | ||
| } | ||
|
|
||
| // listTeams returned 404 → org doesn't exist | ||
| if (listTeamsError instanceof ApiError && listTeamsError.status === 404) { | ||
| const orgHint = await fetchOrgListHint( | ||
| `Specify org explicitly: ${USAGE_HINT}` | ||
| ); | ||
| throw new CliError(`Organization '${orgSlug}' not found.\n\n${orgHint}`); | ||
| } | ||
|
|
||
| // listTeams failed for other reasons (403, 5xx, network) — can't disambiguate | ||
| throw new CliError( | ||
| `Failed to create project '${name}' in ${orgSlug}.\n\n` + | ||
| "The organization or team may not exist, or you may lack access.\n\n" + | ||
| "Try:\n" + | ||
| ` sentry project create ${orgSlug}/${name} ${platform} --team <team-slug>` | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Create a project with user-friendly error handling. | ||
| * Wraps API errors with actionable messages instead of raw HTTP status codes. | ||
| */ | ||
| async function createProjectWithErrors( | ||
| orgSlug: string, | ||
| teamSlug: string, | ||
| name: string, | ||
| platform: string | ||
| ): Promise<SentryProject> { | ||
| try { | ||
| return await createProject(orgSlug, teamSlug, { name, platform }); | ||
| } catch (error) { | ||
| if (error instanceof ApiError) { | ||
| if (error.status === 409) { | ||
| const slug = slugify(name); | ||
| throw new CliError( | ||
| `A project named '${name}' already exists in ${orgSlug}.\n\n` + | ||
| `View it: sentry project view ${orgSlug}/${slug}` | ||
| ); | ||
cursor[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| if (error.status === 400 && isPlatformError(error)) { | ||
| throw new CliError(buildPlatformError(`${orgSlug}/${name}`, platform)); | ||
| } | ||
| if (error.status === 404) { | ||
| await handleCreateProject404(orgSlug, teamSlug, name, platform); | ||
| } | ||
| throw new CliError( | ||
| `Failed to create project '${name}' in ${orgSlug}.\n\n` + | ||
| `API error (${error.status}): ${error.detail ?? error.message}` | ||
| ); | ||
| } | ||
| throw error; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Write key-value pairs with aligned columns. | ||
| * Used for human-readable output after resource creation. | ||
| */ | ||
| function writeKeyValue( | ||
| stdout: Writer, | ||
| pairs: [label: string, value: string][] | ||
| ): void { | ||
| const maxLabel = Math.max(...pairs.map(([l]) => l.length)); | ||
| for (const [label, value] of pairs) { | ||
| stdout.write(` ${label.padEnd(maxLabel + 2)}${value}\n`); | ||
| } | ||
| } | ||
|
|
||
| export const createCommand = buildCommand({ | ||
| docs: { | ||
| brief: "Create a new project", | ||
| fullDescription: | ||
| "Create a new Sentry project in an organization.\n\n" + | ||
| "The name supports org/name syntax to specify the organization explicitly.\n" + | ||
| "If omitted, the org is auto-detected from config defaults or DSN.\n\n" + | ||
| "Projects are created under a team. If the org has one team, it is used\n" + | ||
| "automatically. Otherwise, specify --team.\n\n" + | ||
| "Examples:\n" + | ||
| " sentry project create my-app node\n" + | ||
| " sentry project create acme-corp/my-app javascript-nextjs\n" + | ||
| " sentry project create my-app python-django --team backend\n" + | ||
| " sentry project create my-app go --json", | ||
| }, | ||
| parameters: { | ||
| positional: { | ||
| kind: "tuple", | ||
| parameters: [ | ||
| { | ||
| placeholder: "name", | ||
| brief: "Project name (supports org/name syntax)", | ||
| parse: String, | ||
| optional: true, | ||
| }, | ||
| { | ||
| placeholder: "platform", | ||
| brief: "Project platform (e.g., node, python, javascript-nextjs)", | ||
| parse: String, | ||
| optional: true, | ||
| }, | ||
| ], | ||
| }, | ||
| flags: { | ||
| team: { | ||
| kind: "parsed", | ||
| parse: String, | ||
| brief: "Team to create the project under", | ||
| optional: true, | ||
| }, | ||
| json: { | ||
| kind: "boolean", | ||
| brief: "Output as JSON", | ||
| default: false, | ||
| }, | ||
| }, | ||
| aliases: { t: "team" }, | ||
| }, | ||
| async func( | ||
| this: SentryContext, | ||
| flags: CreateFlags, | ||
| nameArg?: string, | ||
| platformArg?: string | ||
| ): Promise<void> { | ||
| const { stdout, cwd } = this; | ||
|
|
||
| if (!nameArg) { | ||
| throw new ContextError( | ||
| "Project name", | ||
| "sentry project create <name> <platform>", | ||
| [ | ||
| `Use org/name syntax: ${USAGE_HINT}`, | ||
| "Specify team: sentry project create <name> <platform> --team <slug>", | ||
| ] | ||
| ); | ||
| } | ||
|
|
||
| if (!platformArg) { | ||
| throw new CliError(buildPlatformError(nameArg)); | ||
| } | ||
|
|
||
| const { org: explicitOrg, name } = parseOrgPrefixedArg( | ||
| nameArg, | ||
| "Project name", | ||
| USAGE_HINT | ||
| ); | ||
|
|
||
| // Resolve organization | ||
| const resolved = await resolveOrg({ org: explicitOrg, cwd }); | ||
| if (!resolved) { | ||
| throw new ContextError("Organization", USAGE_HINT, [ | ||
| `Include org in name: ${USAGE_HINT}`, | ||
| "Set a default: sentry org view <org>", | ||
| "Run from a directory with a Sentry DSN configured", | ||
| ]); | ||
| } | ||
| const orgSlug = resolved.org; | ||
|
|
||
| // Resolve team | ||
| const teamSlug = await resolveTeam(orgSlug, { | ||
| team: flags.team, | ||
| detectedFrom: resolved.detectedFrom, | ||
| usageHint: USAGE_HINT, | ||
| }); | ||
|
|
||
| // Create the project | ||
| const project = await createProjectWithErrors( | ||
| orgSlug, | ||
| teamSlug, | ||
| name, | ||
| platformArg | ||
| ); | ||
|
|
||
| // Fetch DSN (best-effort) | ||
| const dsn = await tryGetPrimaryDsn(orgSlug, project.slug); | ||
|
|
||
| // JSON output | ||
| if (flags.json) { | ||
| writeJson(stdout, { ...project, dsn }); | ||
| return; | ||
| } | ||
|
|
||
| // Human-readable output | ||
| const url = buildProjectUrl(orgSlug, project.slug); | ||
| const fields: [string, string][] = [ | ||
| ["Project", project.name], | ||
| ["Slug", project.slug], | ||
| ["Org", orgSlug], | ||
| ["Team", teamSlug], | ||
| ["Platform", project.platform || platformArg], | ||
| ]; | ||
| if (dsn) { | ||
| fields.push(["DSN", dsn]); | ||
| } | ||
| fields.push(["URL", url]); | ||
|
|
||
| stdout.write(`\nCreated project '${project.name}' in ${orgSlug}\n`); | ||
|
|
||
| // Sentry may adjust the slug to avoid collisions (e.g., "my-app" → "my-app-0g") | ||
| const expectedSlug = slugify(name); | ||
| if (project.slug !== expectedSlug) { | ||
| stdout.write( | ||
| `Note: Slug '${project.slug}' was assigned because '${expectedSlug}' is already taken.\n` | ||
| ); | ||
| } | ||
|
|
||
| stdout.write("\n"); | ||
| writeKeyValue(stdout, fields); | ||
|
|
||
| writeFooter( | ||
| stdout, | ||
| `Tip: Use 'sentry project view ${orgSlug}/${project.slug}' for details` | ||
| ); | ||
| }, | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.