Create an issue

Any user with pull access to a repository can create an issue.

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
 * Create an issue
6
 * Any user with pull access to a repository can create an issue.
7
 */
8
export async function main(
9
  auth: Github,
10
  owner: string,
11
  repo: string,
12
  body: {
13
    assignee?: string;
14
    assignees?: string[];
15
    body?: string;
16
    labels?: (
17
      | string
18
      | {
19
          color?: string;
20
          description?: string;
21
          id?: number;
22
          name?: string;
23
          [k: string]: unknown;
24
        }
25
    )[];
26
    milestone?: string | number;
27
    title: string | number;
28
    [k: string]: unknown;
29
  }
30
) {
31
  const url = new URL(`https://api.github.com/repos/${owner}/${repo}/issues`);
32

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