import { ClientOptions } from './clients/base-client.js'; import { PullRequestClient, CreatePROptions, UpdatePROptions, MergePROptions } from './clients/pull-request-client.js'; import { RepositoryClient } from './clients/repository-client.js'; import { CommentClient, AddCommentOptions, CreateTaskOptions, UpdateTaskOptions } from './clients/comment-client.js'; export type { CreatePROptions, UpdatePROptions, MergePROptions }; export type { AddCommentOptions, CreateTaskOptions, UpdateTaskOptions }; export type { ClientOptions as BitbucketClientOptions }; export class BitbucketClient { private pr!: PullRequestClient; private repo!: RepositoryClient; private comment!: CommentClient; // ── Pull Request read ───────────────────────────────────────────────────── listPullRequests!: (workspace: string, repo: string, options?: any) => Promise; getPullRequest!: (workspace: string, repo: string, prId: number) => Promise; getPullRequestActivities!: (workspace: string, repo: string, prId: number, options?: any) => Promise; getPullRequestChanges!: (workspace: string, repo: string, prId: number, options?: any) => Promise; getPullRequestCommits!: (workspace: string, repo: string, prId: number, options?: any) => Promise; getPullRequestDiff!: (workspace: string, repo: string, prId: number, options?: any) => Promise; getPullRequestPatch!: (workspace: string, repo: string, prId: number) => Promise; getPullRequestParticipants!: (workspace: string, repo: string, prId: number) => Promise; getPullRequestReviewers!: (workspace: string, repo: string, prId: number) => Promise; getPullRequestStatus!: (workspace: string, repo: string, prId: number) => Promise; getPullRequestTasks!: (workspace: string, repo: string, prId: number) => Promise; getPullRequestTaskCount!: (workspace: string, repo: string, prId: number) => Promise; getFullPullRequest!: (workspace: string, repo: string, prId: number) => Promise; // ── Pull Request write ──────────────────────────────────────────────────── createPullRequest!: (workspace: string, repo: string, options: CreatePROptions) => Promise; updatePullRequest!: (workspace: string, repo: string, prId: number, options: UpdatePROptions) => Promise; mergePullRequest!: (workspace: string, repo: string, prId: number, options: MergePROptions) => Promise; declinePullRequest!: (workspace: string, repo: string, prId: number) => Promise; approvePullRequest!: (workspace: string, repo: string, prId: number) => Promise; unapprovePullRequest!: (workspace: string, repo: string, prId: number) => Promise; requestChangesPullRequest!: (workspace: string, repo: string, prId: number) => Promise; removeRequestChangesPullRequest!: (workspace: string, repo: string, prId: number) => Promise; // ── Repository / workspace ──────────────────────────────────────────────── listWorkspaces!: (options?: any) => Promise; listRepositories!: (workspace: string, options?: any) => Promise; getRepository!: (workspace: string, repo: string) => Promise; listBranches!: (workspace: string, repo: string, options?: any) => Promise; // ── Comment read ────────────────────────────────────────────────────────── getPullRequestComments!: (workspace: string, repo: string, prId: number, options?: any) => Promise; getPullRequestComment!: (workspace: string, repo: string, prId: number, commentId: number) => Promise; // ── Comment / task write ────────────────────────────────────────────────── addPullRequestComment!: (workspace: string, repo: string, prId: number, options: AddCommentOptions) => Promise; updatePullRequestComment!: (workspace: string, repo: string, prId: number, commentId: number, options: AddCommentOptions) => Promise; deletePullRequestComment!: (workspace: string, repo: string, prId: number, commentId: number) => Promise; createPullRequestTask!: (workspace: string, repo: string, prId: number, options: CreateTaskOptions) => Promise; updatePullRequestTask!: (workspace: string, repo: string, prId: number, taskId: number, options: UpdateTaskOptions) => Promise; deletePullRequestTask!: (workspace: string, repo: string, prId: number, taskId: number) => Promise; constructor(options: ClientOptions = {}) { this.pr = new PullRequestClient(options); this.repo = new RepositoryClient(options); this.comment = new CommentClient(options); // Bind all PR methods this.listPullRequests = this.pr.listPullRequests.bind(this.pr); this.getPullRequest = this.pr.getPullRequest.bind(this.pr); this.getPullRequestActivities = this.pr.getPullRequestActivities.bind(this.pr); this.getPullRequestChanges = this.pr.getPullRequestChanges.bind(this.pr); this.getPullRequestCommits = this.pr.getPullRequestCommits.bind(this.pr); this.getPullRequestDiff = this.pr.getPullRequestDiff.bind(this.pr); this.getPullRequestPatch = this.pr.getPullRequestPatch.bind(this.pr); this.getPullRequestParticipants = this.pr.getPullRequestParticipants.bind(this.pr); this.getPullRequestReviewers = this.pr.getPullRequestReviewers.bind(this.pr); this.getPullRequestStatus = this.pr.getPullRequestStatus.bind(this.pr); this.getPullRequestTasks = this.pr.getPullRequestTasks.bind(this.pr); this.getPullRequestTaskCount = this.pr.getPullRequestTaskCount.bind(this.pr); this.getFullPullRequest = this.pr.getFullPullRequest.bind(this.pr); this.createPullRequest = this.pr.createPullRequest.bind(this.pr); this.updatePullRequest = this.pr.updatePullRequest.bind(this.pr); this.mergePullRequest = this.pr.mergePullRequest.bind(this.pr); this.declinePullRequest = this.pr.declinePullRequest.bind(this.pr); this.approvePullRequest = this.pr.approvePullRequest.bind(this.pr); this.unapprovePullRequest = this.pr.unapprovePullRequest.bind(this.pr); this.requestChangesPullRequest = this.pr.requestChangesPullRequest.bind(this.pr); this.removeRequestChangesPullRequest = this.pr.removeRequestChangesPullRequest.bind(this.pr); // Bind all repo methods this.listWorkspaces = this.repo.listWorkspaces.bind(this.repo); this.listRepositories = this.repo.listRepositories.bind(this.repo); this.getRepository = this.repo.getRepository.bind(this.repo); this.listBranches = this.repo.listBranches.bind(this.repo); // Bind all comment methods this.getPullRequestComments = this.comment.getPullRequestComments.bind(this.comment); this.getPullRequestComment = this.comment.getPullRequestComment.bind(this.comment); this.addPullRequestComment = this.comment.addPullRequestComment.bind(this.comment); this.updatePullRequestComment = this.comment.updatePullRequestComment.bind(this.comment); this.deletePullRequestComment = this.comment.deletePullRequestComment.bind(this.comment); this.createPullRequestTask = this.comment.createPullRequestTask.bind(this.comment); this.updatePullRequestTask = this.comment.updatePullRequestTask.bind(this.comment); this.deletePullRequestTask = this.comment.deletePullRequestTask.bind(this.comment); } async validateToken(): Promise<{ valid: boolean; message: string }> { try { await this.repo.listWorkspaces({ pagelen: 1 }); return { valid: true, message: 'Token is valid' }; } catch (error: any) { const status = error?.response?.status ?? error?.cause?.response?.status; if (status === 401) return { valid: false, message: 'Token is invalid or expired' }; if (status === 403) return { valid: false, message: 'Token lacks required permissions' }; return { valid: false, message: `Token validation failed: ${error?.message || error}` }; } } } export default BitbucketClient;