import {z} from 'zod' export type GetApiCiOwnerRepositoriesRepositoryTestSelectionFailureFirstArgs = {repository: string, owner: string}; export type GetApiCiOwnerRepositoriesRepositoryTestSelectionFailureFirstOptions = {baseUrl: string, AdminApplicationKey?: string | undefined, fetch?: typeof fetch | undefined}; export const failureFirstStatus = z.object({state: z.enum(["not_served", "active"]), since: z.string(), steps: z.number().int(), stopped_early_steps: z.number().int(), estimated_test_time_saved_ms: z.number().int(), unmeasured_stopped_early_jobs: z.number().int()}); 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 what Fail First did on a repository * * GET /ci/{owner}/repositories/{repository}/test-selection/failure-first * * Also accepts auth: MergifyTokenBearerAuth, GitHubTokenBearerAuth */ export const getApiCiOwnerRepositoriesRepositoryTestSelectionFailureFirst = async (args: GetApiCiOwnerRepositoriesRepositoryTestSelectionFailureFirstArgs, options: GetApiCiOwnerRepositoriesRepositoryTestSelectionFailureFirstOptions) => { const {baseUrl, fetch: fetchFn = fetch, AdminApplicationKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (AdminApplicationKey !== undefined) headers.set('authorization', `Bearer ${AdminApplicationKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/ci/${encodeURIComponent(String(args.owner))}/repositories/${encodeURIComponent(String(args.repository))}/test-selection/failure-first${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(failureFirstStatus, await res.json()) };