@@ -4,36 +4,51 @@ var fs = require('fs');
4
4
5
5
var github = new GitHubApi ( {
6
6
version : '3.0.0' ,
7
- debug : true
7
+ debug : false
8
8
} ) ;
9
9
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 ) {
12
19
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 ) {
16
22
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 ;
18
33
}
19
-
20
34
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 ) ;
25
39
} ) ;
26
-
27
- if ( users . length >= limit || ! github . hasNextPage ( res ) ) {
28
- resolve ( users ) ;
40
+ if ( items . length >= limit || ! github . hasNextPage ( res ) ) {
41
+ resolve ( items ) ;
29
42
} else {
30
- github . getNextPage ( res , recvUsers ) ;
43
+ github . getNextPage ( res , recv ) ;
31
44
}
32
45
} ) ;
33
46
} ) ;
34
47
}
35
48
36
- getUsersByLocation ( 'Singapore' , 50 )
49
+ fetch ( github . search . users , {
50
+ q : 'location:' + 'Singapore'
51
+ } , 50 )
37
52
. then ( function ( users ) {
38
53
console . log ( 'Got:' , users ) ;
39
54
fs . writeFile ( 'users.json' , JSON . stringify ( users , null , 2 ) , function ( err ) {
@@ -43,6 +58,27 @@ getUsersByLocation('Singapore', 50)
43
58
console . log ( 'JSON saved' ) ;
44
59
}
45
60
} ) ;
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 ;
46
82
} )
47
83
. catch ( function ( err ) {
48
84
console . error ( err ) ;
0 commit comments