Created
May 29, 2015 20:59
-
-
Save freyes/e08a0c38456f503c953a to your computer and use it in GitHub Desktop.
willie bot module to display launchpad bugs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| lp.py - Willie Launchpad Module | |
| Copyright (C) 2015 Felipe Reyes <felipe.reyes@canonical.com> | |
| 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, either version 3 of the License, or | |
| (at your option) any later version. | |
| 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 <http://www.gnu.org/licenses/>. | |
| """ | |
| import os | |
| import re | |
| import sys | |
| from willie.module import rule, commands, example | |
| from willie.logger import get_logger | |
| from launchpadlib.launchpad import Launchpad | |
| LOGGER = get_logger(__name__) | |
| launchpad = None | |
| BUG = u"\U0001F41E" # u"\U0001F41B" <- another bug symbol | |
| FLAME = u"\U0001F525" | |
| # http://www.mirc.com/colors.html | |
| # yeah yeah, I should be using constants, .... but I won't :D | |
| RED = 4 | |
| COLOR_IMPORTANCE = {'Undecided': 14, | |
| 'Critical': RED, | |
| 'High': 7, | |
| 'Medium': 3, | |
| 'Low': None, | |
| 'Whishlist': 12} | |
| COLOR_STATUS = {'New': 5, | |
| 'Incomplete': RED, | |
| 'Opinion': 2, | |
| 'Invalid': 14, | |
| "Won't Fix": 14, | |
| "Confirmed": RED, | |
| "Triaged": 7, | |
| "In Progress": None, | |
| "Fix Committed": 3, | |
| "Fix Released": 9} | |
| HEAT_REQ_TO_DISPLAY = 20 | |
| def configure(config): | |
| if not config.option("Configure launchpad bugs to be displayed", False): | |
| return | |
| config.add_section('launchpad') | |
| config.interactive_add('launchpad', 'use_oauth', | |
| 'Set to 1 to use oauth credentials', 'false') | |
| def setup(bot): | |
| global launchpad | |
| if (hasattr(bot.config, 'launchpad') | |
| and bot.config.launchpad.use_oauth == '1'): | |
| launchpad = Launchpad.login_with('willie-lp', 'production', | |
| credential_save_failed=no_credential) | |
| else: | |
| launchpad = Launchpad.login_anonymously('just testing', 'production', | |
| os.path.expanduser('~/.cache')) | |
| def no_credential(): | |
| raise Exception("Can't proceed without Launchpad credential.") | |
| def broadcast_bug(bot, bug_id): | |
| bug = launchpad.bugs[bug_id] | |
| if bug.information_type != 'Public': | |
| information_type = "(%s) " % bug.information_type | |
| else: | |
| information_type = "" | |
| sev = bug.bug_tasks[0].importance | |
| fg_sev = COLOR_IMPORTANCE.get(sev) | |
| status = bug.bug_tasks[0].status | |
| fg_status = COLOR_STATUS.get(status) | |
| msg = u"%s %s%s [%s,%s] %s" % ( | |
| BUG, information_type, bug.title, colour_text(sev, fg_sev), | |
| colour_text(status, fg_status), colour_text(bug.web_link, 14)) | |
| if bug.heat >= HEAT_REQ_TO_DISPLAY: | |
| msg += colour_text(u"%s %s" % (FLAME, bug.heat), RED) | |
| bot.say(msg) | |
| def silent_errors(func): | |
| def func_wrapper(*args, **kwargs): | |
| try: | |
| return func(*args, **kwargs) | |
| except Exception as ex: | |
| LOGGER.debug(str(ex)) | |
| LOGGER.error("Unexpected error: %s" % | |
| sys.exc_info()[0]) | |
| return func_wrapper | |
| @rule('.*(bug|\#|LP|lp)\s*?(\d+).*') | |
| @silent_errors | |
| def lp_keyword(bot, trigger): | |
| broadcast_bug(bot, int(trigger.group(2))) | |
| @rule(".*(https:\/\/(?:bugs\.)?launchpad\.net\/.*\/)(\d+).*") | |
| @silent_errors | |
| def lp_url(bot, trigger): | |
| broadcast_bug(bot, int(trigger.group(2))) | |
| @commands('lp') | |
| @example('.lp 1234') | |
| @silent_errors | |
| def lp_bug(bot, trigger, resource_id=None): | |
| i = 10 # max 10 bugs per request | |
| for s in trigger.group(2).split(' '): | |
| m = re.search(r'\d+', s) | |
| if not m: | |
| continue | |
| bug_id = int(m.group(0)) | |
| broadcast_bug(bot, bug_id) | |
| i -= 1 | |
| if i <= 0: | |
| return | |
| ########## | |
| # grabbed from willie/modules.rss.py | |
| # https://github.com/embolalia/willie/blob/master/willie/modules/rss.py | |
| def colour_text(text, fg=None, bg=None): | |
| """Given some text and fore/back colours, return a coloured text string.""" | |
| if fg is None: | |
| return text | |
| else: | |
| colour = u'{0},{1}'.format(fg, bg) if bg is not None else fg | |
| return u"\x03{0}{1}\x03".format(colour, text) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment