import {z} from 'zod' export type GetApiTreeOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const pipelineState = z.enum(["queued", "running", "paused", "skipped", "not_built", "finished", "success", "unstable", "failure", "unknown", "aborted"]); export const inputStep = z.object({message: z.string().optional(), cancel: z.string().optional(), id: z.string().optional(), ok: z.string().optional(), parameters: z.boolean().optional(), canProceed: z.boolean().optional(), canAbort: z.boolean().optional(), approvers: z.array(z.string()).optional()}); export const pipelineStage: z.ZodType = z.object({id: z.string().optional(), name: z.string().optional(), state: pipelineState.optional(), type: z.string().optional(), title: z.string().optional(), pauseDurationMillis: z.number().int().optional(), startTimeMillis: z.number().int().optional(), totalDurationMillis: z.unknown(), children: z.array(z.lazy(() => pipelineStage)).optional(), seqContainerName: z.unknown(), nextSibling: z.lazy(() => pipelineStage.optional()), isSequential: z.boolean().optional(), synthetic: z.boolean().optional(), placeholder: z.boolean().optional(), agent: z.unknown(), causeOfBlockage: z.unknown(), url: z.string().optional(), inputStep: inputStep.optional()}); export const pipelineGraph = z.object({complete: z.boolean().optional(), stages: z.array(pipelineStage).optional()}); export const pipelineTreeResponse = z.object({status: z.literal("ok").optional(), data: pipelineGraph.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 pipeline stage tree * * GET /tree */ export const getApiTree = async (options?: GetApiTreeOptions) => { const {baseUrl = "http://localhost:8080/jenkins/job/my-pipeline/1/stages", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/tree${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(pipelineTreeResponse, await res.json()) };