@@ -6,6 +6,7 @@ var debug = require('debug')('strider-github:api');
66var Step = require ( 'step' ) ;
77var superagent = require ( 'superagent' ) ;
88var url = require ( 'url' ) ;
9+ var util = require ( 'util' ) ;
910
1011var GITHUB_API_ENDPOINT = process . env . PLUGIN_GITHUB_API_ENDPOINT || 'https://api.github.com' ;
1112
@@ -47,6 +48,7 @@ function createHooks(reponame, url, secret, token, callback) {
4748 . set ( 'User-Agent' , 'StriderCD (http://stridercd.com)' )
4849 . end ( function ( err , res ) {
4950 if ( err ) return callback ( err ) ;
51+
5052 if ( res . statusCode !== 201 ) {
5153 var badStatusErr = new Error ( 'Bad status code: ' + res . statusCode ) ;
5254 badStatusErr . statusCode = res . statusCode ;
@@ -73,7 +75,8 @@ function deleteHooks(reponame, url, token, callback) {
7375 . get ( apiUrl )
7476 . set ( 'Authorization' , 'token ' + token )
7577 . set ( 'User-Agent' , 'StriderCD (http://stridercd.com)' )
76- . end ( function ( res ) {
78+ . end ( function ( err , res ) {
79+ if ( err ) return callback ( err ) ;
7780 if ( res . status > 300 ) {
7881 debug ( 'Error getting hooks' , res . status , res . text ) ;
7982 return callback ( res . status ) ;
@@ -87,7 +90,8 @@ function deleteHooks(reponame, url, token, callback) {
8790 . del ( hook . url )
8891 . set ( 'Authorization' , 'token ' + token )
8992 . set ( 'User-Agent' , 'StriderCD (http://stridercd.com)' )
90- . end ( function ( res ) {
93+ . end ( function ( err , res ) {
94+ if ( err ) return next ( new Error ( 'Failed to delete webhook ' + hook . url + 'Error: ' + err ) ) ;
9195 if ( res . status !== 204 ) {
9296 debug ( 'bad status' , res . status , hook . id , hook . url ) ;
9397 return next ( new Error ( 'Failed to delete a webhook: status for url ' + hook . url + ': ' + res . status ) ) ;
@@ -127,7 +131,8 @@ function getFile(filename, ref, accessToken, owner, repo, done) {
127131 if ( accessToken ) {
128132 req = req . set ( 'Authorization' , 'token ' + accessToken ) ;
129133 }
130- req . end ( function ( res ) {
134+ req . end ( function ( err , res ) {
135+ if ( err ) return done ( err , null ) ;
131136 if ( res . error ) return done ( res . error , null ) ;
132137 if ( ! res . body . content ) {
133138 return done ( ) ;
@@ -152,6 +157,7 @@ var get_oauth2 = module.exports.get_oauth2 = function (url, q_params, access_tok
152157 // Construct the query. Allow the user to override the access_token through q_params.
153158 var query = _ . assign ( { } , { access_token : access_token } , q_params ) ;
154159 debug ( 'GET OAUTH2 URL:' , url ) ;
160+ debug ( 'Inside get_oauth2: Callback type: ' + typeof callback + ' Number of arguments expected by: ' + callback . length ) ;
155161 client
156162 . get ( url )
157163 . query ( query )
@@ -173,12 +179,13 @@ var api_call = module.exports.api_call = function (path, access_token, callback,
173179 // If the user provided a superagent instance, use that.
174180 client = client || superagent ;
175181 var url = GITHUB_API_ENDPOINT + path ;
176- debug ( 'API CALL:' , url , params , access_token ) ;
177- get_oauth2 ( url , { } , access_token , function ( error , res , body ) {
182+ debug ( 'API CALL:' , url , access_token ) ;
183+ get_oauth2 ( url , { } , access_token , function ( error , res ) {
178184 if ( ! error && res . statusCode == 200 ) {
179- var data = JSON . parse ( body ) ;
185+ var data = res . body ;
180186 callback ( null , res , data ) ;
181187 } else {
188+ debug ( "We get an error from the API: " + error ) ;
182189 callback ( error , res , null ) ;
183190 }
184191 } , client ) ;
@@ -242,11 +249,12 @@ var pageinated_api_call = module.exports.pageinated_api_call = function (path, a
242249
243250 function loop ( uri , page ) {
244251 debug ( 'PAGINATED API CALL URL:' , uri ) ;
245- get_oauth2 ( uri , { per_page : 30 , page : page } , access_token , function ( error , res , body ) {
252+ get_oauth2 ( uri , { per_page : 30 , page : page } , access_token , function ( error , res ) {
253+
246254 if ( ! error && res . statusCode == 200 ) {
247255 var data ;
248256 try {
249- data = JSON . parse ( body ) ;
257+ data = res . body ;
250258 } catch ( e ) {
251259 return callback ( e , null ) ;
252260 }
@@ -267,11 +275,13 @@ var pageinated_api_call = module.exports.pageinated_api_call = function (path, a
267275 }
268276 } else {
269277 if ( ! error ) {
278+ debug ( "We did not get an error, but status code was: " + res . statusCode ) ;
270279 if ( res . statusCode === 401 || res . statusCode === 403 ) {
271280 return callback ( new Error ( 'Github app is not authorized. Did you revoke access?' ) )
272281 }
273- return callback ( new Error ( 'Status code is ' + res . statusCode + ' not 200. Body: ' + body ) )
282+ return callback ( new Error ( 'Status code is ' + res . statusCode + ' not 200. Body: ' + res . body ) )
274283 } else {
284+ debug ( "We did get an error from the API " + error ) ;
275285 return callback ( error , null ) ;
276286 }
277287 }
@@ -302,6 +312,7 @@ function getRepos(token, username, callback) {
302312 // see strider-extension-loader for details
303313
304314 /* jshint -W064 */
315+ /* jshint -W040 */
305316 Step (
306317 function fetchReposAndOrgs ( ) {
307318 // First fetch the user's repositories and organizations in parallel.
@@ -343,13 +354,16 @@ function getRepos(token, username, callback) {
343354 } ,
344355 function fetchTeamDetails ( err , results ) {
345356 if ( err ) {
357+ debug ( err . message ) ;
358+ debug ( err . name ) ;
359+ debug ( err . stack ) ;
346360 debug ( 'get_github_repos() - Error fetching Org Teams response - %s' , err ) ;
347361 return callback ( err ) ;
348362 }
349363 var teams = [ ] ;
350364 _ . each ( results , function ( result ) {
351365 try {
352- var team_data = JSON . parse ( result . body ) ;
366+ var team_data = result . body ;
353367 _ . each ( team_data , function ( t ) {
354368 teams . push ( t ) ;
355369 } ) ;
@@ -372,23 +386,24 @@ function getRepos(token, username, callback) {
372386 var team_details = [ ] ;
373387 _ . each ( results , function ( result ) {
374388 try {
375- var td = JSON . parse ( result . body ) ;
389+ var td = result . body ;
376390 team_details . push ( td ) ;
377391 } catch ( e ) {
378392 debug ( 'get_github_repos(): Error parsing JSON in detail team response - %s' , e ) ;
379393 }
380394 } ) ;
381395 // For each Team with admin privs, test for membership
382396 var group = this . group ( ) ;
397+
383398 var team_detail_requests = { } ;
384399 _ . each ( team_details , function ( team_details ) {
385400 if ( team_details . permission != 'admin' ) {
386401 return ;
387402 }
388403 team_detail_requests [ team_details . id ] = team_details ;
389- var url = GITHUB_API_ENDPOINT + '/teams/' + team_details . id + '/members/' + username ;
404+ var url = '/teams/' + team_details . id + '/members/' + username ;
390405 debug ( 'TEAM DETAIL URL' , url ) ;
391- get_oauth2 ( url , { } , token , group ( ) ) ;
406+ api_call ( url , token , group ( ) ) ;
392407 } ) ;
393408 this . team_detail_requests = team_detail_requests ;
394409 } ,
@@ -401,18 +416,14 @@ function getRepos(token, username, callback) {
401416 }
402417 var team_detail_requests = this . team_detail_requests ;
403418 var group = this . group ( ) ;
404- _ . each ( results , function ( res ) {
405- var team_id = res . request . uri . path . split ( '/' ) ;
406- if ( res . request . uri . path . indexOf ( '/api/v3/' ) > - 1 ) {
407- // Github Enterprise url
408- team_id = team_id [ 4 ] ;
409- }
410- else {
411- // Github url
412- team_id = team_id [ 2 ] ;
413- }
419+
420+ _ . each ( results , function ( result ) {
421+ var team_id = result . req . path . split ( '/' ) ;
422+ debug ( "We get the following repo path: " + util . inspect ( result . req . path ) ) ;
423+ team_id = team_id [ 2 ] ;
424+
414425 var team_detail = team_detail_requests [ parseInt ( team_id , 10 ) ] ;
415- if ( res . statusCode === 204 ) {
426+ if ( result . statusCode === 204 ) {
416427 pageinated_api_call ( '/teams/' + team_id + '/repos' , token , group ( ) ) ;
417428 }
418429
@@ -424,6 +435,7 @@ function getRepos(token, username, callback) {
424435 debug ( 'get_github_repos(): Error with team repos request: %s' , err ) ;
425436 return callback ( err ) ;
426437 }
438+
427439 _ . each ( results , function ( result ) {
428440 if ( result && result . data ) {
429441 _ . each ( result . data , function ( team_repo ) {
@@ -445,6 +457,7 @@ function getRepos(token, username, callback) {
445457 } ) ;
446458 }
447459 } ) ;
460+
448461 // Sometimes we can get multiple copies of the same team repo, so we uniq it
449462 team_repos = _ . uniq ( team_repos , false , function ( item ) {
450463 return item . id ;
0 commit comments