import {z} from 'zod' export type TimeZone = number; export type RepositoryID = string; export type RepositorySet = Array; export type PullRequestEvent = 'created' | 'committed' | 'review_requested' | 'reviewed' | 'approved' | 'changes_requested' | 'merged' | 'released' | 'rejected' | 'deployed'; export type PullRequestStage = 'wip' | 'reviewing' | 'merging' | 'releasing' | 'force_push_dropped' | 'done' | 'release_ignored' | 'deployed'; export type DeveloperID = string; export type DeveloperSet = Array; export type PullRequestWith = {author?: DeveloperSet | undefined, reviewer?: DeveloperSet | undefined, commit_author?: DeveloperSet | undefined, commit_committer?: DeveloperSet | undefined, commenter?: DeveloperSet | undefined, merger?: DeveloperSet | undefined, releaser?: DeveloperSet | undefined}; export type JIRAStatusCategory = 'To Do' | 'In Progress' | 'Done' | 'No Category'; export type JIRAFilter = {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}; export type FilterPullRequestsRequest = {account: number, date_from: string, date_to: string, timezone?: TimeZone | undefined, in: RepositorySet, events?: Array | undefined, stages?: Array | undefined, with?: PullRequestWith | undefined, labels_include?: Array | undefined, labels_exclude?: Array | undefined, exclude_inactive: boolean, jira?: JIRAFilter | undefined, updated_from?: string | undefined, updated_to?: string | undefined, limit?: (number | null) | undefined, environments?: Array | undefined}; export type PaginatePullRequestsRequest = {batch: number, request: FilterPullRequestsRequest}; export type CreateApiPaginatePullRequestsArgs = {body: PaginatePullRequestsRequest}; export type CreateApiPaginatePullRequestsOptions = {baseUrl: string, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const pullRequestPaginationPlan = z.object({updated: z.array(z.string()).optional()}); 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 }; /** * Compute the balanced pagination plan for `/filter/pull_requests`. * * POST /paginate/pull_requests * * Also accepts auth: apiKeyAuth */ export const createApiPaginatePullRequests = async (args: CreateApiPaginatePullRequestsArgs, options: CreateApiPaginatePullRequestsOptions) => { 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(/\/+$/, '')}/paginate/pull_requests${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(pullRequestPaginationPlan, await res.json()) };