Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions python/lib/sift_py/rule/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,11 @@ def as_json(self) -> Any:

if self.action.tags is not None and len(self.action.tags) > 0:
hash_map["tags"] = self.action.tags
elif isinstance(self.action, RuleActionCreateNotification):
hash_map["type"] = RuleActionKindStrRep.NOTIFICATION.value

if self.action.recipients is not None and len(self.action.recipients) > 0:
hash_map["recipients"] = self.action.recipients
else:
kind = self.action.kind() if self.action else self.action
raise TypeError(f"Unsupported rule action '{kind}'.")
Expand Down Expand Up @@ -177,6 +182,24 @@ def kind(self) -> RuleActionKind:
return RuleActionKind.ANNOTATION


class RuleActionCreateNotification(RuleAction):
"""
Action to send a notification to one or more users when a rule evaluates to a truthy value.

- `recipients`: List of emails of users in the organization to notify.
"""

recipients: List[str]
tags: Optional[List[str]]

def __init__(self, recipients: List[str]):
self.recipients = recipients
self.tags = None

def kind(self) -> RuleActionKind:
return RuleActionKind.NOTIFICATION


class RuleActionKind(Enum):
NOTIFICATION = ActionKind.NOTIFICATION
ANNOTATION = ActionKind.ANNOTATION
Expand Down
29 changes: 26 additions & 3 deletions python/lib/sift_py/rule/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from sift.common.type.v1.user_pb2 import User as UserPb
from sift.rules.v1.rules_pb2 import (
ANNOTATION,
NOTIFICATION,
AnnotationActionConfiguration,
BatchUpdateRulesRequest,
BatchUpdateRulesResponse,
Expand All @@ -26,6 +27,7 @@
CreateRuleRequest,
GetRuleRequest,
GetRuleResponse,
NotificationActionConfiguration,
Rule,
RuleActionConfiguration,
RuleAssetConfiguration,
Expand Down Expand Up @@ -53,6 +55,7 @@
RuleAction,
RuleActionAnnotationKind,
RuleActionCreateDataReviewAnnotation,
RuleActionCreateNotification,
RuleActionCreatePhaseAnnotation,
RuleActionKind,
RuleConfig,
Expand Down Expand Up @@ -425,10 +428,30 @@ def _update_req_from_rule_config(

actions = []
if config.action.kind() == RuleActionKind.NOTIFICATION:
raise NotImplementedError(
"Notification actions are not yet supported."
"Please contact the Sift team for assistance."
if not isinstance(config.action, RuleActionCreateNotification):
raise TypeError(
f"Expected RuleActionCreateNotification for notification action, "
f"got {type(config.action).__name__}."
)

recipient_user_ids: List[str] = []
for email in config.action.recipients:
users = self._get_active_users(filter=f"name=='{email}'")
if not users:
raise ValueError(f"Cannot find user '{email}'.")
if len(users) > 1:
raise ValueError(f"Multiple users found with name '{email}'.")
recipient_user_ids.append(users[0].user_id)

action_config = UpdateActionRequest(
action_type=NOTIFICATION,
configuration=RuleActionConfiguration(
notification=NotificationActionConfiguration(
recipient_user_ids=recipient_user_ids,
)
),
)
actions.append(action_config)
elif config.action.kind() == RuleActionKind.ANNOTATION:
annotation_tag_ids = (
[tag.tag_id for tag in annotation_tags] if annotation_tags else None
Expand Down
Loading