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.
This commit is contained in:
Dunedan 2024-09-21 10:40:50 +02:00
parent aeaa93d82e
commit 0d22b8e05b
Signed by: Dunedan
GPG Key ID: 885B16854284E0B2
2 changed files with 50 additions and 19 deletions

View File

@ -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"<title>(.*)</title>", 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"<title>(.*)</title>", 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

34
test.py
View File

@ -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())