gitlab-to-irc/index.js

173 lines
5.0 KiB
JavaScript

var irc = require('irc');
var express = require('express');
var bodyParser = require('body-parser');
var request = require('request');
var config = require('./config');
var channels = [];
var hookToChannel = {};
for (var who in config.reports) {
if (who.indexOf('#') === 0)
channels.push(who);
var hooks = config.reports[who];
for (var i = 0; i < hooks.length; i++) {
var hook = hooks[i];
(hookToChannel[hook] = hookToChannel[hook] || []).push(who);
}
}
var client = new irc.Client(config.server, config.nick, {
//debug: true,
channels: channels,
userName: config.userName,
realName: config.realName,
retryDelay: 120000
});
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 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, say) {
var user = body.user_name;
var projectName = body.project.name;
var commits = body.commits;
var numCommits = body.total_commits_count;
var branchName = body.ref.replace('refs/heads/', '');
var msg = [];
if (!numCommits) {
// Special case: a branch was created or deleted.
var action = 'created';
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 + '.');
var lastCommit = commits[0];
shortenURL(lastCommit.url, function(shortUrl) {
if (!err) {
msg.push('last commit: ' + lastCommit.message + ' : ' + shortUrl);
say(msg);
}
});
}
},
issue: function(body, say) {
var user = body.user.name;
var projectName = body.project.name;
var issue = body.object_attributes;
var issueNumber = issue.iid;
var issueTitle = issue.title;
var issueState = issue.state;
var url = issue.url;
// Don't trigger the hook on issue's update;
if (issue.action === 'update')
return;
shortenURL(url, function(shortUrl) {
var msg = [projectName + ': issue #' + issueNumber + ' has changed state ("' + issueState + '")'];
msg.push(issueTitle + ' ' + shortUrl);
say(msg)
});
},
merge_request: function(body, say) {
var user = body.user.name;
var request = body.object_attributes;
var projectName = request.target.name;
var from = request.source_branch;
var to = request.target_branch;
var id = request.iid;
var title = request.title;
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 + ' ' + shortUrl);
say(msg);
});
},
build: function(body, msg) {
var id = body.build_id;
var status = body.build_status;
var isFinished = body.build_finished_at !== null;
var duration = body.build_duration;
var projectName = body.project_name;
var stage = body.build_stage;
var msg = [];
msg.push(projectName + ': build #' + id + ' (' + stage + ') changed status: ' + status);
if (isFinished)
msg.push('build finished in ' + duration + ' seconds. ');
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])
handlers[body.object_kind](body, say);
else
console.log("Unexpected object_kind:", body.object_kind);
res.sendStatus(200);
});
app.listen(config.port, config.hostname, function() {
console.log('gitlab-to-irc running.');
});