Skip to content

Commit 85a7429

Browse files
committed
specify action
1 parent 39c3513 commit 85a7429

3 files changed

Lines changed: 50 additions & 7 deletions

File tree

taskbadger/cli.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import subprocess
2-
from typing import Optional
2+
from typing import Optional, List, Tuple
33

44
import typer
55
from rich import print
66

77
import taskbadger as tb
8+
from taskbadger import Action
9+
from taskbadger import integrations
810
from taskbadger.config import get_config, write_config
911
from taskbadger.exceptions import ConfigurationError
1012

@@ -21,10 +23,21 @@ def _configure_api(ctx):
2123

2224

2325
@app.command(context_settings={"allow_extra_args": True, "ignore_unknown_options": True})
24-
def monitor(ctx: typer.Context, name: str):
26+
def monitor(
27+
ctx: typer.Context,
28+
name: str,
29+
action_def: Tuple[str, integrations.Integrations, str] = typer.Option(
30+
(None, None, None), "--action", "-a", metavar="<trigger integration config>", show_default=False,
31+
help="Action definition e.g. \"success,error email to:me@email.com\""
32+
)
33+
):
2534
"""Monitor a command"""
2635
_configure_api(ctx)
27-
task = tb.Task.create(name)
36+
action = None
37+
if all(action_def):
38+
trigger, integration, config = action_def
39+
action = Action(trigger, integrations.from_config(integration, config))
40+
task = tb.Task.create(name, actions=[action] if action else None)
2841
try:
2942
result = subprocess.run(ctx.args, env={"TASKBADGER_TASK_ID": task.id}, shell=True)
3043
except Exception as e:

taskbadger/integrations.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,21 @@
11
import dataclasses
2-
from typing import Any, Dict
2+
from enum import Enum
3+
from typing import Any, Dict, List, Generator, Tuple
34

45
from taskbadger.internal.models import ActionRequest, ActionRequestConfig
56

67

8+
class Integrations(str, Enum):
9+
email = "email"
10+
11+
12+
def from_config(integration: Integrations, config: str):
13+
if integration == Integrations.email:
14+
split_ = [tuple(item.split(':', 1)) for item in config.split(',')]
15+
kwargs = dict(split_)
16+
return EmailIntegration(**kwargs)
17+
18+
719
class Integration:
820
name: str
921

tests/test_monitor.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,19 @@ def test_monitor_error():
3030
_test_monitor(["not-a-command"], 127)
3131

3232

33-
def _test_monitor(command, return_code):
33+
@mock.patch.dict(os.environ, {
34+
"TASKBADGER_ORG": "org",
35+
"TASKBADGER_PROJECT": "project",
36+
"TASKBADGER_TOKEN": "token",
37+
}, clear=True)
38+
def test_monitor_action():
39+
_test_monitor(
40+
["echo", "test"], 0, ["-a", "success,error", "email", "to:me@test.com"],
41+
action={'trigger': 'success,error', 'integration': 'email', 'config': {'to': 'me@test.com'}}
42+
)
43+
44+
45+
def _test_monitor(command, return_code, args=None, action=None):
3446
with (
3547
mock.patch("taskbadger.sdk.task_create.sync_detailed") as create,
3648
mock.patch("taskbadger.sdk.task_partial_update.sync_detailed") as update
@@ -39,15 +51,21 @@ def _test_monitor(command, return_code):
3951
create.return_value = Response(HTTPStatus.OK, b"", {}, task)
4052

4153
update.return_value = Response(HTTPStatus.OK, b"", {}, task)
42-
result = runner.invoke(app, ["monitor", "task_name", "--"] + command)
54+
args = args or []
55+
result = runner.invoke(app, ["monitor", "task_name"] + args + ["--"] + command)
4356
assert result.exit_code == return_code
4457

4558
settings = _get_settings()
59+
request = TaskRequest(name="task_name", status=StatusEnum.PENDING)
60+
if action:
61+
request.additional_properties = {
62+
'actions': [action]
63+
}
4664
create.assert_called_with(
4765
client=settings.client,
4866
organization_slug="org",
4967
project_slug="project",
50-
json_body=TaskRequest(name="task_name", status=StatusEnum.PENDING)
68+
json_body=request
5169
)
5270

5371
if return_code == 0:

0 commit comments

Comments
 (0)