0

Set GitHub Actions permissions for a repository

by
Published Oct 25, 2023

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

Script github Verified

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 398 days ago
1
type Github = {
2
  token: string;
3
};
4
/**
5
 * Set GitHub Actions permissions for a repository
6
 * Sets the GitHub Actions permissions policy for enabling GitHub Actions and allowed actions and reusable workflows in the repository.
7

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