import {z} from 'zod' export type GetApiRepositoriesWorkspaceRepoSlugHooksArgs = {workspace: string, repo_slug: string}; export type GetApiRepositoriesWorkspaceRepoSlugHooksOptions = {baseUrl?: string | undefined, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const object = z.object({type: z.string()}).and(z.record(z.string(), z.unknown())); export const webhooksSubscriptionLink = z.object({href: z.string().optional()}); export const webhooksSubscriptionLinks = z.object({self: webhooksSubscriptionLink.optional()}).and(z.record(z.string(), z.unknown())); export const webhookSubscription = z.object({type: z.string(), uuid: z.string().optional(), url: z.string().optional(), description: z.string().optional(), subject_type: z.enum(["repository", "workspace"]).optional(), subject: object.optional(), active: z.boolean().optional(), created_at: z.string().optional(), events: z.array(z.enum(["issue:comment_created", "issue:created", "issue:updated", "pipeline:span_created", "project:updated", "pullrequest:approved", "pullrequest:changes_request_created", "pullrequest:changes_request_removed", "pullrequest:comment_created", "pullrequest:comment_deleted", "pullrequest:comment_reopened", "pullrequest:comment_resolved", "pullrequest:comment_updated", "pullrequest:created", "pullrequest:fulfilled", "pullrequest:push", "pullrequest:rejected", "pullrequest:unapproved", "pullrequest:updated", "repo:commit_comment_created", "repo:commit_status_created", "repo:commit_status_updated", "repo:created", "repo:deleted", "repo:fork", "repo:imported", "repo:push", "repo:transfer", "repo:updated"])).optional(), secret_set: z.boolean().optional(), secret: z.string().max(128).optional(), links: webhooksSubscriptionLinks.optional()}).and(z.record(z.string(), z.unknown())); export const paginatedWebhookSubscriptions = z.object({values: z.array(webhookSubscription).optional(), pagelen: z.number().int().gte(1).lte(100).optional(), next: z.string().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 }; /** * List webhooks for a repository * * GET /repositories/{workspace}/{repo_slug}/hooks * * Also accepts auth: basic, api_key */ export const getApiRepositoriesWorkspaceRepoSlugHooks = async (args: GetApiRepositoriesWorkspaceRepoSlugHooksArgs, options?: GetApiRepositoriesWorkspaceRepoSlugHooksOptions) => { const {baseUrl = "https://api.bitbucket.org/2.0", fetch: fetchFn = fetch, oauth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() 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))}/hooks${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(paginatedWebhookSubscriptions, await res.json()) };