0

Create Repository

by
Published today

Create a repository for the authenticated user, or in an organization when org is set.

Script github Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 1 hour ago
1
//native
2

3
/**
4
 * Create Repository
5
 * Create a repository for the authenticated user, or in an organization when org is set.
6
 */
7
export async function main(
8
  auth: RT.Github,
9
  name: string,
10
  org: string | undefined,
11
  description: string | undefined,
12
  private_repo: boolean = false,
13
  auto_init: boolean = false
14
) {
15
  const path = org ? `orgs/${org}/repos` : "user/repos"
16
  const response = await fetch(`https://api.github.com/${path}`, {
17
    method: "POST",
18
    headers: {
19
      Authorization: `Bearer ${auth.token}`,
20
      "Content-Type": "application/json",
21
      Accept: "application/vnd.github+json",
22
      "X-GitHub-Api-Version": "2022-11-28",
23
    },
24
    body: JSON.stringify({
25
      name,
26
      description,
27
      private: private_repo,
28
      auto_init,
29
    }),
30
  })
31

32
  if (!response.ok) {
33
    throw new Error(`${response.status} ${await response.text()}`)
34
  }
35

36
  return await response.json()
37
}
38