diff --git a/zerver/webhooks/opsgenie/tests.py b/zerver/webhooks/opsgenie/tests.py index 62ce07de87..66f9cb78f2 100644 --- a/zerver/webhooks/opsgenie/tests.py +++ b/zerver/webhooks/opsgenie/tests.py @@ -9,145 +9,172 @@ class OpsGenieHookTests(WebhookTestCase): def test_acknowledge_alert(self) -> None: expected_topic = u"Integration1" - expected_message = (u"**OpsGenie: [Alert for Integration1.](https://app.opsgenie.com/alert/V2#/show/052652ac-5d1c-464a-812a-7dd18bbfba8c)**\n" - u"Type: *Acknowledge*\n" - u"Message: *test alert*\n" - u"`tag1` `tag2`" - ) + expected_message = """ +[OpsGenie Alert for Integration1](https://app.opsgenie.com/alert/V2#/show/052652ac-5d1c-464a-812a-7dd18bbfba8c): +* **Type**: Acknowledge +* **Message**: test alert +* **Tags**: `tag1`, `tag2` +""".strip() + self.send_and_test_stream_message('acknowledge', expected_topic, expected_message, content_type="application/x-www-form-urlencoded") def test_addnote_alert(self) -> None: expected_topic = u"Integration1" - expected_message = (u"**OpsGenie: [Alert for Integration1.](https://app.opsgenie.com/alert/V2#/show/052652ac-5d1c-464a-812a-7dd18bbfba8c)**\n" - u"Type: *AddNote*\n" - u"Note: *note to test alert*\n" - u"Message: *test alert*\n" - u"`tag1` `tag2`" - ) + expected_message = """ +[OpsGenie Alert for Integration1](https://app.opsgenie.com/alert/V2#/show/052652ac-5d1c-464a-812a-7dd18bbfba8c): +* **Type**: AddNote +* **Note**: note to test alert +* **Message**: test alert +* **Tags**: `tag1`, `tag2` +""".strip() + self.send_and_test_stream_message('addnote', expected_topic, expected_message, content_type="application/x-www-form-urlencoded") def test_addrecipient_alert(self) -> None: expected_topic = u"Integration1" - expected_message = (u"**OpsGenie: [Alert for Integration1.](https://app.opsgenie.com/alert/V2#/show/052652ac-5d1c-464a-812a-7dd18bbfba8c)**\n" - u"Type: *AddRecipient*\n" - u"Recipient: *team2_escalation*\n" - u"Message: *test alert*\n" - u"`tag1` `tag2`" - ) - # use fixture named helloworld_hello + expected_message = """ +[OpsGenie Alert for Integration1](https://app.opsgenie.com/alert/V2#/show/052652ac-5d1c-464a-812a-7dd18bbfba8c): +* **Type**: AddRecipient +* **Recipient**: team2_escalation +* **Message**: test alert +* **Tags**: `tag1`, `tag2` +""".strip() + self.send_and_test_stream_message('addrecipient', expected_topic, expected_message, content_type="application/x-www-form-urlencoded") def test_addtags_alert(self) -> None: expected_topic = u"Integration1" - expected_message = (u"**OpsGenie: [Alert for Integration1.](https://app.opsgenie.com/alert/V2#/show/052652ac-5d1c-464a-812a-7dd18bbfba8c)**\n" - u"Type: *AddTags*\n" - u"Added tags: *tag1,tag2,tag3*\n" - u"Message: *test alert*\n" - u"`tag1` `tag2` `tag3`" - ) + expected_message = """ +[OpsGenie Alert for Integration1](https://app.opsgenie.com/alert/V2#/show/052652ac-5d1c-464a-812a-7dd18bbfba8c): +* **Type**: AddTags +* **Tags added**: tag1,tag2,tag3 +* **Message**: test alert +* **Tags**: `tag1`, `tag2`, `tag3` +""".strip() + self.send_and_test_stream_message('addtags', expected_topic, expected_message, content_type="application/x-www-form-urlencoded") def test_addteam_alert(self) -> None: expected_topic = u"Integration1" - expected_message = (u"**OpsGenie: [Alert for Integration1.](https://app.opsgenie.com/alert/V2#/show/052652ac-5d1c-464a-812a-7dd18bbfba8c)**\n" - u"Type: *AddTeam*\n" - u"Added team: *team2*\n" - u"Message: *test alert*\n" - u"`tag1` `tag2`" - ) + expected_message = """ +[OpsGenie Alert for Integration1](https://app.opsgenie.com/alert/V2#/show/052652ac-5d1c-464a-812a-7dd18bbfba8c): +* **Type**: AddTeam +* **Team added**: team2 +* **Message**: test alert +* **Tags**: `tag1`, `tag2` +""".strip() + self.send_and_test_stream_message('addteam', expected_topic, expected_message, content_type="application/x-www-form-urlencoded") def test_assignownership_alert(self) -> None: expected_topic = u"Integration1" - expected_message = (u"**OpsGenie: [Alert for Integration1.](https://app.opsgenie.com/alert/V2#/show/052652ac-5d1c-464a-812a-7dd18bbfba8c)**\n" - u"Type: *AssignOwnership*\n" - u"Assigned owner: *user2@ifountain.com*\n" - u"Message: *test alert*\n" - u"`tag1` `tag2`" - ) + expected_message = """ +[OpsGenie Alert for Integration1](https://app.opsgenie.com/alert/V2#/show/052652ac-5d1c-464a-812a-7dd18bbfba8c): +* **Type**: AssignOwnership +* **Assigned owner**: user2@ifountain.com +* **Message**: test alert +* **Tags**: `tag1`, `tag2` +""".strip() + self.send_and_test_stream_message('assignownership', expected_topic, expected_message, content_type="application/x-www-form-urlencoded") def test_close_alert(self) -> None: expected_topic = u"Integration1" - expected_message = (u"**OpsGenie: [Alert for Integration1.](https://app.opsgenie.com/alert/V2#/show/052652ac-5d1c-464a-812a-7dd18bbfba8c)**\n" - u"Type: *Close*\n" - u"Message: *test alert*" - ) + expected_message = """ +[OpsGenie Alert for Integration1](https://app.opsgenie.com/alert/V2#/show/052652ac-5d1c-464a-812a-7dd18bbfba8c): +* **Type**: Close +* **Message**: test alert +""".strip() + self.send_and_test_stream_message('close', expected_topic, expected_message, content_type="application/x-www-form-urlencoded") def test_create_alert(self) -> None: expected_topic = u"Webhook" - expected_message = (u"**OpsGenie: [Alert for Webhook.](https://app.opsgenie.com/alert/V2#/show/ec03dad6-62c8-4c94-b38b-d88f398e900f)**\n" - u"Type: *Create*\n" - u"Message: *another alert*\n" - u"`vip`" - ) + expected_message = """ +[OpsGenie Alert for Webhook](https://app.opsgenie.com/alert/V2#/show/ec03dad6-62c8-4c94-b38b-d88f398e900f): +* **Type**: Create +* **Message**: another alert +* **Tags**: `vip` +""".strip() + self.send_and_test_stream_message('create', expected_topic, expected_message, content_type="application/x-www-form-urlencoded") def test_customaction_alert(self) -> None: expected_topic = u"Integration1" - expected_message = (u"**OpsGenie: [Alert for Integration1.](https://app.opsgenie.com/alert/V2#/show/052652ac-5d1c-464a-812a-7dd18bbfba8c)**\n" - u"Type: *TestAction*\n" - u"Message: *test alert*\n" - u"`tag1` `tag2`" - ) + expected_message = """ +[OpsGenie Alert for Integration1](https://app.opsgenie.com/alert/V2#/show/052652ac-5d1c-464a-812a-7dd18bbfba8c): +* **Type**: TestAction +* **Message**: test alert +* **Tags**: `tag1`, `tag2` +""".strip() + self.send_and_test_stream_message('customaction', expected_topic, expected_message, content_type="application/x-www-form-urlencoded") def test_delete_alert(self) -> None: expected_topic = u"Integration1" - expected_message = (u"**OpsGenie: [Alert for Integration1.](https://app.opsgenie.com/alert/V2#/show/052652ac-5d1c-464a-812a-7dd18bbfba8c)**\n" - u"Type: *Delete*\n" - u"Message: *test alert*" - ) + expected_message = """ +[OpsGenie Alert for Integration1](https://app.opsgenie.com/alert/V2#/show/052652ac-5d1c-464a-812a-7dd18bbfba8c): +* **Type**: Delete +* **Message**: test alert +""".strip() + self.send_and_test_stream_message('delete', expected_topic, expected_message, content_type="application/x-www-form-urlencoded") def test_escalate_alert(self) -> None: expected_topic = u"Webhook_Test" - expected_message = (u"**OpsGenie: [Alert for Webhook_Test.](https://app.opsgenie.com/alert/V2#/show/7ba97e3a-d328-4b5e-8f9a-39e945a3869a)**\n" - u"Type: *Escalate*\n" - u"Escalation: *test_esc*" - ) + expected_message = """ +[OpsGenie Alert for Webhook_Test](https://app.opsgenie.com/alert/V2#/show/7ba97e3a-d328-4b5e-8f9a-39e945a3869a): +* **Type**: Escalate +* **Escalation**: test_esc +""".strip() + self.send_and_test_stream_message('escalate', expected_topic, expected_message, content_type="application/x-www-form-urlencoded") def test_removetags_alert(self) -> None: expected_topic = u"Integration1" - expected_message = (u"**OpsGenie: [Alert for Integration1.](https://app.opsgenie.com/alert/V2#/show/052652ac-5d1c-464a-812a-7dd18bbfba8c)**\n" - u"Type: *RemoveTags*\n" - u"Removed tags: *tag3*\n" - u"Message: *test alert*\n" - u"`tag1` `tag2`" - ) + expected_message = """ +[OpsGenie Alert for Integration1](https://app.opsgenie.com/alert/V2#/show/052652ac-5d1c-464a-812a-7dd18bbfba8c): +* **Type**: RemoveTags +* **Tags removed**: tag3 +* **Message**: test alert +* **Tags**: `tag1`, `tag2` +""".strip() + self.send_and_test_stream_message('removetags', expected_topic, expected_message, content_type="application/x-www-form-urlencoded") def test_takeownership_alert(self) -> None: expected_topic = u"Webhook" - expected_message = (u"**OpsGenie: [Alert for Webhook.](https://app.opsgenie.com/alert/V2#/show/8a745a79-3ed3-4044-8427-98e067c0623c)**\n" - u"Type: *TakeOwnership*\n" - u"Message: *message test*\n" - u"`tag1` `tag2`" - ) + expected_message = """ +[OpsGenie Alert for Webhook](https://app.opsgenie.com/alert/V2#/show/8a745a79-3ed3-4044-8427-98e067c0623c): +* **Type**: TakeOwnership +* **Message**: message test +* **Tags**: `tag1`, `tag2` +""".strip() + self.send_and_test_stream_message('takeownership', expected_topic, expected_message, content_type="application/x-www-form-urlencoded") def test_unacknowledge_alert(self) -> None: expected_topic = u"Integration1" - expected_message = (u"**OpsGenie: [Alert for Integration1.](https://app.opsgenie.com/alert/V2#/show/052652ac-5d1c-464a-812a-7dd18bbfba8c)**\n" - u"Type: *UnAcknowledge*\n" - u"Message: *test alert*\n" - u"`tag1` `tag2`" - ) + expected_message = """ +[OpsGenie Alert for Integration1](https://app.opsgenie.com/alert/V2#/show/052652ac-5d1c-464a-812a-7dd18bbfba8c): +* **Type**: UnAcknowledge +* **Message**: test alert +* **Tags**: `tag1`, `tag2` +""".strip() + self.send_and_test_stream_message('unacknowledge', expected_topic, expected_message, content_type="application/x-www-form-urlencoded") diff --git a/zerver/webhooks/opsgenie/view.py b/zerver/webhooks/opsgenie/view.py index 6dd2e1dda2..1baca36b35 100644 --- a/zerver/webhooks/opsgenie/view.py +++ b/zerver/webhooks/opsgenie/view.py @@ -14,37 +14,70 @@ def api_opsgenie_webhook(request: HttpRequest, user_profile: UserProfile, payload: Dict[str, Any]=REQ(argument_type='body')) -> HttpResponse: # construct the body of the message - info = {"additional_info": '', - "alert_type": payload['action'], - "alert_id": payload['alert']['alertId'], - "integration_name": payload['integrationName'], - "tags": ' '.join(['`' + tag + '`' for tag in payload['alert'].get('tags', [])]), - } + info = { + "additional_info": '', + "alert_type": payload['action'], + "alert_id": payload['alert']['alertId'], + "integration_name": payload['integrationName'], + "tags": ', '.join(['`' + tag + '`' for tag in payload['alert'].get('tags', [])]), + } + topic = info['integration_name'] + bullet_template = "* **{key}**: {value}\n" + if 'note' in payload['alert']: - info['additional_info'] += "Note: *{}*\n".format(payload['alert']['note']) + info['additional_info'] += bullet_template.format( + key='Note', + value=payload['alert']['note'] + ) if 'recipient' in payload['alert']: - info['additional_info'] += "Recipient: *{}*\n".format(payload['alert']['recipient']) + info['additional_info'] += bullet_template.format( + key='Recipient', + value=payload['alert']['recipient'] + ) if 'addedTags' in payload['alert']: - info['additional_info'] += "Added tags: *{}*\n".format(payload['alert']['addedTags']) + info['additional_info'] += bullet_template.format( + key='Tags added', + value=payload['alert']['addedTags'] + ) if 'team' in payload['alert']: - info['additional_info'] += "Added team: *{}*\n".format(payload['alert']['team']) + info['additional_info'] += bullet_template.format( + key='Team added', + value=payload['alert']['team'] + ) if 'owner' in payload['alert']: - info['additional_info'] += "Assigned owner: *{}*\n".format(payload['alert']['owner']) + info['additional_info'] += bullet_template.format( + key='Assigned owner', + value=payload['alert']['owner'] + ) if 'escalationName' in payload: - info['additional_info'] += "Escalation: *{}*\n".format(payload['escalationName']) + info['additional_info'] += bullet_template.format( + key='Escalation', + value=payload['escalationName'] + ) if 'removedTags' in payload['alert']: - info['additional_info'] += "Removed tags: *{}*\n".format(payload['alert']['removedTags']) + info['additional_info'] += bullet_template.format( + key='Tags removed', + value=payload['alert']['removedTags'] + ) if 'message' in payload['alert']: - info['additional_info'] += "Message: *{}*\n".format(payload['alert']['message']) - body = '' - body_template = "**OpsGenie: [Alert for {integration_name}.]" \ - "(https://app.opsgenie.com/alert/V2#/show/{alert_id})**\n" \ - "Type: *{alert_type}*\n" \ - "{additional_info}" \ - "{tags}" - body += body_template.format(**info) - # send the message + info['additional_info'] += bullet_template.format( + key='Message', + value=payload['alert']['message'] + ) + if info['tags']: + info['additional_info'] += bullet_template.format( + key='Tags', + value=info['tags'] + ) + + body_template = """ +[OpsGenie Alert for {integration_name}](https://app.opsgenie.com/alert/V2#/show/{alert_id}): +* **Type**: {alert_type} +{additional_info} +""".strip() + + body = body_template.format(**info) check_send_webhook_message(request, user_profile, topic, body) return json_success()