diff --git a/src/clients/comment-client.ts b/src/clients/comment-client.ts new file mode 100644 index 0000000..3e5611e --- /dev/null +++ b/src/clients/comment-client.ts @@ -0,0 +1,154 @@ +import { BaseClient, ClientOptions } from './base-client.js'; + +export interface AddCommentOptions { + content: string; + inline?: { path: string; to: number }; + parent_id?: number; +} + +export interface CreateTaskOptions { + content: string; + comment_id?: number; +} + +export interface UpdateTaskOptions { + content?: string; + state?: 'RESOLVED' | 'UNRESOLVED'; +} + +export class CommentClient extends BaseClient { + constructor(options: ClientOptions = {}) { + super(options); + } + + async getPullRequestComments( + workspace: string, repoSlug: string, prId: number, + options?: { limit?: number; start?: number }, + ): Promise { + await this.ensureInitialized(); + try { + const params: Record = {}; + if (options?.limit) params.limit = options.limit; + if (options?.start !== undefined) params.start = options.start; + const response = await this.axiosInstance.get( + `/repositories/${workspace}/${repoSlug}/pullrequests/${prId}/comments`, + { params }, + ); + return response.data; + } catch (error) { + throw new Error(`Failed to get comments: ${this.formatError(error)}`); + } + } + + async getPullRequestComment( + workspace: string, repoSlug: string, prId: number, commentId: number, + ): Promise { + await this.ensureInitialized(); + try { + const response = await this.axiosInstance.get( + `/repositories/${workspace}/${repoSlug}/pullrequests/${prId}/comments/${commentId}`, + ); + return response.data; + } catch (error) { + throw new Error(`Failed to get comment: ${this.formatError(error)}`); + } + } + + async addPullRequestComment( + workspace: string, repoSlug: string, prId: number, opts: AddCommentOptions, + ): Promise { + await this.ensureInitialized(); + try { + const body: Record = { content: { raw: opts.content } }; + if (opts.inline) body.inline = opts.inline; + if (opts.parent_id !== undefined) body.parent = { id: opts.parent_id }; + const response = await this.axiosInstance.post( + `/repositories/${workspace}/${repoSlug}/pullrequests/${prId}/comments`, + body, + ); + return response.data; + } catch (error) { + throw new Error(`Failed to add comment: ${this.formatError(error)}`); + } + } + + async updatePullRequestComment( + workspace: string, repoSlug: string, prId: number, commentId: number, + opts: { content: string }, + ): Promise { + await this.ensureInitialized(); + try { + const response = await this.axiosInstance.put( + `/repositories/${workspace}/${repoSlug}/pullrequests/${prId}/comments/${commentId}`, + { content: { raw: opts.content } }, + ); + return response.data; + } catch (error) { + throw new Error(`Failed to update comment: ${this.formatError(error)}`); + } + } + + async deletePullRequestComment( + workspace: string, repoSlug: string, prId: number, commentId: number, + ): Promise { + await this.ensureInitialized(); + try { + await this.axiosInstance.delete( + `/repositories/${workspace}/${repoSlug}/pullrequests/${prId}/comments/${commentId}`, + ); + return { success: true }; + } catch (error) { + throw new Error(`Failed to delete comment: ${this.formatError(error)}`); + } + } + + async createPullRequestTask( + workspace: string, repoSlug: string, prId: number, opts: CreateTaskOptions, + ): Promise { + await this.ensureInitialized(); + try { + const body: Record = { content: { raw: opts.content } }; + if (opts.comment_id !== undefined) body.comment = { id: opts.comment_id }; + const response = await this.axiosInstance.post( + `/repositories/${workspace}/${repoSlug}/pullrequests/${prId}/tasks`, + body, + ); + return response.data; + } catch (error) { + throw new Error(`Failed to create task: ${this.formatError(error)}`); + } + } + + async updatePullRequestTask( + workspace: string, repoSlug: string, prId: number, taskId: number, + opts: UpdateTaskOptions, + ): Promise { + await this.ensureInitialized(); + try { + const body: Record = {}; + if (opts.content !== undefined) body.content = { raw: opts.content }; + if (opts.state !== undefined) body.state = opts.state; + const response = await this.axiosInstance.put( + `/repositories/${workspace}/${repoSlug}/pullrequests/${prId}/tasks/${taskId}`, + body, + ); + return response.data; + } catch (error) { + throw new Error(`Failed to update task: ${this.formatError(error)}`); + } + } + + async deletePullRequestTask( + workspace: string, repoSlug: string, prId: number, taskId: number, + ): Promise { + await this.ensureInitialized(); + try { + await this.axiosInstance.delete( + `/repositories/${workspace}/${repoSlug}/pullrequests/${prId}/tasks/${taskId}`, + ); + return { success: true }; + } catch (error) { + throw new Error(`Failed to delete task: ${this.formatError(error)}`); + } + } +}