import {z} from 'zod' export type GetApiTeamsTeamIdInsightsOwnershipArgs = {team_id: string}; export type GetApiTeamsTeamIdInsightsOwnershipOptions = {baseUrl: string, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const teamOwnershipRollup = z.object({team_id: z.string(), repo_count: z.number().int().optional(), repos_analyzed: z.number().int().optional(), total_owners: z.number().int().optional(), bus_factor_risk_file_count: z.number().int().optional(), bus_factor_heuristic: z.string().optional()}); export const teamOwnerEntry = z.object({key: z.string(), name: z.string(), email: z.string().nullable().optional(), files_owned: z.number().int().optional(), hotspots_owned: z.number().int().optional(), bus_factor_risk_files: z.number().int().optional(), commit_count_90d: z.number().int().nullable().optional(), silo_modules: z.number().int().optional(), repos_touched: z.number().int().optional(), last_commit_at: z.string().nullable().optional()}); export const teamBusFactorFile = z.object({repo_name: z.string(), file_path: z.string(), primary_owner_name: z.string().nullable().optional(), bus_factor: z.number().int().optional(), is_hotspot: z.boolean().optional(), churn_percentile: z.number().optional()}); export const teamOwnershipResponse = z.object({rollup: teamOwnershipRollup, top_owners: z.array(teamOwnerEntry), bus_factor_files: z.array(teamBusFactorFile)}); 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 }; /** * Get Team Ownership * * GET /teams/{team_id}/insights/ownership */ export const getApiTeamsTeamIdInsightsOwnership = async (args: GetApiTeamsTeamIdInsightsOwnershipArgs, options: GetApiTeamsTeamIdInsightsOwnershipOptions) => { const {baseUrl, fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/teams/${encodeURIComponent(String(args.team_id))}/insights/ownership${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(teamOwnershipResponse, await res.json()) };