import {z} from 'zod' export type ReleasePair = {old: string, new: string}; export type DiffReleasesRequest = {account: number, borders: {[key: string]: Array}}; export type CreateApiDiffReleasesArgs = {body: DiffReleasesRequest}; export type CreateApiDiffReleasesOptions = {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 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 releaseDiff = z.object({old: z.string(), new: z.string(), releases: z.array(filteredRelease)}); export const diffedReleases = z.object({include: z.object({users: z.record(z.string(), includedNativeUser), jira: z.record(z.string(), linkedJIRAIssue).optional()}), data: z.record(z.string(), z.array(releaseDiff))}); 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 between the two given ones per repository. * * POST /diff/releases * * Also accepts auth: apiKeyAuth */ export const createApiDiffReleases = async (args: CreateApiDiffReleasesArgs, options: CreateApiDiffReleasesOptions) => { 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(/\/+$/, '')}/diff/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(diffedReleases, await res.json()) };