import {z} from 'zod' export type GetApiStepsArgs = {nodeId: string}; export type GetApiStepsOptions = {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()}); export const pipelineStep = 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(), stageId: z.string().optional(), inputStep: inputStep.optional()}); export const stepList = z.object({steps: z.array(pipelineStep).optional(), runIsComplete: z.boolean().optional()}); export const stepListResponse = z.object({status: z.literal("ok").optional(), data: stepList.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 steps for a specific node * * GET /steps */ export const getApiSteps = async (args: GetApiStepsArgs, options?: GetApiStepsOptions) => { 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") if (args.nodeId != null) query.append("nodeId", String(args.nodeId)) const url = `${baseUrl.replace(/\/+$/, '')}/steps${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(stepListResponse, await res.json()) };