List organization projects

Lists the projects in an organization. Returns a `404 Not Found` status if projects are disabled in the organization. If you do not have sufficient privileges to perform this action, a `401 Unauthorized` or `410 Gone` status is returned.

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 projects
6
 * Lists the projects in an organization. Returns a `404 Not Found` status if projects are disabled in the organization. If you do not have sufficient privileges to perform this action, a `401 Unauthorized` or `410 Gone` status is returned.
7
 */
8
export async function main(
9
  auth: Github,
10
  org: string,
11
  state: "open" | "closed" | "all" | undefined,
12
  per_page: string | undefined,
13
  page: string | undefined
14
) {
15
  const url = new URL(`https://api.github.com/orgs/${org}/projects`);
16
  for (const [k, v] of [
17
    ["state", state],
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