Don't match ticket ids embedded in words

Only match ticket ids which are a separate word or are prepended by a
special character to avoid false-positives matches.
This commit is contained in:
Dunedan 2024-09-09 06:39:39 +02:00
parent 3d38561875
commit 925e95e847
Signed by untrusted user: Dunedan
GPG Key ID: 885B16854284E0B2
2 changed files with 31 additions and 2 deletions

View File

@ -57,6 +57,8 @@ def __init__(self, irc):
self.__parent = super(wfg, self)
self.__parent.__init__(irc)
self.ticket_pattern = re.compile(r"(?:^|\W)#(\d+)")
def doPrivmsg(self, irc, msg):
channel = msg.args[0]
payload = msg.args[1]
@ -65,7 +67,7 @@ def doPrivmsg(self, irc, msg):
if channels and channel not in channels:
return
matchobj = re.search(r'#(\d+)', payload)
matchobj = self.ticket_pattern.search(payload)
if matchobj is None:
return

29
test.py
View File

@ -96,4 +96,31 @@ def testPullRequestUrl(self):
count += 1
self.assertEqual(count, 1)
# vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79:
def test_dont_match_in_word(self):
obj = self.irc.getCallback('wfg')
obj.doPrivmsg(
self.irc,
ircmsgs.privmsg(self.config["supybot.plugins.wfg.channels"][0],
"https://example.tld/foo#1234")
)
self.assertIsNone(self.irc.takeMsg())
def test_ticket_id_at_the_beginning(self):
obj = self.irc.getCallback('wfg')
obj.doPrivmsg(
self.irc,
ircmsgs.privmsg(self.config["supybot.plugins.wfg.channels"][0],
"#1234 should be fine")
)
msg = self.irc.takeMsg()
self.assertIsInstance(msg, ircmsgs.IrcMsg)
self.assertEqual(msg.nick, "")
self.assertEqual(msg.args[0], self.config["supybot.plugins.wfg.channels"][0])
self.assertEqual(msg.args[1],
"#1234 - [PATCH] Read formations from json files – "
"https://gitea.wildfiregames.com/0ad/0ad/issues/1234")
self.assertIsNone(self.irc.takeMsg())