Fixes #9: Shorten URLs with lstu;
This commit is contained in:
parent
107c3d83ad
commit
e5840d521c
87
index.js
87
index.js
@ -1,6 +1,7 @@
|
|||||||
var irc = require('irc');
|
var irc = require('irc');
|
||||||
var express = require('express');
|
var express = require('express');
|
||||||
var bodyParser = require('body-parser');
|
var bodyParser = require('body-parser');
|
||||||
|
var request = require('request');
|
||||||
|
|
||||||
var config = require('./config');
|
var config = require('./config');
|
||||||
|
|
||||||
@ -25,20 +26,29 @@ var client = new irc.Client(config.server, config.nick, {
|
|||||||
retryDelay: 120000
|
retryDelay: 120000
|
||||||
});
|
});
|
||||||
|
|
||||||
var say = client.say.bind(client);
|
|
||||||
|
|
||||||
var app = express();
|
var app = express();
|
||||||
|
|
||||||
app.use(bodyParser.json()); // for parsing application/json
|
app.use(bodyParser.json()); // for parsing application/json
|
||||||
app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded
|
app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded
|
||||||
|
|
||||||
function formatCommit(commit) {
|
function shortenURL(url, callback) {
|
||||||
return commit.message + ' ' + commit.url;
|
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 = {
|
var handlers = {
|
||||||
|
|
||||||
push: function(body) {
|
push: function(body, say) {
|
||||||
var user = body.user_name;
|
var user = body.user_name;
|
||||||
var projectName = body.project.name;
|
var projectName = body.project.name;
|
||||||
|
|
||||||
@ -54,21 +64,22 @@ var handlers = {
|
|||||||
if (body.after === '0000000000000000000000000000000000000000')
|
if (body.after === '0000000000000000000000000000000000000000')
|
||||||
action = 'deleted';
|
action = 'deleted';
|
||||||
msg.push(projectName + ': ' + user + ' ' + action + ' branch ' + branchName);
|
msg.push(projectName + ': ' + user + ' ' + action + ' branch ' + branchName);
|
||||||
|
say(msg);
|
||||||
} else {
|
} else {
|
||||||
var maybeS = numCommits === 1 ? '' : 's';
|
var maybeS = numCommits === 1 ? '' : 's';
|
||||||
msg.push('push on ' + projectName + '@' + branchName + ': ' + user + ' pushed ' + commits.length + ' commit' + maybeS + '.');
|
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 user = body.user.name;
|
||||||
var projectName = body.project.name;
|
var projectName = body.project.name;
|
||||||
|
|
||||||
@ -82,13 +93,14 @@ var handlers = {
|
|||||||
if (issue.action === 'update')
|
if (issue.action === 'update')
|
||||||
return;
|
return;
|
||||||
|
|
||||||
var msg = [projectName + ': issue #' + issueNumber + ' has changed state ("' + issueState + '")'];
|
shortenURL(url, function(shortUrl) {
|
||||||
msg.push(issueTitle + ' ' + url);
|
var msg = [projectName + ': issue #' + issueNumber + ' has changed state ("' + issueState + '")'];
|
||||||
|
msg.push(issueTitle + ' ' + shortUrl);
|
||||||
return msg;
|
say(msg)
|
||||||
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
merge_request: function(body) {
|
merge_request: function(body, say) {
|
||||||
var user = body.user.name;
|
var user = body.user.name;
|
||||||
|
|
||||||
var request = body.object_attributes;
|
var request = body.object_attributes;
|
||||||
@ -103,13 +115,14 @@ var handlers = {
|
|||||||
var url = request.url;
|
var url = request.url;
|
||||||
var state = request.state;
|
var state = request.state;
|
||||||
|
|
||||||
var msg = [projectName + ': merge request (' + from + ':' + to + ') #' + id + ' has changed state ("' + state + '")'];
|
shortenURL(url, function(shortUrl) {
|
||||||
msg.push(title + ' ' + url);
|
var msg = [projectName + ': merge request (' + from + ':' + to + ') #' + id + ' has changed state ("' + state + '")'];
|
||||||
|
msg.push(title + ' ' + shortUrl);
|
||||||
return msg;
|
say(msg);
|
||||||
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
build: function(body) {
|
build: function(body, msg) {
|
||||||
|
|
||||||
var id = body.build_id;
|
var id = body.build_id;
|
||||||
var status = body.build_status;
|
var status = body.build_status;
|
||||||
@ -125,30 +138,32 @@ var handlers = {
|
|||||||
if (isFinished)
|
if (isFinished)
|
||||||
msg.push('build finished in ' + duration + ' seconds. ');
|
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) {
|
app.post('/', function(req, res) {
|
||||||
var body = req.body || {};
|
var body = req.body || {};
|
||||||
|
|
||||||
var msgs = null;
|
var msgs = null;
|
||||||
if (body.object_kind && handlers[body.object_kind])
|
if (body.object_kind && handlers[body.object_kind])
|
||||||
msgs = handlers[body.object_kind](body);
|
handlers[body.object_kind](body, say);
|
||||||
else
|
else
|
||||||
console.log("Unexpected object_kind:", body.object_kind);
|
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);
|
res.sendStatus(200);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -8,6 +8,7 @@
|
|||||||
"dependencies": {
|
"dependencies": {
|
||||||
"body-parser": "1.15.2",
|
"body-parser": "1.15.2",
|
||||||
"express": "4.14.0",
|
"express": "4.14.0",
|
||||||
"irc": "0.3.9"
|
"irc": "0.3.9",
|
||||||
|
"request": "2.73.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user