0
List pull requests
One script reply has been approved by the moderators Verified

Draft pull requests are available in public repositories with GitHub Free and GitHub Free for organizations, GitHub Pro, and legacy per-repository billing plans, and in public and private repositories with GitHub Team and GitHub Enterprise Cloud. For more information, see GitHub's products in the GitHub Help documentation.

Created by hugo697 200 days ago Viewed 6175 times
0
Submitted by hugo697 Typescript (fetch-only)
Verified 200 days ago
1
type Github = {
2
  token: string;
3
};
4
/**
5
 * List pull requests
6
 * Draft pull requests are available in public repositories with GitHub Free and GitHub Free for organizations, GitHub Pro, and legacy per-repository billing plans, and in public and private repositories with GitHub Team and GitHub Enterprise Cloud. For more information, see [GitHub's products](https://docs.github.com/github/getting-started-with-github/githubs-products) in the GitHub Help documentation.
7
 */
8
export async function main(
9
  auth: Github,
10
  owner: string,
11
  repo: string,
12
  state: "open" | "closed" | "all" | undefined,
13
  head: string | undefined,
14
  base: string | undefined,
15
  sort: "created" | "updated" | "popularity" | "long-running" | undefined,
16
  direction: "asc" | "desc" | undefined,
17
  per_page: string | undefined,
18
  page: string | undefined
19
) {
20
  const url = new URL(`https://api.github.com/repos/${owner}/${repo}/pulls`);
21
  for (const [k, v] of [
22
    ["state", state],
23
    ["head", head],
24
    ["base", base],
25
    ["sort", sort],
26
    ["direction", direction],
27
    ["per_page", per_page],
28
    ["page", page],
29
  ]) {
30
    if (v !== undefined && v !== "") {
31
      url.searchParams.append(k, v);
32
    }
33
  }
34
  const response = await fetch(url, {
35
    method: "GET",
36
    headers: {
37
      Authorization: "Bearer " + auth.token,
38
    },
39
    body: undefined,
40
  });
41
  if (!response.ok) {
42
    const text = await response.text();
43
    throw new Error(`${response.status} ${text}`);
44
  }
45
  return await response.json();
46
}
47