import {z} from 'zod' export type ProtosAiV1CreateFixJobRequest = Record; export type CreateApiApiAgentDeploymentsDeploymentIdIssuesIssueIdFixJobsArgs = {deploymentId: number, issueId: number, body: ProtosAiV1CreateFixJobRequest}; export type CreateApiApiAgentDeploymentsDeploymentIdIssuesIssueIdFixJobsOptions = {baseUrl: string, SemgrepWebToken?: string | undefined, fetch?: typeof fetch | undefined}; export const protosAiV1GitHubAppInfo = z.object({appId: z.number().int().optional(), appName: z.string().optional(), installationTarget: z.string().optional(), configUrl: z.string().optional()}); export const protosAiV1FixJobError = z.object({errorType: z.enum(["FIX_JOB_ERROR_TYPE_NO_WRITE_ACCESS", "FIX_JOB_ERROR_TYPE_INVALID_REPOSITORY", "FIX_JOB_ERROR_TYPE_SCM_CONFIG_NOT_FOUND", "FIX_JOB_ERROR_TYPE_CREDITS_EXHAUSTED", "FIX_JOB_ERROR_TYPE_SCM_TOKEN_FAILED", "FIX_JOB_ERROR_TYPE_CODE_ACCESS_NOT_GRANTED"]).optional(), message: z.string().optional(), githubAppInfo: protosAiV1GitHubAppInfo.optional()}); export const protosAiV1CreateFixJobResponse = z.object({errors: z.array(protosAiV1FixJobError).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 Fix Job * * POST /api/agent/deployments/{deploymentId}/issues/{issueId}/fix_jobs * * Also accepts auth: SemgrepJWT */ export const createApiApiAgentDeploymentsDeploymentIdIssuesIssueIdFixJobs = async (args: CreateApiApiAgentDeploymentsDeploymentIdIssuesIssueIdFixJobsArgs, options: CreateApiApiAgentDeploymentsDeploymentIdIssuesIssueIdFixJobsOptions) => { 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/agent/deployments/${encodeURIComponent(String(args.deploymentId))}/issues/${encodeURIComponent(String(args.issueId))}/fix_jobs${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(protosAiV1CreateFixJobResponse, await res.json()) };