From 277c8929e946184738d22fc17ade0d24780ea939 Mon Sep 17 00:00:00 2001 From: Rafael Poveda Date: Sat, 4 May 2019 00:08:43 -0400 Subject: [PATCH 1/3] fix: ignore karma tokens inside code blocks --- scripts/karma.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/scripts/karma.js b/scripts/karma.js index 25bdba1e..222bfe7d 100644 --- a/scripts/karma.js +++ b/scripts/karma.js @@ -156,6 +156,14 @@ module.exports = robot => { return tokens.filter(token => urls.reduce((acc, url) => acc && url.indexOf(token) === -1, true)) } + // Sometimes we want to use code that contains ++ or --. This function allow us + // to find the text that is inside code blocks so we can compare and avoid false positives + // in karma assignation. + const findTextInsideCodeBlocks = message => { + const regex = /`(.*?)`/g + return [...message.matchAll(regex)].map(el => el[0]) + } + robot.hear(/([a-zA-Z0-9-_\.]|[^\,\-\s\+$!(){}"'`~%=^:;#°|¡¿?]+?)(\b\+{2}|-{2})([^,]?|\s|$)/g, response => { stripRegex = /~!@#$`%^&*()|\=?;:'",<>\{\}/gi const tokens = removeURLFromTokens(response.match, response.message.text) @@ -164,7 +172,13 @@ module.exports = robot => { if (!robot.adapter.client.rtm.dataStore.getChannelGroupOrDMById(response.envelope.room).is_channel) return } + const codeBlocks = findTextInsideCodeBlocks(response.message.text) + tokens + .filter(token => { + // If the token match with a codeBlock it is remove from the karma assignation + return codeBlocks.filter(codeBlock => codeBlock.includes(token)).length === 0 + }) .slice(0, 5) .map(token => { const opRegex = /(\+{2}|-{2})/g From 5517a0cd18e471395962882988a0e0f79f4ad9fb Mon Sep 17 00:00:00 2001 From: Rafael Poveda Date: Sat, 4 May 2019 09:31:45 -0400 Subject: [PATCH 2/3] refactor: remove matchAll string method for not being supported in node yet --- scripts/karma.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/scripts/karma.js b/scripts/karma.js index 222bfe7d..4da30f8d 100644 --- a/scripts/karma.js +++ b/scripts/karma.js @@ -161,7 +161,12 @@ module.exports = robot => { // in karma assignation. const findTextInsideCodeBlocks = message => { const regex = /`(.*?)`/g - return [...message.matchAll(regex)].map(el => el[0]) + let matches = [] + let match + while ((match = regex.exec(message)) !== null) { + matches.push(match[0]) + } + return matches } robot.hear(/([a-zA-Z0-9-_\.]|[^\,\-\s\+$!(){}"'`~%=^:;#°|¡¿?]+?)(\b\+{2}|-{2})([^,]?|\s|$)/g, response => { From 8cb5bc598ae86fd4e34a7d418215a5464ef78af1 Mon Sep 17 00:00:00 2001 From: Rafael Poveda Date: Sat, 4 May 2019 11:33:24 -0400 Subject: [PATCH 3/3] test: add new test for karma assignation --- test/karma.test.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/test/karma.test.js b/test/karma.test.js index c1e63f46..1ce3c005 100644 --- a/test/karma.test.js +++ b/test/karma.test.js @@ -254,6 +254,16 @@ test.cb.serial('Aplica karma solo si es menos a uno mismo', t => { t.end() }, 500) }) +test.cb.serial('No aplicar karma si se pone los ++ o -- como codigo', t => { + t.context.room.user.say('user', 'ienc-- y `raerpo++`') + setTimeout(() => { + t.deepEqual(t.context.room.messages, [ + ['user', 'ienc-- y `raerpo++`'], + ['hubot', 'i.enc ahora tiene -1 puntos de karma.'] + ]) + t.end() + }, 500) +}) test.cb.serial('No Debe aplicar karma', t => { t.context.room.user.say('user', 'leon++') setTimeout(() => {