Skip to content

Commit 6b0c16e

Browse files
committed
Handle rate limit more gracefully
Use OAuth client ID & secret Fetch recent issues of each user
1 parent 4c41b88 commit 6b0c16e

File tree

2 files changed

+54
-17
lines changed

2 files changed

+54
-17
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
node_modules
22
*.log
3+
*.json

index.js

+53-17
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,51 @@ var fs = require('fs');
44

55
var github = new GitHubApi({
66
version: '3.0.0',
7-
debug: true
7+
debug: false
88
});
99

10-
function getUsersByLocation(location, limit) {
11-
var users = [];
10+
if (process.env.GITHUB_CLIENT_ID && process.env.GITHUB_CLIENT_SECRET) {
11+
github.authenticate({
12+
type: 'oauth',
13+
key: process.env.GITHUB_CLIENT_ID,
14+
secret: process.env.GITHUB_CLIENT_SECRET
15+
});
16+
}
17+
18+
function fetch(method, args, limit) {
1219
return new Promise(function (resolve, reject) {
13-
github.search.users({
14-
q: 'location:' + location
15-
}, function recvUsers(err, res) {
20+
var items = [];
21+
method(args, function recv(err, res) {
1622
if (err) {
17-
reject(err);
23+
if (err.code === 403) {
24+
console.log('Rate limited');
25+
setTimeout(function () {
26+
console.log('Retrying');
27+
method(args, recv);
28+
}, 60000);
29+
} else {
30+
reject(err);
31+
}
32+
return;
1833
}
19-
2034
res.items
21-
.slice(0, limit - users.length)
22-
.forEach(function (user) {
23-
users.push(user.login);
24-
console.log(users.length, user.login);
35+
.slice(0, limit - items.length)
36+
.forEach(function (item) {
37+
items.push(item);
38+
console.log(items.length, item);
2539
});
26-
27-
if (users.length >= limit || !github.hasNextPage(res)) {
28-
resolve(users);
40+
if (items.length >= limit || !github.hasNextPage(res)) {
41+
resolve(items);
2942
} else {
30-
github.getNextPage(res, recvUsers);
43+
github.getNextPage(res, recv);
3144
}
3245
});
3346
});
3447
}
3548

36-
getUsersByLocation('Singapore', 50)
49+
fetch(github.search.users, {
50+
q: 'location:' + 'Singapore'
51+
}, 50)
3752
.then(function (users) {
3853
console.log('Got:', users);
3954
fs.writeFile('users.json', JSON.stringify(users, null, 2), function (err) {
@@ -43,6 +58,27 @@ getUsersByLocation('Singapore', 50)
4358
console.log('JSON saved');
4459
}
4560
});
61+
return users;
62+
})
63+
.then(function (users) {
64+
return Promise.all(
65+
users.map(function (user) {
66+
return fetch(github.search.issues, {
67+
q: 'type:pr+state:closed+author:' + user
68+
}, 5)
69+
})
70+
);
71+
})
72+
.then(function (issues) {
73+
console.log('Got:', issues);
74+
fs.writeFile('issues.json', JSON.stringify(issues, null, 2), function (err) {
75+
if(err) {
76+
console.log(err);
77+
} else {
78+
console.log('JSON saved');
79+
}
80+
});
81+
return issues;
4682
})
4783
.catch(function (err) {
4884
console.error(err);

0 commit comments

Comments
 (0)