X-Git-Url: https://git.kaliko.me/?a=blobdiff_plain;f=sid%2Fbts.py;h=2835efff9127701d4d8b4724ad992717bde0f348;hb=HEAD;hp=efe703356e4e9fb9fb63774fee312d9061699fb1;hpb=05082f879f25814b1b9e951b0b9141252f4f0b2d;p=sid.git diff --git a/sid/bts.py b/sid/bts.py index efe7033..fa88244 100644 --- a/sid/bts.py +++ b/sid/bts.py @@ -1,20 +1,11 @@ # -*- coding: utf-8 -*- +# SPDX-FileCopyrightText: 2015, 2021 kaliko +# SPDX-FileCopyrightText: 2010, 2011 Anaël Verrier +# SPDX-License-Identifier: GPL-3.0-or-later +"""Intercepts bugs numbers in MUC messages and send info about it -# Copyright (C) 2010, 2011 Anaël Verrier -# Copyright (C) 2015 kaliko - -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, version 3 only. - -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. - -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . - +>>> from sid.bts import Bugs +""" from re import compile as re_compile @@ -24,21 +15,32 @@ from .plugin import Plugin, botcmd class Bugs(Plugin): - re_bugs = re_compile('(?<=#)(\d{6,7})') + """Gets bugs info from the BTS + + .. note:: + This plugin depends on external module: **python-debianbts** + """ + #: Bug id regexp, intercepts bug id in strings : "#629234", "bugs.debian.org/629234" and "bugreport.cgi?bug=629234" + re_bugs = re_compile(r'(?:(?<=#)|(?<=bugreport\.cgi\?bug=)|(?<=bugs\.debian\.org/))(\d{6,7})') + #: Package name regexp + re_pkg = re_compile(r'(?P[0-9a-z.+-]+)$') def __init__(self, bot): Plugin.__init__(self, bot) - bot.add_event_handler("muc::%s::message" % self.bot.room, self.muc_message) + bot.add_event_handler("muc::%s::message" % + self.bot.room, self.muc_message) def muc_message(self, msg): + """Handler method dealing with MUC incoming messages. + + Intercepts bugs number in MUC messages (as #629234), replies a bug + summary.""" # Does not reply to myself if msg['mucnick'] == self.bot.nick: return - if '#' not in msg['body']: - return bugs = list() for bug_id in set(Bugs.re_bugs.findall(msg['body'].strip())): - self.log.debug('got bug id: %s' % bug_id) + self.log.debug('got bug id: %s', bug_id) query = debianbts.get_status(bug_id) if len(query) == 1: bug = query[0] @@ -55,13 +57,34 @@ class Bugs(Plugin): message = 'Invalid bug id: {id}'.format(**bug) self.reply(msg, message) else: - message = {'mhtml': '#%(id)s: %(package)s « %(summary)s »' % bug, - 'mbody': '#%(id)s: %(package)s « %(summary)s » %(url)s' % bug} + message = {'mhtml': '#%(id)s: %(package)s “ %(summary)s ”' % bug, + 'mbody': '#%(id)s: %(package)s “ %(summary)s ” %(url)s' % bug} self.reply(msg, message) @botcmd - def bugs(self, message, args): - """ intercepts bugs number in any message, looking for string like #629234 - !bugs : nothing yet. + def bugs(self, rcv, args): + """Gets bugs info from the BTS + + ``!bugs pkg-name`` : Returns latest bug reports if any """ - return None + if not args: + return + if len(args) > 1: + self.log.info('more than one packages provided') + pkg = Bugs.re_pkg.match(args[0]) + if not pkg: + msg = 'Wrong package name format re: "{}"'.format(Bugs.re_pkg.pattern) + self.reply(rcv, msg) + return + reports_ids = debianbts.get_bugs(status='open', **pkg.groupdict()) + if not reports_ids: + self.reply(rcv, 'No open bugs for "{}"'.format(pkg.string)) + return + reports = debianbts.get_status(reports_ids) + reports = sorted(reports, key=lambda r: r.date) + msg = ['Latest reports for {1} (total {0})'.format(len(reports), pkg.string)] + # Reverse and take last reports + for rep in reports[::-1][:4]: + msg.append('{r.bug_num}: {r.date:%Y-%m-%d} {r.subject}'.format(r=rep)) + message = {'mbody': '\n'.join(msg)} + self.reply(rcv, message)