import {z} from 'zod' export type GetApiCiOwnerRepositoriesRepositoryTestsRunsArgs = {repository: string, owner: string, head_sha: string}; export type GetApiCiOwnerRepositoriesRepositoryTestsRunsOptions = {baseUrl: string, AdminApplicationKey?: string | undefined, fetch?: typeof fetch | undefined}; export const testRunUploader = z.object({method: z.enum(["plugin", "report", "unknown"]), name: z.string().nullable(), version: z.string().nullable()}); export const testRun = z.object({job_leg: z.string().nullable(), run_attempt: z.number().int().nullable(), framework: z.string().nullable(), framework_version: z.string().nullable(), uploader: testRunUploader, tests: z.number().int(), failed: z.number().int(), skipped: z.number().int(), dropped_tests: z.number().int(), ingestion: z.enum(["complete", "in_progress", "incomplete"]), ended_at: z.string()}); export const jobTestRuns = z.object({pipeline_name: z.string().nullable(), job_name: z.string().nullable(), test_results: z.enum(["results_received", "upload_error", "ingesting"]), test_runs: z.array(testRun)}); export const testRunsResponse = z.object({head_sha: z.string(), test_results_ingestion: z.enum(["enabled", "not_enabled", "paused"]), pending_uploads: z.number().int(), jobs: z.array(jobTestRuns)}); 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 }; /** * List the test runs of a commit * * GET /ci/{owner}/repositories/{repository}/tests/runs * * Also accepts auth: CIApplicationKey, MergifyTokenBearerAuth, GitHubTokenBearerAuth */ export const getApiCiOwnerRepositoriesRepositoryTestsRuns = async (args: GetApiCiOwnerRepositoriesRepositoryTestsRunsArgs, options: GetApiCiOwnerRepositoriesRepositoryTestsRunsOptions) => { const {baseUrl, fetch: fetchFn = fetch, AdminApplicationKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.head_sha != null) query.append("head_sha", String(args.head_sha)) if (AdminApplicationKey !== undefined) headers.set('authorization', `Bearer ${AdminApplicationKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/ci/${encodeURIComponent(String(args.owner))}/repositories/${encodeURIComponent(String(args.repository))}/tests/runs${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(testRunsResponse, await res.json()) };