import {z} from 'zod' export type TimeZone = number; export type RepositoryID = string; export type RepositorySet = Array; export type DeveloperID = string; export type ReleaseWith = {pr_author?: Array | undefined, commit_author?: Array | undefined, releaser?: Array | 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 FilterReleasesRequest = {account: number, date_from: string, date_to: string, timezone?: TimeZone | undefined, in?: RepositorySet | undefined, with?: ReleaseWith | undefined, labels_include?: Array | undefined, labels_exclude?: Array | undefined, jira?: JIRAFilter | undefined}; export type CreateApiFilterReleasesArgs = {body: FilterReleasesRequest}; export type CreateApiFilterReleasesOptions = {baseUrl: string, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const includedNativeUser = z.object({avatar: z.string()}); 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 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 developerID = z.string(); export const releasedPullRequest = z.object({additions: z.number().int(), author: z.union([developerID, z.string().nullable()]), created: z.string(), deletions: z.number().int(), jira: z.array(z.string()).optional(), number: z.number().int(), title: z.string()}); export const filteredRelease = z.object({added_lines: z.number().int().gte(0), age: z.string(), commits: z.number().int().gte(0), commit_authors: z.array(developerID), deleted_lines: z.number().int().gte(0), deployments: z.array(z.string()).optional(), name: z.string(), prs: z.array(releasedPullRequest), published: z.string(), publisher: z.string().nullable(), repository: z.string(), sha: z.string().min(40).max(40), url: z.string()}); export const releaseSet = z.object({include: z.object({users: z.record(z.string(), includedNativeUser), jira: z.record(z.string(), linkedJIRAIssue).optional(), deployments: z.record(z.string(), deploymentNotification).optional()}).optional(), data: z.array(filteredRelease).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 releases that were published in the given time fram in the given repositories. * * POST /filter/releases * * Also accepts auth: apiKeyAuth */ export const createApiFilterReleases = async (args: CreateApiFilterReleasesArgs, options: CreateApiFilterReleasesOptions) => { 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/releases${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(releaseSet, await res.json()) };