From eeb39b8aea0af47c53535786694468c201b092b7 Mon Sep 17 00:00:00 2001 From: HgO Date: Mon, 12 Aug 2024 13:24:40 +0200 Subject: [PATCH 1/4] fix dm label filtering --- matrix_alertbot/webhook.py | 36 ++++++++++++++--------- tests/test_webhook.py | 59 ++++++++++++++++++++++++-------------- 2 files changed, 60 insertions(+), 35 deletions(-) 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 From 08264b9e75ac9ef851aa495d54b54c68bf2c6bdb Mon Sep 17 00:00:00 2001 From: HgO Date: Mon, 12 Aug 2024 13:24:47 +0200 Subject: [PATCH 2/4] find dm room with only bot and user in it --- matrix_alertbot/matrix.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/matrix_alertbot/matrix.py b/matrix_alertbot/matrix.py index 8112201..4b6da27 100644 --- a/matrix_alertbot/matrix.py +++ b/matrix_alertbot/matrix.py @@ -206,6 +206,9 @@ class MatrixClientPool: f"Bot {account.id} | Found {len(room_members)} room members in {room_id}" ) + if len(room_members) > len(self._matrix_clients) + 1: + continue + all_accounts_in_room = True for user_id in unactive_user_ids: if user_id not in room_members: From e7d4471c3dcea86ee1b3be088e24c35068d57bbf Mon Sep 17 00:00:00 2001 From: HgO Date: Mon, 12 Aug 2024 13:27:04 +0200 Subject: [PATCH 3/4] remove debug message --- matrix_alertbot/webhook.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/matrix_alertbot/webhook.py b/matrix_alertbot/webhook.py index b4b214e..cce97f9 100644 --- a/matrix_alertbot/webhook.py +++ b/matrix_alertbot/webhook.py @@ -156,7 +156,7 @@ async def create_alert( if alert.match_all_labels(config.dm_filter_labels): logger.info( - f"Found all DM filter labels in alert labels {config.dm_filter_labels}" + f"Found all DM filter labels in alert labels" ) if config.dm_select_label and config.dm_select_label not in alert.labels: logger.warning( From 6d7980e6d5a671776fb130f8607c2c694b3c4d79 Mon Sep 17 00:00:00 2001 From: HgO Date: Mon, 12 Aug 2024 13:28:00 +0200 Subject: [PATCH 4/4] linting --- matrix_alertbot/webhook.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/matrix_alertbot/webhook.py b/matrix_alertbot/webhook.py index cce97f9..a2a824b 100644 --- a/matrix_alertbot/webhook.py +++ b/matrix_alertbot/webhook.py @@ -155,9 +155,7 @@ async def create_alert( config: Config = request.app["config"] if alert.match_all_labels(config.dm_filter_labels): - logger.info( - f"Found all DM filter labels in alert labels" - ) + logger.info("Found all DM filter labels in alert 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"