Created
June 5, 2014 02:59
-
-
Save TanookiMario/f15a94b427a2a7227723 to your computer and use it in GitHub Desktop.
hubot pr roulette
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Description: | |
# Assign open pull requests with the label 'Needs Code Review' to developers | |
# that are currently in the 'team chat' room and not a part of black list. | |
# | |
# Dependencies: | |
# None | |
# | |
# Configuration: | |
# HUBOT_GITHUB_TOKEN | |
# | |
# Commands: | |
# hubot pr roulette -> sets the wheel in motion | |
# | |
# Author: | |
# TanookiMario | |
module.exports = (robot) -> | |
robot.respond /pr roulette/i, (msg) -> | |
prs = [] | |
prs = getPullRequests msg, (prs) -> | |
getDevs msg, (devs) -> | |
assignedPRs = assignPRs devs, prs | |
sendPRs msg, assignedPRs | |
sendPRs = (msg, prs) -> | |
for pr in prs | |
sendPR pr, msg | |
sendPR = (pr, msg) -> | |
token = process.env.HUBOT_HIPCHAT_TOKEN | |
path = "https://api.hipchat.com/v1/users/show?user_id=#{pr.dev}&format=json&auth_token=#{token}" | |
msg.http(path).get() (err, res, body) -> | |
if err | |
msg.send "Hipchat says: " + err | |
else if res.statusCode != 200 | |
content = JSON.parse(body) | |
msg.send "Status code: " + res.statusCode + ". Hipchat says: " + content.message | |
else | |
content = JSON.parse(body) | |
msg.send "@" + content.user.mention_name + ' -> ' + pr.pr | |
getPullRequests = (msg, callback) -> | |
needsCodeReview = [] | |
path = "https://api.github.com/orgs/thinkthroughmath/issues?filter=all" | |
token = "token " + process.env.HUBOT_GITHUB_TOKEN | |
msg.http(path).headers(Authorization: token).get() (err, res, body) -> | |
if err | |
msg.send "Github says: " + err | |
else if res.statusCode != 200 | |
content = JSON.parse(body) | |
msg.send "Status code: " + res.statusCode + ". Github says: " + content.message | |
else | |
content = JSON.parse(body) | |
callback(getOpenNeedsCodeReviewPRs(msg, content)) | |
getOpenNeedsCodeReviewPRs = (msg, prs) -> | |
needsCodeReview = [] | |
for pr in prs | |
for label in pr.labels | |
if label.name == 'Needs Code Review' | |
needsCodeReview.push pr.html_url | |
needsCodeReview | |
getDevs = (msg, callback) -> | |
token = process.env.HUBOT_HIPCHAT_TOKEN | |
path = "https://api.hipchat.com/v1/rooms/show?room_id=181830&format=json&auth_token=#{token}" | |
msg.http(path).get() (err, res, body) -> | |
if err | |
msg.send "Hipchat says: " + err | |
else if res.statusCode != 200 | |
content = JSON.parse(body) | |
msg.send "Status code: " + res.statusCode + ". Hipchat says: " + content.message | |
else | |
content = JSON.parse(body) | |
callback(getDevNames(msg,content)) | |
getDevNames = (msg, devs) -> | |
availableDevs = [] | |
for dev in devs.room.participants | |
availableDevs.push dev.user_id | |
availableDevs | |
assignPRs = (devs, prs) -> | |
prAssignments = [] | |
blackList = [ | |
298784, #tim | |
298790, #mathbot | |
298793, #mario | |
298798, #jeff | |
298888, #rondo | |
300063, #jim | |
312353, #andre | |
419874, #rob | |
420154, #john | |
453148, #kate | |
488356, #carl | |
629120, #abby | |
815162, #drew | |
] | |
for dev in blackList | |
index = devs.indexOf(dev) | |
if (index > -1) | |
devs.splice(index,1) | |
while (prs.length > 0 && devs.length > 0) | |
prNum = Math.floor(Math.random() * prs.length) | |
devNum = Math.floor(Math.random() * devs.length) | |
dev = devs[devNum] | |
pr = prs[prNum] | |
prAssignments.push {dev: dev, pr: pr} | |
index = devs.indexOf(dev) | |
devs.splice(index,1) | |
index = prs.indexOf(pr) | |
prs.splice(index,1) | |
prAssignments |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment