Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ignore karma tokens inside code blocks #547

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions scripts/karma.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,19 @@ 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
let matches = []
let match
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cual es el beneficio de este let?

while ((match = regex.exec(message)) !== null) {
matches.push(match[0])
}
Comment on lines +166 to +168
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no sería mejor usar un mecanismo tipo optional para esto?

Encuentro que darle con el while es como darse muchas vueltas

return matches
}

robot.hear(/([a-zA-Z0-9-_\.]|[^\,\-\s\+$!(){}"'`~%=^:;#°|¡¿?]+?)(\b\+{2}|-{2})([^,]?|\s|$)/g, response => {
stripRegex = /~!@#$`%^&*()|\=?;:'",<>\{\}/gi
const tokens = removeURLFromTokens(response.match, response.message.text)
Expand All @@ -164,7 +177,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
Expand Down
10 changes: 10 additions & 0 deletions test/karma.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand Down