import {z} from 'zod' export type RepositoryID = string; export type LogicalPRRules = {title?: string | undefined, labels_include?: Array | undefined}; export type ReleaseMatchStrategy = 'branch' | 'event' | 'tag' | 'tag_or_branch'; export type ReleaseMatchSetting = {branches: string, tags: string, events?: string | undefined, match: ReleaseMatchStrategy, default_branch?: string | undefined}; export type LogicalDeploymentRules = {title?: string | undefined, labels_include?: {[key: string]: Array} | undefined}; export type LogicalRepositoryRequest = {account: number, name: string, parent: RepositoryID, prs: LogicalPRRules, releases: ReleaseMatchSetting, deployments?: LogicalDeploymentRules | undefined}; export type UpdateApiSettingsLogicalRepositoryArgs = {body: LogicalRepositoryRequest}; export type UpdateApiSettingsLogicalRepositoryOptions = {baseUrl: string, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const repositoryID = z.string(); export const logicalPRRules = z.object({title: z.string().optional(), labels_include: z.array(z.string()).optional()}); export const releaseMatchStrategy = z.enum(["branch", "event", "tag", "tag_or_branch"]); export const releaseMatchSetting = z.object({branches: z.string(), tags: z.string(), events: z.string().optional(), match: releaseMatchStrategy, default_branch: z.string().optional()}); export const logicalDeploymentRules = z.object({title: z.string().optional(), labels_include: z.record(z.string(), z.array(z.string())).optional()}); export const logicalRepository = z.object({name: z.string().max(100), parent: repositoryID, prs: logicalPRRules, releases: releaseMatchSetting, deployments: logicalDeploymentRules.optional()}); 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 }; /** * 🛡️👤 Insert or update a logical repository - a named set of rules to match PRs, releases, * and deployments that pretends to be a regular GitHub repository everywhere in UI and * API requests. * * * PUT /settings/logical_repository */ export const updateApiSettingsLogicalRepository = async (args: UpdateApiSettingsLogicalRepositoryArgs, options: UpdateApiSettingsLogicalRepositoryOptions) => { const {baseUrl, fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/settings/logical_repository${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(logicalRepository, await res.json()) };