-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathview.ts
More file actions
266 lines (237 loc) · 7.72 KB
/
view.ts
File metadata and controls
266 lines (237 loc) · 7.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/**
* sentry event view
*
* View detailed information about a Sentry event.
*/
import type { SentryContext } from "../../context.js";
import { getEvent } from "../../lib/api-client.js";
import {
ProjectSpecificationType,
parseOrgProjectArg,
spansFlag,
} from "../../lib/arg-parsing.js";
import { openInBrowser } from "../../lib/browser.js";
import { buildCommand } from "../../lib/command.js";
import { ContextError } from "../../lib/errors.js";
import { formatEventDetails, writeJson } from "../../lib/formatters/index.js";
import {
resolveOrgAndProject,
resolveProjectBySlug,
} from "../../lib/resolve-target.js";
import {
applySentryUrlContext,
parseSentryUrl,
} from "../../lib/sentry-url-parser.js";
import { buildEventSearchUrl } from "../../lib/sentry-urls.js";
import { getSpanTreeLines } from "../../lib/span-tree.js";
import type { SentryEvent, Writer } from "../../types/index.js";
type ViewFlags = {
readonly json: boolean;
readonly web: boolean;
readonly spans: number;
};
type HumanOutputOptions = {
event: SentryEvent;
detectedFrom?: string;
spanTreeLines?: string[];
};
/**
* Write human-readable event output to stdout.
*
* @param stdout - Output stream
* @param options - Output options including event, detectedFrom, and spanTreeLines
*/
function writeHumanOutput(stdout: Writer, options: HumanOutputOptions): void {
const { event, detectedFrom, spanTreeLines } = options;
const lines = formatEventDetails(event, `Event ${event.eventID}`);
// Skip leading empty line for standalone display
const output = lines.slice(1);
stdout.write(`${output.join("\n")}\n`);
if (spanTreeLines && spanTreeLines.length > 0) {
stdout.write(`${spanTreeLines.join("\n")}\n`);
}
if (detectedFrom) {
stdout.write(`\nDetected from ${detectedFrom}\n`);
}
}
/** Usage hint for ContextError messages */
const USAGE_HINT = "sentry event view <org>/<project> <event-id>";
/**
* Parse positional arguments for event view.
*
* Handles:
* - `<event-id>` — event ID only (auto-detect org/project)
* - `<target> <event-id>` — explicit target + event ID
* - `<sentry-url>` — extract eventId and org from a Sentry event URL
* (e.g., `https://sentry.example.com/organizations/my-org/issues/123/events/abc/`)
*
* For event URLs, the org is returned as `targetArg` in `"{org}/"` format
* (OrgAll). Since event URLs don't contain a project slug, the caller
* must fall back to auto-detection for the project. The URL must contain
* an eventId segment — issue-only URLs are not valid for event view.
*
* @returns Parsed event ID and optional target arg
*/
export function parsePositionalArgs(args: string[]): {
eventId: string;
targetArg: string | undefined;
} {
if (args.length === 0) {
throw new ContextError("Event ID", USAGE_HINT);
}
const first = args[0];
if (first === undefined) {
throw new ContextError("Event ID", USAGE_HINT);
}
// URL detection — extract eventId and org from Sentry event URLs
const urlParsed = parseSentryUrl(first);
if (urlParsed) {
applySentryUrlContext(urlParsed.baseUrl);
if (urlParsed.eventId) {
// Event URL: pass org as OrgAll target ("{org}/").
// Event URLs don't contain a project slug, so viewCommand falls
// back to auto-detect for the project while keeping the org context.
return { eventId: urlParsed.eventId, targetArg: `${urlParsed.org}/` };
}
// URL recognized but no eventId — not valid for event view
throw new ContextError(
"Event ID in URL (use a URL like /issues/{id}/events/{eventId}/)",
USAGE_HINT
);
}
if (args.length === 1) {
// Single arg - must be event ID
return { eventId: first, targetArg: undefined };
}
const second = args[1];
if (second === undefined) {
// Should not happen given length check, but TypeScript needs this
return { eventId: first, targetArg: undefined };
}
// Two or more args - first is target, second is event ID
return { eventId: second, targetArg: first };
}
/**
* Resolved target type for event commands.
* Uses ResolvedTarget from resolve-target.ts.
* @internal Exported for testing
*/
export type ResolvedEventTarget = {
org: string;
project: string;
orgDisplay: string;
projectDisplay: string;
detectedFrom?: string;
};
export const viewCommand = buildCommand({
docs: {
brief: "View details of a specific event",
fullDescription:
"View detailed information about a Sentry event by its ID.\n\n" +
"Target specification:\n" +
" sentry event view <event-id> # auto-detect from DSN or config\n" +
" sentry event view <org>/<proj> <event-id> # explicit org and project\n" +
" sentry event view <project> <event-id> # find project across all orgs",
},
parameters: {
positional: {
kind: "array",
parameter: {
placeholder: "args",
brief:
"[<org>/<project>] <event-id> - Target (optional) and event ID (required)",
parse: String,
},
},
flags: {
json: {
kind: "boolean",
brief: "Output as JSON",
default: false,
},
web: {
kind: "boolean",
brief: "Open in browser",
default: false,
},
...spansFlag,
},
aliases: { w: "web" },
},
async func(
this: SentryContext,
flags: ViewFlags,
...args: string[]
): Promise<void> {
const { stdout, cwd } = this;
// Parse positional args
const { eventId, targetArg } = parsePositionalArgs(args);
const parsed = parseOrgProjectArg(targetArg);
let target: ResolvedEventTarget | null = null;
switch (parsed.type) {
case ProjectSpecificationType.Explicit:
target = {
org: parsed.org,
project: parsed.project,
orgDisplay: parsed.org,
projectDisplay: parsed.project,
};
break;
case ProjectSpecificationType.ProjectSearch: {
const resolved = await resolveProjectBySlug(
parsed.projectSlug,
USAGE_HINT,
`sentry event view <org>/${parsed.projectSlug} ${eventId}`
);
target = {
...resolved,
orgDisplay: resolved.org,
projectDisplay: resolved.project,
};
break;
}
case ProjectSpecificationType.OrgAll:
// Org-only (e.g., from event URL that has no project slug).
// Fall through to auto-detect — SENTRY_URL is already set for
// self-hosted, and auto-detect will resolve the project from
// DSN, config defaults, or directory name inference.
// falls through
case ProjectSpecificationType.AutoDetect:
target = await resolveOrgAndProject({ cwd, usageHint: USAGE_HINT });
break;
default:
// Exhaustive check - should never reach here
throw new ContextError("Organization and project", USAGE_HINT);
}
if (!target) {
throw new ContextError("Organization and project", USAGE_HINT);
}
if (flags.web) {
await openInBrowser(
stdout,
buildEventSearchUrl(target.org, eventId),
"event"
);
return;
}
const event = await getEvent(target.org, target.project, eventId);
// Fetch span tree data (for both JSON and human output)
// Skip when spans=0 (disabled via --spans no or --spans 0)
const spanTreeResult =
flags.spans > 0
? await getSpanTreeLines(target.org, event, flags.spans)
: undefined;
if (flags.json) {
const trace = spanTreeResult?.success
? { traceId: spanTreeResult.traceId, spans: spanTreeResult.spans }
: null;
writeJson(stdout, { event, trace });
return;
}
writeHumanOutput(stdout, {
event,
detectedFrom: target.detectedFrom,
spanTreeLines: spanTreeResult?.lines,
});
},
});