List repository collaborators

For organization-owned repositories, the list of collaborators includes outside collaborators, organization members that are direct collaborators, organization members with access through team memberships, organization members with access through default organization permissions, and organization owners.

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 repository collaborators
6
 * For organization-owned repositories, the list of collaborators includes outside collaborators, organization members that are direct collaborators, organization members with access through team memberships, organization members with access through default organization permissions, and organization owners.
7
 */
8
export async function main(
9
  auth: Github,
10
  owner: string,
11
  repo: string,
12
  affiliation: "outside" | "direct" | "all" | undefined,
13
  permission: "pull" | "triage" | "push" | "maintain" | "admin" | undefined,
14
  per_page: string | undefined,
15
  page: string | undefined
16
) {
17
  const url = new URL(
18
    `https://api.github.com/repos/${owner}/${repo}/collaborators`
19
  );
20
  for (const [k, v] of [
21
    ["affiliation", affiliation],
22
    ["permission", permission],
23
    ["per_page", per_page],
24
    ["page", page],
25
  ]) {
26
    if (v !== undefined && v !== "") {
27
      url.searchParams.append(k, v);
28
    }
29
  }
30
  const response = await fetch(url, {
31
    method: "GET",
32
    headers: {
33
      Authorization: "Bearer " + auth.token,
34
    },
35
    body: undefined,
36
  });
37
  if (!response.ok) {
38
    const text = await response.text();
39
    throw new Error(`${response.status} ${text}`);
40
  }
41
  return await response.json();
42
}
43