1 | |
2 |
|
3 | async function graphql(auth: RT.Github, query: string, variables: object) { |
4 | const response = await fetch("https://api.github.com/graphql", { |
5 | method: "POST", |
6 | headers: { |
7 | Authorization: `Bearer ${auth.token}`, |
8 | "Content-Type": "application/json", |
9 | Accept: "application/json", |
10 | }, |
11 | body: JSON.stringify({ query, variables }), |
12 | }) |
13 | if (!response.ok) { |
14 | throw new Error(`${response.status} ${await response.text()}`) |
15 | } |
16 | const { data, errors } = (await response.json()) as { |
17 | data: any |
18 | errors?: unknown[] |
19 | } |
20 | if (errors?.length) throw new Error(JSON.stringify(errors)) |
21 | return data |
22 | } |
23 |
|
24 | |
25 | * List Projects |
26 | * List the GitHub Projects (v2) of a user or organization. Paginate with after = pageInfo.endCursor. Classic tokens and OAuth connections need the read:project scope. |
27 | */ |
28 | export async function main( |
29 | auth: RT.Github, |
30 | owner: string, |
31 | first: number = 30, |
32 | after: string | undefined |
33 | ) { |
34 | const data = await graphql( |
35 | auth, |
36 | ` |
37 | query ($owner: String!, $first: Int!, $after: String) { |
38 | repositoryOwner(login: $owner) { |
39 | ... on ProjectV2Owner { |
40 | projectsV2(first: $first, after: $after) { |
41 | nodes { |
42 | id |
43 | number |
44 | title |
45 | shortDescription |
46 | url |
47 | closed |
48 | public |
49 | updatedAt |
50 | } |
51 | pageInfo { |
52 | endCursor |
53 | hasNextPage |
54 | } |
55 | } |
56 | } |
57 | } |
58 | } |
59 | `, |
60 | { owner, first, after: after || null } |
61 | ) |
62 | if (!data.repositoryOwner) throw new Error(`Owner ${owner} not found`) |
63 | return data.repositoryOwner.projectsV2 |
64 | } |
65 |
|