|
5 | 5 | from django import forms |
6 | 6 | from django.utils.translation import gettext_lazy as _ |
7 | 7 |
|
| 8 | +from sentry.integrations.opsgenie.client import OpsgenieClient |
8 | 9 | from sentry.integrations.opsgenie.utils import get_team |
9 | 10 | from sentry.services.hybrid_cloud.integration import integration_service |
10 | | -from sentry.services.hybrid_cloud.integration.model import RpcOrganizationIntegration |
| 11 | +from sentry.services.hybrid_cloud.integration.model import ( |
| 12 | + RpcIntegration, |
| 13 | + RpcOrganizationIntegration, |
| 14 | +) |
| 15 | +from sentry.shared_integrations.exceptions import ApiError |
| 16 | + |
| 17 | +INVALID_TEAM = 1 |
| 18 | +INVALID_KEY = 2 |
| 19 | +VALID_TEAM = 3 |
11 | 20 |
|
12 | 21 |
|
13 | 22 | def _validate_int_field(field: str, cleaned_data: Mapping[str, Any]) -> int | None: |
@@ -46,25 +55,67 @@ def __init__(self, *args, **kwargs): |
46 | 55 | self.fields["team"].choices = teams |
47 | 56 | self.fields["team"].widget.choices = self.fields["team"].choices |
48 | 57 |
|
49 | | - def _team_is_valid( |
50 | | - self, team_id: str | None, org_integration: RpcOrganizationIntegration | None |
51 | | - ) -> bool: |
52 | | - return True if get_team(team_id, org_integration) is not None else False |
| 58 | + def _get_team_status( |
| 59 | + self, |
| 60 | + team_id: str | None, |
| 61 | + integration: RpcIntegration, |
| 62 | + org_integration: RpcOrganizationIntegration, |
| 63 | + ) -> int: |
| 64 | + team = get_team(team_id, org_integration) |
| 65 | + if not team: |
| 66 | + return INVALID_TEAM |
| 67 | + |
| 68 | + integration_key = team["integration_key"] |
| 69 | + client = OpsgenieClient( |
| 70 | + integration=integration, |
| 71 | + integration_key=integration_key, |
| 72 | + org_integration_id=org_integration.id, |
| 73 | + ) |
| 74 | + # the integration should be of type "sentry" |
| 75 | + # there's no way to authenticate that a key is an integration key |
| 76 | + # without specifying the type... even though the type is arbitrary |
| 77 | + # and all integration keys do the same thing |
| 78 | + try: |
| 79 | + client.authorize_integration(type="sentry") |
| 80 | + except ApiError: |
| 81 | + return INVALID_KEY |
| 82 | + |
| 83 | + return VALID_TEAM |
53 | 84 |
|
54 | 85 | def _validate_team(self, team_id: str | None, integration_id: int | None) -> None: |
55 | 86 | params = { |
56 | 87 | "account": dict(self.fields["account"].choices).get(integration_id), |
57 | 88 | "team": dict(self.fields["team"].choices).get(team_id), |
58 | 89 | } |
| 90 | + integration = integration_service.get_integration( |
| 91 | + integration_id=integration_id, provider="opsgenie" |
| 92 | + ) |
59 | 93 | org_integration = integration_service.get_organization_integration( |
60 | 94 | integration_id=integration_id, |
61 | 95 | organization_id=self.org_id, |
62 | 96 | ) |
63 | | - |
64 | | - if not self._team_is_valid(team_id=team_id, org_integration=org_integration): |
| 97 | + if integration is None or org_integration is None: |
| 98 | + raise forms.ValidationError( |
| 99 | + _("The Opsgenie integration does not exist."), |
| 100 | + code="invalid_integration", |
| 101 | + params=params, |
| 102 | + ) |
| 103 | + team_status = self._get_team_status( |
| 104 | + team_id=team_id, integration=integration, org_integration=org_integration |
| 105 | + ) |
| 106 | + if team_status == INVALID_TEAM: |
65 | 107 | raise forms.ValidationError( |
66 | 108 | _('The team "%(team)s" does not belong to the %(account)s Opsgenie account.'), |
67 | | - code="invalid", |
| 109 | + code="invalid_team", |
| 110 | + params=params, |
| 111 | + ) |
| 112 | + elif team_status == INVALID_KEY: |
| 113 | + raise forms.ValidationError( |
| 114 | + _( |
| 115 | + 'The provided API key is invalid. Please make sure that the Opsgenie API \ |
| 116 | + key is an integration key of type "Sentry".' |
| 117 | + ), |
| 118 | + code="invalid_key", |
68 | 119 | params=params, |
69 | 120 | ) |
70 | 121 |
|
|
0 commit comments