|
| 1 | +import dataclasses |
| 2 | +from abc import ABC, abstractmethod |
| 3 | +from dataclasses import dataclass |
| 4 | +from typing import Any, ClassVar |
| 5 | + |
| 6 | +from sentry.notifications.models.notificationaction import ActionTarget |
| 7 | +from sentry.utils.registry import Registry |
| 8 | +from sentry.workflow_engine.models.action import Action |
| 9 | + |
| 10 | +# Keep existing excluded keys constant |
| 11 | +EXCLUDED_ACTION_DATA_KEYS = ["uuid", "id"] |
| 12 | + |
| 13 | + |
| 14 | +class BaseActionTranslator(ABC): |
| 15 | + action_type: ClassVar[Action.Type] |
| 16 | + registry_id: ClassVar[str] |
| 17 | + |
| 18 | + def __init__(self, action: dict[str, Any]): |
| 19 | + self.action = action |
| 20 | + |
| 21 | + @property |
| 22 | + @abstractmethod |
| 23 | + def required_fields(self) -> list[str]: |
| 24 | + """Return the required fields for this action""" |
| 25 | + pass |
| 26 | + |
| 27 | + @property |
| 28 | + def missing_fields(self) -> list[str]: |
| 29 | + """Return the missing fields for this action""" |
| 30 | + return [field for field in self.required_fields if self.action.get(field) is None] |
| 31 | + |
| 32 | + @property |
| 33 | + @abstractmethod |
| 34 | + def target_type(self) -> ActionTarget: |
| 35 | + """Return the target type for this action""" |
| 36 | + pass |
| 37 | + |
| 38 | + @property |
| 39 | + @abstractmethod |
| 40 | + def integration_id(self) -> Any | None: |
| 41 | + """Return the integration ID for this action, if any""" |
| 42 | + pass |
| 43 | + |
| 44 | + @property |
| 45 | + @abstractmethod |
| 46 | + def target_identifier(self) -> str | None: |
| 47 | + """Return the target identifier for this action, if any""" |
| 48 | + pass |
| 49 | + |
| 50 | + @property |
| 51 | + @abstractmethod |
| 52 | + def target_display(self) -> str | None: |
| 53 | + """Return the display name for the target, if any""" |
| 54 | + pass |
| 55 | + |
| 56 | + @property |
| 57 | + @abstractmethod |
| 58 | + def blob_type(self) -> type["DataBlob"] | None: |
| 59 | + """Return the blob type for this action, if any""" |
| 60 | + pass |
| 61 | + |
| 62 | + def is_valid(self) -> bool: |
| 63 | + """ |
| 64 | + Validate that all required fields for this action are present. |
| 65 | + Should be overridden by subclasses to add specific validation. |
| 66 | + """ |
| 67 | + return len(self.missing_fields) == 0 |
| 68 | + |
| 69 | + def get_sanitized_data(self) -> dict[str, Any]: |
| 70 | + """ |
| 71 | + Return sanitized data for this action |
| 72 | + If a blob type is specified, convert the action data to a dataclass |
| 73 | + Otherwise, remove excluded keys |
| 74 | + """ |
| 75 | + if self.blob_type: |
| 76 | + # Convert to dataclass if blob type is specified |
| 77 | + blob_instance = self.blob_type( |
| 78 | + **{k.name: self.action.get(k.name, "") for k in dataclasses.fields(self.blob_type)} |
| 79 | + ) |
| 80 | + return dataclasses.asdict(blob_instance) |
| 81 | + else: |
| 82 | + # Remove excluded keys |
| 83 | + return {k: v for k, v in self.action.items() if k not in EXCLUDED_ACTION_DATA_KEYS} |
| 84 | + |
| 85 | + |
| 86 | +issue_alert_action_translator_registry = Registry[type[BaseActionTranslator]]() |
| 87 | + |
| 88 | + |
| 89 | +@issue_alert_action_translator_registry.register( |
| 90 | + "sentry.integrations.slack.notify_action.SlackNotifyServiceAction" |
| 91 | +) |
| 92 | +class SlackActionTranslator(BaseActionTranslator): |
| 93 | + action_type = Action.Type.SLACK |
| 94 | + registry_id = "sentry.integrations.slack.notify_action.SlackNotifyServiceAction" |
| 95 | + |
| 96 | + @property |
| 97 | + def required_fields(self) -> list[str]: |
| 98 | + return ["channel_id", "workspace", "channel"] |
| 99 | + |
| 100 | + @property |
| 101 | + def target_type(self) -> ActionTarget: |
| 102 | + return ActionTarget.SPECIFIC |
| 103 | + |
| 104 | + @property |
| 105 | + def integration_id(self) -> Any | None: |
| 106 | + return self.action.get("workspace") |
| 107 | + |
| 108 | + @property |
| 109 | + def target_identifier(self) -> str | None: |
| 110 | + return self.action.get("channel_id") |
| 111 | + |
| 112 | + @property |
| 113 | + def target_display(self) -> str | None: |
| 114 | + return self.action.get("channel") |
| 115 | + |
| 116 | + @property |
| 117 | + def blob_type(self) -> type["DataBlob"]: |
| 118 | + return SlackDataBlob |
| 119 | + |
| 120 | + |
| 121 | +@dataclass |
| 122 | +class DataBlob: |
| 123 | + """DataBlob is a generic type that represents the data blob for a notification action.""" |
| 124 | + |
| 125 | + pass |
| 126 | + |
| 127 | + |
| 128 | +@dataclass |
| 129 | +class SlackDataBlob(DataBlob): |
| 130 | + """SlackDataBlob is a specific type that represents the data blob for a Slack notification action.""" |
| 131 | + |
| 132 | + tags: str = "" |
| 133 | + notes: str = "" |
0 commit comments