Compare commits

...

13 Commits

3 changed files with 106 additions and 11 deletions

View File

@ -1,3 +1,11 @@
NOTICE
======
This project has been merged within
[meta-bot](https://github.com/bnjbvr/meta-bot), from the same author. It will
not be maintained anymore! It is highly recommended to migrate to meta-bot and
use the gitlab-notifications and gitlab-helpers modules instead.
gitlab-to-irc
===

View File

@ -8,8 +8,15 @@ module.exports = {
'somebody': ['push', 'merge_request', 'issue', 'build']
},
// Project URL used for mentions of MR (e.g. !123) or issues (#122).
projectUrl: "https://framagit.org/bnjbvr/kresus/",
// Amout of messages between which no other mentions to a same MR/issue
// will be done.
cacheDuration: 15,
branches: ['master'],
// IRC nick/names for the bot
nick: 'gitlab-bot',
userName: 'gitlab-bot',

102
index.js
View File

@ -69,16 +69,32 @@ if (config.lstu) {
};
}
function conjugatePast(verb) {
// Make action displayable (e.g., open -> opened, close -> closed, merge -> merged).
return verb + (verb.substr(-1) === 'e' ? '' : 'e') + 'd';
}
var lastIssueActions = {};
var handlers = {
push: function(body, say) {
var user = body.user_name;
var user = body.user_username;
var projectName = body.project.name;
var commits = body.commits;
var numCommits = body.total_commits_count;
var branchName = body.ref.replace('refs/heads/', '');
var found = false;
for (var i = 0; i < config.branches.length; i++) {
if (branchName === config.branches[i]) {
found = true;
break;
}
}
if (!found)
return;
var msg = null;
if (!numCommits) {
@ -86,22 +102,26 @@ var handlers = {
var action = 'created';
if (body.after === '0000000000000000000000000000000000000000')
action = 'deleted';
msg = projectName + ': ' + user + ' ' + action + ' branch ' + branchName;
msg = user + ' ' + action + ' branch ' + branchName + ' on ' + projectName + '.';
say(msg);
} else {
var maybeS = numCommits === 1 ? '' : 's';
var lastCommit = commits[commits.length - 1];
var lastCommitMessage = lastCommit.message.trim().split('\n')[0].trim();
shortenURL(lastCommit.url, function(shortUrl) {
msg = 'push on ' + projectName + '@' + branchName + ' (by ' + user + '): ' +
commits.length + ' commit' + maybeS + ' (last: ' + lastCommitMessage + ') ' + shortUrl;
msg = user + ' pushed on ' + projectName + '@' + branchName + ': ';
if (numCommits === 1) {
msg += lastCommitMessage + ' ' + shortUrl;
} else {
msg += commits.length + ' commits (last: ' + lastCommitMessage + ') ' + shortUrl;
}
say(msg);
});
}
},
issue: function(body, say) {
var user = body.user.name;
var user = body.user.username;
var projectName = body.project.name;
var issue = body.object_attributes;
@ -114,14 +134,21 @@ var handlers = {
if (issue.action === 'update')
return;
// Don't trigger several close event.
if (issue.action === lastIssueActions[issue.iid])
return;
lastIssueActions[issue.iid] = issue.action;
var displayedAction = conjugatePast(issue.action);
shortenURL(url, function(shortUrl) {
var msg = projectName + ': issue #' + issueNumber + ' ("' + issueTitle + '") changed state ("' + issueState + '") ' + shortUrl;
var msg = user + ' ' + displayedAction + ' issue #' + issueNumber + ' ("' + issueTitle + '") on ' + projectName + ' ' + shortUrl;
say(msg);
});
},
merge_request: function(body, say) {
var user = body.user.name;
var user = body.user.username;
var request = body.object_attributes;
@ -140,9 +167,11 @@ var handlers = {
return;
}
var displayedAction = conjugatePast(request.action);
shortenURL(url, function(shortUrl) {
var msg = projectName + ': MR# ' + id + ' (' + from + '->' + to + ': ' + title + ') ' +
' has been ' + state + '; ' + shortUrl;
var msg = user + ' ' + displayedAction + ' MR !' + id + ' (' + from + '->' + to + ': ' + title + ') ' +
' on ' + projectName + '; ' + shortUrl;
say(msg);
});
},
@ -196,7 +225,26 @@ app.post('/', function(req, res) {
res.sendStatus(200);
});
var chanMessageCounters = {};
var mentionCache = {};
function makeCacheKey(isIssue, id, chan) {
return chan + '-' + (isIssue ? 'issue' : 'mr') + id;
}
function fetch_and_say(isIssue, id, from, chan) {
var cacheKey = makeCacheKey(isIssue, id, chan);
// Don't mention if it's been already mentioned in the last
// config.cacheDuration messages.
var mentionCounter = mentionCache[cacheKey];
if (typeof mentionCounter !== 'undefined') {
if (chanMessageCounters[chan] - mentionCounter <= config.cacheDuration) {
return;
}
}
var path, text_prefix;
if (isIssue) {
path = 'issues/';
@ -218,16 +266,48 @@ function fetch_and_say(isIssue, id, from, chan) {
} else {
client.say(to, text_prefix + id + ": " + url);
}
mentionCache[cacheKey] = chanMessageCounters[chan];
}
});
}
var issueRegexp = /(?:\s|^)#(\d+)/g;
var mergeRequestRegexp = /(?:\s|^)!(\d+)/g;
function testIssueRegexp(r) {
function test(input, expected) {
var match = r.exec(input);
var found = 0;
while (match !== null) {
if (match[1] !== expected[found].toString()) {
throw new Error('should have found ' + expected[found]);
}
found++;
match = r.exec(input);
}
if (expected.length !== found) {
throw new Error('missing expected occurrences: ' + expected.length + 'vs expected ' + found);
}
r.lastIndex = 0;
}
test('hello #301 jeej', [301]);
test('#302 lol', [302]);
test('lol#303', []);
test('lol #303', [303]);
test('\t#304', [304]);
test(' #305', [305]);
test('hello#305 #306 jeej#42 #307 #lol # #308', [306, 307, 308]);
};
testIssueRegexp(issueRegexp);
app.listen(config.port, config.hostname, function() {
console.log('gitlab-to-irc running.');
var issueRegexp = /#(\d+)/g;
var mergeRequestRegexp = /!(\d+)/g;
client.on('message', function(from, chan, message) {
chanMessageCounters[chan] = (chanMessageCounters[chan] || 0) + 1;
var matches = null;
while ((matches = issueRegexp.exec(message)) !== null) {
var issueId = matches[1];