Merge branch 'fix-dm-label-filtering' into 'master'
Fix DM label filtering and find DM rooms with only the bot(s) and user in it See merge request Neutrinet/matrix-alertbot!15
This commit is contained in:
commit
6feeadda69
3 changed files with 61 additions and 35 deletions
|
@ -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:
|
||||
|
|
|
@ -154,20 +154,28 @@ 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("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"
|
||||
)
|
||||
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:
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
Loading…
Reference in a new issue