List organization repositories

Lists repositories for the specified organization. **Note:** In order to see the `security_and_analysis` block for a repository you must have admin permissions for the repository or be an owner or security manager for the organization that owns the repository. For more information, see "[Managing security managers in your organization](https://docs.github.com/organizations/managing-peoples-access-to-your-organization-with-roles/managing-security-managers-in-your-organization)."

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 organization repositories
6
 * Lists repositories for the specified organization.
7

8
**Note:** In order to see the `security_and_analysis` block for a repository you must have admin permissions for the repository or be an owner or security manager for the organization that owns the repository. For more information, see "[Managing security managers in your organization](https://docs.github.com/organizations/managing-peoples-access-to-your-organization-with-roles/managing-security-managers-in-your-organization)."
9
 */
10
export async function main(
11
  auth: Github,
12
  org: string,
13
  type:
14
    | "all"
15
    | "public"
16
    | "private"
17
    | "forks"
18
    | "sources"
19
    | "member"
20
    | undefined,
21
  sort: "created" | "updated" | "pushed" | "full_name" | undefined,
22
  direction: "asc" | "desc" | undefined,
23
  per_page: string | undefined,
24
  page: string | undefined
25
) {
26
  const url = new URL(`https://api.github.com/orgs/${org}/repos`);
27
  for (const [k, v] of [
28
    ["type", type],
29
    ["sort", sort],
30
    ["direction", direction],
31
    ["per_page", per_page],
32
    ["page", page],
33
  ]) {
34
    if (v !== undefined && v !== "") {
35
      url.searchParams.append(k, v);
36
    }
37
  }
38
  const response = await fetch(url, {
39
    method: "GET",
40
    headers: {
41
      Authorization: "Bearer " + auth.token,
42
    },
43
    body: undefined,
44
  });
45
  if (!response.ok) {
46
    const text = await response.text();
47
    throw new Error(`${response.status} ${text}`);
48
  }
49
  return await response.json();
50
}
51