import {z} from 'zod' export type ProtosAiV1CreateAutofixRequest = Record; export type CreateApiApiV1DeploymentsDeploymentIdIssuesIssueIdAutofixArgs = {deploymentId: number, issueId: number, body: ProtosAiV1CreateAutofixRequest}; export type CreateApiApiV1DeploymentsDeploymentIdIssuesIssueIdAutofixOptions = {baseUrl: string, SemgrepWebToken?: string | undefined, fetch?: typeof fetch | undefined}; export const protosAiV1AutofixError = z.object({errorType: z.enum(["AUTOFIX_ERROR_TYPE_ISSUE_NOT_FOUND", "AUTOFIX_ERROR_TYPE_UNSUPPORTED_ISSUE_TYPE", "AUTOFIX_ERROR_TYPE_AUTOFIX_NOT_ENABLED", "AUTOFIX_ERROR_TYPE_INVALID_REPOSITORY", "AUTOFIX_ERROR_TYPE_NO_WRITE_ACCESS", "AUTOFIX_ERROR_TYPE_SCM_CONFIG_NOT_FOUND", "AUTOFIX_ERROR_TYPE_SCM_TOKEN_FAILED", "AUTOFIX_ERROR_TYPE_CODE_ACCESS_NOT_GRANTED", "AUTOFIX_ERROR_TYPE_CREDITS_EXHAUSTED", "AUTOFIX_ERROR_TYPE_IN_FLIGHT_FIX_PENDING", "AUTOFIX_ERROR_TYPE_NO_RECOMMENDED_UPGRADE", "AUTOFIX_ERROR_TYPE_INVALID_ISSUE_STATE"]).optional(), message: z.string().optional()}); export const protosAiV1CreateAutofixResponse = z.object({errors: z.array(protosAiV1AutofixError).optional(), success: z.boolean().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 }; /** * Create Autofix * * POST /api/v1/deployments/{deploymentId}/issues/{issueId}/autofix * * Also accepts auth: SemgrepJWT */ export const createApiApiV1DeploymentsDeploymentIdIssuesIssueIdAutofix = async (args: CreateApiApiV1DeploymentsDeploymentIdIssuesIssueIdAutofixArgs, options: CreateApiApiV1DeploymentsDeploymentIdIssuesIssueIdAutofixOptions) => { 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))}/issues/${encodeURIComponent(String(args.issueId))}/autofix${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(protosAiV1CreateAutofixResponse, await res.json()) };