Some checks failed
Build and Release / Test (push) Has been cancelled
Build and Release / Build Windows (push) Has been cancelled
Build and Release / Build Linux (push) Has been cancelled
Build and Release / Build macOS (push) Has been cancelled
Build and Release / Create Release (push) Has been cancelled
Build and Release / Development Build (push) Has been cancelled
109 lines
2.4 KiB
TypeScript
109 lines
2.4 KiB
TypeScript
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<PullRequest[]> {
|
|
const { data } = await this.client.get<PullRequest[]>(
|
|
`/repos/${owner}/${repo}/pulls`,
|
|
{ params: options },
|
|
);
|
|
return data;
|
|
}
|
|
|
|
async getPullRequest(owner: string, repo: string, index: number): Promise<PullRequest> {
|
|
const { data } = await this.client.get<PullRequest>(
|
|
`/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<PullRequest> {
|
|
const { data } = await this.client.post<PullRequest>(
|
|
`/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<PullRequest> {
|
|
const { data } = await this.client.patch<PullRequest>(
|
|
`/repos/${owner}/${repo}/pulls/${index}`,
|
|
payload,
|
|
);
|
|
return data;
|
|
}
|
|
|
|
async mergePullRequest(
|
|
owner: string,
|
|
repo: string,
|
|
index: number,
|
|
method?: "merge" | "rebase" | "squash",
|
|
): Promise<void> {
|
|
await this.client.post(`/repos/${owner}/${repo}/pulls/${index}/merge`, {
|
|
Do: method || "merge",
|
|
});
|
|
}
|
|
|
|
async isPullRequestMerged(owner: string, repo: string, index: number): Promise<boolean> {
|
|
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<Commit[]> {
|
|
const { data } = await this.client.get<Commit[]>(
|
|
`/repos/${owner}/${repo}/pulls/${index}/commits`,
|
|
);
|
|
return data;
|
|
}
|
|
|
|
async listPullRequestFiles(
|
|
owner: string,
|
|
repo: string,
|
|
index: number,
|
|
): Promise<FileContent[]> {
|
|
const { data } = await this.client.get<FileContent[]>(
|
|
`/repos/${owner}/${repo}/pulls/${index}/files`,
|
|
);
|
|
return data;
|
|
}
|
|
}
|