Set GitHub Actions permissions for an organization

Sets the GitHub Actions permissions policy for repositories and allowed actions and reusable workflows in an organization. You must authenticate using an access token with the `admin:org` scope to use this endpoint. GitHub Apps must have the `administration` organization permission to use this API.

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 GitHub Actions permissions for an organization
6
 * Sets the GitHub Actions permissions policy for repositories and allowed actions and reusable workflows in an organization.
7

8
You must authenticate using an access token with the `admin:org` scope to use this endpoint. GitHub Apps must have the `administration` organization permission to use this API.
9
 */
10
export async function main(
11
  auth: Github,
12
  org: string,
13
  body: {
14
    allowed_actions?: "all" | "local_only" | "selected";
15
    enabled_repositories: "all" | "none" | "selected";
16
    [k: string]: unknown;
17
  }
18
) {
19
  const url = new URL(`https://api.github.com/orgs/${org}/actions/permissions`);
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