Create a repository for the authenticated user

Creates a new repository for the authenticated user. **OAuth scope requirements** When using [OAuth](https://docs.github.com/apps/building-oauth-apps/understanding-scopes-for-oauth-apps/), authorizations must include: * `public_repo` scope or `repo` scope to create a public repository. Note: For GitHub AE, use `repo` scope to create an internal repository. * `repo` scope to create a private repository.

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 a repository for the authenticated user
6
 * Creates a new repository for the authenticated user.
7

8
**OAuth scope requirements**
9

10
When using [OAuth](https://docs.github.com/apps/building-oauth-apps/understanding-scopes-for-oauth-apps/), authorizations must include:
11

12
*   `public_repo` scope or `repo` scope to create a public repository. Note: For GitHub AE, use `repo` scope to create an internal repository.
13
*   `repo` scope to create a private repository.
14
 */
15
export async function main(
16
  auth: Github,
17
  body: {
18
    allow_auto_merge?: boolean;
19
    allow_merge_commit?: boolean;
20
    allow_rebase_merge?: boolean;
21
    allow_squash_merge?: boolean;
22
    auto_init?: boolean;
23
    delete_branch_on_merge?: boolean;
24
    description?: string;
25
    gitignore_template?: string;
26
    has_discussions?: boolean;
27
    has_downloads?: boolean;
28
    has_issues?: boolean;
29
    has_projects?: boolean;
30
    has_wiki?: boolean;
31
    homepage?: string;
32
    is_template?: boolean;
33
    license_template?: string;
34
    merge_commit_message?: "PR_BODY" | "PR_TITLE" | "BLANK";
35
    merge_commit_title?: "PR_TITLE" | "MERGE_MESSAGE";
36
    name: string;
37
    private?: boolean;
38
    squash_merge_commit_message?: "PR_BODY" | "COMMIT_MESSAGES" | "BLANK";
39
    squash_merge_commit_title?: "PR_TITLE" | "COMMIT_OR_PR_TITLE";
40
    team_id?: number;
41
    [k: string]: unknown;
42
  }
43
) {
44
  const url = new URL(`https://api.github.com/user/repos`);
45

46
  const response = await fetch(url, {
47
    method: "POST",
48
    headers: {
49
      "Content-Type": "application/json",
50
      Authorization: "Bearer " + auth.token,
51
    },
52
    body: JSON.stringify(body),
53
  });
54
  if (!response.ok) {
55
    const text = await response.text();
56
    throw new Error(`${response.status} ${text}`);
57
  }
58
  return await response.json();
59
}
60