diff options
Diffstat (limited to 'labbot')
-rw-r--r-- | labbot/bot.py | 9 | ||||
-rw-r--r-- | labbot/commands.py | 58 |
2 files changed, 66 insertions, 1 deletions
diff --git a/labbot/bot.py b/labbot/bot.py index 72d01ca..f5371f6 100644 --- a/labbot/bot.py +++ b/labbot/bot.py @@ -6,6 +6,7 @@ import logging import labbot import labbot.config +import labbot.commands log = logging.getLogger(__name__) @@ -26,7 +27,7 @@ class Bot: if "addon_path" in self.config: self.addon_paths.append(self.config.get("addon_path")) - self.instance = GitLabBot(self.name, **kwargs) + self.instance = GitLabBot(self.name, access_token=self.access_token, secret=self.secret, **kwargs) for path in self.addon_paths: sys.path.insert(0, path) @@ -37,9 +38,12 @@ class Bot: for path in self.addon_paths: sys.path.remove(path) + labbot.commands.commands.setup_hook(self) + def load_addon(self, addon: str) -> None: try: + module = import_module(f"{addon}") import_module(f"{addon}").setup(self) log.info(f"Loaded {addon}") self.addons.append(addon) @@ -58,6 +62,9 @@ class Bot: def register_issue_hook(self, func, *args, **kwargs) -> None: self.register(func, "Issue Hook", *args, **kwargs) + def register_comment_hook(self, func, *args, **kwargs) -> None: + self.register(func, "Issue Hook", *args, **kwargs) + def register_note_hook(self, func, *args, **kwargs) -> None: self.register(func, "Note Hook", *args, **kwargs) diff --git a/labbot/commands.py b/labbot/commands.py new file mode 100644 index 0000000..10b6325 --- /dev/null +++ b/labbot/commands.py @@ -0,0 +1,58 @@ +""" +Command Framework for Lab-Bot +Allows the creation of functions that run when specific comments are made +e.g. +!close -> closes issue +""" + +import logging + +log = logging.getLogger(__name__) + +class Commands: + + def __init__(self, **kwargs): + self.prefix = kwargs.get("prefix", "!") + self.hooks = {} + + def command(self, *args, name=None, aliases=None): + def decorator(func): + nonlocal name + nonlocal aliases + + if name is None: + name = func.__name__ + + if aliases is None: + aliases = [] + + aliases.append(name) + + for alias in aliases: + self.hooks[alias] = func + + return decorator + + async def process_note(self, event, gl, *args, **kwargs): + note = event.object_attributes.get("note", "").strip() + if not note.startswith(self.prefix): + return + + note = note[len(self.prefix):] + + note = note.split() + command = note[0] + arguments = note[1:] + + hook = None + try: + hook = self.hooks[command] + except KeyError: + log.warn(f"Attempted to invoke nonexistant command `{command}`") + + if hook: + await hook(event, gl, *arguments) + def setup_hook(self, bot): + bot.register_note_hook(self.process_note) + +commands = Commands() |