-
Notifications
You must be signed in to change notification settings - Fork 3
/
fetchAuthors.js
76 lines (57 loc) · 1.67 KB
/
fetchAuthors.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
var request = require('request')
var url = require('url')
var userAgent = process.env.USER_AGENT || 'hypermarkdown'
module.exports = fetchAuthorData
function fetchAuthorData (ownerAndRepo, query, callback) {
fetchCommits(ownerAndRepo, query, afterFetchCommits)
function afterFetchCommits (err, result) {
if (err) { return callback(err) }
var users = mapAuthorData(result)
callback(null, users)
}
}
function fetchCommits (ownerAndRepo, query, callback) {
request( buildGithubRequest('repos/'+ownerAndRepo+'/commits', query), function(err, response, body) {
if (err) { callback(err) }
fetchMoreCommits(response, query, callback)
var responseArray = JSON.parse(body)
callback(null, responseArray)
})
}
function buildGithubRequest (apiPath, params) {
var request = {
url: url.format({
protocol: 'https',
host: 'api.github.com',
pathname: apiPath,
query: params,
}),
headers: {
'User-Agent': userAgent,
},
}
return request
}
function fetchMoreCommits(response, query, callback) {
if (response.headers.link == null) return
var paginationRegex = /&page\=(\d+)\>; rel\=.next/
var nextPageMatch = response.headers.link.match(paginationRegex)
if (nextPageMatch != null) {
var newQuery = query
newQuery['page'] = nextPageMatch[1]
fetchCommits(newQuery, callback)
}
}
function mapAuthorData (array) {
var users = {}
array.forEach( function(el) {
var author =
//if (users[author]) { next }
users[el.author.login] = {
'author': el.author.login,
'avatar_url': el.author.avatar_url,
'html_url': el.author.html_url,
}
})
return users;
}