Skip to content

Commit ddf1299

Browse files
committed
WIP: incorrect no. of args for superagent callback
The plugin was failing to fetch the repos when configuring a github account for the first time OR when trying to do a "Refresh Account" from the web page. There were a couple of issues here: a. JSON.parse was not necessary since superagent returns a parsed object if we receive json. https://visionmedia.github.io/superagent/#parsing-response%20bodies b. We were passing a callback with three arguments to superagent in .end(callback) for example in - get_oauth2(uri, {per_page: 30, page: page}, access_token, function (error, response, body) { // ^ here, we should send // only error and response //In any case superagent puts the body inside response.body //https://visionmedia.github.io/superagent/ This may be verified by looking at superagent/lib/node/index.js Around line 623 - (debugs added) Request.prototype.callback = function(err, res){ debug('When calling the callback - err is ' + JSON.stringify(err, null, 4)); var fn = this._callback; debug("And the number of arguments the callback expects is: " + fn.length ); this.clearTimeout(); if (this.called) return console.warn('double callback!'); this.called = true; if (2 == fn.length) return fn(err, res);//<====== we want this to be true if (err) return this.emit('error', err); debug("Here, we are sending the first parameter to the callback as the response, " + "but GOTCHA - our callback thinks the first param is an error and THROWS!"); fn(res); }; c. Still need to understand the presence of this.group and group() as present in the comments in the code. The reason why it fails near get_github_repos(): Error with admin team memberships: [object Object] is because, the previous function in the "Step" has a call to get_oauth2(url, {}, token, group()) - which basically sends the last parameter as it is to superagent. And thus we have an object with data received from the API, which we think is an err.
1 parent 489484d commit ddf1299

2 files changed

Lines changed: 39 additions & 18 deletions

File tree

lib/api.js

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ function deleteHooks(reponame, url, token, callback) {
7373
.get(apiUrl)
7474
.set('Authorization', 'token ' + token)
7575
.set('User-Agent', 'StriderCD (http://stridercd.com)')
76-
.end(function (res) {
76+
.end(function (res) { //CHECK: have a callback with the first param as err and process that err
7777
if (res.status > 300) {
7878
debug('Error getting hooks', res.status, res.text);
7979
return callback(res.status);
@@ -87,7 +87,7 @@ function deleteHooks(reponame, url, token, callback) {
8787
.del(hook.url)
8888
.set('Authorization', 'token ' + token)
8989
.set('User-Agent', 'StriderCD (http://stridercd.com)')
90-
.end(function (res) {
90+
.end(function (res) { //CHECK: have a callback with the first param as err and process that err
9191
if (res.status !== 204) {
9292
debug('bad status', res.status, hook.id, hook.url);
9393
return next(new Error('Failed to delete a webhook: status for url ' + hook.url + ': ' + res.status));
@@ -127,7 +127,7 @@ function getFile(filename, ref, accessToken, owner, repo, done) {
127127
if (accessToken) {
128128
req = req.set('Authorization', 'token ' + accessToken);
129129
}
130-
req.end(function (res) {
130+
req.end(function (res) { //CHECK: have a callback with the first param as err and process that err
131131
if (res.error) return done(res.error, null);
132132
if (!res.body.content) {
133133
return done();
@@ -152,6 +152,7 @@ var get_oauth2 = module.exports.get_oauth2 = function (url, q_params, access_tok
152152
// Construct the query. Allow the user to override the access_token through q_params.
153153
var query = _.assign({}, {access_token : access_token}, q_params);
154154
debug('GET OAUTH2 URL:', url);
155+
debug('Inside get_oauth2: Callback type: ' + typeof callback + ' Number of arguments expected by: ' + callback.length);
155156
client
156157
.get(url)
157158
.query(query)
@@ -173,10 +174,11 @@ var api_call = module.exports.api_call = function (path, access_token, callback,
173174
// If the user provided a superagent instance, use that.
174175
client = client || superagent;
175176
var url = GITHUB_API_ENDPOINT + path;
176-
debug('API CALL:', url, params, access_token);
177-
get_oauth2(url, {}, access_token, function (error, res, body) {
177+
debug('API CALL:', url, access_token);
178+
get_oauth2(url, {}, access_token, function (error, res) {
179+
debug("We get an error from the API: " + error);
178180
if (!error && res.statusCode == 200) {
179-
var data = JSON.parse(body);
181+
var data = res.body;
180182
callback(null, res, data);
181183
} else {
182184
callback(error, res, null);
@@ -242,11 +244,12 @@ var pageinated_api_call = module.exports.pageinated_api_call = function (path, a
242244

243245
function loop(uri, page) {
244246
debug('PAGINATED API CALL URL:', uri);
245-
get_oauth2(uri, {per_page: 30, page: page}, access_token, function (error, res, body) {
247+
get_oauth2(uri, {per_page: 30, page: page}, access_token, function (error, res) {
248+
debug("We get an error: " + error);
246249
if (!error && res.statusCode == 200) {
247250
var data;
248251
try {
249-
data = JSON.parse(body);
252+
data = res.body;
250253
} catch (e) {
251254
return callback(e, null);
252255
}
@@ -267,11 +270,13 @@ var pageinated_api_call = module.exports.pageinated_api_call = function (path, a
267270
}
268271
} else {
269272
if (!error) {
273+
debug("We did not get an error, but status code was: " + res.statusCode);
270274
if (res.statusCode === 401 || res.statusCode === 403) {
271275
return callback(new Error('Github app is not authorized. Did you revoke access?'))
272276
}
273277
return callback(new Error('Status code is ' + res.statusCode + ' not 200. Body: ' + body))
274278
} else {
279+
debug("We did get an error from the API " + error);
275280
return callback(error, null);
276281
}
277282
}
@@ -343,13 +348,16 @@ function getRepos(token, username, callback) {
343348
},
344349
function fetchTeamDetails(err, results) {
345350
if (err) {
351+
debug(err.message);
352+
debug(err.name);
353+
debug(err.stack);
346354
debug('get_github_repos() - Error fetching Org Teams response - %s', err);
347355
return callback(err);
348356
}
349357
var teams = [];
350358
_.each(results, function (result) {
351359
try {
352-
var team_data = JSON.parse(result.body);
360+
var team_data = result.body;
353361
_.each(team_data, function (t) {
354362
teams.push(t);
355363
});
@@ -372,14 +380,17 @@ function getRepos(token, username, callback) {
372380
var team_details = [];
373381
_.each(results, function (result) {
374382
try {
375-
var td = JSON.parse(result.body);
383+
var td = result.body;
376384
team_details.push(td);
377385
} catch (e) {
378386
debug('get_github_repos(): Error parsing JSON in detail team response - %s', e);
379387
}
380388
});
381389
// For each Team with admin privs, test for membership
382390
var group = this.group();
391+
debug("****************************************************************\nThis is: " + JSON.stringify(this, null, 4)); //CHECK: undefined
392+
debug("****************************************************************\ngroup is: " + JSON.stringify(group, null, 4)); //CHECK: undefined
393+
383394
var team_detail_requests = {};
384395
_.each(team_details, function (team_details) {
385396
if (team_details.permission != 'admin') {
@@ -388,7 +399,16 @@ function getRepos(token, username, callback) {
388399
team_detail_requests[team_details.id] = team_details;
389400
var url = GITHUB_API_ENDPOINT + '/teams/' + team_details.id + '/members/' + username;
390401
debug('TEAM DETAIL URL', url);
391-
get_oauth2(url, {}, token, group());
402+
get_oauth2(url, {}, token, group()); //<------------------- this is a problem
403+
//get_oauth sends this callback as it is to superagent
404+
//superagent expects a callback that takes two arguments
405+
//in the other places it works because we are sending group() as
406+
//as a parameter to api_call or pageinated_api_call, and they are now correctly wrapping
407+
//get_oauth, passing in a callback that expects two arguments
408+
//if we were to replace this get_oauth2 call by a call to api_call,
409+
//it would probably work as a hack, but the intention behind group()
410+
//and this.group() is yet to be understood by me
411+
392412
});
393413
this.team_detail_requests = team_detail_requests;
394414
},

lib/index.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ var GITHUB_API_ENDPOINT = 'https://api.github.com';
2323
* @param {Object} client An alternative superagent instance to use.
2424
*/
2525
var get_oauth2 = module.exports.get_oauth2 = function (url, q_params, access_token, callback, client) {
26+
console.debug('OAUTH2 CALLBACK LENGTH:', callback.length);
2627
// If the user provided a superagent instance, use that.
2728
client = client || superagent;
2829
// Construct the query. Allow the user to override the access_token through q_params.
@@ -50,9 +51,9 @@ var api_call = module.exports.api_call = function (path, access_token, callback,
5051
client = client || superagent;
5152
var url = GITHUB_API_ENDPOINT + path;
5253
debug('github api_call(): path %s', path);
53-
get_oauth2(url, {}, access_token, function (error, res, body) {
54+
get_oauth2(url, {}, access_token, function (error, res) {
5455
if (!error && res.statusCode == 200) {
55-
var data = JSON.parse(body);
56+
var data = res.body;
5657
callback(null, res, data);
5758
} else {
5859
callback(error, res, null);
@@ -117,11 +118,11 @@ var pageinated_api_call = module.exports.pageinated_api_call = function (path, a
117118
var pages = [];
118119

119120
function loop(uri, page) {
120-
get_oauth2(uri, {per_page: 30, page: page}, access_token, function (error, response, body) {
121+
get_oauth2(uri, {per_page: 30, page: page}, access_token, function (error, response) {
121122
if (!error && response.statusCode == 200) {
122123
var data;
123124
try {
124-
data = JSON.parse(body);
125+
data = response.body;
125126
} catch (e) {
126127
return callback(e, null);
127128
}
@@ -142,7 +143,7 @@ var pageinated_api_call = module.exports.pageinated_api_call = function (path, a
142143
}
143144
} else {
144145
if (!error) {
145-
return callback('Status code is ' + response.statusCode + ' not 200. Body: ' + body)
146+
return callback('Status code is ' + response.statusCode + ' not 200. Body: ' + response.body)
146147
} else {
147148
return callback(error, null);
148149
}
@@ -210,7 +211,7 @@ module.exports.get_github_repos = function (user, callback) {
210211
_.each(results, function (result) {
211212
try {
212213
debug('For Organizations: %s', result.request.uri.path.split('/')[2]);
213-
var team_data = JSON.parse(result.body);
214+
var team_data = result.body;
214215
_.each(team_data, function (t) {
215216
debug('Team details: %j', t);
216217
teams.push(t);
@@ -235,7 +236,7 @@ module.exports.get_github_repos = function (user, callback) {
235236
var team_details = [];
236237
_.each(results, function (result) {
237238
try {
238-
var td = JSON.parse(result.body);
239+
var td = result.body;
239240
team_details.push(td);
240241
} catch (e) {
241242
debug('get_github_repos(): Error parsing JSON in detail team response - %s', e);

0 commit comments

Comments
 (0)