0
Set labels for an issue
One script reply has been approved by the moderators Verified

Removes any previous labels and sets the new labels for an issue.

Created by hugo697 200 days ago Viewed 6184 times
0
Submitted by hugo697 Typescript (fetch-only)
Verified 200 days ago
1
type Github = {
2
  token: string;
3
};
4
/**
5
 * Set labels for an issue
6
 * Removes any previous labels and sets the new labels for an issue.
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: "PUT",
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