-
-
Notifications
You must be signed in to change notification settings - Fork 2
feat(log): add view command to display log entry details #212
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
079be5e
docs: add documentation for log command
betegon 682405f
chore: regenerate skill with log command examples
betegon 6d9efec
feat(log): add view command to display log entry details
betegon 82f56fd
docs(log): improve JSDoc on view command functions
betegon c8e1611
test(log): add tests for log view command
betegon 899d66f
test(log): add property-based tests for log view command
betegon ac4f66e
Merge remote-tracking branch 'origin/main' into feat/log-view-command
betegon b9b9fa0
chore: regenerate skill with log view command
betegon d28ea90
fix(log): address review comments
betegon 78fe982
fix(log): filter by project and use ValidationError for not found
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,238 @@ | ||
| /** | ||
| * sentry log view | ||
| * | ||
| * View detailed information about a Sentry log entry. | ||
| */ | ||
|
|
||
| import { buildCommand } from "@stricli/core"; | ||
| import type { SentryContext } from "../../context.js"; | ||
| import { findProjectsBySlug, getLog } from "../../lib/api-client.js"; | ||
| import { parseOrgProjectArg } from "../../lib/arg-parsing.js"; | ||
| import { openInBrowser } from "../../lib/browser.js"; | ||
| import { ContextError, ValidationError } from "../../lib/errors.js"; | ||
| import { formatLogDetails, writeJson } from "../../lib/formatters/index.js"; | ||
| import { resolveOrgAndProject } from "../../lib/resolve-target.js"; | ||
| import { buildLogsUrl } from "../../lib/sentry-urls.js"; | ||
| import type { DetailedSentryLog, Writer } from "../../types/index.js"; | ||
|
|
||
| type ViewFlags = { | ||
| readonly json: boolean; | ||
| readonly web: boolean; | ||
| }; | ||
|
|
||
| /** Usage hint for ContextError messages */ | ||
| const USAGE_HINT = "sentry log view <org>/<project> <log-id>"; | ||
|
|
||
| /** | ||
| * Parse positional arguments for log view. | ||
| * Handles: `<log-id>` or `<target> <log-id>` | ||
| * | ||
| * @param args - Positional arguments from CLI | ||
| * @returns Parsed log ID and optional target arg | ||
| * @throws {ContextError} If no arguments provided | ||
| */ | ||
| export function parsePositionalArgs(args: string[]): { | ||
| logId: string; | ||
| targetArg: string | undefined; | ||
| } { | ||
| if (args.length === 0) { | ||
| throw new ContextError("Log ID", USAGE_HINT); | ||
| } | ||
|
|
||
| const first = args[0]; | ||
| if (first === undefined) { | ||
| throw new ContextError("Log ID", USAGE_HINT); | ||
| } | ||
|
|
||
| if (args.length === 1) { | ||
| // Single arg - must be log ID | ||
| return { logId: first, targetArg: undefined }; | ||
| } | ||
|
|
||
| const second = args[1]; | ||
| if (second === undefined) { | ||
| return { logId: first, targetArg: undefined }; | ||
| } | ||
|
|
||
| // Two or more args - first is target, second is log ID | ||
| return { logId: second, targetArg: first }; | ||
| } | ||
|
|
||
| /** | ||
| * Resolved target type for log commands. | ||
| * @internal Exported for testing | ||
| */ | ||
| export type ResolvedLogTarget = { | ||
| org: string; | ||
| project: string; | ||
| detectedFrom?: string; | ||
| }; | ||
|
|
||
| /** | ||
| * Resolve target from a project search result. | ||
| * | ||
| * Searches for a project by slug across all accessible organizations. | ||
| * Throws if no project found or if multiple projects found in different orgs. | ||
| * | ||
| * @param projectSlug - Project slug to search for | ||
| * @param logId - Log ID (used in error messages) | ||
| * @returns Resolved target with org and project info | ||
| * @throws {ContextError} If no project found | ||
| * @throws {ValidationError} If project exists in multiple organizations | ||
| * | ||
| * @internal Exported for testing | ||
| */ | ||
| export async function resolveFromProjectSearch( | ||
| projectSlug: string, | ||
| logId: string | ||
| ): Promise<ResolvedLogTarget> { | ||
| const found = await findProjectsBySlug(projectSlug); | ||
| if (found.length === 0) { | ||
| throw new ContextError(`Project "${projectSlug}"`, USAGE_HINT, [ | ||
| "Check that you have access to a project with this slug", | ||
| ]); | ||
| } | ||
| if (found.length > 1) { | ||
| const orgList = found.map((p) => ` ${p.orgSlug}/${p.slug}`).join("\n"); | ||
| throw new ValidationError( | ||
| `Project "${projectSlug}" exists in multiple organizations.\n\n` + | ||
| `Specify the organization:\n${orgList}\n\n` + | ||
| `Example: sentry log view <org>/${projectSlug} ${logId}` | ||
| ); | ||
| } | ||
| // Safe assertion: length is exactly 1 after the checks above | ||
| const foundProject = found[0] as (typeof found)[0]; | ||
| return { | ||
| org: foundProject.orgSlug, | ||
| project: foundProject.slug, | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Write human-readable log output to stdout. | ||
| * | ||
| * @param stdout - Output stream | ||
| * @param log - The log entry to display | ||
| * @param orgSlug - Organization slug for trace URLs | ||
| * @param detectedFrom - Optional context detection source to display | ||
| */ | ||
| function writeHumanOutput( | ||
| stdout: Writer, | ||
| log: DetailedSentryLog, | ||
| orgSlug: string, | ||
| detectedFrom?: string | ||
| ): void { | ||
| const lines = formatLogDetails(log, orgSlug); | ||
| stdout.write(`${lines.join("\n")}\n`); | ||
|
|
||
| if (detectedFrom) { | ||
| stdout.write(`\nDetected from ${detectedFrom}\n`); | ||
| } | ||
| } | ||
|
|
||
| export const viewCommand = buildCommand({ | ||
| docs: { | ||
| brief: "View details of a specific log entry", | ||
| fullDescription: | ||
| "View detailed information about a Sentry log entry by its ID.\n\n" + | ||
| "Target specification:\n" + | ||
| " sentry log view <log-id> # auto-detect from DSN or config\n" + | ||
| " sentry log view <org>/<proj> <log-id> # explicit org and project\n" + | ||
| " sentry log view <project> <log-id> # find project across all orgs\n\n" + | ||
| "The log ID is the 32-character hexadecimal identifier shown in log listings.", | ||
| }, | ||
| parameters: { | ||
| positional: { | ||
| kind: "array", | ||
| parameter: { | ||
| placeholder: "args", | ||
| brief: | ||
| "[<org>/<project>] <log-id> - Target (optional) and log ID (required)", | ||
| parse: String, | ||
| }, | ||
| }, | ||
| flags: { | ||
| json: { | ||
| kind: "boolean", | ||
| brief: "Output as JSON", | ||
| default: false, | ||
| }, | ||
| web: { | ||
| kind: "boolean", | ||
| brief: "Open in browser", | ||
| default: false, | ||
| }, | ||
| }, | ||
| aliases: { w: "web" }, | ||
| }, | ||
| async func( | ||
| this: SentryContext, | ||
| flags: ViewFlags, | ||
| ...args: string[] | ||
| ): Promise<void> { | ||
| const { stdout, cwd, setContext } = this; | ||
|
|
||
| // Parse positional args | ||
| const { logId, targetArg } = parsePositionalArgs(args); | ||
| const parsed = parseOrgProjectArg(targetArg); | ||
|
|
||
| let target: ResolvedLogTarget | null = null; | ||
|
|
||
| switch (parsed.type) { | ||
| case "explicit": | ||
| target = { | ||
| org: parsed.org, | ||
| project: parsed.project, | ||
| }; | ||
| break; | ||
|
|
||
| case "project-search": | ||
| target = await resolveFromProjectSearch(parsed.projectSlug, logId); | ||
| break; | ||
|
|
||
| case "org-all": | ||
| throw new ContextError("Specific project", USAGE_HINT); | ||
|
|
||
| case "auto-detect": | ||
| target = await resolveOrgAndProject({ cwd, usageHint: USAGE_HINT }); | ||
| break; | ||
|
|
||
| default: { | ||
| // Exhaustive check - should never reach here | ||
| const _exhaustiveCheck: never = parsed; | ||
| throw new ValidationError( | ||
| `Invalid target specification: ${_exhaustiveCheck}` | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| if (!target) { | ||
| throw new ContextError("Organization and project", USAGE_HINT); | ||
| } | ||
|
|
||
| // Set telemetry context | ||
| setContext([target.org], [target.project]); | ||
|
|
||
| if (flags.web) { | ||
| await openInBrowser(stdout, buildLogsUrl(target.org, logId), "log"); | ||
| return; | ||
| } | ||
|
|
||
| // Fetch the log entry | ||
| const log = await getLog(target.org, target.project, logId); | ||
|
|
||
| if (!log) { | ||
| throw new ValidationError( | ||
| `No log found with ID "${logId}" in ${target.org}/${target.project}.\n\n` + | ||
| "Make sure the log ID is correct and the log was sent within the last 90 days." | ||
| ); | ||
cursor[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
cursor[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| if (flags.json) { | ||
| writeJson(stdout, log); | ||
| return; | ||
| } | ||
|
|
||
| writeHumanOutput(stdout, log, target.org, target.detectedFrom); | ||
| }, | ||
| }); | ||
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.