0

List Pull Requests

by
Published today

List a repository's pull requests, filtered by state, head or base branch. Paginate with page.

Script github Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 1 hour ago
1
//native
2

3
/**
4
 * List Pull Requests
5
 * List a repository's pull requests, filtered by state, head or base branch. Paginate with page.
6
 */
7
export async function main(
8
  auth: RT.Github,
9
  owner: string,
10
  repo: string,
11
  state: "open" | "closed" | "all" | undefined,
12
  head: string | undefined,
13
  base: string | undefined,
14
  sort: "created" | "updated" | "popularity" | "long-running" | undefined,
15
  direction: "asc" | "desc" | undefined,
16
  per_page: number = 30,
17
  page: number = 1
18
) {
19
  const url = new URL(`https://api.github.com/repos/${owner}/${repo}/pulls`)
20
  for (const [k, v] of Object.entries({
21
    state,
22
    head,
23
    base,
24
    sort,
25
    direction,
26
    per_page,
27
    page,
28
  })) {
29
    if (v !== undefined && v !== "") url.searchParams.append(k, String(v))
30
  }
31

32
  const response = await fetch(url, {
33
    headers: {
34
      Authorization: `Bearer ${auth.token}`,
35
      Accept: "application/vnd.github+json",
36
      "X-GitHub-Api-Version": "2022-11-28",
37
    },
38
  })
39

40
  if (!response.ok) {
41
    throw new Error(`${response.status} ${await response.text()}`)
42
  }
43

44
  return await response.json()
45
}
46