Create a repository using a template

Creates a new repository using a repository template.

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
 * Create a repository using a template
6
 * Creates a new repository using a repository template.
7
 */
8
export async function main(
9
  auth: Github,
10
  template_owner: string,
11
  template_repo: string,
12
  body: {
13
    description?: string;
14
    include_all_branches?: boolean;
15
    name: string;
16
    owner?: string;
17
    private?: boolean;
18
    [k: string]: unknown;
19
  }
20
) {
21
  const url = new URL(
22
    `https://api.github.com/repos/${template_owner}/${template_repo}/generate`
23
  );
24

25
  const response = await fetch(url, {
26
    method: "POST",
27
    headers: {
28
      "Content-Type": "application/json",
29
      Authorization: "Bearer " + auth.token,
30
    },
31
    body: JSON.stringify(body),
32
  });
33
  if (!response.ok) {
34
    const text = await response.text();
35
    throw new Error(`${response.status} ${text}`);
36
  }
37
  return await response.json();
38
}
39