From 0d22b8e05b88b902a889cab880a4dd2bb2b3e19c Mon Sep 17 00:00:00 2001 From: Dunedan Date: Sat, 21 Sep 2024 10:40:50 +0200 Subject: [PATCH] Print links for all mentioned tickets Up to now this plugin only printed the link for the first encountered ticket id. With this change links for all tickets are printed instead. --- plugin.py | 35 ++++++++++++++++------------------- test.py | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 19 deletions(-) diff --git a/plugin.py b/plugin.py index ee4fb32..b6314e7 100755 --- a/plugin.py +++ b/plugin.py @@ -49,28 +49,25 @@ class wfg(callbacks.Plugin): if channels and channel not in channels: return - matchobj = self.ticket_pattern.search(payload) - if matchobj is None: - return - - ticket_number = matchobj.group(1) ticket_url = self.registryValue('ticketUrl') - url = f'{ticket_url.rstrip("/")}/{ticket_number}' - self.log.info(f'Fetching title for {url}') - # Fetch the title of the ticket page - try: - response = urllib.request.urlopen(url) - html = response.read().decode() - except Exception: - return + for ticket_number in sorted(set(self.ticket_pattern.findall(payload))): + url = f'{ticket_url.rstrip("/")}/{ticket_number}' - title_match = re.search(r"(.*)", html) - title = title_match[1].strip() if title_match else "No title found" - # Clean up the title if necessary - title = self.title_pattern.sub('', title) + self.log.info(f'Fetching title for {url}') + # Fetch the title of the ticket page + try: + response = urllib.request.urlopen(url) + html = response.read().decode() + except Exception: + return - response = f'{title} – {response.url}' - irc.queueMsg(ircmsgs.privmsg(channel, response)) + title_match = re.search(r"(.*)", html) + title = title_match[1].strip() if title_match else "No title found" + # Clean up the title if necessary + title = self.title_pattern.sub('', title) + + response = f'{title} – {response.url}' + irc.queueMsg(ircmsgs.privmsg(channel, response)) Class = wfg diff --git a/test.py b/test.py index 1342738..eef9714 100755 --- a/test.py +++ b/test.py @@ -124,3 +124,37 @@ class WFGTestCase(PluginTestCase): "https://gitea.wildfiregames.com/0ad/0ad/issues/1234") self.assertIsNone(self.irc.takeMsg()) + + def test_match_multiple_ticket_ids(self): + obj = self.irc.getCallback('wfg') + + obj.doPrivmsg( + self.irc, + ircmsgs.privmsg(self.config["supybot.plugins.wfg.channels"][0], + "References for this are #1234, #1234,#1235 and #1236") + ) + 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") + + 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], + "#1235 - selection box moves when moving the camera – " + "https://gitea.wildfiregames.com/0ad/0ad/issues/1235") + + 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], + "#1236 - WP Theming - Fine Footer – " + "https://gitea.wildfiregames.com/0ad/0ad/issues/1236") + + self.assertIsNone(self.irc.takeMsg())