-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathview.ts
More file actions
220 lines (191 loc) · 5.97 KB
/
view.ts
File metadata and controls
220 lines (191 loc) · 5.97 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
/**
* 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, ValidationError } from "../../lib/errors.js";
import { formatEventDetails, writeJson } from "../../lib/formatters/index.js";
import {
type ResolvedTarget,
resolveOrgAndProject,
resolveProjectBySlug,
} from "../../lib/resolve-target.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>` or `<target> <event-id>`
*
* @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);
}
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 = ResolvedTarget;
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:
target = await resolveProjectBySlug(parsed.projectSlug, {
usageHint: USAGE_HINT,
contextValue: eventId,
});
break;
case ProjectSpecificationType.OrgAll:
throw new ContextError("Specific project", USAGE_HINT);
case ProjectSpecificationType.AutoDetect:
target = await resolveOrgAndProject({ cwd, usageHint: USAGE_HINT });
break;
default:
// Exhaustive check - should never reach here
throw new ValidationError("Invalid target specification");
}
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,
});
},
});