import {z} from 'zod' export type GetApiCiOwnerRepositoriesRepositoryTestsStatsStatArgs = {stat: 'total_tests' | 'healthy_tests' | 'broken_tests' | 'flaky_tests' | 'failed_executions' | 'median_test_run_duration', repository: string, owner: string, test_name?: (Array | null) | undefined, test_name_exclude?: (Array | null) | undefined, job_name?: (Array | null) | undefined, job_name_exclude?: (Array | null) | undefined, pipeline_name?: (Array | null) | undefined, pipeline_name_exclude?: (Array | null) | undefined, failed_executions_impact?: (Array<'low' | 'medium' | 'high'> | null) | undefined, failed_executions_impact_exclude?: (Array<'low' | 'medium' | 'high'> | null) | undefined, branch_name?: (string | null) | undefined}; export type GetApiCiOwnerRepositoriesRepositoryTestsStatsStatOptions = {baseUrl: string, AdminApplicationKey?: string | undefined, fetch?: typeof fetch | undefined}; export const testStatBucket = z.object({start: z.string(), end: z.string(), value: z.number().int()}); export const getTestStatResponse = z.object({stat: z.enum(["total_tests", "healthy_tests", "broken_tests", "flaky_tests", "failed_executions", "median_test_run_duration"]), value: z.number().int(), value_over_time: z.array(testStatBucket)}); 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 a single test stat as a time series * * GET /ci/{owner}/repositories/{repository}/tests/stats/{stat} * * Also accepts auth: MergifyTokenBearerAuth, GitHubTokenBearerAuth */ export const getApiCiOwnerRepositoriesRepositoryTestsStatsStat = async (args: GetApiCiOwnerRepositoriesRepositoryTestsStatsStatArgs, options: GetApiCiOwnerRepositoriesRepositoryTestsStatsStatOptions) => { 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.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.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.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)) 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))}/tests/stats/${encodeURIComponent(String(args.stat))}${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(getTestStatResponse, await res.json()) };