List repositories accessible to the user access token

List repositories that the authenticated user has explicit permission (`:read`, `:write`, or `:admin`) to access for an installation.

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
 * List repositories accessible to the user access token
6
 * List repositories that the authenticated user has explicit permission (`:read`, `:write`, or `:admin`) to access for an installation.
7
 */
8
export async function main(
9
  auth: Github,
10
  installation_id: string,
11
  per_page: string | undefined,
12
  page: string | undefined
13
) {
14
  const url = new URL(
15
    `https://api.github.com/user/installations/${installation_id}/repositories`
16
  );
17
  for (const [k, v] of [
18
    ["per_page", per_page],
19
    ["page", page],
20
  ]) {
21
    if (v !== undefined && v !== "") {
22
      url.searchParams.append(k, v);
23
    }
24
  }
25
  const response = await fetch(url, {
26
    method: "GET",
27
    headers: {
28
      Authorization: "Bearer " + auth.token,
29
    },
30
    body: undefined,
31
  });
32
  if (!response.ok) {
33
    const text = await response.text();
34
    throw new Error(`${response.status} ${text}`);
35
  }
36
  return await response.json();
37
}
38