import {z} from 'zod' export type JobMetadata = {scoutVersion?: string | undefined, errorMessage?: string | undefined, errorStack?: string | undefined}; export type PatchJob = {status: 'COMPLETED' | 'FAILED', metadata?: JobMetadata | undefined}; export type PatchApiOrgsOrgIdProjectsProjectIdScoutTasksJobIdArgs = {'x-redocly-scout-version'?: string | undefined, orgId: string, projectId: string, jobId: unknown, body: PatchJob}; export type PatchApiOrgsOrgIdProjectsProjectIdScoutTasksJobIdOptions = {baseUrl?: string | undefined, ApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const gitCommitCheck = z.object({name: z.string(), status: z.enum(["IN_PROGRESS", "SUCCESS", "FAILED"]), description: z.string().optional(), targetUrl: z.string().optional()}); export const jobMetadata = z.object({scoutVersion: z.string().optional(), errorMessage: z.string().optional(), errorStack: z.string().optional()}); export const job = z.object({id: z.string(), status: z.enum(["PENDING", "PROCESSING", "COMPLETED", "FAILED"]), type: z.enum(["PROCESS_GIT_REPO", "UPDATE_STATUS"]), organizationId: z.string().optional(), projectId: z.string().optional(), providerId: z.string().optional(), providerType: z.enum(["GITHUB_CLOUD", "GITHUB_SERVER", "GITLAB_CLOUD", "GITLAB_SELF_MANAGED", "BITBUCKET_CLOUD", "BITBUCKET_DATACENTER", "AZURE"]), namespaceId: z.string(), repositoryId: z.string(), branch: z.string(), commitSha: z.string(), isMainBranch: z.boolean().optional(), prId: z.string().optional(), attempts: z.number().int(), parentJobId: z.string().optional(), checks: z.array(gitCommitCheck).optional(), metadata: jobMetadata.optional(), startedAt: z.string().optional(), createdAt: z.string(), updatedAt: z.string()}); 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 }; /** * Update job * * PATCH /orgs/{orgId}/projects/{projectId}/scout/tasks/{jobId} */ export const patchApiOrgsOrgIdProjectsProjectIdScoutTasksJobId = async (args: PatchApiOrgsOrgIdProjectsProjectIdScoutTasksJobIdArgs, options?: PatchApiOrgsOrgIdProjectsProjectIdScoutTasksJobIdOptions) => { const {baseUrl = "https://app.cloud.redocly.com/api", fetch: fetchFn = fetch, ApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['x-redocly-scout-version'] != null) headers.set("x-redocly-scout-version", String(args['x-redocly-scout-version'])) if (ApiKey !== undefined) headers.set('authorization', `Bearer ${ApiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/orgs/${encodeURIComponent(String(args.orgId))}/projects/${encodeURIComponent(String(args.projectId))}/scout/tasks/${encodeURIComponent(String(args.jobId))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(job, await res.json()) };