diff --git a/index.js b/index.js index 31f1dd8..e5cd86d 100644 --- a/index.js +++ b/index.js @@ -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); } }); });