-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgithub.ts
More file actions
101 lines (86 loc) · 2.43 KB
/
github.ts
File metadata and controls
101 lines (86 loc) · 2.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// deno-lint-ignore-file
import { Octokit } from "https://esm.sh/octokit@2.0.14";
import {
IssueComment,
IssueCommentConnection,
IssueConnection,
PullRequestConnection,
} from "https://esm.sh/@octokit/graphql-schema@13.4.0";
const GITHUB_API_TOKEN = Deno.env.get("GITHUB_TOKEN");
const octokit = new Octokit({ auth: GITHUB_API_TOKEN });
async function loopEdges<A>(
connection: IssueConnection | PullRequestConnection | IssueCommentConnection,
options: Partial<{
onNext: (cursor: string, data: A[]) => Promise<A[] | void>;
onNode: (node: A) => A | Promise<A>;
}> = {},
): Promise<A[]> {
const data: A[] = [];
let lastCursor: string | null = null;
if (connection.edges && connection.edges.length) {
for (const edge of connection.edges) {
if (edge) {
let node = edge.node as unknown as A;
if (options.onNode) {
node = await options.onNode(node);
}
data.push(node);
lastCursor = edge.cursor;
}
}
}
if (connection.pageInfo.hasNextPage && lastCursor && options.onNext) {
const response = await options.onNext(lastCursor, data);
if (response) {
data.push(...response);
}
}
return data;
}
export type QueryOption = Record<string, string | number | string[]>;
// @ts-ignore
export const getAllIssues = async (options: QueryOption = {}) => {
const QUERY = `
query ($owner: String!, $repo: String!, $after: String, $num: Int = 100) {
repository(owner:$owner, name:$repo) {
issues(first:$num, after:$after, orderBy: {field: CREATED_AT, direction: DESC}) {
pageInfo {
startCursor
endCursor
hasNextPage
}
edges {
cursor
node {
number
title
body
comments(first: 100) {
edges {
node {
body
}
}
}
}
}
}
}
}
`;
const response = await octokit.graphql(QUERY, {
owner: "filecoin-project",
repo: "notary-governance",
num: 100,
...options,
});
const issuesConnection = response.repository?.issues;
return await loopEdges<IssueComment>(issuesConnection, {
onNext: (cursor) => getAllIssues({ ...options, after: cursor }),
});
};
export async function getIssues() {
const allIssues = await getAllIssues();
console.log("allIssues.length ->", allIssues.length);
return allIssues;
}