import {z} from 'zod' export type GetJIRAIssuesInclude = 'comments' | 'description' | 'github_users' | 'jira_users'; export type GetJIRAIssuesRequest = {account: number, include?: Array | undefined, issues: Array}; export type CreateApiGetJiraIssuesArgs = {body: GetJIRAIssuesRequest}; export type CreateApiGetJiraIssuesOptions = {baseUrl: string, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const includedNativeUser = z.object({avatar: z.string()}); export const jIRAUser = z.object({name: z.string(), avatar: z.string(), type: z.enum(["atlassian", "app", "customer"]), developer: z.string().optional()}); export const jIRAComment = z.object({author: z.string(), created: z.string(), rendered_body: z.string()}); 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 jIRAIssue = z.object({id: z.string(), title: z.string(), created: z.string(), updated: z.string(), work_began: z.string().optional(), resolved: z.string().optional(), acknowledge_time: z.string(), lead_time: z.string().optional(), life_time: z.string(), reporter: z.string(), assignee: z.string().optional(), comments: z.number().int(), comment_list: z.array(jIRAComment).optional(), priority: z.string().nullable(), rendered_description: z.string().optional(), status: z.string(), story_points: z.number().optional(), type: z.string(), url: z.string(), project: z.string(), prs: z.array(pullRequest).optional()}); export const getJIRAIssuesResponse = z.object({include: z.object({github_users: z.record(z.string(), includedNativeUser).optional(), jira_users: z.array(jIRAUser).optional()}).optional(), issues: z.array(jIRAIssue)}); 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 }; /** * Retrieve Jira issues by key. * * POST /get/jira_issues * * Also accepts auth: apiKeyAuth */ export const createApiGetJiraIssues = async (args: CreateApiGetJiraIssuesArgs, options: CreateApiGetJiraIssuesOptions) => { 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/jira_issues${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(getJIRAIssuesResponse, await res.json()) };