Factor out url fetching and saying + make it work;

This commit is contained in:
Benjamin Bouvier 2017-06-28 11:49:04 +02:00
parent 392c2c3577
commit 6f478ff1c1
1 changed files with 27 additions and 22 deletions

View File

@ -213,6 +213,31 @@ app.post('/', function(req, res) {
res.sendStatus(200);
});
function fetch_and_say(isIssue, id, from, chan) {
var path, text_prefix;
if (isIssue) {
path = 'issues/';
text_prefix = 'Issue #';
} else {
path = 'merge_requests/';
text_prefix = 'Merge request !';
}
var to = chan === config.nick ? from : chan;
var url = config.projectUrl + path + id;
request(url, function(err, res, body) {
if (res && res.statusCode === 200) {
var title = cheerio.load(body)('head title').text();
if (title.length) {
client.say(to, title);
} else {
client.say(to, text_prefix + id + ": " + url);
}
}
});
}
app.listen(config.port, config.hostname, function() {
console.log('gitlab-to-irc running.');
@ -222,32 +247,12 @@ app.listen(config.port, config.hostname, function() {
var matches = null;
while ((matches = issueRegexp.exec(message)) !== null) {
var issueId = matches[1];
var url = config.projectUrl + 'issues/' + issueId;
request(url, function(err, res, body) {
if (res && res.statusCode === '200') {
var title = cheerio.load(body)('head title').text();
if (title.length) {
client.say(chan, title);
} else {
client.say(chan, "Issue #" + issueId + ": " + url);
}
}
});
fetch_and_say(true, issueId, from, chan);
}
while ((matches = mergeRequestRegexp.exec(message)) !== null) {
var mrId = matches[1];
var url = config.projectUrl + 'merge_requests/' + mrId;
request(url, function(err, res, body) {
if (res && res.statusCode === '200') {
var title = cheerio.load(body)('head title').text();
if (title.length) {
client.say(chan, title);
} else {
client.say(chan, "Merge request !" + mrId + ": " + url);
}
}
});
fetch_and_say(false, mrId, from, chan);
}
});
});