66from 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
2014class 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
3337class Action :
@@ -41,8 +45,8 @@ def to_dict(self) -> Dict[str, Any]:
4145@dataclasses .dataclass
4246class 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
0 commit comments