diff --git a/matrix_alertbot/webhook.py b/matrix_alertbot/webhook.py index 5fe2a98..b4b214e 100644 --- a/matrix_alertbot/webhook.py +++ b/matrix_alertbot/webhook.py @@ -154,20 +154,30 @@ async def create_alert( cache: Cache = request.app["cache"] config: Config = request.app["config"] - if config.dm_select_label and config.dm_select_label in alert.labels: - if alert.match_all_labels(config.dm_filter_labels): - dm_select_value = alert.labels[config.dm_select_label] - if dm_select_value not in config.dm_users: - logger.warning( - f"Cannot find user with label {config.dm_select_label}={dm_select_value}" - ) - return + if alert.match_all_labels(config.dm_filter_labels): + logger.info( + f"Found all DM filter labels in alert labels {config.dm_filter_labels}" + ) + if config.dm_select_label and config.dm_select_label not in alert.labels: + logger.warning( + f"Dismissing alert: Cannot find select label {config.dm_select_label} in alert labels" + ) + return - user_id = config.dm_users[dm_select_value] - if user_id not in matrix_client_pool.dm_rooms: - logger.warning(f"Cannot find a matrix room for user {user_id}") - return - room_id = matrix_client_pool.dm_rooms[user_id] + dm_select_value = alert.labels[config.dm_select_label] + if dm_select_value not in config.dm_users: + logger.warning( + f"Dismissing alert: Cannot find user with label {config.dm_select_label}={dm_select_value}" + ) + return + + user_id = config.dm_users[dm_select_value] + if user_id not in matrix_client_pool.dm_rooms: + logger.warning( + f"Dismissing alert: Cannot find a matrix room for user {user_id}" + ) + return + room_id = matrix_client_pool.dm_rooms[user_id] if alert.firing: try: diff --git a/tests/test_webhook.py b/tests/test_webhook.py index cd092ac..1f3f197 100644 --- a/tests/test_webhook.py +++ b/tests/test_webhook.py @@ -56,7 +56,7 @@ class WebhookApplicationTestCase(aiohttp.test_utils.AioHTTPTestCase): self.fake_config.cache_expire_time = 0 self.fake_config.template_dir = None self.fake_config.dm_select_label = "uuid" - self.fake_config.dm_filter_labels = {"matrix-alertbot": re.compile("dm")} + self.fake_config.dm_filter_labels = {"matrix": re.compile("dm")} self.fake_config.dm_users = BiDict( {"a7b37c33-574c-45ac-bb07-a3b314c2da54": "@fake_dm_user:example.com"} ) @@ -107,7 +107,7 @@ class WebhookApplicationTestCase(aiohttp.test_utils.AioHTTPTestCase): "severity": "warning", "job": "job", "uuid": "a7b37c33-574c-45ac-bb07-a3b314c2da54", - "matrix-alertbot": "dm", + "matrix": "dm", }, "annotations": {"description": "some description"}, } @@ -517,10 +517,13 @@ class WebhookApplicationTestCase(aiohttp.test_utils.AioHTTPTestCase): ) async def test_create_alert_update_silence(self) -> None: - fake_alert = Mock(spec=Alert) - fake_alert.firing = True - fake_alert.fingerprint = "fingerprint" - fake_alert.labels = [] + fake_alert = Alert( + fingerprint="fingerprint", + url="https://example.com", + firing=True, + labels={"severity": "critical"}, + annotations={"description": "dummy description"}, + ) await create_alert(fake_alert, self.fake_room_id, self.fake_request) @@ -533,10 +536,13 @@ class WebhookApplicationTestCase(aiohttp.test_utils.AioHTTPTestCase): async def test_create_alert_with_silence_not_found_error( self, fake_send_text_to_room: Mock ) -> None: - fake_alert = Mock(spec=Alert) - fake_alert.firing = True - fake_alert.fingerprint = "fingerprint" - fake_alert.labels = [] + fake_alert = Alert( + fingerprint="fingerprint", + url="https://example.com", + firing=True, + labels={"severity": "critical"}, + annotations={"description": "dummy description"}, + ) self.fake_alertmanager_client.update_silence.side_effect = SilenceNotFoundError @@ -562,10 +568,13 @@ class WebhookApplicationTestCase(aiohttp.test_utils.AioHTTPTestCase): async def test_create_alert_with_silence_extend_error( self, fake_send_text_to_room: Mock ) -> None: - fake_alert = Mock(spec=Alert) - fake_alert.firing = True - fake_alert.fingerprint = "fingerprint" - fake_alert.labels = [] + fake_alert = Alert( + fingerprint="fingerprint", + url="https://example.com", + firing=True, + labels={"severity": "critical"}, + annotations={"description": "dummy description"}, + ) self.fake_alertmanager_client.update_silence.side_effect = SilenceExtendError @@ -589,10 +598,13 @@ class WebhookApplicationTestCase(aiohttp.test_utils.AioHTTPTestCase): @patch.object(matrix_alertbot.webhook, "send_text_to_room", autospec=True) async def test_create_alert_not_firing(self, fake_send_text_to_room: Mock) -> None: - fake_alert = Mock(spec=Alert) - fake_alert.firing = False - fake_alert.fingerprint = "fingerprint" - fake_alert.labels = [] + fake_alert = Alert( + fingerprint="fingerprint", + url="https://example.com", + firing=False, + labels={}, + annotations={"description": "dummy description"}, + ) await create_alert(fake_alert, self.fake_room_id, self.fake_request) @@ -610,10 +622,13 @@ class WebhookApplicationTestCase(aiohttp.test_utils.AioHTTPTestCase): async def test_create_alert_not_firing_raise_matrix_client_error( self, fake_send_text_to_room: Mock ) -> None: - fake_alert = Mock(spec=Alert) - fake_alert.firing = False - fake_alert.fingerprint = "fingerprint" - fake_alert.labels = [] + fake_alert = Alert( + fingerprint="fingerprint", + url="https://example.com", + firing=False, + labels={}, + annotations={"description": "dummy description"}, + ) self.fake_matrix_client_pool.matrix_client = None