import {z} from 'zod' export type HealthStatus = 'healthy' | 'flaky' | 'broken'; export type TestStateValue = 'quarantined' | 'skipped'; export type GetApiCiOwnerRepositoriesRepositorySearchTestsArgs = {repository: string, owner: string, test_name?: (Array | null) | undefined, test_name_exclude?: (Array | null) | undefined, pipeline_name?: (Array | null) | undefined, pipeline_name_exclude?: (Array | null) | undefined, job_name?: (Array | null) | undefined, job_name_exclude?: (Array | null) | undefined, health_status?: (Array | null) | undefined, health_status_exclude?: (Array | null) | undefined, failed_executions_impact?: (Array<'low' | 'medium' | 'high'> | null) | undefined, failed_executions_impact_exclude?: (Array<'low' | 'medium' | 'high'> | null) | undefined, state?: (Array | null) | undefined, state_exclude?: (Array | null) | undefined, is_quarantined?: (boolean | null) | undefined, cursor?: (string | null) | undefined, per_page?: number | undefined, sort?: ('executions_count' | 'test_name' | 'pipeline_name' | 'job_name' | 'health_status' | 'state' | 'failed_executions_impact' | 'failed_executions_ratio') | undefined, direction?: ('asc' | 'desc') | undefined, branch_name?: (string | null) | undefined}; export type GetApiCiOwnerRepositoriesRepositorySearchTestsOptions = {baseUrl: string, AdminApplicationKey?: string | undefined, fetch?: typeof fetch | undefined}; export const testStateValue = z.enum(["quarantined", "skipped"]); export const healthStatus = z.enum(["healthy", "flaky", "broken"]); export const testSearchResult = z.object({test_id: z.string(), test_name: z.string(), pipeline_name: z.string(), job_name: z.string(), state: testStateValue.nullable(), health_status: healthStatus, failed_executions_impact: z.enum(["low", "medium", "high"]).nullable()}); export const searchTestsResponse = z.object({size: z.number().int(), per_page: z.number().int(), tests: z.array(testSearchResult), total: 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 }; /** * Search tests * * GET /ci/{owner}/repositories/{repository}/search/tests * * Also accepts auth: MergifyTokenBearerAuth, GitHubTokenBearerAuth */ export const getApiCiOwnerRepositoriesRepositorySearchTests = async (args: GetApiCiOwnerRepositoriesRepositorySearchTestsArgs, options: GetApiCiOwnerRepositoriesRepositorySearchTestsOptions) => { const {baseUrl, fetch: fetchFn = fetch, AdminApplicationKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") for (const item of args.test_name ?? []) query.append("test_name", String(item)) for (const item of args.test_name_exclude ?? []) query.append("test_name_exclude", String(item)) for (const item of args.pipeline_name ?? []) query.append("pipeline_name", String(item)) for (const item of args.pipeline_name_exclude ?? []) query.append("pipeline_name_exclude", String(item)) for (const item of args.job_name ?? []) query.append("job_name", String(item)) for (const item of args.job_name_exclude ?? []) query.append("job_name_exclude", String(item)) for (const item of args.health_status ?? []) query.append("health_status", String(item)) for (const item of args.health_status_exclude ?? []) query.append("health_status_exclude", String(item)) for (const item of args.failed_executions_impact ?? []) query.append("failed_executions_impact", String(item)) for (const item of args.failed_executions_impact_exclude ?? []) query.append("failed_executions_impact_exclude", String(item)) for (const item of args.state ?? []) query.append("state", String(item)) for (const item of args.state_exclude ?? []) query.append("state_exclude", String(item)) if (args.is_quarantined != null) query.append("is_quarantined", String(args.is_quarantined)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (args.per_page != null) query.append("per_page", String(args.per_page)) if (args.sort != null) query.append("sort", String(args.sort)) if (args.direction != null) query.append("direction", String(args.direction)) if (args.branch_name != null) query.append("branch_name", String(args.branch_name)) if (AdminApplicationKey !== undefined) headers.set('authorization', `Bearer ${AdminApplicationKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/ci/${encodeURIComponent(String(args.owner))}/repositories/${encodeURIComponent(String(args.repository))}/search/tests${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(searchTestsResponse, await res.json()) };