From 8efdd4544a679c780c4b8cd5c439e54c09b91d86 Mon Sep 17 00:00:00 2001 From: Benjamin Bouvier Date: Wed, 28 Jun 2017 12:50:07 +0200 Subject: [PATCH] Improve issue/merge_request regexps; --- index.js | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index a2c3c4a..c2117e0 100644 --- a/index.js +++ b/index.js @@ -222,11 +222,40 @@ function fetch_and_say(isIssue, id, from, 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) { var matches = null; while ((matches = issueRegexp.exec(message)) !== null) {