From 49d95432d29abae4c65330f6b56fa65f244651e4 Mon Sep 17 00:00:00 2001 From: Michael Scherer Date: Sep 04 2020 12:30:05 +0000 Subject: Remove fenced code block when checking mention Fix bug #4978 We have to remove code using the '~~~~' markup since this result in incorrect trigger, especially on private ticket. --- diff --git a/pagure/lib/notify.py b/pagure/lib/notify.py index 0c124db..02c277c 100644 --- a/pagure/lib/notify.py +++ b/pagure/lib/notify.py @@ -35,6 +35,7 @@ import pagure.lib.query import pagure.lib.tasks_services from pagure.config import config as pagure_config from pagure.pfmarkdown import MENTION_RE +from markdown.extensions.fenced_code import FencedBlockPreprocessor _log = logging.getLogger(__name__) @@ -234,7 +235,10 @@ def _add_mentioned_users(emails, comment): """ Check the comment to see if an user is mentioned in it and if so add this user to the list of people to notify. """ - for username in re.findall(MENTION_RE, comment): + filtered_comment = re.sub( + FencedBlockPreprocessor.FENCED_BLOCK_RE, "", comment + ) + for username in re.findall(MENTION_RE, filtered_comment): user = pagure.lib.query.search_user(flask.g.session, username=username) if user: emails.add(user.default_email) diff --git a/tests/test_pagure_lib_notify.py b/tests/test_pagure_lib_notify.py index 8d31cb7..78af57a 100644 --- a/tests/test_pagure_lib_notify.py +++ b/tests/test_pagure_lib_notify.py @@ -25,6 +25,7 @@ import pagure.lib.model import pagure.lib.notify import pagure.lib.query import tests +import munch class PagureLibNotifytests(tests.Modeltests): @@ -543,6 +544,31 @@ RW1haWwgY29udGVudA== """ self.assertEqual(email.as_string(), exp) + def test_notification_mention(self): + g = munch.Munch() + g.session = self.session + with patch("flask.g", g): + + def _check_mention(comment, exp): + emails = set([]) + emails = pagure.lib.notify._add_mentioned_users( + emails, comment + ) + + self.assertEqual(emails, exp) + + exp = set(["bar@pingou.com"]) + comment = "I think we should ask @pingou how to pronounce pagure" + _check_mention(comment, exp) + + exp = set([]) + comment = """Let me quote him: +~~~~ + @pingou> Pagure is pronounced 'pa-gure', not 'pagu-re' +~~~~ +""" + _check_mention(comment, exp) + if __name__ == "__main__": unittest.main(verbosity=2)