0
Add labels to an issue
One script reply has been approved by the moderators Verified
Created by hugo697 173 days ago Viewed 3732 times
0
Submitted by hugo697 Typescript (fetch-only)
Verified 173 days ago
1
type Github = {
2
  token: string;
3
};
4
/**
5
 * Add labels to an issue
6
 *
7
 */
8
export async function main(
9
  auth: Github,
10
  owner: string,
11
  repo: string,
12
  issue_number: string,
13
  body:
14
    | { labels?: string[]; [k: string]: unknown }
15
    | string[]
16
    | {
17
        labels?: { name: string; [k: string]: unknown }[];
18
        [k: string]: unknown;
19
      }
20
    | { name: string; [k: string]: unknown }[]
21
    | string
22
) {
23
  const url = new URL(
24
    `https://api.github.com/repos/${owner}/${repo}/issues/${issue_number}/labels`
25
  );
26

27
  const response = await fetch(url, {
28
    method: "POST",
29
    headers: {
30
      "Content-Type": "application/json",
31
      Authorization: "Bearer " + auth.token,
32
    },
33
    body: JSON.stringify(body),
34
  });
35
  if (!response.ok) {
36
    const text = await response.text();
37
    throw new Error(`${response.status} ${text}`);
38
  }
39
  return await response.json();
40
}
41