Skip to content

Commit 92cd130

Browse files
committed
Merge pull request #555 from CodeNow/improvements-for-github-prs
Improvements for GitHub PR's commenting
2 parents 398ca9c + a8d1785 commit 92cd130

File tree

3 files changed

+69
-10
lines changed

3 files changed

+69
-10
lines changed

lib/models/apis/github.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,8 @@ Github.prototype.addComment = function(shortRepo, issueId, text, cb) {
487487
debug('back from creating issue comment', err);
488488
if (err) {
489489
err = (err.code === 404) ?
490-
Boom.notFound('Issue ' + shortRepo + '/issues/' + issueId + ' not found.', { err: err }) :
490+
Boom.notFound('Issue ' + shortRepo + '/issues/' + issueId + ' not found.',
491+
{ err: err, report: false }) :
491492
Boom.create(502, 'Failed to get issue comments ' + shortRepo + '/issues/' + issueId ,
492493
{ err: err });
493494
cb(err);
@@ -593,7 +594,8 @@ Github.prototype.listOpenPullRequestsForBranch = function (shortRepo, branch, cb
593594
this.pullRequests.getAll(query, function (err, prs) {
594595
if (err) {
595596
err = (err.code === 404) ?
596-
Boom.notFound('Cannot find open PRs for ' + shortRepo + '@' + branch, { err: err }) :
597+
Boom.notFound('Cannot find open PRs for ' + shortRepo + '@' + branch,
598+
{ err: err, report: false }) :
597599
Boom.create(502, 'Failed to get PRs for ' + shortRepo + '@' + branch, { err: err });
598600
cb(err);
599601
}

lib/routes/actions/github.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ var pluck = require('101/pluck');
2929
var noop = require('101/noop');
3030
var error = require('error');
3131
var github = require('middlewares/apis').github;
32+
var dogstatsd = require('models/datadog');
3233

3334
/** Receive the Github hooks
3435
* @event POST rest/actions/github
@@ -43,6 +44,7 @@ var pushSessionUser = {
4344
};
4445

4546
app.post('/actions/github/',
47+
reportDatadogEvent,
4648
mw.headers('user-agent').require().matches(/^GitHub.*$/),
4749
mw.headers('x-github-event', 'x-github-delivery').require(),
4850
mw.headers('x-github-event').matches(/^ping$/).then(
@@ -141,6 +143,13 @@ app.post('/actions/github/',
141143
mw.res.status(202),
142144
mw.res.send('No action set up for that payload.'));
143145

146+
147+
function reportDatadogEvent (req, res, next) {
148+
var eventName = req.get('x-github-event') || '';
149+
dogstatsd.increment('api.actions.github.events', ['event:' + eventName]);
150+
next();
151+
}
152+
144153
function parseGitHubPRData (req, res, next) {
145154
var repository = keypather.get(req, 'body.repository');
146155
if (!repository) {

scripts/update-github-webhooks-event.js

Lines changed: 56 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
2050
function 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

3262
function 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
}
5199
async.waterfall([

0 commit comments

Comments
 (0)