Skip to content

Commit b8457a1

Browse files
authored
fix(msteams): Allow new install event to pass (#67925)
When a new installation event comes in from the MSTeams bot, we need to allow that to go through to the Webhook event handler, instead of doing other checks and trying to get the integration or regions, as the integration does not exist yet. We were erroring because while we can technically infer an integration, it is new, and not existing. ![Screenshot 2024-03-28 at 5 26 45 PM](https://github.com/getsentry/sentry/assets/5581484/22f5caed-d61e-40ec-adf7-0d21a350b9b1) The greater fix resolves around updating this entire middleware and changing the logic. Resolves: #66678
1 parent 7c89c55 commit b8457a1

File tree

3 files changed

+63
-3
lines changed

3 files changed

+63
-3
lines changed

src/sentry/integrations/msteams/webhook.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,22 @@ def can_infer_integration(self, data: Mapping[str, Any]) -> bool:
200200
or self.infer_team_id_from_channel_data(data=data) is not None
201201
)
202202

203+
@classmethod
204+
def is_new_integration_installation_event(cls, data: Mapping[str, Any]) -> bool:
205+
try:
206+
raw_event_type = data["type"]
207+
event_type = MsTeamsEvents.get_from_value(value=raw_event_type)
208+
if event_type != MsTeamsEvents.INSTALLATION_UPDATE:
209+
return False
210+
211+
action = data.get("action", None)
212+
if action is None or action != "add":
213+
return False
214+
215+
return True
216+
except Exception:
217+
return False
218+
203219

204220
class MsTeamsEvents(Enum):
205221
INSTALLATION_UPDATE = "installationUpdate"

src/sentry/middleware/integrations/parsers/msteams.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,21 @@ def _check_if_event_should_be_sync(cls, data: Mapping[str, Any]) -> bool:
6868
def get_response(self) -> HttpResponseBase:
6969
if self.view_class not in self.region_view_classes:
7070
logger.info(
71-
"View class not in region",
71+
"View class not in region, sending to webhook handler",
7272
extra={"request_data": self.request_data},
7373
)
7474
return self.get_response_from_control_silo()
7575

7676
if not self.can_infer_integration(data=self.request_data):
7777
logger.info(
78-
"Could not infer integration",
78+
"Could not infer integration, sending to webhook handler",
79+
extra={"request_data": self.request_data},
80+
)
81+
return self.get_response_from_control_silo()
82+
83+
if self.is_new_integration_installation_event(data=self.request_data):
84+
logger.info(
85+
"New installation event detected, sending to webhook handler",
7986
extra={"request_data": self.request_data},
8087
)
8188
return self.get_response_from_control_silo()
@@ -116,7 +123,7 @@ def get_response(self) -> HttpResponseBase:
116123

117124
if self._check_if_event_should_be_sync(data=self.request_data):
118125
logger.info(
119-
"MSTeams event should be handled synchronously",
126+
"MSTeams event should be handled synchronously, sending to webhook handler",
120127
extra={"request_data": self.request_data},
121128
)
122129
return self.get_response_from_control_silo()
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
from typing import Any
2+
3+
from sentry.integrations.msteams import MsTeamsWebhookMixin
4+
5+
6+
class TestIsNewIntegrationInstallationEvent:
7+
def test_valid_new_installation_event(self) -> None:
8+
data: dict[str, Any] = {"type": "installationUpdate", "action": "add"}
9+
assert MsTeamsWebhookMixin.is_new_integration_installation_event(data) is True
10+
11+
def test_valid_non_installation_event(self) -> None:
12+
data: dict[str, Any] = {"type": "message", "action": "add"}
13+
assert MsTeamsWebhookMixin.is_new_integration_installation_event(data) is False
14+
15+
def test_invalid_missing_type_field(self) -> None:
16+
data: dict[str, Any] = {"action": "add"}
17+
assert MsTeamsWebhookMixin.is_new_integration_installation_event(data) is False
18+
19+
def test_only_required_fields(self) -> None:
20+
data: dict[str, Any] = {"type": "installationUpdate"}
21+
assert MsTeamsWebhookMixin.is_new_integration_installation_event(data) is False
22+
23+
def test_additional_fields(self) -> None:
24+
data: dict[str, Any] = {"type": "installationUpdate", "action": "add", "extra": "field"}
25+
assert MsTeamsWebhookMixin.is_new_integration_installation_event(data) is True
26+
27+
def test_minimum_input(self) -> None:
28+
data: dict[str, Any] = {"type": "installationUpdate", "action": "add"}
29+
assert MsTeamsWebhookMixin.is_new_integration_installation_event(data) is True
30+
31+
def test_invalid_event_type(self) -> None:
32+
data: dict[str, Any] = {"type": "invalidType", "action": "add"}
33+
assert MsTeamsWebhookMixin.is_new_integration_installation_event(data) is False
34+
35+
def test_invalid_action(self) -> None:
36+
data: dict[str, Any] = {"type": "installationUpdate", "action": "remove"}
37+
assert MsTeamsWebhookMixin.is_new_integration_installation_event(data) is False

0 commit comments

Comments
 (0)