0

Search Issues and Pull Requests

by
Published today

Search issues and pull requests across GitHub with the search syntax, e.g. 'repo:owner/name is:pr is:open label:bug'. Returns at most 1000 results.

Script github Verified

The script

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

3
/**
4
 * Search Issues and Pull Requests
5
 * Search issues and pull requests across GitHub with the search syntax, e.g. 'repo:owner/name is:pr is:open label:bug'. Returns at most 1000 results.
6
 */
7
export async function main(
8
  auth: RT.Github,
9
  q: string,
10
  sort: "comments" | "reactions" | "created" | "updated" | undefined,
11
  order: "asc" | "desc" | undefined,
12
  per_page: number = 30,
13
  page: number = 1
14
) {
15
  const url = new URL("https://api.github.com/search/issues")
16
  for (const [k, v] of Object.entries({ q, sort, order, per_page, page })) {
17
    if (v !== undefined && v !== "") url.searchParams.append(k, String(v))
18
  }
19

20
  const response = await fetch(url, {
21
    headers: {
22
      Authorization: `Bearer ${auth.token}`,
23
      Accept: "application/vnd.github+json",
24
      "X-GitHub-Api-Version": "2022-11-28",
25
    },
26
  })
27

28
  if (!response.ok) {
29
    throw new Error(`${response.status} ${await response.text()}`)
30
  }
31

32
  return await response.json()
33
}
34