import {z} from 'zod' export type TimeZone = number; export type RepositoryID = string; export type RepositorySet = Array; export type DeveloperID = string; export type DeveloperSet = Array; export type FilterCommitsRequest = {account: number, date_from: string, date_to: string, timezone?: TimeZone | undefined, in: RepositorySet, with_author?: DeveloperSet | undefined, with_committer?: DeveloperSet | undefined, property: 'bypassing_prs' | 'everything' | 'no_pr_merges', only_default_branch?: boolean | undefined}; export type CreateApiFilterCommitsArgs = {body: FilterCommitsRequest}; export type CreateApiFilterCommitsOptions = {baseUrl: string, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const developerID = z.string(); export const commitSignature = z.object({login: developerID.optional(), name: z.string().optional(), email: z.string().optional(), timestamp: z.string(), timezone: z.number()}); export const repositoryID = z.string(); export const commit = z.object({author: commitSignature, children: z.array(z.string()).optional(), committer: commitSignature, deployments: z.array(z.string()).optional(), files_changed: z.number().int().gte(0), hash: z.string(), message: z.string(), repository: repositoryID, size_added: z.number().int().gte(0), size_removed: z.number().int().gte(0)}); 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 includedNativeUser = z.object({avatar: z.string()}); export const commitsList = z.object({data: z.array(commit), include: z.object({deployments: z.record(z.string(), deploymentNotification).optional(), users: z.record(z.string(), includedNativeUser)})}); 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 commits that match the specified query. * * POST /filter/commits * * Also accepts auth: apiKeyAuth */ export const createApiFilterCommits = async (args: CreateApiFilterCommitsArgs, options: CreateApiFilterCommitsOptions) => { 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/commits${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(commitsList, await res.json()) };