import {z} from 'zod' export type GetApiServiceOwnerUsernameReposArgs = {active?: boolean | undefined, names?: Array | undefined, owner_username: string, page?: number | undefined, page_size?: number | undefined, search?: string | undefined, service: 'bitbucket' | 'bitbucket_server' | 'github' | 'github_enterprise' | 'gitlab' | 'gitlab_enterprise'}; export type GetApiServiceOwnerUsernameReposOptions = {baseUrl?: string | undefined, tokenAuth?: string | undefined, fetch?: typeof fetch | undefined}; 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 repo = z.object({name: z.string(), private: z.boolean(), updatestamp: z.string(), author: owner, language: z.string(), branch: z.string(), active: z.boolean(), activated: z.boolean(), totals: commitTotals}); export const paginatedRepoList = z.object({count: z.number().int(), next: z.string().nullable().optional(), previous: z.string().nullable().optional(), results: z.array(repo)}); 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 }; /** * Repository list * * GET /{service}/{owner_username}/repos/ */ export const getApiServiceOwnerUsernameRepos = async (args: GetApiServiceOwnerUsernameReposArgs, options?: GetApiServiceOwnerUsernameReposOptions) => { 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.active != null) query.append("active", String(args.active)) for (const item of args.names ?? []) query.append("names", String(item)) if (args.page != null) query.append("page", String(args.page)) if (args.page_size != null) query.append("page_size", String(args.page_size)) if (args.search != null) query.append("search", String(args.search)) if (tokenAuth !== undefined) headers.set('authorization', `Bearer ${tokenAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/${encodeURIComponent(String(args.service))}/${encodeURIComponent(String(args.owner_username))}/repos/${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(paginatedRepoList, await res.json()) };