Skip to content

Instantly share code, notes, and snippets.

@Vbitz
Created May 4, 2014 22:11
Show Gist options
  • Save Vbitz/c1422fe960c999b126b3 to your computer and use it in GitHub Desktop.
Save Vbitz/c1422fe960c999b126b3 to your computer and use it in GitHub Desktop.
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