Search issues and pull requests

Find issues by state and keyword.

Script github Verified

by hugo697 ยท 10/25/2023

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 367 days ago
1
type Github = {
2
  token: string;
3
};
4
/**
5
 * Search issues and pull requests
6
 * Find issues by state and keyword.
7
 */
8
export async function main(
9
  auth: Github,
10
  q: string | undefined,
11
  sort:
12
    | "comments"
13
    | "reactions"
14
    | "reactions-+1"
15
    | "reactions--1"
16
    | "reactions-smile"
17
    | "reactions-thinking_face"
18
    | "reactions-heart"
19
    | "reactions-tada"
20
    | "interactions"
21
    | "created"
22
    | "updated"
23
    | undefined,
24
  order: "desc" | "asc" | undefined,
25
  per_page: string | undefined,
26
  page: string | undefined
27
) {
28
  const url = new URL(`https://api.github.com/search/issues`);
29
  for (const [k, v] of [
30
    ["q", q],
31
    ["sort", sort],
32
    ["order", order],
33
    ["per_page", per_page],
34
    ["page", page],
35
  ]) {
36
    if (v !== undefined && v !== "") {
37
      url.searchParams.append(k, v);
38
    }
39
  }
40
  const response = await fetch(url, {
41
    method: "GET",
42
    headers: {
43
      Authorization: "Bearer " + auth.token,
44
    },
45
    body: undefined,
46
  });
47
  if (!response.ok) {
48
    const text = await response.text();
49
    throw new Error(`${response.status} ${text}`);
50
  }
51
  return await response.json();
52
}
53