2022-12-01 16:31:04 +00:00
|
|
|
import secrets
|
|
|
|
|
import sys
|
|
|
|
|
from typing import Any
|
|
|
|
|
|
|
|
|
|
import click
|
|
|
|
|
|
2023-11-07 15:14:56 +01:00
|
|
|
from ops_bot.config import RoutingKey, hook_types, load_config, save_config
|
2022-12-01 16:31:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@click.group()
|
|
|
|
|
@click.option(
|
|
|
|
|
"--config-file", help="the path to the config file", default="config.json"
|
|
|
|
|
)
|
|
|
|
|
@click.pass_context
|
|
|
|
|
def cli(ctx: Any, config_file: str) -> None:
|
|
|
|
|
ctx.obj = config_file
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@cli.command(help="Add a new routing key to the configuration file")
|
|
|
|
|
@click.option(
|
|
|
|
|
"--name", help="a friendly detailed name for the hook so you can remember it later"
|
|
|
|
|
)
|
|
|
|
|
@click.option(
|
|
|
|
|
"--hook-type",
|
|
|
|
|
help="The type of webhook to add",
|
|
|
|
|
type=click.Choice(["gitlab", "pagerduty", "aws-sns"], case_sensitive=False),
|
|
|
|
|
)
|
|
|
|
|
@click.option("--room-id", help="The room ID to send the messages to")
|
|
|
|
|
@click.pass_obj
|
2023-11-07 15:14:56 +01:00
|
|
|
def add_hook(config_file: str, name: str, maybe_hook_type: str, room_id: str) -> None:
|
2022-12-01 16:31:04 +00:00
|
|
|
settings = load_config(config_file)
|
|
|
|
|
path_key = secrets.token_urlsafe(30)
|
|
|
|
|
secret_token = secrets.token_urlsafe(30)
|
|
|
|
|
if name in set([key.name for key in settings.routing_keys]):
|
|
|
|
|
print("Error: A hook with that name already exists")
|
|
|
|
|
sys.exit(1)
|
2023-11-07 15:14:56 +01:00
|
|
|
if maybe_hook_type not in hook_types:
|
|
|
|
|
print(f"Invalid hook type {maybe_hook_type}")
|
|
|
|
|
print("Must be one of ", ", ".join(hook_types))
|
2022-12-01 16:31:04 +00:00
|
|
|
settings.routing_keys.append(
|
|
|
|
|
RoutingKey(
|
|
|
|
|
name=name,
|
|
|
|
|
path_key=path_key,
|
|
|
|
|
secret_token=secret_token,
|
|
|
|
|
room_id=room_id,
|
2023-11-07 15:14:56 +01:00
|
|
|
hook_type=maybe_hook_type, # type: ignore[arg-type]
|
2022-12-01 16:31:04 +00:00
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
save_config(settings)
|
|
|
|
|
|
|
|
|
|
url = f"/hook/{path_key}"
|
|
|
|
|
|
|
|
|
|
print("Hook added successfully")
|
|
|
|
|
print()
|
|
|
|
|
print("Your webhook URL is:")
|
|
|
|
|
print(f"\t{url}")
|
|
|
|
|
print("The secret token is:")
|
|
|
|
|
print(f"\t{secret_token}")
|