@@ -17,10 +17,40 @@ function findAllRepos(cb) {
1717}
1818
1919
20+ var allErrors = [ ] ;
21+
22+
23+ function findUser ( users , cb ) {
24+ var user ;
25+ var count = 0 ;
26+ async . whilst (
27+ function ( ) { return count < users . length ; } ,
28+ function ( callback ) {
29+ var userId = users [ count ] ;
30+ User . findByGithubId ( userId , function ( err , gitHubUser ) {
31+ count ++ ;
32+ if ( gitHubUser ) {
33+ // force finish
34+ user = gitHubUser ;
35+ count = users . length ;
36+ }
37+ callback ( ) ;
38+ } ) ;
39+ } ,
40+ function ( err ) {
41+ if ( err ) {
42+ return cb ( err ) ;
43+ }
44+ cb ( null , user ) ;
45+ }
46+ ) ;
47+ }
48+
49+
2050function findUsersForRepos ( repos , cb ) {
2151 debug ( 'findUsersForRepos' , 'total repos num:' , repos . length ) ;
2252 async . map ( repos , function ( repo , callback ) {
23- User . findByGithubId ( repo . creators [ 0 ] , function ( err , user ) {
53+ findUser ( repo . creators , function ( err , user ) {
2454 if ( err ) { return callback ( err ) ; }
2555 repo . user = user ;
2656 callback ( null , repo ) ;
@@ -31,21 +61,39 @@ function findUsersForRepos(repos, cb) {
3161
3262function updateHooksEvents ( repos , cb ) {
3363 debug ( 'updateHooksEvents' , 'total repos num:' , repos . length ) ;
34- async . mapLimit ( repos , function ( repo , callback ) {
64+ async . mapLimit ( repos , 50 , function ( repo , callback ) {
65+ debug ( 'processing repo' , repo ) ;
66+ if ( ! repo . user ) {
67+ debug ( 'user not found for the repo' , repo ) ;
68+ return callback ( ) ;
69+ }
3570 var github = new GitHub ( { token : repo . user . accounts . github . accessToken } ) ;
3671 // this will actually update hook (not just create if missing)
37- github . createRepoHookIfNotAlready ( repo . _id , function ( err , result ) {
72+ github . createRepoHookIfNotAlready ( repo . _id , function ( err ) {
3873 if ( err ) {
39- console . log ( 'failed to update webhook for:' , repo , '; error: ' , err ) ;
74+ allErrors . push ( err ) ;
75+ if ( err . output . statusCode === 404 ) {
76+ debug ( 'repos not found. just skip it' , repo ) ;
77+ callback ( null ) ;
78+ }
79+ else if ( err . output . statusCode === 502 ) {
80+ debug ( 'access token removed. just skip it' , repo ) ;
81+ callback ( null ) ;
82+ }
83+ else {
84+ callback ( err ) ;
85+ }
86+ }
87+ else {
88+ callback ( null ) ;
4089 }
41- callback ( null , result ) ;
4290 } ) ;
43- } , 10 , cb ) ;
91+ } , cb ) ;
4492}
4593
46- function finish ( err , results ) {
94+ function finish ( err ) {
4795 console . log ( 'DONE: err?' , err ) ;
48- console . log ( 'all results ' , results ) ;
96+ console . log ( 'all errors ' , allErrors ) ;
4997 process . exit ( ) ;
5098}
5199async . waterfall ( [
0 commit comments