Fixes #9: Shorten URLs with lstu;

This commit is contained in:
Benjamin Bouvier 2016-07-16 19:22:27 +02:00
parent 107c3d83ad
commit e5840d521c
2 changed files with 53 additions and 37 deletions

View File

@ -1,6 +1,7 @@
var irc = require('irc');
var express = require('express');
var bodyParser = require('body-parser');
var request = require('request');
var config = require('./config');
@ -25,20 +26,29 @@ var client = new irc.Client(config.server, config.nick, {
retryDelay: 120000
});
var say = client.say.bind(client);
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;
function shortenURL(url, callback) {
request('https://lstu.fr/a', { method: 'POST', form: { lsturl: url, format: 'json' } }, function (err, res, body) {
try {
body = JSON.parse(body);
} catch(err) {
body = {err: 'cant parse JSON'};
}
if (err || !body.success) {
console.error("Error when shortening link: (status: " + res.statusCode + ")", '\nerror:', err, '\nfailure reason:', body.msg);
} else {
callback(body.short);
}
});
}
var handlers = {
push: function(body) {
push: function(body, say) {
var user = body.user_name;
var projectName = body.project.name;
@ -54,21 +64,22 @@ var handlers = {
if (body.after === '0000000000000000000000000000000000000000')
action = 'deleted';
msg.push(projectName + ': ' + user + ' ' + action + ' branch ' + branchName);
say(msg);
} else {
var maybeS = numCommits === 1 ? '' : 's';
msg.push('push on ' + projectName + '@' + branchName + ': ' + user + ' pushed ' + commits.length + ' commit' + maybeS + '.');
if (numCommits < 4) {
msg = msg.concat(commits.map(formatCommit));
} else {
msg.push(formatCommit(commits[0]) + ' ...');
msg.push(formatCommit(commits[commits.length - 1]));
}
}
return msg;
var lastCommit = commits[0];
shortenURL(lastCommit.url, function(shortUrl) {
if (!err) {
msg.push('last commit: ' + lastCommit.message + ' : ' + shortUrl);
say(msg);
}
});
}
},
issue: function(body) {
issue: function(body, say) {
var user = body.user.name;
var projectName = body.project.name;
@ -82,13 +93,14 @@ var handlers = {
if (issue.action === 'update')
return;
shortenURL(url, function(shortUrl) {
var msg = [projectName + ': issue #' + issueNumber + ' has changed state ("' + issueState + '")'];
msg.push(issueTitle + ' ' + url);
return msg;
msg.push(issueTitle + ' ' + shortUrl);
say(msg)
});
},
merge_request: function(body) {
merge_request: function(body, say) {
var user = body.user.name;
var request = body.object_attributes;
@ -103,13 +115,14 @@ var handlers = {
var url = request.url;
var state = request.state;
shortenURL(url, function(shortUrl) {
var msg = [projectName + ': merge request (' + from + ':' + to + ') #' + id + ' has changed state ("' + state + '")'];
msg.push(title + ' ' + url);
return msg;
msg.push(title + ' ' + shortUrl);
say(msg);
});
},
build: function(body) {
build: function(body, msg) {
var id = body.build_id;
var status = body.build_status;
@ -125,30 +138,32 @@ var handlers = {
if (isFinished)
msg.push('build finished in ' + duration + ' seconds. ');
return msg;
say(msg);
}
};
function say(msgs) {
if (msgs) {
var whom = hookToChannel[body.object_kind] || [];
if (msgs instanceof Array) {
for (var i = 0; i < msgs.length; i++)
client.say(whom, msgs[i]);
} else {
client.say(whom, msgs);
}
}
}
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);
handlers[body.object_kind](body, say);
else
console.log("Unexpected object_kind:", body.object_kind);
if (msgs) {
var whom = hookToChannel[body.object_kind] || [];
if (msgs instanceof Array) {
for (var i = 0; i < msgs.length; i++)
say(whom, msgs[i]);
} else {
say(whom, msgs);
}
}
res.sendStatus(200);
});

View File

@ -8,6 +8,7 @@
"dependencies": {
"body-parser": "1.15.2",
"express": "4.14.0",
"irc": "0.3.9"
"irc": "0.3.9",
"request": "2.73.0"
}
}