Replace all repository topics

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
 * Replace all repository topics
6
 *
7
 */
8
export async function main(
9
  auth: Github,
10
  owner: string,
11
  repo: string,
12
  body: { names: string[]; [k: string]: unknown }
13
) {
14
  const url = new URL(`https://api.github.com/repos/${owner}/${repo}/topics`);
15

16
  const response = await fetch(url, {
17
    method: "PUT",
18
    headers: {
19
      "Content-Type": "application/json",
20
      Authorization: "Bearer " + auth.token,
21
    },
22
    body: JSON.stringify(body),
23
  });
24
  if (!response.ok) {
25
    const text = await response.text();
26
    throw new Error(`${response.status} ${text}`);
27
  }
28
  return await response.json();
29
}
30