-
-
Notifications
You must be signed in to change notification settings - Fork 2
feat: add sentry schema command for API introspection
#437
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
BYK
wants to merge
1
commit into
main
Choose a base branch
from
feat/schema-command
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
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
Large diffs are not rendered by default.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,224 @@ | ||
| #!/usr/bin/env bun | ||
| /** | ||
| * Generate API Schema Index from Sentry's OpenAPI Specification | ||
| * | ||
| * Fetches the dereferenced OpenAPI spec from the sentry-api-schema repository | ||
| * and extracts a lightweight JSON index of all API endpoints. This index is | ||
| * bundled into the CLI for runtime introspection via `sentry schema`. | ||
| * | ||
| * Data source: https://github.com/getsentry/sentry-api-schema | ||
| * - openapi-derefed.json — full dereferenced OpenAPI 3.0 spec | ||
| * | ||
| * Also reads SDK function names from the installed @sentry/api package to | ||
| * map operationIds to their TypeScript SDK function names. | ||
| * | ||
| * Usage: | ||
| * bun run script/generate-api-schema.ts | ||
| * | ||
| * Output: | ||
| * src/generated/api-schema.json | ||
| */ | ||
|
|
||
| import { resolve } from "node:path"; | ||
|
|
||
| const OUTPUT_PATH = "src/generated/api-schema.json"; | ||
|
|
||
| /** | ||
| * Build the OpenAPI spec URL from the installed @sentry/api version. | ||
| * The sentry-api-schema repo tags match the @sentry/api npm version. | ||
| */ | ||
| function getOpenApiUrl(): string { | ||
| const pkgPath = require.resolve("@sentry/api/package.json"); | ||
| const pkg = require(pkgPath) as { version: string }; | ||
| return `https://raw.githubusercontent.com/getsentry/sentry-api-schema/${pkg.version}/openapi-derefed.json`; | ||
| } | ||
|
|
||
| /** Regex to extract path parameters from URL templates */ | ||
| const PATH_PARAM_PATTERN = /\{(\w+)\}/g; | ||
|
|
||
| // Single source of truth for the ApiEndpoint type lives in src/lib/api-schema.ts. | ||
| // Re-export so this file remains a module (required for top-level await). | ||
| export type { ApiEndpoint } from "../src/lib/api-schema.js"; | ||
|
|
||
| import type { ApiEndpoint } from "../src/lib/api-schema.js"; | ||
|
|
||
| // --------------------------------------------------------------------------- | ||
| // OpenAPI Types (minimal subset we need) | ||
| // --------------------------------------------------------------------------- | ||
|
|
||
| type OpenApiSpec = { | ||
| paths: Record<string, Record<string, OpenApiOperation>>; | ||
| }; | ||
|
|
||
| type OpenApiOperation = { | ||
| operationId?: string; | ||
| description?: string; | ||
| deprecated?: boolean; | ||
| parameters?: OpenApiParameter[]; | ||
| }; | ||
|
|
||
| type OpenApiParameter = { | ||
| in: "path" | "query" | "header" | "cookie"; | ||
| name: string; | ||
| required?: boolean; | ||
| description?: string; | ||
| schema?: { type?: string }; | ||
| }; | ||
|
|
||
| // --------------------------------------------------------------------------- | ||
| // SDK Function Name Mapping | ||
| // --------------------------------------------------------------------------- | ||
|
|
||
| /** | ||
| * Build a map from URL+method to SDK function name by parsing | ||
| * the @sentry/api index.js bundle. | ||
| */ | ||
| async function buildSdkFunctionMap(): Promise<Map<string, string>> { | ||
| const pkgDir = resolve( | ||
| require.resolve("@sentry/api/package.json"), | ||
| "..", | ||
| "dist" | ||
| ); | ||
| const js = await Bun.file(`${pkgDir}/index.js`).text(); | ||
| const results = new Map<string, string>(); | ||
|
|
||
| // Match: var NAME = (options...) => (options...client ?? client).METHOD({ | ||
| const funcPattern = | ||
| /var (\w+) = \(options\S*\) => \(options\S*client \?\? client\)\.(\w+)\(/g; | ||
| // Match: url: "..." | ||
BYK marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const urlPattern = /url: "([^"]+)"/g; | ||
|
|
||
| // Extract all function declarations with their positions | ||
| const funcs: { name: string; method: string; index: number }[] = []; | ||
| let match = funcPattern.exec(js); | ||
| while (match !== null) { | ||
| funcs.push({ | ||
| name: match[1], | ||
| method: match[2].toUpperCase(), | ||
| index: match.index, | ||
| }); | ||
| match = funcPattern.exec(js); | ||
| } | ||
|
|
||
| // Extract all URLs with their positions | ||
| const urls: { url: string; index: number }[] = []; | ||
| match = urlPattern.exec(js); | ||
| while (match !== null) { | ||
| urls.push({ url: match[1], index: match.index }); | ||
| match = urlPattern.exec(js); | ||
| } | ||
|
|
||
| // Match each function to its nearest following URL | ||
| for (const func of funcs) { | ||
| const nextUrl = urls.find((u) => u.index > func.index); | ||
| if (nextUrl) { | ||
| const key = `${func.method}:${nextUrl.url}`; | ||
| results.set(key, func.name); | ||
| } | ||
| } | ||
BYK marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| return results; | ||
| } | ||
|
|
||
| // --------------------------------------------------------------------------- | ||
| // Resource Derivation | ||
| // --------------------------------------------------------------------------- | ||
|
|
||
| /** | ||
| * Derive the resource name from a URL template. | ||
| * Uses the last non-parameter path segment. | ||
| * | ||
| * @example "/api/0/organizations/{org}/issues/" → "issues" | ||
| * @example "/api/0/issues/{issue_id}/" → "issues" | ||
| */ | ||
| function deriveResource(url: string): string { | ||
| const segments = url | ||
| .split("/") | ||
| .filter((s) => s.length > 0 && !s.startsWith("{")); | ||
| const meaningful = segments.filter((s) => s !== "api" && s !== "0"); | ||
| return meaningful.at(-1) ?? "unknown"; | ||
| } | ||
|
|
||
| /** | ||
| * Extract path parameter names from a URL template. | ||
| */ | ||
| function extractPathParams(url: string): string[] { | ||
| const params: string[] = []; | ||
| const pattern = new RegExp( | ||
| PATH_PARAM_PATTERN.source, | ||
| PATH_PARAM_PATTERN.flags | ||
| ); | ||
| let match = pattern.exec(url); | ||
| while (match !== null) { | ||
| params.push(match[1]); | ||
| match = pattern.exec(url); | ||
| } | ||
| return params; | ||
| } | ||
|
|
||
| // --------------------------------------------------------------------------- | ||
| // Main | ||
| // --------------------------------------------------------------------------- | ||
|
|
||
| const openApiUrl = getOpenApiUrl(); | ||
| console.log(`Fetching OpenAPI spec from ${openApiUrl}...`); | ||
| const response = await fetch(openApiUrl); | ||
| if (!response.ok) { | ||
| throw new Error( | ||
| `Failed to fetch OpenAPI spec: ${response.status} ${response.statusText}` | ||
| ); | ||
| } | ||
| const spec = (await response.json()) as OpenApiSpec; | ||
|
|
||
| console.log("Building SDK function name map from @sentry/api..."); | ||
| const sdkMap = await buildSdkFunctionMap(); | ||
|
|
||
| const endpoints: ApiEndpoint[] = []; | ||
| const HTTP_METHODS = ["get", "post", "put", "delete", "patch"]; | ||
|
|
||
| for (const [urlPath, pathItem] of Object.entries(spec.paths)) { | ||
| for (const method of HTTP_METHODS) { | ||
| const operation = pathItem[method] as OpenApiOperation | undefined; | ||
| if (!operation) { | ||
| continue; | ||
| } | ||
|
|
||
| const methodUpper = method.toUpperCase(); | ||
| const sdkKey = `${methodUpper}:${urlPath}`; | ||
| const fn = sdkMap.get(sdkKey) ?? ""; | ||
|
|
||
| const queryParams = (operation.parameters ?? []) | ||
| .filter((p) => p.in === "query") | ||
| .map((p) => p.name); | ||
|
|
||
| endpoints.push({ | ||
| fn, | ||
| method: methodUpper, | ||
| path: urlPath, | ||
| description: (operation.description ?? "").trim(), | ||
| pathParams: extractPathParams(urlPath), | ||
| queryParams, | ||
| deprecated: | ||
| operation.deprecated === true || | ||
| fn.startsWith("deprecated") || | ||
| (operation.operationId ?? "").toLowerCase().includes("deprecated"), | ||
| resource: deriveResource(urlPath), | ||
| operationId: operation.operationId ?? "", | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| // Sort by resource, then method for stable output | ||
| endpoints.sort((a, b) => { | ||
| const resourceCmp = a.resource.localeCompare(b.resource); | ||
| if (resourceCmp !== 0) { | ||
| return resourceCmp; | ||
| } | ||
| return a.operationId.localeCompare(b.operationId); | ||
| }); | ||
|
|
||
| await Bun.write(OUTPUT_PATH, JSON.stringify(endpoints, null, 2)); | ||
|
|
||
| console.log( | ||
| `Generated ${OUTPUT_PATH} (${endpoints.length} endpoints, ${Math.round(JSON.stringify(endpoints).length / 1024)}KB)` | ||
| ); | ||
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.