Skip to content

Instantly share code, notes, and snippets.

@chris-gunawardena
Created September 7, 2016 09:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chris-gunawardena/8ef260f0663f954d8383432515c06c5e to your computer and use it in GitHub Desktop.
Save chris-gunawardena/8ef260f0663f954d8383432515c06c5e to your computer and use it in GitHub Desktop.
// Description:
// Returns the top approvers for the last n pull requests
//
// Commands:
// sportsbot pull-me WEB/mobile
// sportsbot pull-me MIC/apicoreservices 50
// sportsbot pull-me help
//
var https = require('https');
var _ = require('underscore');
function sortObject(obj) {
var arr = [];
for (var prop in obj) {
if (obj.hasOwnProperty(prop)) {
arr.push({
'key': prop,
'value': obj[prop]
});
}
}
arr.sort(function(a, b) { return b.value - a.value; });
//arr.sort(function(a, b) { a.value.toLowerCase().localeCompare(b.value.toLowerCase()); }); //use this to sort as strings
return arr; // returns array
}
module.exports = function(robot) {
robot.respond(/pull-me[ ]+([A-Z]{3})\/([a-z_-]+)[ ]*([0-9]*)|pull-me/i, function(msg){
var project = msg.match[1] || 'WEB';
var repo = msg.match[2] || 'mobile';
var limit = msg.match[3] || '100';
https.get({
host: 'stash.sbetcorp.com.au',
port: 443,
path: '/rest/api/latest/projects/' + project + '/repos/' + repo + '/pull-requests?order=newest&state=MERGED&start=0&limit=' + limit,
auth: process.env.HUBOT_HIPCHAT_NAME + ':' + process.env.HUBOT_HIPCHAT_PASSWORD
}, function(response) {
var reviewers = {};
var body = '';
response.on('data', function(d) {
body += d;
});
response.on('end', function() {
var pull_requests = JSON.parse(body);
_.each(pull_requests.values, function(pull_request){
_.each(pull_request.reviewers, function(reviewer){
if(reviewer.approved) {
reviewers[reviewer.user.displayName] = (parseInt(reviewers[reviewer.user.displayName], 10) || 0) + 1;
}
});
});
var topReviewers = '\n';
_.each(sortObject(reviewers), function(reviewer, i){
topReviewers = topReviewers + (i+1) + '. ' + reviewer.key + ': ' + reviewer.value + '\n';
});
msg.reply(topReviewers);
});
});
});
robot.respond(/pull-me help/i, function(msg){
var resp = '\n'
+ 'Returns the top approvers for the last 100 pull requests. eg: `sportsbot pull-me WEB/mobile`'
+ '\n'
+ 'Returns the top approvers for the last 50 pull requests. eg: `sportsbot pull-me WEB/mobile 50`';
msg.reply(resp);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment