Create or update an organization secret

Creates or updates an organization secret with an encrypted value.

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 or update an organization secret
6
 * Creates or updates an organization secret with an encrypted value.
7
 */
8
export async function main(
9
  auth: Github,
10
  org: string,
11
  secret_name: string,
12
  body: {
13
    encrypted_value?: string;
14
    key_id?: string;
15
    selected_repository_ids?: string[];
16
    visibility: "all" | "private" | "selected";
17
    [k: string]: unknown;
18
  }
19
) {
20
  const url = new URL(
21
    `https://api.github.com/orgs/${org}/dependabot/secrets/${secret_name}`
22
  );
23

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