Set selected repositories for a user secret

Select the repositories that will use a user's codespace secret. You must authenticate using an access token with the `codespace` or `codespace:secrets` scope to use this endpoint. User must have Codespaces access to use this endpoint. GitHub Apps must have write access to the `codespaces_user_secrets` user permission and write access to the `codespaces_secrets` repository permission on all referenced repositories 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
 * Set selected repositories for a user secret
6
 * Select the repositories that will use a user's codespace secret.
7

8
You must authenticate using an access token with the `codespace` or `codespace:secrets` scope to use this endpoint. User must have Codespaces access to use this endpoint.
9

10
GitHub Apps must have write access to the `codespaces_user_secrets` user permission and write access to the `codespaces_secrets` repository permission on all referenced repositories to use this endpoint.
11
 */
12
export async function main(
13
  auth: Github,
14
  secret_name: string,
15
  body: { selected_repository_ids: number[]; [k: string]: unknown }
16
) {
17
  const url = new URL(
18
    `https://api.github.com/user/codespaces/secrets/${secret_name}/repositories`
19
  );
20

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