]> kaliko git repositories - sid.git/blob - sid/bts.py
201df9a0fb5c8bfbb3704d520567f0fa842a78df
[sid.git] / sid / bts.py
1 # -*- coding: utf-8 -*-
2
3 # Copyright (C) 2010, 2011 Anaël Verrier <elghinn@free.fr>
4 # Copyright (C) 2015, 2020 kaliko <kaliko@azylum.org>
5
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, version 3 only.
9
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14
15 # You should have received a copy of the GNU General Public License
16 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18
19 from re import compile as re_compile
20
21 import debianbts
22
23 from .plugin import Plugin, botcmd
24
25
26 class Bugs(Plugin):
27     """Gets bugs info from the BTS
28     """
29     re_bugs = re_compile(r'(?<=#)(\d{6,7})')
30     re_pkg = re_compile(r'(?P<package>[0-9a-z.+-]+)$')
31
32     def __init__(self, bot):
33         Plugin.__init__(self, bot)
34         bot.add_event_handler("muc::%s::message" % self.bot.room, self.muc_message)
35
36     def muc_message(self, msg):
37         """Handler method dealing with MUC incoming messages"""
38         # Does not reply to myself
39         if msg['mucnick'] == self.bot.nick:
40             return
41         if '#' not in msg['body']:
42             return
43         bugs = list()
44         for bug_id in set(Bugs.re_bugs.findall(msg['body'].strip())):
45             self.log.debug('got bug id: %s', bug_id)
46             query = debianbts.get_status(bug_id)
47             if len(query) == 1:
48                 bug = query[0]
49                 url = debianbts.BTS_URL + bug_id
50                 bugs.append({'id': bug_id,
51                              'package': bug.package,
52                              'summary': bug.subject,
53                              'url': url})
54             else:
55                 self.log.warning('Wrong bug number "%s"?', bug_id)
56                 bugs.append({'id': bug_id})
57         for bug in bugs:
58             if len(bug) == 1:
59                 message = 'Invalid bug id: {id}'.format(**bug)
60                 self.reply(msg, message)
61             else:
62                 message = {'mhtml': '<a href="%(url)s">#%(id)s</a>: %(package)s “ %(summary)s ”' % bug,
63                            'mbody': '#%(id)s: %(package)s “ %(summary)s ” %(url)s' % bug}
64                 self.reply(msg, message)
65
66     @botcmd
67     def bugs(self, rcv, args):
68         """ intercepts bugs number in any message, looking for string like #629234 and display bug summary.
69         !bugs pkg-name : Returns latest bug reports if any
70         """
71         if not args:
72             return
73         if len(args) > 1:
74             self.log.info('more than one packages provided')
75         pkg = Bugs.re_pkg.match(args[0])
76         if not pkg:
77             msg = 'Wrong package name format re: "{}"'.format(Bugs.re_pkg.pattern)
78             self.reply(rcv, msg)
79             return
80         reports_ids = debianbts.get_bugs(status='open', **pkg.groupdict())
81         if not reports_ids:
82             self.reply(rcv, 'No open bugs for "{}"'.format(pkg.string))
83             return
84         reports = debianbts.get_status(reports_ids)
85         reports = sorted(reports, key=lambda r: r.date)
86         rprt_nb = len(reports)
87         msg = ['Open reports for {1} (total {0})'.format(rprt_nb, pkg.string)]
88         # Reverse and take last reports
89         for rep in reports[::-1][:4]:
90             msg.append('{r.bug_num}: {r.date:%Y-%m-%d} {r.subject}'.format(r=rep))
91         message = {'mbody': '\n'.join(msg)}
92         self.reply(rcv, message)
93