Create a codespace from a pull request

Creates a codespace owned by the authenticated user for the specified pull request. You must authenticate using an access token with the `codespace` scope to use this endpoint. GitHub Apps must have write access to the `codespaces` repository permission to use this endpoint.

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 codespace from a pull request
6
 * Creates a codespace owned by the authenticated user for the specified pull request.
7

8
You must authenticate using an access token with the `codespace` scope to use this endpoint.
9

10
GitHub Apps must have write access to the `codespaces` repository permission to use this endpoint.
11
 */
12
export async function main(
13
  auth: Github,
14
  owner: string,
15
  repo: string,
16
  pull_number: string,
17
  body: {
18
    client_ip?: string;
19
    devcontainer_path?: string;
20
    display_name?: string;
21
    idle_timeout_minutes?: number;
22
    location?: string;
23
    machine?: string;
24
    multi_repo_permissions_opt_out?: boolean;
25
    retention_period_minutes?: number;
26
    working_directory?: string;
27
    [k: string]: unknown;
28
  }
29
) {
30
  const url = new URL(
31
    `https://api.github.com/repos/${owner}/${repo}/pulls/${pull_number}/codespaces`
32
  );
33

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