import {z} from 'zod' export type Link = {href?: string | undefined, name?: string | undefined}; export type AccountLinks = {avatar?: Link | undefined} | {[key: string]: unknown}; export type Account = {type: string, links?: AccountLinks | undefined, created_on?: string | undefined, display_name?: string | undefined, uuid?: string | undefined} | {[key: string]: unknown}; export type Team = {type: string, links?: ({avatar?: Link | undefined, self?: Link | undefined, html?: Link | undefined, members?: Link | undefined, projects?: Link | undefined, repositories?: Link | undefined} | {[key: string]: unknown}) | undefined, created_on?: string | undefined, display_name?: string | undefined, uuid?: string | undefined} | {[key: string]: unknown}; export type Project = {type: string, links?: ({html?: ({href?: string | undefined, name?: string | undefined}) | undefined, avatar?: ({href?: string | undefined, name?: string | undefined}) | undefined}) | undefined, uuid?: string | undefined, key?: string | undefined, owner?: Team | undefined, name?: string | undefined, description?: string | undefined, is_private?: boolean | undefined, created_on?: string | undefined, updated_on?: string | undefined, has_publicly_visible_repos?: boolean | undefined} | {[key: string]: unknown}; export type Author = {type: string, raw?: string | undefined, user?: Account | undefined} | {[key: string]: unknown}; export type Committer = {type: string, raw?: string | undefined, user?: Account | undefined} | {[key: string]: unknown}; export type BaseCommit = {type: string, hash?: string | undefined, date?: string | undefined, author?: Author | undefined, committer?: Committer | undefined, message?: string | undefined, summary?: ({raw?: string | undefined, markup?: ('markdown' | 'creole' | 'plaintext') | undefined, html?: string | undefined}) | undefined, parents?: Array | undefined} | {[key: string]: unknown}; export type Participant = {type: string, user?: Account | undefined, role?: ('PARTICIPANT' | 'REVIEWER') | undefined, approved?: boolean | undefined, state?: string | undefined, participated_on?: string | undefined} | {[key: string]: unknown}; export type Commit = {type: string, hash?: string | undefined, date?: string | undefined, author?: Author | undefined, committer?: Committer | undefined, message?: string | undefined, summary?: ({raw?: string | undefined, markup?: ('markdown' | 'creole' | 'plaintext') | undefined, html?: string | undefined}) | undefined, parents?: Array | undefined, repository?: Repository | undefined, participants?: Array | undefined} | {[key: string]: unknown}; export type Branch = {type: string, links?: ({self?: ({href?: string | undefined, name?: string | undefined}) | undefined, commits?: ({href?: string | undefined, name?: string | undefined}) | undefined, html?: ({href?: string | undefined, name?: string | undefined}) | undefined}) | undefined, name?: string | undefined, target?: Commit | undefined, merge_strategies?: (Array<'merge_commit' | 'squash' | 'fast_forward' | 'squash_fast_forward' | 'rebase_fast_forward' | 'rebase_merge'>) | undefined, default_merge_strategy?: string | undefined} | {[key: string]: unknown}; export type Repository = {type: string, links?: ({self?: ({href?: string | undefined, name?: string | undefined}) | undefined, html?: ({href?: string | undefined, name?: string | undefined}) | undefined, avatar?: ({href?: string | undefined, name?: string | undefined}) | undefined, pullrequests?: ({href?: string | undefined, name?: string | undefined}) | undefined, commits?: ({href?: string | undefined, name?: string | undefined}) | undefined, forks?: ({href?: string | undefined, name?: string | undefined}) | undefined, watchers?: ({href?: string | undefined, name?: string | undefined}) | undefined, downloads?: ({href?: string | undefined, name?: string | undefined}) | undefined, clone?: (Array<{href?: string | undefined, name?: string | undefined}>) | undefined, hooks?: ({href?: string | undefined, name?: string | undefined}) | undefined}) | undefined, uuid?: string | undefined, full_name?: string | undefined, is_private?: boolean | undefined, parent?: Repository | undefined, scm?: 'git' | undefined, owner?: Account | undefined, name?: string | undefined, description?: string | undefined, created_on?: string | undefined, updated_on?: string | undefined, size?: number | undefined, language?: string | undefined, has_issues?: boolean | undefined, has_wiki?: boolean | undefined, fork_policy?: ('allow_forks' | 'no_public_forks' | 'no_forks') | undefined, enforced_signed_commits?: boolean | undefined, project?: Project | undefined, mainbranch?: Branch | undefined} | {[key: string]: unknown}; export type UpdateApiRepositoriesWorkspaceRepoSlugArgs = {body: Repository}; export type UpdateApiRepositoriesWorkspaceRepoSlugOptions = {baseUrl?: string | undefined, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const link = z.object({href: z.string().optional(), name: z.string().optional()}); export const accountLinks = z.object({avatar: link.optional()}).and(z.record(z.string(), z.unknown())); export const account = z.object({type: z.string(), links: accountLinks.optional(), created_on: z.string().optional(), display_name: z.string().optional(), uuid: z.string().optional()}).and(z.record(z.string(), z.unknown())); export const team = z.object({type: z.string(), links: z.object({avatar: link.optional(), self: link.optional(), html: link.optional(), members: link.optional(), projects: link.optional(), repositories: link.optional()}).and(z.record(z.string(), z.unknown())).optional(), created_on: z.string().optional(), display_name: z.string().optional(), uuid: z.string().optional()}).and(z.record(z.string(), z.unknown())); export const project = z.object({type: z.string(), links: z.object({html: z.object({href: z.string().optional(), name: z.string().optional()}).optional(), avatar: z.object({href: z.string().optional(), name: z.string().optional()}).optional()}).optional(), uuid: z.string().optional(), key: z.string().optional(), owner: team.optional(), name: z.string().optional(), description: z.string().optional(), is_private: z.boolean().optional(), created_on: z.string().optional(), updated_on: z.string().optional(), has_publicly_visible_repos: z.boolean().optional()}).and(z.record(z.string(), z.unknown())); export const author = z.object({type: z.string(), raw: z.string().optional(), user: account.optional()}).and(z.record(z.string(), z.unknown())); export const committer = z.object({type: z.string(), raw: z.string().optional(), user: account.optional()}).and(z.record(z.string(), z.unknown())); export const baseCommit: z.ZodType = z.object({type: z.string(), hash: z.string().optional(), date: z.string().optional(), author: author.optional(), committer: committer.optional(), message: z.string().optional(), summary: z.object({raw: z.string().optional(), markup: z.enum(["markdown", "creole", "plaintext"]).optional(), html: z.string().optional()}).optional(), parents: z.array(z.lazy(() => baseCommit)).optional()}).and(z.record(z.string(), z.unknown())); export const participant = z.object({type: z.string(), user: account.optional(), role: z.enum(["PARTICIPANT", "REVIEWER"]).optional(), approved: z.boolean().optional(), state: z.string().optional(), participated_on: z.string().optional()}).and(z.record(z.string(), z.unknown())); export const commit = z.object({type: z.string(), hash: z.string().optional(), date: z.string().optional(), author: author.optional(), committer: committer.optional(), message: z.string().optional(), summary: z.object({raw: z.string().optional(), markup: z.enum(["markdown", "creole", "plaintext"]).optional(), html: z.string().optional()}).optional(), parents: z.array(baseCommit).optional(), repository: z.lazy(() => repository.optional()), participants: z.array(participant).optional()}).and(z.record(z.string(), z.unknown())); export const branch = z.object({type: z.string(), links: z.object({self: z.object({href: z.string().optional(), name: z.string().optional()}).optional(), commits: z.object({href: z.string().optional(), name: z.string().optional()}).optional(), html: z.object({href: z.string().optional(), name: z.string().optional()}).optional()}).optional(), name: z.string().optional(), target: commit.optional(), merge_strategies: z.array(z.enum(["merge_commit", "squash", "fast_forward", "squash_fast_forward", "rebase_fast_forward", "rebase_merge"])).optional(), default_merge_strategy: z.string().optional()}).and(z.record(z.string(), z.unknown())); export const repository: z.ZodType = z.object({type: z.string(), links: z.object({self: z.object({href: z.string().optional(), name: z.string().optional()}).optional(), html: z.object({href: z.string().optional(), name: z.string().optional()}).optional(), avatar: z.object({href: z.string().optional(), name: z.string().optional()}).optional(), pullrequests: z.object({href: z.string().optional(), name: z.string().optional()}).optional(), commits: z.object({href: z.string().optional(), name: z.string().optional()}).optional(), forks: z.object({href: z.string().optional(), name: z.string().optional()}).optional(), watchers: z.object({href: z.string().optional(), name: z.string().optional()}).optional(), downloads: z.object({href: z.string().optional(), name: z.string().optional()}).optional(), clone: z.array(z.object({href: z.string().optional(), name: z.string().optional()})).optional(), hooks: z.object({href: z.string().optional(), name: z.string().optional()}).optional()}).optional(), uuid: z.string().optional(), full_name: z.string().optional(), is_private: z.boolean().optional(), parent: z.lazy(() => repository.optional()), scm: z.literal("git").optional(), owner: account.optional(), name: z.string().optional(), description: z.string().optional(), created_on: z.string().optional(), updated_on: z.string().optional(), size: z.number().int().optional(), language: z.string().optional(), has_issues: z.boolean().optional(), has_wiki: z.boolean().optional(), fork_policy: z.enum(["allow_forks", "no_public_forks", "no_forks"]).optional(), enforced_signed_commits: z.boolean().optional(), project: project.optional(), mainbranch: branch.optional()}).and(z.record(z.string(), z.unknown())); 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 a repository * * PUT /repositories/{workspace}/{repo_slug} * * Also accepts auth: basic, api_key */ export const updateApiRepositoriesWorkspaceRepoSlug = async (args: UpdateApiRepositoriesWorkspaceRepoSlugArgs, options?: UpdateApiRepositoriesWorkspaceRepoSlugOptions) => { const {baseUrl = "https://api.bitbucket.org/2.0", fetch: fetchFn = fetch, oauth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (oauth2 !== undefined) headers.set('authorization', `Bearer ${oauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/repositories/${encodeURIComponent(String(args.workspace))}/${encodeURIComponent(String(args.repo_slug))}${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(repository, await res.json()) };