0
Create an organization repository
One script reply has been approved by the moderators Verified

Creates a new repository in the specified organization. The authenticated user must be a member of the organization.

OAuth scope requirements

When using OAuth, 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
Created by hugo697 631 days ago Viewed 22519 times
0
Submitted by hugo697 Typescript (fetch-only)
Verified 631 days ago
1
type Github = {
2
  token: string;
3
};
4
/**
5
 * Create an organization repository
6
 * Creates a new repository in the specified organization. The authenticated user must be a member of the organization.
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
  org: string,
18
  body: {
19
    allow_auto_merge?: boolean;
20
    allow_merge_commit?: boolean;
21
    allow_rebase_merge?: boolean;
22
    allow_squash_merge?: boolean;
23
    auto_init?: boolean;
24
    delete_branch_on_merge?: boolean;
25
    description?: string;
26
    gitignore_template?: string;
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
    use_squash_pr_title_as_default?: boolean;
42
    visibility?: "public" | "private";
43
    [k: string]: unknown;
44
  }
45
) {
46
  const url = new URL(`https://api.github.com/orgs/${org}/repos`);
47

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