]> kaliko git repositories - sid.git/blob - sid/bts.py
Bump version
[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     .. note::
30       This plugin depends on external module: **python-debianbts**
31     """
32     re_bugs = re_compile(r'(?<=#)(\d{6,7})')
33     re_pkg = re_compile(r'(?P<package>[0-9a-z.+-]+)$')
34
35     def __init__(self, bot):
36         Plugin.__init__(self, bot)
37         bot.add_event_handler("muc::%s::message" %
38                               self.bot.room, self.muc_message)
39
40     def muc_message(self, msg):
41         """Handler method dealing with MUC incoming messages.
42
43         Intercepts bugs number in MUC messages (as #629234), replies a bug
44         summary."""
45         # Does not reply to myself
46         if msg['mucnick'] == self.bot.nick:
47             return
48         if '#' not in msg['body']:
49             return
50         bugs = list()
51         for bug_id in set(Bugs.re_bugs.findall(msg['body'].strip())):
52             self.log.debug('got bug id: %s', bug_id)
53             query = debianbts.get_status(bug_id)
54             if len(query) == 1:
55                 bug = query[0]
56                 url = debianbts.BTS_URL + bug_id
57                 bugs.append({'id': bug_id,
58                              'package': bug.package,
59                              'summary': bug.subject,
60                              'url': url})
61             else:
62                 self.log.warning('Wrong bug number "%s"?', bug_id)
63                 bugs.append({'id': bug_id})
64         for bug in bugs:
65             if len(bug) == 1:
66                 message = 'Invalid bug id: {id}'.format(**bug)
67                 self.reply(msg, message)
68             else:
69                 message = {'mhtml': '<a href="%(url)s">#%(id)s</a>: %(package)s “ %(summary)s ”' % bug,
70                            'mbody': '#%(id)s: %(package)s “ %(summary)s ” %(url)s' % bug}
71                 self.reply(msg, message)
72
73     @botcmd
74     def bugs(self, rcv, args):
75         """Gets bugs info from the BTS
76
77         ``!bugs pkg-name`` : Returns latest bug reports if any
78         """
79         if not args:
80             return
81         if len(args) > 1:
82             self.log.info('more than one packages provided')
83         pkg = Bugs.re_pkg.match(args[0])
84         if not pkg:
85             msg = 'Wrong package name format re: "{}"'.format(Bugs.re_pkg.pattern)
86             self.reply(rcv, msg)
87             return
88         reports_ids = debianbts.get_bugs(status='open', **pkg.groupdict())
89         if not reports_ids:
90             self.reply(rcv, 'No open bugs for "{}"'.format(pkg.string))
91             return
92         reports = debianbts.get_status(reports_ids)
93         reports = sorted(reports, key=lambda r: r.date)
94         msg = ['Latest reports for {1} (total {0})'.format(len(reports), pkg.string)]
95         # Reverse and take last reports
96         for rep in reports[::-1][:4]:
97             msg.append('{r.bug_num}: {r.date:%Y-%m-%d} {r.subject}'.format(r=rep))
98         message = {'mbody': '\n'.join(msg)}
99         self.reply(rcv, message)