Search labels

Find labels in a repository with names or descriptions that match search keywords.

Script github Verified

by hugo697 ยท 10/25/2023

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 366 days ago
1
type Github = {
2
  token: string;
3
};
4
/**
5
 * Search labels
6
 * Find labels in a repository with names or descriptions that match search keywords.
7
 */
8
export async function main(
9
  auth: Github,
10
  repository_id: string | undefined,
11
  q: string | undefined,
12
  sort: "created" | "updated" | undefined,
13
  order: "desc" | "asc" | undefined,
14
  per_page: string | undefined,
15
  page: string | undefined
16
) {
17
  const url = new URL(`https://api.github.com/search/labels`);
18
  for (const [k, v] of [
19
    ["repository_id", repository_id],
20
    ["q", q],
21
    ["sort", sort],
22
    ["order", order],
23
    ["per_page", per_page],
24
    ["page", page],
25
  ]) {
26
    if (v !== undefined && v !== "") {
27
      url.searchParams.append(k, v);
28
    }
29
  }
30
  const response = await fetch(url, {
31
    method: "GET",
32
    headers: {
33
      Authorization: "Bearer " + auth.token,
34
    },
35
    body: undefined,
36
  });
37
  if (!response.ok) {
38
    const text = await response.text();
39
    throw new Error(`${response.status} ${text}`);
40
  }
41
  return await response.json();
42
}
43