List pending organization invitations

The return hash contains a `role` field which refers to the Organization Invitation role and will be one of the following values: `direct_member`, `admin`, `billing_manager`, or `hiring_manager`. If the invitee is not a GitHub member, the `login` field in the return hash will be `null`.

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
 * List pending organization invitations
6
 * The return hash contains a `role` field which refers to the Organization Invitation role and will be one of the following values: `direct_member`, `admin`, `billing_manager`, or `hiring_manager`. If the invitee is not a GitHub member, the `login` field in the return hash will be `null`.
7
 */
8
export async function main(
9
  auth: Github,
10
  org: string,
11
  per_page: string | undefined,
12
  page: string | undefined,
13
  role:
14
    | "all"
15
    | "admin"
16
    | "direct_member"
17
    | "billing_manager"
18
    | "hiring_manager"
19
    | undefined,
20
  invitation_source: "all" | "member" | "scim" | undefined
21
) {
22
  const url = new URL(`https://api.github.com/orgs/${org}/invitations`);
23
  for (const [k, v] of [
24
    ["per_page", per_page],
25
    ["page", page],
26
    ["role", role],
27
    ["invitation_source", invitation_source],
28
  ]) {
29
    if (v !== undefined && v !== "") {
30
      url.searchParams.append(k, v);
31
    }
32
  }
33
  const response = await fetch(url, {
34
    method: "GET",
35
    headers: {
36
      Authorization: "Bearer " + auth.token,
37
    },
38
    body: undefined,
39
  });
40
  if (!response.ok) {
41
    const text = await response.text();
42
    throw new Error(`${response.status} ${text}`);
43
  }
44
  return await response.json();
45
}
46