-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi.js
115 lines (95 loc) · 2.47 KB
/
api.js
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
const swagger = require("swagger-client");
async function createClient(host, token) {
return swagger({
url: `${host}/swagger.v1.json`,
authorizations: {
Token: token
}
});
}
async function fetchData(client, org) {
let repos = await listOrgRepos(client, org);
for (const repo of repos) {
// Limit concurrent requests
const issues = await listRepoIssues(client, org, repo.name);
repo.issues = issues;
for (const issue of issues) {
const comments = await listIssueComments(
client,
org,
repo.name,
issue.number
);
issue.comments = comments;
if (issue.pull_request !== null) {
// Retrieve additional info (base, head branch)
issue.pull_request = await getRepoPR(
client,
org,
repo.name,
issue.number
);
}
}
}
return repos;
}
async function listOrgRepos(client, org) {
console.log(`[listOrgRepos] org=${org}`);
const { body: repos } = await client.apis.organization.orgListRepos({ org });
return repos;
}
async function listRepoIssues(client, org, repo) {
console.log(`[listRepoIssues] org=${org} repo=${repo}`);
async function addIssuesWithState(state) {
const issuesWithState = [];
let page = 1;
while (true) {
console.log(
`[addIssuesWithState] org=${org} repo=${repo} state=${state} page=${page}`
);
const { body: pagedIssues } = await client.apis.issue.issueListIssues({
owner: org,
repo,
page,
state
});
if (pagedIssues.length == 0) {
break;
}
issuesWithState.push(...pagedIssues);
page++;
}
return issuesWithState;
}
// Open
const [openIssues, closedIssues] = await Promise.all([
addIssuesWithState("open"),
addIssuesWithState("closed")
]);
return [...openIssues, ...closedIssues];
}
async function listIssueComments(client, org, repo, index) {
console.log(`[listIssueComment] org=${org} repo=${repo} index=${index}`);
const { body: comments } = await client.apis.issue.issueGetComments({
owner: org,
repo,
index
});
return comments;
}
async function getRepoPR(client, org, repo, index) {
console.log(`[getRepoPR] org=${org} repo=${repo} index=${index}`);
const { body: pullRequest } = await client.apis.repository.repoGetPullRequest(
{
owner: org,
repo,
index
}
);
return pullRequest;
}
module.exports = {
createClient,
fetchData
};