Skip to content

Commit

Permalink
fix failing test and record halt for invalid API key
Browse files Browse the repository at this point in the history
  • Loading branch information
mifu67 committed Nov 23, 2024
1 parent c2484ff commit 0e0bfe1
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/sentry/integrations/on_call/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,4 @@ class OnCallIntegrationsHaltReason(StrEnum):

INVALID_TEAM = "invalid_team"
INVALID_SERVICE = "invalid_service"
INVALID_KEY = "invalid_key"
5 changes: 3 additions & 2 deletions src/sentry/integrations/opsgenie/integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
)
from sentry.integrations.models.integration import Integration
from sentry.integrations.models.organization_integration import OrganizationIntegration
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.tasks import migrate_opsgenie_plugin
from sentry.organizations.services.organization import RpcOrganizationSummary
Expand Down Expand Up @@ -183,7 +183,7 @@ def update_organization_config(self, data: MutableMapping[str, Any]) -> None:
team["id"] = str(self.org_integration.id) + "-" + team["team"]

invalid_keys = []
with record_event(OnCallInteractionType.VERIFY_KEYS).capture():
with record_event(OnCallInteractionType.VERIFY_KEYS).capture() as lifecycle:
for team in teams:
# skip if team, key pair already exist in config
if (team["team"], team["integration_key"]) in existing_team_key_pairs:
Expand Down Expand Up @@ -213,6 +213,7 @@ def update_organization_config(self, data: MutableMapping[str, Any]) -> None:
raise

if invalid_keys:
lifecycle.record_halt(OnCallIntegrationsHaltReason.INVALID_KEY)
raise ApiUnauthorized(f"Invalid integration key: {str(invalid_keys)}")

return super().update_organization_config(data)
Expand Down
5 changes: 4 additions & 1 deletion tests/sentry/integrations/pagerduty/test_notify_action.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import responses

from sentry.integrations.models.organization_integration import OrganizationIntegration
from sentry.integrations.on_call.metrics import OnCallIntegrationsHaltReason
from sentry.integrations.pagerduty.actions.notification import PagerDutyNotifyServiceAction
from sentry.integrations.pagerduty.utils import add_service
from sentry.integrations.types import EventLifecycleOutcome
Expand All @@ -13,6 +14,7 @@
from sentry.testutils.helpers.notifications import TEST_ISSUE_OCCURRENCE
from sentry.testutils.silo import assume_test_silo_mode
from sentry.testutils.skips import requires_snuba
from tests.sentry.integrations.utils.test_assert_metrics import assert_halt_metric

pytestmark = [requires_snuba]

Expand Down Expand Up @@ -330,4 +332,5 @@ def test_invalid_service_selected(self, mock_record):
assert len(mock_record.mock_calls) == 2
start, halt = mock_record.mock_calls
assert start.args[0] == EventLifecycleOutcome.STARTED
assert halt.args[0] == EventLifecycleOutcome.FAILURE
assert halt.args[0] == EventLifecycleOutcome.HALTED
assert_halt_metric(mock_record, OnCallIntegrationsHaltReason.INVALID_SERVICE.value)

0 comments on commit 0e0bfe1

Please sign in to comment.