import {z} from 'zod' export type GetApiServiceOwnerUsernameReposRepoNameCompareArgs = {base?: string | undefined, head?: string | undefined, owner_username: string, pullid?: number | undefined, repo_name: string, service: 'bitbucket' | 'bitbucket_server' | 'github' | 'github_enterprise' | 'gitlab' | 'gitlab_enterprise'}; export type GetApiServiceOwnerUsernameReposRepoNameCompareOptions = {baseUrl?: string | undefined, tokenAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const reportTotals = z.object({files: z.number().int(), lines: z.number().int(), hits: z.number().int(), misses: z.number().int(), partials: z.number().int(), coverage: z.number(), branches: z.number().int(), methods: z.number().int(), messages: z.number().int(), sessions: z.number().int(), complexity: z.number(), complexity_total: z.number(), complexity_ratio: z.number(), diff: z.unknown()}); export const totalsComparison = z.object({base: reportTotals, head: reportTotals, patch: reportTotals}); export const serviceEnum = z.enum(["github", "gitlab", "bitbucket", "github_enterprise", "gitlab_enterprise", "bitbucket_server"]); export const owner = z.object({service: serviceEnum, username: z.string().nullable(), name: z.string().nullable()}); export const commitTotals = z.object({files: z.number().int(), lines: z.number().int(), hits: z.number().int(), misses: z.number().int(), partials: z.number().int(), coverage: z.number(), branches: z.number().int(), methods: z.number().int(), sessions: z.number().int(), complexity: z.number(), complexity_total: z.number(), complexity_ratio: z.number(), diff: z.number().int()}); export const stateB73Enum = z.enum(["complete", "pending", "error", "skipped"]); export const commit = z.object({commitid: z.string(), message: z.string(), timestamp: z.string(), ci_passed: z.boolean(), author: owner, branch: z.string(), totals: commitTotals, state: stateB73Enum, parent: z.string()}); export const comparison = z.object({base_commit: z.string(), head_commit: z.string(), totals: totalsComparison, commit_uploads: z.array(commit), diff: z.record(z.string(), z.unknown()), files: z.array(z.record(z.string(), z.unknown())), untracked: z.array(z.string())}); 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 }; /** * Comparison * * GET /{service}/{owner_username}/repos/{repo_name}/compare/ */ export const getApiServiceOwnerUsernameReposRepoNameCompare = async (args: GetApiServiceOwnerUsernameReposRepoNameCompareArgs, options?: GetApiServiceOwnerUsernameReposRepoNameCompareOptions) => { const {baseUrl = "https://api.codecov.io/api/v2", fetch: fetchFn = fetch, tokenAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.base != null) query.append("base", String(args.base)) if (args.head != null) query.append("head", String(args.head)) if (args.pullid != null) query.append("pullid", String(args.pullid)) if (tokenAuth !== undefined) headers.set('authorization', `Bearer ${tokenAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/${encodeURIComponent(String(args.service))}/${encodeURIComponent(String(args.owner_username))}/repos/${encodeURIComponent(String(args.repo_name))}/compare/${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(comparison, await res.json()) };