feat: add CommentClient with comment and task CRUD
This commit is contained in:
154
src/clients/comment-client.ts
Normal file
154
src/clients/comment-client.ts
Normal file
@@ -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<any> {
|
||||
await this.ensureInitialized();
|
||||
try {
|
||||
const params: Record<string, any> = {};
|
||||
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<any> {
|
||||
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<any> {
|
||||
await this.ensureInitialized();
|
||||
try {
|
||||
const body: Record<string, any> = { 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<any> {
|
||||
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<any> {
|
||||
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<any> {
|
||||
await this.ensureInitialized();
|
||||
try {
|
||||
const body: Record<string, any> = { 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<any> {
|
||||
await this.ensureInitialized();
|
||||
try {
|
||||
const body: Record<string, any> = {};
|
||||
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<any> {
|
||||
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)}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user