import type { AxiosInstance } from "axios"; import type { PullRequest, Commit, FileContent } from "./types"; export class PullRequestAPI { constructor(private client: AxiosInstance) {} async listPullRequests( owner: string, repo: string, options?: { state?: "open" | "closed" | "all"; page?: number; limit?: number }, ): Promise { const { data } = await this.client.get( `/repos/${owner}/${repo}/pulls`, { params: options }, ); return data; } async getPullRequest(owner: string, repo: string, index: number): Promise { const { data } = await this.client.get( `/repos/${owner}/${repo}/pulls/${index}`, ); return data; } async createPullRequest( owner: string, repo: string, payload: { title: string; body?: string; head: string; base: string; assignees?: string[]; labels?: number[]; milestone?: number; }, ): Promise { const { data } = await this.client.post( `/repos/${owner}/${repo}/pulls`, payload, ); return data; } async editPullRequest( owner: string, repo: string, index: number, payload: Partial<{ title: string; body: string; state: "open" | "closed"; base: string; assignees: string[]; labels: number[]; milestone: number; }>, ): Promise { const { data } = await this.client.patch( `/repos/${owner}/${repo}/pulls/${index}`, payload, ); return data; } async mergePullRequest( owner: string, repo: string, index: number, method?: "merge" | "rebase" | "squash", ): Promise { await this.client.post(`/repos/${owner}/${repo}/pulls/${index}/merge`, { Do: method || "merge", }); } async isPullRequestMerged(owner: string, repo: string, index: number): Promise { try { await this.client.get(`/repos/${owner}/${repo}/pulls/${index}/merge`); return true; } catch { return false; } } async listPullRequestCommits( owner: string, repo: string, index: number, ): Promise { const { data } = await this.client.get( `/repos/${owner}/${repo}/pulls/${index}/commits`, ); return data; } async listPullRequestFiles( owner: string, repo: string, index: number, ): Promise { const { data } = await this.client.get( `/repos/${owner}/${repo}/pulls/${index}/files`, ); return data; } }