Add or update team repository permissions

To add a repository to a team or update the team's permission on a repository, the authenticated user must have admin access to the repository, and must be able to see the team.

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
 * Add or update team repository permissions
6
 * To add a repository to a team or update the team's permission on a repository, the authenticated user must have admin access to the repository, and must be able to see the team.
7
 */
8
export async function main(
9
  auth: Github,
10
  org: string,
11
  team_slug: string,
12
  owner: string,
13
  repo: string,
14
  body: { permission?: string; [k: string]: unknown }
15
) {
16
  const url = new URL(
17
    `https://api.github.com/orgs/${org}/teams/${team_slug}/repos/${owner}/${repo}`
18
  );
19

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