-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
ref(integration): Break apart commonality #66623
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,15 +5,21 @@ | |
|
|
||
| from sentry.incidents.models.alert_rule import AlertRuleTriggerAction | ||
| from sentry.incidents.models.incident import Incident | ||
| from sentry.integrations.repository.base import BaseNewNotificationMessage, BaseNotificationMessage | ||
| from sentry.integrations.repository.base import ( | ||
| BaseNewNotificationMessage, | ||
| BaseNotificationMessage, | ||
| NotificationMessageValidationError, | ||
| ) | ||
| from sentry.models.notificationmessage import NotificationMessage | ||
|
|
||
| _default_logger: Logger = getLogger(__name__) | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class MetricAlertNotificationMessage(BaseNotificationMessage): | ||
| # TODO(Yash): do we really need this entire model, or can we whittle it down to what we need? | ||
| incident: Incident | None = None | ||
| # TODO(Yash): do we really need this entire model, or can we whittle it down to what we need? | ||
| trigger_action: AlertRuleTriggerAction | None = None | ||
|
|
||
| @classmethod | ||
|
|
@@ -34,45 +40,33 @@ def from_model(cls, instance: NotificationMessage) -> MetricAlertNotificationMes | |
| ) | ||
|
|
||
|
|
||
| class NewMetricAlertNotificationMessageValidationError(Exception): | ||
| class NewMetricAlertNotificationMessageValidationError(NotificationMessageValidationError): | ||
cathteng marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| pass | ||
|
|
||
|
|
||
| class IncidentAndTriggerActionValidationError(NewMetricAlertNotificationMessageValidationError): | ||
| message = "both incident and trigger action need to exist together with a reference" | ||
|
|
||
|
|
||
| class MessageIdentifierWithErrorValidationError(NewMetricAlertNotificationMessageValidationError): | ||
| message = ( | ||
| "cannot create a new notification message with message identifier when an error exists" | ||
| ) | ||
|
|
||
|
|
||
| @dataclass | ||
| class NewMetricAlertNotificationMessage(BaseNewNotificationMessage): | ||
| incident_id: int | None = None | ||
| trigger_action_id: int | None = None | ||
|
|
||
| def get_validation_error(self) -> Exception | None: | ||
| """ | ||
| Helper method for getting any potential validation errors based on the state of the data. | ||
| There are particular restrictions about the various fields, and this is to help the user check before | ||
| trying to instantiate a new instance in the datastore. | ||
| """ | ||
| if self.message_identifier is not None: | ||
| if self.error_code is not None or self.error_details is not None: | ||
| return MessageIdentifierWithErrorValidationError() | ||
| error = super().get_validation_error() | ||
| if error is not None: | ||
| return error | ||
|
|
||
| if self.message_identifier is not None: | ||
| # If a message_identifier exists, that means a successful notification happened for an incident and trigger | ||
| # This means that neither of them can be empty | ||
| if self.incident_id is None or self.trigger_action_id is None: | ||
| return IncidentAndTriggerActionValidationError() | ||
|
|
||
| incident_exists_without_trigger = ( | ||
| self.incident_id is not None and self.trigger_action_id is None | ||
| ) | ||
| trigger_exists_without_incident = ( | ||
| self.incident_id is None and self.trigger_action_id is not None | ||
| ) | ||
| if incident_exists_without_trigger or trigger_exists_without_incident: | ||
| # We can create a NotificationMessage if it has both, or neither, of incident and trigger. | ||
| # The following is an XNOR check for incident and trigger | ||
| if (self.incident_id is not None) != (self.trigger_action_id is not None): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice nice |
||
| return IncidentAndTriggerActionValidationError() | ||
|
|
||
| return None | ||
|
|
||
Empty file.
44 changes: 44 additions & 0 deletions
44
tests/sentry/integrations/repository/base/test_base_new_notification_message.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| import pytest | ||
|
|
||
| from sentry.integrations.repository.base import ( | ||
| BaseNewNotificationMessage, | ||
| MessageIdentifierWithErrorValidationError, | ||
| ) | ||
|
|
||
|
|
||
| class TestGetValidationError: | ||
| @classmethod | ||
| def _raises_error_for_obj( | ||
| cls, obj: BaseNewNotificationMessage, expected_error: type[Exception] | ||
| ) -> None: | ||
| error = obj.get_validation_error() | ||
| assert error is not None | ||
| with pytest.raises(expected_error): | ||
| raise error | ||
|
|
||
| def test_returns_error_when_message_identifier_has_error_code(self) -> None: | ||
| obj = BaseNewNotificationMessage( | ||
| message_identifier="abc", | ||
| error_code=400, | ||
| ) | ||
| self._raises_error_for_obj(obj, MessageIdentifierWithErrorValidationError) | ||
|
|
||
| def test_returns_error_when_message_identifier_has_error_details(self) -> None: | ||
| obj = BaseNewNotificationMessage( | ||
| message_identifier="abc", | ||
| error_details={"some_key": 123}, | ||
| ) | ||
| self._raises_error_for_obj(obj, MessageIdentifierWithErrorValidationError) | ||
|
|
||
| def test_returns_error_when_message_identifier_has_error(self) -> None: | ||
| obj = BaseNewNotificationMessage( | ||
| message_identifier="abc", | ||
| error_code=400, | ||
| error_details={"some_key": 123}, | ||
| ) | ||
| self._raises_error_for_obj(obj, MessageIdentifierWithErrorValidationError) | ||
|
|
||
| def test_simple(self) -> None: | ||
| obj = BaseNewNotificationMessage() | ||
| error = obj.get_validation_error() | ||
| assert error is None |
2 changes: 1 addition & 1 deletion
2
...sentry/integrations/repository/metric_alert/test_new_metric_alert_notification_message.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.