import {z} from 'zod' export type RepositoryID = string; export type PullRequestNumbers = {repository: RepositoryID, numbers: Array}; export type GetPullRequestsRequest = {account: number, prs: Array, environments?: Array | undefined}; export type CreateApiGetPullRequestsArgs = {body: GetPullRequestsRequest}; export type CreateApiGetPullRequestsOptions = {baseUrl: string, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const includedNativeUser = z.object({avatar: z.string()}); export const repositoryID = z.string(); export const deployedComponent = z.object({repository: repositoryID, reference: z.string().min(1)}); export const deploymentConclusion = z.enum(["SUCCESS", "FAILURE", "CANCELLED"]); export const deploymentLabels = z.object({}); export const deploymentNotification = z.object({components: z.array(deployedComponent), environment: z.string().min(1), name: z.string().nullable().optional(), url: z.string().optional(), date_started: z.string(), date_finished: z.string(), conclusion: deploymentConclusion, labels: deploymentLabels.optional()}); export const timeDuration = z.string(); export const stageTimings = z.object({wip: timeDuration, review: timeDuration.optional(), merge: timeDuration.optional(), release: timeDuration.optional(), deploy: z.record(z.string(), timeDuration).optional()}); export const pullRequestEvent = z.enum(["created", "committed", "review_requested", "reviewed", "approved", "changes_requested", "merged", "released", "rejected", "deployed"]); export const pullRequestStage = z.enum(["wip", "reviewing", "merging", "releasing", "force_push_dropped", "done", "release_ignored", "deployed"]); export const pullRequestParticipant = z.object({id: z.string(), status: z.array(z.enum(["author", "reviewer", "commit_author", "commit_committer", "commenter", "merger", "releaser"]))}); export const color = z.string(); export const pullRequestLabel = z.object({name: z.string(), description: z.string().optional(), color: color}); export const linkedJIRAIssue = z.object({id: z.string(), title: z.string(), epic: z.string().optional(), labels: z.array(z.string()).optional(), type: z.string()}); export const pullRequest = z.object({repository: z.string(), number: z.number().int().gte(1), title: z.string(), size_added: z.number().int().gte(0), size_removed: z.number().int().gte(0), files_changed: z.number().int().gte(0), created: z.string(), updated: z.string(), closed: z.string().optional(), comments: z.number().int().gte(0), commits: z.number().int().gte(0), review_requested: z.string().optional(), first_review: z.string().optional(), approved: z.string().optional(), review_comments: z.number().int().gte(0).optional(), reviews: z.number().int().gte(0).optional(), merged: z.string().optional(), merged_with_failed_check_runs: z.array(z.string()).optional(), released: z.string().optional(), release_url: z.string().optional(), deployments: z.array(z.string()).optional(), stage_timings: stageTimings, events_time_machine: z.array(pullRequestEvent).optional(), stages_time_machine: z.array(pullRequestStage).optional(), events_now: z.array(pullRequestEvent), stages_now: z.array(pullRequestStage), participants: z.array(pullRequestParticipant), labels: z.array(pullRequestLabel).optional(), jira: z.array(linkedJIRAIssue).optional()}); export const pullRequestSet = z.object({include: z.object({users: z.record(z.string(), includedNativeUser), deployments: z.record(z.string(), deploymentNotification).optional()}), data: z.array(pullRequest)}); 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 }; /** * List pull requests by repository and number. * * POST /get/pull_requests * * Also accepts auth: apiKeyAuth */ export const createApiGetPullRequests = async (args: CreateApiGetPullRequestsArgs, options: CreateApiGetPullRequestsOptions) => { 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(/\/+$/, '')}/get/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(pullRequestSet, await res.json()) };