0

Remove team membership for a user

by
Published Oct 25, 2023

To remove a membership between a user and a team, the authenticated user must have 'admin' permissions to the team or be an owner of the organization that the team is associated with.

Script github Verified

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 398 days ago
1
type Github = {
2
  token: string;
3
};
4
/**
5
 * Remove team membership for a user
6
 * To remove a membership between a user and a team, the authenticated user must have 'admin' permissions to the team or be an owner of the organization that the team is associated with.
7
 */
8
export async function main(
9
  auth: Github,
10
  org: string,
11
  team_slug: string,
12
  username: string
13
) {
14
  const url = new URL(
15
    `https://api.github.com/orgs/${org}/teams/${team_slug}/memberships/${username}`
16
  );
17

18
  const response = await fetch(url, {
19
    method: "DELETE",
20
    headers: {
21
      Authorization: "Bearer " + auth.token,
22
    },
23
    body: undefined,
24
  });
25
  if (!response.ok) {
26
    const text = await response.text();
27
    throw new Error(`${response.status} ${text}`);
28
  }
29
  return await response.text();
30
}
31