import {z} from 'zod' export type RepositoryID = string; export type RepositorySet = Array; export type DeveloperID = string; export type CommitPushers = Array; export type JIRAStatusCategory = 'To Do' | 'In Progress' | 'Done' | 'No Category'; export type TimeZone = number; export type FilterCodeChecksRequest = {in: RepositorySet, triggered_by?: CommitPushers | undefined, labels_include?: Array | undefined, labels_exclude?: Array | undefined, jira?: ({epics?: Array | undefined, labels_include?: Array | undefined, labels_exclude?: Array | undefined, issue_types?: Array | undefined, priorities?: Array | undefined, projects?: Array | undefined, status_categories?: Array | undefined, unmapped?: boolean | undefined}) | undefined, date_from: string, date_to: string, timezone?: TimeZone | undefined, account: number, quantiles?: Array | undefined}; export type CreateApiFilterCodeChecksArgs = {body: FilterCodeChecksRequest}; export type CreateApiFilterCodeChecksOptions = {baseUrl: string, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const repositoryID = z.string(); export const timeDuration = z.string(); export const filteredCodeCheckRun = z.object({title: z.string(), repository: repositoryID, last_execution_time: z.string(), last_execution_url: z.string().optional(), total_stats: z.object({count: z.number().int(), successes: z.number().int().gte(0), critical: z.boolean(), mean_execution_time: z.union([z.string().nullable(), timeDuration]), stddev_execution_time: z.union([z.string().nullable(), timeDuration]), median_execution_time: z.union([z.string().nullable(), timeDuration]), skips: z.number().int().gte(0), flaky_count: z.number().int().gte(0), count_timeline: z.array(z.number().int().gte(0)), successes_timeline: z.array(z.number().int().gte(0)), mean_execution_time_timeline: z.array(timeDuration), median_execution_time_timeline: z.array(timeDuration)}), prs_stats: z.object({count: z.number().int(), successes: z.number().int().gte(0), critical: z.boolean(), mean_execution_time: z.union([z.string().nullable(), timeDuration]), stddev_execution_time: z.union([z.string().nullable(), timeDuration]), median_execution_time: z.union([z.string().nullable(), timeDuration]), skips: z.number().int().gte(0), flaky_count: z.number().int().gte(0), count_timeline: z.array(z.number().int().gte(0)), successes_timeline: z.array(z.number().int().gte(0)), mean_execution_time_timeline: z.array(timeDuration), median_execution_time_timeline: z.array(timeDuration)}), size_groups: z.array(z.number().int().gte(1))}); export const filteredCodeCheckRuns = z.object({timeline: z.array(z.string()), items: z.array(filteredCodeCheckRun)}); 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 }; /** * Find code check runs that match the specified query. [What a code check run is exactly.](https://docs.github.com/en/rest/guides/getting-started-with-the-checks-api#about-check-runs) * * POST /filter/code_checks * * Also accepts auth: apiKeyAuth */ export const createApiFilterCodeChecks = async (args: CreateApiFilterCodeChecksArgs, options: CreateApiFilterCodeChecksOptions) => { const {baseUrl, fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/filter/code_checks${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(filteredCodeCheckRuns, await res.json()) };