import {z} from 'zod' export type GetApiApiV2DeploymentsDeploymentIdScansScanIdArgs = {deploymentId: unknown, scanId: unknown}; export type GetApiApiV2DeploymentsDeploymentIdScansScanIdOptions = {baseUrl: string, SemgrepWebToken?: string | undefined, fetch?: typeof fetch | undefined}; export const protosScanV2ScanFindingsCounts = z.object({total: z.number().int().optional(), code: z.number().int().optional(), supplyChain: z.number().int().optional(), secrets: z.number().int().optional()}); export const protosScanV2Scan = z.object({id: z.string().optional(), deploymentId: z.string().optional(), projectId: z.string().optional(), branchId: z.string().optional(), commit: z.string().optional(), startTime: z.string().optional(), completeTime: z.string().optional(), exitCode: z.string().optional(), isFullScan: z.boolean().optional(), duration: z.string().optional(), findingsCounts: protosScanV2ScanFindingsCounts.optional(), status: z.enum(["SCAN_STATUS_RUNNING", "SCAN_STATUS_COMPLETED", "SCAN_STATUS_PENDING", "SCAN_STATUS_CANCELLED", "SCAN_STATUS_ERROR", "SCAN_STATUS_NEVER_FINISHED"]).optional(), enabledProducts: z.array(z.enum(["PRODUCT_UNSPECIFIED", "PRODUCT_SAST", "PRODUCT_SCA", "PRODUCT_SECRETS", "PRODUCT_AI_SAST"])).optional()}); export const protosScanV2GetScanResponse = z.object({scan: protosScanV2Scan.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 }; /** * Get Scan * * GET /api/v2/deployments/{deploymentId}/scans/{scanId} * * Also accepts auth: SemgrepJWT */ export const getApiApiV2DeploymentsDeploymentIdScansScanId = async (args: GetApiApiV2DeploymentsDeploymentIdScansScanIdArgs, options: GetApiApiV2DeploymentsDeploymentIdScansScanIdOptions) => { const {baseUrl, fetch: fetchFn = fetch, SemgrepWebToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (SemgrepWebToken !== undefined) headers.set('authorization', `Bearer ${SemgrepWebToken}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v2/deployments/${encodeURIComponent(String(args.deploymentId))}/scans/${encodeURIComponent(String(args.scanId))}${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(protosScanV2GetScanResponse, await res.json()) };