import {z} from 'zod' export type GetApiV3EnterpriseCodeScansFindingsArgs = {after?: (string | null) | undefined, first?: number | undefined, scan_id?: (string | null) | undefined, repo_name?: (string | null) | undefined, severity?: (Array<'critical' | 'high' | 'medium' | 'low'> | null) | undefined, status?: (Array<'open' | 'dismissed' | 'resolved'> | null) | undefined, org_ids?: (Array | null) | undefined}; export type GetApiV3EnterpriseCodeScansFindingsOptions = {baseUrl: string, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const codeScanReferenceSnippetResponse = z.object({code: z.string().nullable().optional(), commentary: z.string(), end_line: z.number().int(), file_path: z.string(), start_line: z.number().int()}); export const codeScanFindingResponse = z.object({category: z.string().nullable(), code_owners: z.array(z.string()), created_at: z.number().int(), description: z.string().nullable(), finding_id: z.string(), note: z.string().nullable(), orchestrator_session_id: z.string().nullable(), pr_url: z.string().nullable(), recommendation: z.string().nullable(), reference_snippets: z.array(codeScanReferenceSnippetResponse), related_finding_ids: z.array(z.string()).optional(), repo_name: z.string(), scan_id: z.string(), session_id: z.string().nullable(), severity: z.enum(["critical", "high", "medium", "low"]), status: z.enum(["open", "dismissed", "resolved"]), title: z.string().nullable()}); export const paginatedResponseCodeScanFindingResponse = z.object({end_cursor: z.string().nullable().optional(), has_next_page: z.boolean().optional(), items: z.array(codeScanFindingResponse), total: z.number().int().nullable().optional()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * List Code Scan Findings * * GET /v3/enterprise/code-scans/findings */ export const getApiV3EnterpriseCodeScansFindings = async (args: GetApiV3EnterpriseCodeScansFindingsArgs, options: GetApiV3EnterpriseCodeScansFindingsOptions) => { const {baseUrl, fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.after != null) query.append("after", String(args.after)) if (args.first != null) query.append("first", String(args.first)) if (args.scan_id != null) query.append("scan_id", String(args.scan_id)) if (args.repo_name != null) query.append("repo_name", String(args.repo_name)) for (const item of args.severity ?? []) query.append("severity", String(item)) for (const item of args.status ?? []) query.append("status", String(item)) for (const item of args.org_ids ?? []) query.append("org_ids", String(item)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v3/enterprise/code-scans/findings${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(paginatedResponseCodeScanFindingResponse, await res.json()) };