-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgithub-repo-list-forks.js
312 lines (254 loc) · 5.97 KB
/
github-repo-list-forks.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
// javascript client for github v4 graphQL API
// list all forks of a repository
// including commit count, modify date, github stars
// public domain + zero warranty
// original repository
let repoOwner = "rugantio"
let repoName = "fbcrawl"
// basic auth
// only for testing
let username = 'your_github_username'
let password = 'your_github_password'
// TODO use OAuth2 token
// https://github.com/octokit/auth-oauth-app.js
// node.js
// npm install node-fetch
const fetch = require('node-fetch')
Headers = fetch.Headers
function btoa(s){
return Buffer.from(s, 'binary').toString('base64')
}
let headers = new Headers({
// TODO use oauth token
'Authorization': 'Basic '+btoa(username+":"+password),
'Content-Type': 'application/json',
'Accept': 'application/json',
})
// graphQL query string
// inspired by
// https://gist.github.com/thewoolleyman/2294542455a8e673e0a844362e0b8bac
// https://gist.github.com/jonathansick/8bbe88a85addaeeea4e7fe9ef15b016b
// TODO clean up
let graph_query = `
query (
$repoOwner: String!,
$repoName: String!,
$refOrder: RefOrder!,
$forksPerPage: Int!, # forks per page
$forksCursor: String, # forks pagination cursor
) {
#repository (owner:"rugantio", name:"fbcrawl") {
repository (
owner: $repoOwner,
name: $repoName,
) {
nameWithOwner
pushedAt
#updatedAt
stargazers {
## StargazerConnection
totalCount
}
#commitComments(first:10) {
# totalCount
#}
#isFork
forkCount
refs(refPrefix: "refs/", first: 100) {
#refs(refPrefix: "refs/heads/", first: 100) {
##RefConnection
#totalCount
## only one head?
#pageInfo {
# hasNextPage
#}
#edges { node {
nodes {
#name # = heads/master
target {
... on Commit {
history(first: 5) {
totalCount # = total_commits of original
#edges {
# node {
# oid
# messageHeadline
# committedDate
# #message
# #author {
# # name
# # email
# # date
# }
# }
#}
}
}
}
}
#}
}
forks(
first: $forksPerPage,
after: $forksCursor,
) {
totalCount
#totalDiskUsage
## forks pagination
pageInfo {
endCursor
hasNextPage
hasPreviousPage
startCursor
}
#nodes {
edges{
#cursor
node{
... on Repository {
nameWithOwner
pushedAt
#updatedAt
stargazers {
totalCount
}
refs(refPrefix:"refs/",orderBy:$refOrder,first:1){
#edges{ node{
nodes{
... on Ref{
target{
... on Commit{
history(first:10){
totalCount # = total_commits of fork
#edges{
# node{
# ... on Commit{
# committedDate
# }
# }
#}
}
}
}
}
}
#}
}
}
}
}
}
}
}
`
// debug helper
// print object properties
function dumpVars(R, s){
s.split('\n').forEach(L => {
n = L.split('//')[0].trim()
if (n == '') {
return
}
n2 = n.slice(1+n.indexOf('.'))
v = eval('R.'+n2) // R = local root object
console.log(n+' = '+v)
})
}
// TODO retry loop, using retry-fetch ?
/*
var fetch = require('fetch-retry');
fetch(url, {
// retry on status 403 Forbidden
retryOn: [403],
// Exponential backoff
retryDelay: function(attempt, error, response) {
return Math.pow(2, attempt) * 1000; // 1000, 2000, 4000
},
})
.then(function(response) {
return response.json();
})
.then(function(json) {
// do something with the result
console.log(json);
});
*/
// TODO use graphQL client library
// with pagination plugin?
// https://github.com/octokit/graphql.js
// GitHub GraphQL API client for browsers and Node
// https://github.com/graphql/graphql-js
// A reference implementation of GraphQL
// for JavaScript http://graphql.org/graphql-js/
// send graphQL query, process result
// pagination via recursion
function gql_fetch(url, headers, query, vars){
return fetch(url, {
method: 'POST',
headers: headers,
body: JSON.stringify({
query: graph_query,
variables: vars,
}),
})
.then(r => r.json())
.then(r => {
// print all the data
console.log(JSON.stringify(r, null, ' '))
r = r.data.repository // root node
// original repository
dumpVars(r, `
r.nameWithOwner
r.pushedAt
r.stargazers.totalCount
r.forkCount
r.refs.nodes[0].target.history.totalCount // original commits
`)
// fork repositories
//r.forks.nodes.forEach(f => {
r.forks.edges.forEach(e => {
/*
dumpVars(e,`
e.cursor
`)
*/
f = e.node
dumpVars(f, `
f.nameWithOwner
f.pushedAt
f.stargazers.totalCount
f.refs.nodes[0].target.history.totalCount // fork commits
`)
})
// forks pagination
const {hasNextPage, endCursor} = r.forks.pageInfo
if (hasNextPage) {
console.log('next page ....')
// recursion
vars.forksCursor = endCursor
return gql_fetch(
url,
headers,
query,
vars
)
}
else {
console.log('last page done')
}
})
}
// send query
gql_fetch(
'https://api.github.com/graphql',
headers,
graph_query, {
refOrder: {
direction: "DESC",
field: "TAG_COMMIT_DATE",
},
repoOwner: repoOwner,
repoName: repoName,
forksCursor: null, // first page
forksPerPage: 100,
})