]> kaliko git repositories - sid.git/blob - sid/bts.py
305ef717b50318c750f8a0a1e26bdd18ca77e1df
[sid.git] / sid / bts.py
1 # -*- coding: utf-8 -*-
2 # SPDX-FileCopyrightText: 2015, 2021 kaliko <kaliko@azylum.org>
3 # SPDX-FileCopyrightText: 2010, 2011 Anaël Verrier <elghinn@free.fr>
4 # SPDX-License-Identifier: GPL-3.0-or-later
5
6 from re import compile as re_compile
7
8 import debianbts
9
10 from .plugin import Plugin, botcmd
11
12
13 class Bugs(Plugin):
14     """Gets bugs info from the BTS
15
16     .. note::
17       This plugin depends on external module: **python-debianbts**
18     """
19     #: Bug id regexp, intercepts bug id in strings : "#629234", "bugs.debian.org/629234" and "bugreport.cgi?bug=629234"
20     re_bugs = re_compile(r'(?:(?<=#)|(?<=bugreport\.cgi\?bug=)|(?<=bugs\.debian\.org/))(\d{6,7})')
21     #: Package name regexp
22     re_pkg = re_compile(r'(?P<package>[0-9a-z.+-]+)$')
23
24     def __init__(self, bot):
25         Plugin.__init__(self, bot)
26         bot.add_event_handler("muc::%s::message" %
27                               self.bot.room, self.muc_message)
28
29     def muc_message(self, msg):
30         """Handler method dealing with MUC incoming messages.
31
32         Intercepts bugs number in MUC messages (as #629234), replies a bug
33         summary."""
34         # Does not reply to myself
35         if msg['mucnick'] == self.bot.nick:
36             return
37         bugs = list()
38         for bug_id in set(Bugs.re_bugs.findall(msg['body'].strip())):
39             self.log.debug('got bug id: %s', bug_id)
40             query = debianbts.get_status(bug_id)
41             if len(query) == 1:
42                 bug = query[0]
43                 url = debianbts.BTS_URL + bug_id
44                 bugs.append({'id': bug_id,
45                              'package': bug.package,
46                              'summary': bug.subject,
47                              'url': url})
48             else:
49                 self.log.warning('Wrong bug number "%s"?', bug_id)
50                 bugs.append({'id': bug_id})
51         for bug in bugs:
52             if len(bug) == 1:
53                 message = 'Invalid bug id: {id}'.format(**bug)
54                 self.reply(msg, message)
55             else:
56                 message = {'mhtml': '<a href="%(url)s">#%(id)s</a>: %(package)s “ %(summary)s ”' % bug,
57                            'mbody': '#%(id)s: %(package)s “ %(summary)s ” %(url)s' % bug}
58                 self.reply(msg, message)
59
60     @botcmd
61     def bugs(self, rcv, args):
62         """Gets bugs info from the BTS
63
64         ``!bugs pkg-name`` : Returns latest bug reports if any
65         """
66         if not args:
67             return
68         if len(args) > 1:
69             self.log.info('more than one packages provided')
70         pkg = Bugs.re_pkg.match(args[0])
71         if not pkg:
72             msg = 'Wrong package name format re: "{}"'.format(Bugs.re_pkg.pattern)
73             self.reply(rcv, msg)
74             return
75         reports_ids = debianbts.get_bugs(status='open', **pkg.groupdict())
76         if not reports_ids:
77             self.reply(rcv, 'No open bugs for "{}"'.format(pkg.string))
78             return
79         reports = debianbts.get_status(reports_ids)
80         reports = sorted(reports, key=lambda r: r.date)
81         msg = ['Latest reports for {1} (total {0})'.format(len(reports), pkg.string)]
82         # Reverse and take last reports
83         for rep in reports[::-1][:4]:
84             msg.append('{r.bug_num}: {r.date:%Y-%m-%d} {r.subject}'.format(r=rep))
85         message = {'mbody': '\n'.join(msg)}
86         self.reply(rcv, message)