Exemple #1
0
"""
Slack Bot List Command
"""

import logging
from ebr_trackerbot.bot import register_command, get_storage


def list_command(text, result, payload, config, commands):
    """
    Slack Bot List Command
    """
    logging.debug("List command")

    tests = ""
    tracks = get_storage()["load_for_user"](payload["data"]["user"])
    for record in tracks:
        tests += "*" + record["test"] + "* (" + record["expiry"] + ")" + "\n"
    payload["web_client"].chat_postMessage(
        channel=payload["data"]["channel"],
        text="Tracking for following tests:\n" + tests,
        thread_ts=payload["data"]["ts"],
    )


register_command("list", "List tracked tests", "^list$", list_command)
logging.info("List command registered")
    for test in response.json()["tests"]:
        full_name = test["full_name"]
        if full_name == target_test:
            count = str(test["count"])
            payload["web_client"].chat_postMessage(
                channel=channel_id,
                text="Test *{test}* failed {count} times over the last {duration}".format(
                    test=target_test, count=count, duration=duration
                ),
                thread_ts=thread_ts,
            )
            break
    else:
        payload["web_client"].chat_postMessage(
            channel=channel_id,
            text="Test *{test}* never failed over {duration}".format(test=target_test, duration=duration),
            thread_ts=thread_ts,
        )


register_command(
    "show",
    "Shows the current status of a test over the past time interval. Command syntax: show full_testname over time_interval."
    + "Time interval can contain *s* - seconds, *m* - minutes, *h* - hours, *d* - days"
    + "(eg. 20d5h10m5s)",
    "^show ([^ ]+) over ((?:[0-9]+(?:s|m|h|d))+)$",
    show_command,
)
logging.info("Show command registered")
def test_register_command():
    """Register command test"""
    bot.register_command("test", "description", "regex", "callback")
    assert "test" in map(lambda x: x["command"], bot.STATE.bot_command)
"""
Slack Bot Untrack Command
"""
import logging
from ebr_trackerbot.bot import register_command, get_storage


def untrack_command(text, result, payload, config, commands):
    """
    Slack Bot Untrack Command
    """
    logging.debug("Untrack command")

    test = result.group(1)
    get_storage()["delete_for_user"](payload["data"]["user"], test)

    payload["web_client"].chat_postMessage(
        channel=payload["data"]["channel"],
        text="Tracking was stopped for test *" + test + "*",
        thread_ts=payload["data"]["ts"],
    )


register_command("untrack",
                 "Stops test tracking. Command syntax: untrack full_testname",
                 "^untrack ([^ ]+)$", untrack_command)
logging.info("Untrack command registered")
Slack Bot Help Command
"""
import logging
from ebr_trackerbot import __version__ as version
from ebr_trackerbot.bot import register_command


def help_command(text, result, payload, config, commands):
    """
    Slack Bot Help Command
    """
    logging.debug("Command help")
    user = payload["data"]["user"]
    supported_commands = ""
    for command in commands:
        supported_commands += "*" + command["command"] + "* " + command[
            "description"] + "\n"
    response = payload["web_client"].chat_postMessage(
        channel=payload["data"]["channel"],
        text="Hi <@" + user +
        ">!\nI'm EBR-Trackerbot v{version}\nSupported commands:\n".format(
            version=version) + supported_commands,
        thread_ts=payload["data"]["ts"],
    )
    if "ok" not in response:
        logging.warning(response)


register_command("help", "Display this help", "^help$", help_command)
logging.info("Help command registered")
    channel_id = payload["data"]["channel"]
    thread_ts = payload["data"]["ts"]
    get_storage()["save"](
        payload["data"]["user"],
        {
            "test": test,
            "expiry": expiry.to_iso8601_string(),
            "channel_id": channel_id,
            "thread_ts": thread_ts
        },
    )

    payload["web_client"].chat_postMessage(
        channel=channel_id,
        text="Tracking started for test *" + test + "* for *" + duration +
        "* (" + expiry.to_iso8601_string() + ")",
        thread_ts=thread_ts,
    )


register_command(
    "track",
    "Starts test tracking. Command syntax: track full_testname for time_interval."
    +
    "Time interval can contain *s* - seconds, *m* - minutes, *h* - hours, *d* - days"
    + "(eg. 20d5h10m5s)",
    "^track ([^ ]+) for ((?:[0-9]+(?:s|m|h|d))+)$",
    track_command,
)
logging.info("Track command registered")