0

List Repository Collaborators

by
Published today

List the collaborators of a repository, including organization members with access. Requires push access to the repository.

Script github Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 1 hour ago
1
//native
2

3
/**
4
 * List Repository Collaborators
5
 * List the collaborators of a repository, including organization members with access. Requires push access to the repository.
6
 */
7
export async function main(
8
  auth: RT.Github,
9
  owner: string,
10
  repo: string,
11
  affiliation: "outside" | "direct" | "all" | undefined,
12
  permission: "pull" | "triage" | "push" | "maintain" | "admin" | undefined,
13
  per_page: number = 30,
14
  page: number = 1
15
) {
16
  const url = new URL(
17
    `https://api.github.com/repos/${owner}/${repo}/collaborators`
18
  )
19
  for (const [k, v] of Object.entries({
20
    affiliation,
21
    permission,
22
    per_page,
23
    page,
24
  })) {
25
    if (v !== undefined && v !== "") url.searchParams.append(k, String(v))
26
  }
27

28
  const response = await fetch(url, {
29
    headers: {
30
      Authorization: `Bearer ${auth.token}`,
31
      Accept: "application/vnd.github+json",
32
      "X-GitHub-Api-Version": "2022-11-28",
33
    },
34
  })
35

36
  if (!response.ok) {
37
    throw new Error(`${response.status} ${await response.text()}`)
38
  }
39

40
  return await response.json()
41
}
42