-
-
Notifications
You must be signed in to change notification settings - Fork 2
feat(dashboard): add dashboard list, view, and create commands #406
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
10
commits into
main
Choose a base branch
from
feat/dashboard-commands-core
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.
+2,850
−3
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
f05f91a
feat(dashboard): add dashboard list, view, and create commands
betegon 5d6d389
test(dashboard): add tests for list, create, resolve commands and ext…
betegon eabdc72
refactor(dashboard): align list/view commands to codebase patterns
betegon 1e051b2
chore: regenerate SKILL.md
github-actions[bot] ca41f05
fix(dashboard): use args[1] pattern for positional arg parsing in res…
betegon 2c4da2f
fix(dashboard): remove unused updateDashboard export
betegon cae9905
fix(dashboard): remove unused prepareDashboardForUpdate and stripWidg…
betegon 52ae975
feat(dashboard): add clickable titles, --limit flag, and loading spin…
betegon ee51054
chore: regenerate SKILL.md
github-actions[bot] f13bbd3
fix(dashboard): show URL below title instead of OSC 8 link
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
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,296 @@ | ||
| /** | ||
| * sentry dashboard create | ||
| * | ||
| * Create a new dashboard in a Sentry organization. | ||
| */ | ||
|
|
||
| import type { SentryContext } from "../../context.js"; | ||
| import { createDashboard, getProject } from "../../lib/api-client.js"; | ||
| import { | ||
| type ParsedOrgProject, | ||
| parseOrgProjectArg, | ||
| } from "../../lib/arg-parsing.js"; | ||
| import { buildCommand, numberParser } from "../../lib/command.js"; | ||
| import { ContextError, ValidationError } from "../../lib/errors.js"; | ||
| import { formatDashboardCreated } from "../../lib/formatters/human.js"; | ||
| import { | ||
| fetchProjectId, | ||
| resolveAllTargets, | ||
| resolveOrg, | ||
| resolveProjectBySlug, | ||
| toNumericId, | ||
| } from "../../lib/resolve-target.js"; | ||
| import { buildDashboardUrl } from "../../lib/sentry-urls.js"; | ||
| import { | ||
| assignDefaultLayout, | ||
| type DashboardDetail, | ||
| type DashboardWidget, | ||
| DISPLAY_TYPES, | ||
| parseAggregate, | ||
| parseSortExpression, | ||
| parseWidgetInput, | ||
| prepareWidgetQueries, | ||
| } from "../../types/dashboard.js"; | ||
|
|
||
| type CreateFlags = { | ||
| readonly "widget-title"?: string; | ||
| readonly "widget-display"?: string; | ||
| readonly "widget-dataset"?: string; | ||
| readonly "widget-query"?: string[]; | ||
| readonly "widget-where"?: string; | ||
| readonly "widget-group-by"?: string[]; | ||
| readonly "widget-sort"?: string; | ||
| readonly "widget-limit"?: number; | ||
| readonly json: boolean; | ||
| readonly fields?: string[]; | ||
| }; | ||
|
|
||
| type CreateResult = DashboardDetail & { url: string }; | ||
|
|
||
| /** | ||
| * Parse array positional args for `dashboard create`. | ||
| * | ||
| * Handles: | ||
| * - `<title>` — title only (auto-detect org/project) | ||
| * - `<target> <title>` — explicit target + title | ||
| */ | ||
| function parsePositionalArgs(args: string[]): { | ||
| title: string; | ||
| targetArg: string | undefined; | ||
| } { | ||
| if (args.length === 0) { | ||
| throw new ValidationError("Dashboard title is required.", "title"); | ||
| } | ||
| if (args.length === 1) { | ||
| return { title: args[0] as string, targetArg: undefined }; | ||
| } | ||
| // Two args: first is target, second is title | ||
| return { title: args[1] as string, targetArg: args[0] as string }; | ||
| } | ||
|
|
||
| /** Result of resolving org + project IDs from the parsed target */ | ||
| type ResolvedDashboardTarget = { | ||
| orgSlug: string; | ||
| projectIds: number[]; | ||
| }; | ||
|
|
||
| /** Enrich targets that lack a projectId by calling the project API */ | ||
| async function enrichTargetProjectIds( | ||
| targets: { org: string; project: string; projectId?: number }[] | ||
| ): Promise<number[]> { | ||
| const enriched = await Promise.all( | ||
| targets.map(async (t) => { | ||
| if (t.projectId !== undefined) { | ||
| return t.projectId; | ||
| } | ||
| try { | ||
| const info = await getProject(t.org, t.project); | ||
| return toNumericId(info.id); | ||
| } catch { | ||
| return; | ||
| } | ||
| }) | ||
| ); | ||
| return enriched.filter((id): id is number => id !== undefined); | ||
| } | ||
|
|
||
| /** Resolve org and project IDs from the parsed target argument */ | ||
| async function resolveDashboardTarget( | ||
| parsed: ParsedOrgProject, | ||
| cwd: string | ||
| ): Promise<ResolvedDashboardTarget> { | ||
| switch (parsed.type) { | ||
| case "explicit": { | ||
| const pid = await fetchProjectId(parsed.org, parsed.project); | ||
| return { | ||
| orgSlug: parsed.org, | ||
| projectIds: pid !== undefined ? [pid] : [], | ||
| }; | ||
| } | ||
| case "org-all": | ||
| return { orgSlug: parsed.org, projectIds: [] }; | ||
|
|
||
| case "project-search": { | ||
| const found = await resolveProjectBySlug( | ||
| parsed.projectSlug, | ||
| "sentry dashboard create <org>/<project> <title>" | ||
| ); | ||
| const pid = await fetchProjectId(found.org, found.project); | ||
| return { | ||
| orgSlug: found.org, | ||
| projectIds: pid !== undefined ? [pid] : [], | ||
| }; | ||
| } | ||
| case "auto-detect": { | ||
| const result = await resolveAllTargets({ cwd }); | ||
| if (result.targets.length === 0) { | ||
| const resolved = await resolveOrg({ cwd }); | ||
| if (!resolved) { | ||
| throw new ContextError( | ||
| "Organization", | ||
| "sentry dashboard create <org>/ <title>" | ||
| ); | ||
| } | ||
| return { orgSlug: resolved.org, projectIds: [] }; | ||
| } | ||
| const orgSlug = (result.targets[0] as (typeof result.targets)[0]).org; | ||
| const projectIds = await enrichTargetProjectIds(result.targets); | ||
| return { orgSlug, projectIds }; | ||
| } | ||
| default: { | ||
| const _exhaustive: never = parsed; | ||
| throw new Error( | ||
| `Unexpected parsed type: ${(_exhaustive as { type: string }).type}` | ||
| ); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** Build an inline widget from --widget-* flags */ | ||
| function buildInlineWidget(flags: CreateFlags): DashboardWidget { | ||
| if (!flags["widget-title"]) { | ||
| throw new ValidationError( | ||
| "Missing --widget-title. Both --widget-title and --widget-display are required for inline widgets.\n\n" + | ||
| "Example:\n" + | ||
| " sentry dashboard create 'My Dashboard' --widget-title \"Error Count\" --widget-display big_number --widget-query count", | ||
| "widget-title" | ||
| ); | ||
| } | ||
|
|
||
| const aggregates = (flags["widget-query"] ?? ["count"]).map(parseAggregate); | ||
| const columns = flags["widget-group-by"] ?? []; | ||
| const orderby = flags["widget-sort"] | ||
| ? parseSortExpression(flags["widget-sort"]) | ||
| : undefined; | ||
|
|
||
| const rawWidget = { | ||
| title: flags["widget-title"], | ||
| displayType: flags["widget-display"] as string, | ||
| ...(flags["widget-dataset"] && { widgetType: flags["widget-dataset"] }), | ||
| queries: [ | ||
| { | ||
| aggregates, | ||
| columns, | ||
| conditions: flags["widget-where"] ?? "", | ||
| ...(orderby && { orderby }), | ||
| name: "", | ||
| }, | ||
| ], | ||
| ...(flags["widget-limit"] !== undefined && { | ||
| limit: flags["widget-limit"], | ||
| }), | ||
| }; | ||
| return prepareWidgetQueries(parseWidgetInput(rawWidget)); | ||
| } | ||
|
|
||
| export const createCommand = buildCommand({ | ||
| docs: { | ||
| brief: "Create a dashboard", | ||
| fullDescription: | ||
| "Create a new Sentry dashboard.\n\n" + | ||
| "Examples:\n" + | ||
| " sentry dashboard create 'My Dashboard'\n" + | ||
| " sentry dashboard create my-org/ 'My Dashboard'\n" + | ||
| " sentry dashboard create my-org/my-project 'My Dashboard'\n\n" + | ||
| "With an inline widget:\n" + | ||
| " sentry dashboard create 'My Dashboard' \\\n" + | ||
| ' --widget-title "Error Count" --widget-display big_number --widget-query count', | ||
| }, | ||
| output: { | ||
| json: true, | ||
| human: formatDashboardCreated, | ||
| }, | ||
| parameters: { | ||
| positional: { | ||
| kind: "array", | ||
| parameter: { | ||
| brief: "[<org/project>] <title>", | ||
| parse: String, | ||
| }, | ||
| }, | ||
| flags: { | ||
| "widget-title": { | ||
| kind: "parsed", | ||
| parse: String, | ||
| brief: "Inline widget title", | ||
| optional: true, | ||
| }, | ||
| "widget-display": { | ||
| kind: "parsed", | ||
| parse: String, | ||
| brief: "Inline widget display type (line, bar, table, big_number, ...)", | ||
| optional: true, | ||
| }, | ||
| "widget-dataset": { | ||
| kind: "parsed", | ||
| parse: String, | ||
| brief: "Inline widget dataset (default: spans)", | ||
| optional: true, | ||
| }, | ||
| "widget-query": { | ||
| kind: "parsed", | ||
| parse: String, | ||
| brief: "Inline widget aggregate (e.g. count, p95:span.duration)", | ||
| variadic: true, | ||
| optional: true, | ||
| }, | ||
| "widget-where": { | ||
| kind: "parsed", | ||
| parse: String, | ||
| brief: "Inline widget search conditions filter", | ||
| optional: true, | ||
| }, | ||
| "widget-group-by": { | ||
| kind: "parsed", | ||
| parse: String, | ||
| brief: "Inline widget group-by column (repeatable)", | ||
| variadic: true, | ||
| optional: true, | ||
| }, | ||
| "widget-sort": { | ||
| kind: "parsed", | ||
| parse: String, | ||
| brief: "Inline widget order by (prefix - for desc)", | ||
| optional: true, | ||
| }, | ||
| "widget-limit": { | ||
| kind: "parsed", | ||
| parse: numberParser, | ||
| brief: "Inline widget result limit", | ||
| optional: true, | ||
| }, | ||
| }, | ||
| }, | ||
| async func(this: SentryContext, flags: CreateFlags, ...args: string[]) { | ||
| const { cwd } = this; | ||
|
|
||
| const { title, targetArg } = parsePositionalArgs(args); | ||
| const parsed = parseOrgProjectArg(targetArg); | ||
| const { orgSlug, projectIds } = await resolveDashboardTarget(parsed, cwd); | ||
|
|
||
| const widgets: DashboardWidget[] = []; | ||
| if (flags["widget-display"]) { | ||
| const validated = buildInlineWidget(flags); | ||
| widgets.push(assignDefaultLayout(validated, widgets)); | ||
| } else if (flags["widget-title"]) { | ||
| throw new ValidationError( | ||
| "Missing --widget-display. Both --widget-title and --widget-display are required for inline widgets.\n\n" + | ||
| "Example:\n" + | ||
| " sentry dashboard create 'My Dashboard' --widget-title \"Error Count\" --widget-display big_number --widget-query count\n\n" + | ||
| `Valid display types: ${DISPLAY_TYPES.join(", ")}`, | ||
| "widget-display" | ||
| ); | ||
| } | ||
|
|
||
| const dashboard = await createDashboard(orgSlug, { | ||
| title, | ||
| widgets, | ||
| projects: projectIds.length > 0 ? projectIds : undefined, | ||
| }); | ||
| const url = buildDashboardUrl(orgSlug, dashboard.id); | ||
|
|
||
| return { | ||
| data: { ...dashboard, url } as CreateResult, | ||
| }; | ||
| }, | ||
| }); | ||
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.