import {z} from 'zod' export type TimeZone = number; export type JIRAFilterReturn = 'epics' | 'issues' | 'issue_bodies' | 'labels' | 'issue_types' | 'priorities' | 'statuses' | 'users' | 'only_flying'; export type FilterJIRAStuff = {account: number, date_from: string | null, date_to: string | null, timezone?: TimeZone | undefined, priorities?: Array | undefined, types?: Array | undefined, labels_include?: Array | undefined, labels_exclude?: Array | undefined, exclude_inactive: boolean, with?: ({assignees?: (Array) | undefined, reporters?: Array | undefined, commenters?: Array | undefined}) | undefined, projects?: Array | undefined, return?: Array | undefined}; export type CreateApiFilterJiraArgs = {body: FilterJIRAStuff}; export type CreateApiFilterJiraOptions = {baseUrl: string, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const jIRAComment = z.object({author: z.string(), created: z.string(), rendered_body: z.string()}); export const jIRAEpicChild = 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(), subtasks: z.number().int(), prs: z.number().int()}); export const jIRAEpic = 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(), children: z.array(jIRAEpicChild).optional(), prs: z.number().int()}); 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 jIRAIssueType = z.object({name: z.string(), normalized_name: z.string(), count: z.number().int().gte(1), image: z.string(), project: z.string(), is_subtask: z.boolean(), is_epic: z.boolean()}); export const jIRALabel = z.object({title: z.string(), last_used: z.string(), issues_count: z.number().int(), kind: z.string().optional()}); export const jIRAPriority = z.object({name: z.string(), image: z.string(), rank: z.number().int().gte(1), color: color}); export const jIRAStatusCategory = z.enum(["To Do", "In Progress", "Done", "No Category"]); export const jIRAStatus = z.object({name: z.string(), stage: jIRAStatusCategory, project: 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 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 includedDeployments = z.object({deployments: z.record(z.string(), deploymentNotification).optional()}); export const filteredJIRAStuff = z.object({epics: z.array(jIRAEpic), issues: z.array(jIRAIssue), issue_types: z.array(jIRAIssueType), labels: z.array(jIRALabel), priorities: z.array(jIRAPriority), statuses: z.array(jIRAStatus), users: z.array(jIRAUser), deployments: includedDeployments.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 }; /** * Find various JIRA entities relevant to the specified date interval. * * POST /filter/jira * * Also accepts auth: apiKeyAuth */ export const createApiFilterJira = async (args: CreateApiFilterJiraArgs, options: CreateApiFilterJiraOptions) => { 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/jira${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(filteredJIRAStuff, await res.json()) };