import {z} from 'zod' export type ReleasesDetectionWriteModel = {method: 'tags' | 'pr_to_branch' | 'merge_to_branch' | 'api', pattern?: (string | null) | undefined, backfill_days?: (1 | 90) | undefined}; export type DraftPullRequestsModel = {labels?: Array | undefined, title_pattern?: (string | null) | undefined}; export type DefaultBranchModel = {branch_name?: (string | null) | undefined}; export type PatternsModel = {patterns?: Array | undefined}; export type CodingTimeStrategyWriteModel = {method: 'first_commit' | 'issue_state_pr', backfill_days?: (1 | 90) | undefined}; export type RepositorySettingsWriteModel = {releases_detection?: ReleasesDetectionWriteModel | undefined, draft_pull_requests?: DraftPullRequestsModel | undefined, default_branch?: DefaultBranchModel | undefined, exclude_branches?: PatternsModel | undefined, exclude_base_branches?: PatternsModel | undefined, exclude_file_extensions?: PatternsModel | undefined, coding_time_strategy?: CodingTimeStrategyWriteModel | undefined}; export type PatchApiApiV1GitRepositoriesRepoIdConfigArgs = {repo_id: number, body: RepositorySettingsWriteModel}; export type PatchApiApiV1GitRepositoriesRepoIdConfigOptions = {baseUrl?: string | undefined, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const repositorySettingSectionModel = z.object({value: z.object({}), source: z.enum(["global", "repository"]), global_default: z.object({}).nullable().optional()}); export const repositoryConfigResponseModel = z.object({repository_id: z.number().int(), repository_name: z.string().nullable().optional(), settings: z.record(z.string(), repositorySettingSectionModel)}); 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 }; /** * Update Repository Configuration * * PATCH /api/v1/git/repositories/{repo_id}/config */ export const patchApiApiV1GitRepositoriesRepoIdConfig = async (args: PatchApiApiV1GitRepositoriesRepoIdConfigArgs, options?: PatchApiApiV1GitRepositoriesRepoIdConfigOptions) => { const {baseUrl = "https://public-api.linearb.io", fetch: fetchFn = fetch, APIKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (APIKeyHeader !== undefined) headers.set("x-api-key", APIKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/git/repositories/${encodeURIComponent(String(args.repo_id))}/config${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(repositoryConfigResponseModel, await res.json()) };