diff options
-rw-r--r-- | .gitignore | 3 | ||||
-rw-r--r-- | labbot/__main__.py | 22 | ||||
-rw-r--r-- | labbot/addons/approve-merge.py | 2 | ||||
-rw-r--r-- | labbot/addons/merge-label.py | 5 | ||||
-rw-r--r-- | labbot/bot.py | 16 | ||||
-rw-r--r-- | labbot/config.py | 4 | ||||
-rw-r--r-- | labbot/logger.py | 5 |
7 files changed, 37 insertions, 20 deletions
@@ -2,5 +2,6 @@ env build dist *.egg-info +.mypy_cache __pycache__ -*.pyc +*.pyc
\ No newline at end of file diff --git a/labbot/__main__.py b/labbot/__main__.py index 57ffbcd..551f0f1 100644 --- a/labbot/__main__.py +++ b/labbot/__main__.py @@ -2,6 +2,7 @@ import click import logging from typing import List from importlib import import_module +import json import labbot.bot import labbot.config @@ -19,7 +20,7 @@ def main(): @main.command(help="Create a new instance") @click.option("--name", prompt=True, help="Name the instance will be given") @click.option("--access_token", prompt="Access Token for the Bot account", hide_input=True, help="Access Token to interact with the API") -@click.option("--secret", prompt="Webhook Secret (can be empty)", default="", help="Secret to receive webhook requests (can be empty)") +@click.option("--secret", prompt="Webhook Secret (optional)", default="", help="Secret to receive webhook requests (optional)") def setup(**data): instance_name = data.pop("name", "").replace(" ", "_").lower() data["addons"] = DEFAULT_ADDONS @@ -33,11 +34,15 @@ def setup(**data): @main.command(help="Configure an existing instance") @click.argument('name') -@click.option("--access_token", required=False) -@click.option("--secret", required=False) +@click.option("--access_token", required=False, help="Access Token to interact with the API") +@click.option("--secret", required=False, help="Secret to receive webhook requests (can be empty)") +@click.option("--addons", required=False, help="List of addons to load") +@click.option("--addon_path", required=False, help="Path to load custom addons from") +@click.option("--print", is_flag=True, required=False, help="Print the current config with redacted values") def config(name, **data): data = {k:v for k,v in data.items() if v} + print_config = data.pop("print", False) conf = labbot.config.read_instance_config(name) if conf: @@ -45,8 +50,13 @@ def config(name, **data): conf.update(data) labbot.config.write_instance_config(name, conf) click.echo("configured") - else: - click.echo(f"nothing to change") + elif not print_config: + click.echo("run with `--help` to show usage") + + if print_config: + conf["access_token"] = "************" + conf["secret"] = "******" + click.echo(json.dumps(conf, indent=4)) else: click.echo(f"{name} is not an instance") pass @@ -56,7 +66,7 @@ def config(name, **data): @click.option("--port", default=8080, show_default=True, help="change the webhook port") @click.option("--debug", is_flag=True, default=False, help="enable debug logging") @click.argument('name') -def run(name, port, debug: bool): +def run(name, port: str, debug: bool): conf = labbot.config.read_instance_config(name) if not conf: diff --git a/labbot/addons/approve-merge.py b/labbot/addons/approve-merge.py index 3c0ef30..81ea68d 100644 --- a/labbot/addons/approve-merge.py +++ b/labbot/addons/approve-merge.py @@ -32,5 +32,5 @@ async def merge_label_hook(event, gl, *args, **kwargs): log.debug(f"Cannot merge !{iid} because of its merge_statuc `{merge_status}`") def setup(bot): - bot.register(merge_label_hook, "Merge Request Hook") + bot.register_merge_hook(merge_label_hook) pass
\ No newline at end of file diff --git a/labbot/addons/merge-label.py b/labbot/addons/merge-label.py index 4798d4a..cf7bf82 100644 --- a/labbot/addons/merge-label.py +++ b/labbot/addons/merge-label.py @@ -77,6 +77,5 @@ async def merge_label_hook(event, gl, *args, **kwargs): # unknown state pass -def setup(bot): - bot.register(merge_label_hook, "Merge Request Hook") - pass
\ No newline at end of file +def setup(bot) -> None: + bot.register_merge_hook(merge_label_hook) diff --git a/labbot/bot.py b/labbot/bot.py index 1361656..9b5d70b 100644 --- a/labbot/bot.py +++ b/labbot/bot.py @@ -1,6 +1,6 @@ import os.path import sys -from gidgetlab.aiohttp import GitLabBot +from gidgetlab.aiohttp import GitLabBot # type: ignore from importlib import import_module import logging @@ -12,9 +12,10 @@ log = logging.getLogger(__name__) class Bot: def __init__(self, *args, **kwargs): - self.name = kwargs.get("name", "lab-bot") + self.name = kwargs.pop("name", "lab-bot") + self.access_token = kwargs.get("access_token") self.secret = kwargs.get("secret", "") - self.config = kwargs.get("config", labbot.config.read_instance_config(self.name)) + self.config = kwargs.pop("config", labbot.config.read_instance_config(self.name)) self.addons = self.config.get("addons", []) self.addon_paths = [] @@ -36,16 +37,19 @@ class Bot: sys.path.remove(path) - def load_addon(self, addon: str): + def load_addon(self, addon: str) -> None: try: import_module(f"{addon}").setup(self) log.info(f"Loaded {addon}") except ModuleNotFoundError: log.error(f"No addon named `{addon}`") - def register(self, func, *args, **kwargs): + def register(self, func, *args, **kwargs) -> None: return self.instance.router.register(*args, **kwargs)(func) - def run(self, *args, **kwargs): + def register_merge_hook(self, func, *args, **kwargs) -> None: + return self.instance.router.register("Merge Request Hook", *args, **kwargs)(func) + + def run(self, *args, **kwargs) -> None: log.info(f"Started {self.name}") self.instance.run(*args, **kwargs)
\ No newline at end of file diff --git a/labbot/config.py b/labbot/config.py index 10858ea..f372008 100644 --- a/labbot/config.py +++ b/labbot/config.py @@ -1,6 +1,6 @@ import os import json -from typing import List +from typing import List, Dict, Any from appdirs import user_config_dir # type: ignore CONFIG_FILE = "config.json" @@ -35,7 +35,7 @@ def write_instance_config(name: str, data: dict) -> None: if data != read_instance_config(name): raise ValueError("Config could not be saved properly") -def read_instance_config(name: str) -> dict: +def read_instance_config(name: str) -> Dict[str, Any]: conf_path = os.path.join(instance_config_dir(name), CONFIG_FILE) try: diff --git a/labbot/logger.py b/labbot/logger.py index 067bcac..ebe26c3 100644 --- a/labbot/logger.py +++ b/labbot/logger.py @@ -1,10 +1,13 @@ import sys import logging -def init(level: int): +def init(level: int) -> None: logger = logging.getLogger() logger.setLevel(level) + aiohttp_logger = logging.getLogger("aiohttp") + aiohttp_logger.setLevel(logging.WARNING) + formatter = logging.Formatter( "[{asctime}] [{levelname}] {name}: {message}", datefmt="%Y-%m-%d %H:%M:%S", style="{" ) |