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 BotPrAttributionModel = {method: 'merge_actor' | 'assignee' | 'none'}; export type IncludeFileExtensionsModel = {patterns?: Array | undefined}; export type ActiveBranchesDurationWriteModel = {days: 3 | 7 | 14}; export type IssueStartTimeModel = {method: 'first_transition' | 'last_transition'}; export type GitSettingsWriteModel = {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, bot_pr_attribution?: BotPrAttributionModel | undefined, include_file_extensions?: IncludeFileExtensionsModel | undefined, active_branches_duration?: ActiveBranchesDurationWriteModel | undefined, issue_start_time?: IssueStartTimeModel | undefined}; export type PatchApiApiV1GitConfigArgs = {body: GitSettingsWriteModel}; export type PatchApiApiV1GitConfigOptions = {baseUrl?: string | undefined, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const releasesDetectionModel = z.object({method: z.enum(["tags", "pr_to_branch", "merge_to_branch", "api", "deployment"]), pattern: z.string().nullable().optional()}); export const draftPullRequestsModel = z.object({labels: z.array(z.string()).optional(), title_pattern: z.string().nullable().optional()}); export const defaultBranchModel = z.object({branch_name: z.string().nullable().optional()}); export const patternsModel = z.object({patterns: z.array(z.string()).optional()}); export const codingTimeStrategyModel = z.object({method: z.enum(["first_commit", "issue_state_pr"])}); export const botPrAttributionModel = z.object({method: z.enum(["merge_actor", "assignee", "none"])}); export const includeFileExtensionsModel = z.object({patterns: z.array(z.string()).optional()}); export const activeBranchesDurationModel = z.object({days: z.number().int().gte(3).lte(14)}); export const issueStartTimeModel = z.object({method: z.enum(["first_transition", "last_transition"])}); export const gitSettingsModel = z.object({releases_detection: releasesDetectionModel, draft_pull_requests: draftPullRequestsModel, default_branch: defaultBranchModel, exclude_branches: patternsModel, exclude_base_branches: patternsModel, exclude_file_extensions: patternsModel, coding_time_strategy: codingTimeStrategyModel, bot_pr_attribution: botPrAttributionModel, include_file_extensions: includeFileExtensionsModel, active_branches_duration: activeBranchesDurationModel, issue_start_time: issueStartTimeModel}); export const gitConfigResponseModel = z.object({settings: gitSettingsModel}); 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 Git Configuration * * PATCH /api/v1/git/config */ export const patchApiApiV1GitConfig = async (args: PatchApiApiV1GitConfigArgs, options?: PatchApiApiV1GitConfigOptions) => { 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/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(gitConfigResponseModel, await res.json()) };