import {z} from 'zod' export type GetApiReposOwnerRepositoryStatsQueuesEnteredCountArgs = {repository: string, owner: string, base_ref?: (Array | null) | undefined, queue_name?: (Array | null) | undefined, priority_rule_name?: (Array | null) | undefined, start_at?: (string | null) | undefined, end_at?: (string | null) | undefined}; export type GetApiReposOwnerRepositoryStatsQueuesEnteredCountOptions = {baseUrl: string, AdminApplicationKey?: string | undefined, fetch?: typeof fetch | undefined}; export const enteredInInterval = z.object({start: z.string(), end: z.string(), entered: z.number().int()}); export const enteredGroupResponse = z.object({base_ref: z.string(), queue_name: z.string(), priority_rule_name: z.string().nullable(), stats: z.array(enteredInInterval)}); export const enteredCountResponse = z.object({groups: z.array(enteredGroupResponse)}); 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 the count of pull requests entering the queues * * GET /repos/{owner}/{repository}/stats/queues_entered_count * * Also accepts auth: MergifyTokenBearerAuth, GitHubTokenBearerAuth */ export const getApiReposOwnerRepositoryStatsQueuesEnteredCount = async (args: GetApiReposOwnerRepositoryStatsQueuesEnteredCountArgs, options: GetApiReposOwnerRepositoryStatsQueuesEnteredCountOptions) => { 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.base_ref ?? []) query.append("base_ref", String(item)) for (const item of args.queue_name ?? []) query.append("queue_name", String(item)) for (const item of args.priority_rule_name ?? []) query.append("priority_rule_name", String(item)) if (args.start_at != null) query.append("start_at", String(args.start_at)) if (args.end_at != null) query.append("end_at", String(args.end_at)) if (AdminApplicationKey !== undefined) headers.set('authorization', `Bearer ${AdminApplicationKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/repos/${encodeURIComponent(String(args.owner))}/${encodeURIComponent(String(args.repository))}/stats/queues_entered_count${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(enteredCountResponse, await res.json()) };