Update an organization variable

Updates an organization variable that you can reference in a GitHub Actions workflow. You must authenticate using an access token with the `admin:org` scope to use this endpoint. GitHub Apps must have the `organization_actions_variables:write` organization permission to use this endpoint.

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
 * Update an organization variable
6
 * Updates an organization variable that you can reference in a GitHub Actions workflow.
7
You must authenticate using an access token with the `admin:org` scope to use this endpoint.
8
GitHub Apps must have the `organization_actions_variables:write` organization permission to use this endpoint.
9
 */
10
export async function main(
11
  auth: Github,
12
  org: string,
13
  name: string,
14
  body: {
15
    name?: string;
16
    selected_repository_ids?: number[];
17
    value?: string;
18
    visibility?: "all" | "private" | "selected";
19
    [k: string]: unknown;
20
  }
21
) {
22
  const url = new URL(
23
    `https://api.github.com/orgs/${org}/actions/variables/${name}`
24
  );
25

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