Skip to content

Commit

Permalink
replace validation error failures with halts
Browse files Browse the repository at this point in the history
  • Loading branch information
mifu67 committed Nov 22, 2024
1 parent a3dba87 commit c2484ff
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
11 changes: 10 additions & 1 deletion src/sentry/integrations/on_call/metrics.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from enum import Enum
from enum import Enum, StrEnum

from attr import dataclass

Expand Down Expand Up @@ -56,3 +56,12 @@ def get_integration_name(self) -> str:

def get_interaction_type(self) -> str:
return str(self.interaction_type)


class OnCallIntegrationsHaltReason(StrEnum):
"""
Reasons why on on call integration method may halt without success/failure.
"""

INVALID_TEAM = "invalid_team"
INVALID_SERVICE = "invalid_service"
6 changes: 4 additions & 2 deletions src/sentry/integrations/opsgenie/actions/form.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from django import forms
from django.utils.translation import gettext_lazy as _

from sentry.integrations.on_call.metrics import OnCallInteractionType
from sentry.integrations.on_call.metrics import OnCallIntegrationsHaltReason, OnCallInteractionType
from sentry.integrations.opsgenie.metrics import record_event
from sentry.integrations.opsgenie.utils import get_team
from sentry.integrations.services.integration import integration_service
Expand Down Expand Up @@ -65,7 +65,7 @@ def _get_team_status(
return VALID_TEAM

def _validate_team(self, team_id: str | None, integration_id: int | None) -> None:
with record_event(OnCallInteractionType.VERIFY_TEAM).capture():
with record_event(OnCallInteractionType.VERIFY_TEAM).capture() as lifecyle:
params = {
"account": dict(self.fields["account"].choices).get(integration_id),
"team": dict(self.fields["team"].choices).get(team_id),
Expand All @@ -78,6 +78,7 @@ def _validate_team(self, team_id: str | None, integration_id: int | None) -> Non
organization_id=self.org_id,
)
if integration is None or org_integration is None:
lifecyle.record_halt(str(OnCallIntegrationsHaltReason.INVALID_TEAM))
raise forms.ValidationError(
_("The Opsgenie integration does not exist."),
code="invalid_integration",
Expand All @@ -86,6 +87,7 @@ def _validate_team(self, team_id: str | None, integration_id: int | None) -> Non

team_status = self._get_team_status(team_id=team_id, org_integration=org_integration)
if team_status == INVALID_TEAM:
lifecyle.record_halt(str(OnCallIntegrationsHaltReason.INVALID_TEAM))
raise forms.ValidationError(
_('The team "%(team)s" does not belong to the %(account)s Opsgenie account.'),
code="invalid_team",
Expand Down
5 changes: 3 additions & 2 deletions src/sentry/integrations/pagerduty/actions/form.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from django import forms
from django.utils.translation import gettext_lazy as _

from sentry.integrations.on_call.metrics import OnCallInteractionType
from sentry.integrations.on_call.metrics import OnCallIntegrationsHaltReason, OnCallInteractionType
from sentry.integrations.pagerduty.metrics import record_event
from sentry.integrations.services.integration import integration_service
from sentry.integrations.types import ExternalProviders
Expand Down Expand Up @@ -47,7 +47,7 @@ def __init__(self, *args, **kwargs):
self.fields["service"].widget.choices = self.fields["service"].choices

def _validate_service(self, service_id: int, integration_id: int) -> None:
with record_event(OnCallInteractionType.VALIDATE_SERVICE).capture():
with record_event(OnCallInteractionType.VALIDATE_SERVICE).capture() as lifecycle:
params = {
"account": dict(self.fields["account"].choices).get(integration_id),
"service": dict(self.fields["service"].choices).get(service_id),
Expand All @@ -66,6 +66,7 @@ def _validate_service(self, service_id: int, integration_id: int) -> None:
):
# We need to make sure that the service actually belongs to that integration,
# meaning that it belongs under the appropriate account in PagerDuty.
lifecycle.record_halt(str(OnCallIntegrationsHaltReason.INVALID_SERVICE))
raise forms.ValidationError(
_(
'The service "%(service)s" has not been granted access in the %(account)s Pagerduty account.'
Expand Down

0 comments on commit c2484ff

Please sign in to comment.