import {z} from 'zod' export type ProtosCommonV1FloatRange = {max?: number | undefined, min?: number | undefined}; export type ProtosOpenapiV1SearchScansRequest = {branch?: string | undefined, cursor?: string | undefined, deploymentId?: unknown | undefined, is_full_scan?: number | undefined, limit?: number | undefined, products?: (Array<'PRODUCT_UNSPECIFIED' | 'PRODUCT_SAST' | 'PRODUCT_SCA' | 'PRODUCT_SECRETS' | 'PRODUCT_AI_SAST'>) | undefined, repository_id?: number | undefined, since?: string | undefined, statuses?: number | undefined, total_time?: ProtosCommonV1FloatRange | undefined}; export type CreateApiApiV1DeploymentsDeploymentIdScansSearchArgs = {deploymentId: unknown, body: ProtosOpenapiV1SearchScansRequest}; export type CreateApiApiV1DeploymentsDeploymentIdScansSearchOptions = {baseUrl: string, SemgrepWebToken?: string | undefined, fetch?: typeof fetch | undefined}; export const protosScanV1ScanFindingsCounts = z.object({code: z.string().optional(), secrets: z.string().optional(), supply_chain: z.string().optional(), total: z.string().optional()}); export const protosScanV1ScanPublic = z.object({branch: z.string().optional(), commit: z.string().optional(), completed_at: z.string().optional(), deployment_id: z.string().optional(), enabled_products: z.array(z.string()).optional(), exit_code: z.string().optional(), findings_counts: protosScanV1ScanFindingsCounts.optional(), id: z.string().optional(), is_full_scan: z.boolean().optional(), repository_id: z.string().optional(), started_at: z.string().optional(), status: z.enum(["SCAN_STATUS_RUNNING", "SCAN_STATUS_COMPLETED", "SCAN_STATUS_PENDING", "SCAN_STATUS_CANCELLED", "SCAN_STATUS_ERROR", "SCAN_STATUS_NEVER_FINISHED"]).optional(), total_time: z.number().optional()}); export const protosOpenapiV1SearchScansResponse = z.object({cursor: z.string().optional(), scans: z.array(protosScanV1ScanPublic).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 scans (beta) * * POST /api/v1/deployments/{deploymentId}/scans/search */ export const createApiApiV1DeploymentsDeploymentIdScansSearch = async (args: CreateApiApiV1DeploymentsDeploymentIdScansSearchArgs, options: CreateApiApiV1DeploymentsDeploymentIdScansSearchOptions) => { const {baseUrl, fetch: fetchFn = fetch, SemgrepWebToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (SemgrepWebToken !== undefined) headers.set('authorization', `Bearer ${SemgrepWebToken}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/deployments/${encodeURIComponent(String(args.deploymentId))}/scans/search${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(protosOpenapiV1SearchScansResponse, await res.json()) };