From 37cc889d6427ddff2cdafa48a2ce419efb39264b Mon Sep 17 00:00:00 2001 From: Benjamin Bouvier Date: Thu, 23 Jun 2016 21:50:55 +0200 Subject: [PATCH] React on push; --- index.js | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/index.js b/index.js index 73c0236..6261442 100644 --- a/index.js +++ b/index.js @@ -19,9 +19,48 @@ var app = express(); app.use(bodyParser.json()); // for parsing application/json app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded +function formatCommit(commit) { + return commit.message + ' ' + commit.url; +} + +var handlers = { + + push: function(body) { + var user = body.user_name; + var repoName = body.repository.name; + var commits = body.commits; + + var msg = ['push on ' + repoName + ': ' + user + ' pushed ' + commits.length + ' commits.']; + + if (commits.length < 4) { + msg = msg.concat(commits.map(formatCommit)); + } else { + msg.push(formatCommit(commits[0])); + msg.push('...'); + msg.push(formatCommit(commits[commits.length - 1])); + } + + return msg; + } + +}; + app.post('/', function(req, res) { var body = req.body || {}; + var msgs = null; + if (body.object_kind && handlers[body.object_kind]) + msgs = handlers[body.object_kind](body); + + if (msgs) { + if (msgs instanceof Array) { + for (var i = 0; i < msgs.length; i++) + say(config.channels, msgs[i]); + } else { + say(config.channels, msgs); + } + } + res.sendStatus(200); });