Created
May 4, 2014 22:11
-
-
Save Vbitz/c1422fe960c999b126b3 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
var http = require("http"); | |
var gitioURL = "git.io"; | |
function shortenURL(url, cb) { | |
var req = http.request({ | |
hostname: gitioURL, | |
port: 80, | |
path: "/", | |
method: "post", | |
headers: { | |
"Content-Length": url.length + 4 | |
} | |
}, function (res) { | |
if (res.statusCode !== 201) { | |
cb(new Error("Bad status code: " + res.statusCode)); | |
} else { | |
cb(null, res.headers.location); | |
} | |
res.on("data", function (d) {}); | |
res.on("end", function () {}); | |
}); | |
req.on("error", function (err) { | |
cb(err, url); | |
}); | |
req.write("url=" + url); | |
req.end(); | |
} | |
module.exports = function (robot) { | |
robot.router.post("/" + robot.name + "/github", function (req, res) { | |
var data = req.body; | |
var msgType = req.headers["x-github-event"]; | |
console.log(req.headers); | |
console.log(data); | |
switch (msgType) { | |
case "ping": | |
robot.messageRoom("room", "[github] ping zen: " + data.zen); | |
break; | |
case "issues": | |
switch (data.action) { | |
case "opened": | |
case "closed": | |
robot.messageRoom("room", "[github] " + data.issue.user.login + " " + data.action + " (#" + data.issue.number + ") " + | |
data.issue.title + " in " + data.repository.full_name); | |
break; | |
default: | |
robot.messageRoom("room", "[github] unknown issue type: " + data.action); | |
break; | |
} | |
break; | |
case "push": | |
data.commits.forEach(function (commit) { | |
shortenURL(commit.url, function (err, shortCommitURL) { | |
robot.messageRoom("room", "[github] " + commit.author.username + " commited to " + | |
data.repository.owner.name + "/" + data.repository.name + | |
" \"" + commit.message + "\"" + " (" + shortCommitURL + ")"); | |
}); | |
}); | |
break; | |
default: | |
robot.messageRoom("room", "[github] unknown type: " + msgType); | |
break; | |
} | |
res.send(""); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment