Skip to content

Commit c4571a0

Browse files
committed
webhook actions via cli
1 parent c3327e1 commit c4571a0

File tree

3 files changed

+43
-12
lines changed

3 files changed

+43
-12
lines changed

taskbadger/cli.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def run(
4141
name: str,
4242
monitor_id: str = typer.Option(None, help="Associate this task with a monitor."),
4343
update_frequency: int = typer.Option(5, metavar="SECONDS", min=5, max=300, help="Seconds between updates."),
44-
action_def: Tuple[str, integrations.Integrations, str] = typer.Option(
44+
action_def: Tuple[str, str, str] = typer.Option(
4545
(None, None, None),
4646
"--action",
4747
"-a",
@@ -63,7 +63,7 @@ def run(
6363
"""
6464
_configure_api(ctx)
6565
action = None
66-
if all(action_def):
66+
if any(action_def):
6767
trigger, integration, config = action_def
6868
action = Action(trigger, integrations.from_config(integration, config))
6969
stale_timeout = update_frequency * 2

taskbadger/integrations.py

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,9 @@
66
from taskbadger.internal.models import ActionRequest, ActionRequestConfig
77

88

9-
class Integrations(str, Enum):
10-
email = "email"
11-
12-
13-
def from_config(integration: Integrations, config: str):
14-
if integration == Integrations.email:
15-
split_ = [tuple(item.split(":", 1)) for item in config.split(",")]
16-
kwargs = dict(split_)
17-
return EmailIntegration(**kwargs)
9+
def from_config(integration_id: str, config: str):
10+
cls = integration_from_id(integration_id)
11+
return cls.from_config_string(integration_id, config)
1812

1913

2014
class Integration:
@@ -28,6 +22,16 @@ def __post_init__(self):
2822
def request_config(self):
2923
raise NotImplementedError
3024

25+
@classmethod
26+
def from_config_string(cls, integration_id, config):
27+
kwargs = {"id": integration_id}
28+
if config:
29+
# convert config string to dict
30+
31+
split_ = [tuple(item.split(":", 1)) for item in config.split(",")]
32+
kwargs.update(dict(split_))
33+
return cls(**kwargs)
34+
3135

3236
@dataclasses.dataclass
3337
class Action:
@@ -41,8 +45,8 @@ def to_dict(self) -> Dict[str, Any]:
4145
@dataclasses.dataclass
4246
class EmailIntegration(Integration):
4347
type = "email"
44-
id = "email"
4548
to: str # custom type
49+
id: str = "email"
4650

4751
def request_config(self) -> ActionRequestConfig:
4852
return ActionRequestConfig.from_dict({"to": self.to})
@@ -55,3 +59,21 @@ class WebhookIntegration(Integration):
5559

5660
def request_config(self) -> ActionRequestConfig:
5761
return ActionRequestConfig.from_dict({})
62+
63+
64+
ALL = [EmailIntegration, WebhookIntegration]
65+
BY_TYPE = {cls.type: cls for cls in ALL}
66+
67+
68+
def integration_from_id(integration_id):
69+
type_, _ = get_type_id(integration_id)
70+
try:
71+
return BY_TYPE[type_]
72+
except KeyError:
73+
raise TaskbadgerException(f"Unknown integration type: '{type_}'")
74+
75+
76+
def get_type_id(integration_id: str):
77+
if ":" in integration_id:
78+
return integration_id.split(":", 1)
79+
return integration_id, None

tests/test_cli_run.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,15 @@ def test_cli_run():
5353
)
5454

5555

56+
def test_cli_run_webhook():
57+
_test_cli_run(
58+
["echo test"],
59+
0,
60+
["task_name", "-a", "cancelled", "webhook:123", ""],
61+
action={"trigger": "cancelled", "integration": "webhook:123", "config": {}},
62+
)
63+
64+
5665
def _test_cli_run(command, return_code, args=None, action=None, update_call_count=1):
5766
with (
5867
mock.patch("taskbadger.sdk.task_create.sync_detailed") as create,

0 commit comments

Comments
 (0)