webhooks/opsgenie: Improve punctuation and message formatting.

This commit is contained in:
Eeshan Garg 2019-05-09 00:19:54 -02:30 committed by Tim Abbott
parent 120937a1f4
commit 5a1f70f6e8
2 changed files with 156 additions and 96 deletions

View File

@ -9,145 +9,172 @@ class OpsGenieHookTests(WebhookTestCase):
def test_acknowledge_alert(self) -> None: def test_acknowledge_alert(self) -> None:
expected_topic = u"Integration1" expected_topic = u"Integration1"
expected_message = (u"**OpsGenie: [Alert for Integration1.](https://app.opsgenie.com/alert/V2#/show/052652ac-5d1c-464a-812a-7dd18bbfba8c)**\n" expected_message = """
u"Type: *Acknowledge*\n" [OpsGenie Alert for Integration1](https://app.opsgenie.com/alert/V2#/show/052652ac-5d1c-464a-812a-7dd18bbfba8c):
u"Message: *test alert*\n" * **Type**: Acknowledge
u"`tag1` `tag2`" * **Message**: test alert
) * **Tags**: `tag1`, `tag2`
""".strip()
self.send_and_test_stream_message('acknowledge', expected_topic, expected_message, self.send_and_test_stream_message('acknowledge', expected_topic, expected_message,
content_type="application/x-www-form-urlencoded") content_type="application/x-www-form-urlencoded")
def test_addnote_alert(self) -> None: def test_addnote_alert(self) -> None:
expected_topic = u"Integration1" expected_topic = u"Integration1"
expected_message = (u"**OpsGenie: [Alert for Integration1.](https://app.opsgenie.com/alert/V2#/show/052652ac-5d1c-464a-812a-7dd18bbfba8c)**\n" expected_message = """
u"Type: *AddNote*\n" [OpsGenie Alert for Integration1](https://app.opsgenie.com/alert/V2#/show/052652ac-5d1c-464a-812a-7dd18bbfba8c):
u"Note: *note to test alert*\n" * **Type**: AddNote
u"Message: *test alert*\n" * **Note**: note to test alert
u"`tag1` `tag2`" * **Message**: test alert
) * **Tags**: `tag1`, `tag2`
""".strip()
self.send_and_test_stream_message('addnote', expected_topic, expected_message, self.send_and_test_stream_message('addnote', expected_topic, expected_message,
content_type="application/x-www-form-urlencoded") content_type="application/x-www-form-urlencoded")
def test_addrecipient_alert(self) -> None: def test_addrecipient_alert(self) -> None:
expected_topic = u"Integration1" expected_topic = u"Integration1"
expected_message = (u"**OpsGenie: [Alert for Integration1.](https://app.opsgenie.com/alert/V2#/show/052652ac-5d1c-464a-812a-7dd18bbfba8c)**\n" expected_message = """
u"Type: *AddRecipient*\n" [OpsGenie Alert for Integration1](https://app.opsgenie.com/alert/V2#/show/052652ac-5d1c-464a-812a-7dd18bbfba8c):
u"Recipient: *team2_escalation*\n" * **Type**: AddRecipient
u"Message: *test alert*\n" * **Recipient**: team2_escalation
u"`tag1` `tag2`" * **Message**: test alert
) * **Tags**: `tag1`, `tag2`
# use fixture named helloworld_hello """.strip()
self.send_and_test_stream_message('addrecipient', expected_topic, expected_message, self.send_and_test_stream_message('addrecipient', expected_topic, expected_message,
content_type="application/x-www-form-urlencoded") content_type="application/x-www-form-urlencoded")
def test_addtags_alert(self) -> None: def test_addtags_alert(self) -> None:
expected_topic = u"Integration1" expected_topic = u"Integration1"
expected_message = (u"**OpsGenie: [Alert for Integration1.](https://app.opsgenie.com/alert/V2#/show/052652ac-5d1c-464a-812a-7dd18bbfba8c)**\n" expected_message = """
u"Type: *AddTags*\n" [OpsGenie Alert for Integration1](https://app.opsgenie.com/alert/V2#/show/052652ac-5d1c-464a-812a-7dd18bbfba8c):
u"Added tags: *tag1,tag2,tag3*\n" * **Type**: AddTags
u"Message: *test alert*\n" * **Tags added**: tag1,tag2,tag3
u"`tag1` `tag2` `tag3`" * **Message**: test alert
) * **Tags**: `tag1`, `tag2`, `tag3`
""".strip()
self.send_and_test_stream_message('addtags', expected_topic, expected_message, self.send_and_test_stream_message('addtags', expected_topic, expected_message,
content_type="application/x-www-form-urlencoded") content_type="application/x-www-form-urlencoded")
def test_addteam_alert(self) -> None: def test_addteam_alert(self) -> None:
expected_topic = u"Integration1" expected_topic = u"Integration1"
expected_message = (u"**OpsGenie: [Alert for Integration1.](https://app.opsgenie.com/alert/V2#/show/052652ac-5d1c-464a-812a-7dd18bbfba8c)**\n" expected_message = """
u"Type: *AddTeam*\n" [OpsGenie Alert for Integration1](https://app.opsgenie.com/alert/V2#/show/052652ac-5d1c-464a-812a-7dd18bbfba8c):
u"Added team: *team2*\n" * **Type**: AddTeam
u"Message: *test alert*\n" * **Team added**: team2
u"`tag1` `tag2`" * **Message**: test alert
) * **Tags**: `tag1`, `tag2`
""".strip()
self.send_and_test_stream_message('addteam', expected_topic, expected_message, self.send_and_test_stream_message('addteam', expected_topic, expected_message,
content_type="application/x-www-form-urlencoded") content_type="application/x-www-form-urlencoded")
def test_assignownership_alert(self) -> None: def test_assignownership_alert(self) -> None:
expected_topic = u"Integration1" expected_topic = u"Integration1"
expected_message = (u"**OpsGenie: [Alert for Integration1.](https://app.opsgenie.com/alert/V2#/show/052652ac-5d1c-464a-812a-7dd18bbfba8c)**\n" expected_message = """
u"Type: *AssignOwnership*\n" [OpsGenie Alert for Integration1](https://app.opsgenie.com/alert/V2#/show/052652ac-5d1c-464a-812a-7dd18bbfba8c):
u"Assigned owner: *user2@ifountain.com*\n" * **Type**: AssignOwnership
u"Message: *test alert*\n" * **Assigned owner**: user2@ifountain.com
u"`tag1` `tag2`" * **Message**: test alert
) * **Tags**: `tag1`, `tag2`
""".strip()
self.send_and_test_stream_message('assignownership', expected_topic, expected_message, self.send_and_test_stream_message('assignownership', expected_topic, expected_message,
content_type="application/x-www-form-urlencoded") content_type="application/x-www-form-urlencoded")
def test_close_alert(self) -> None: def test_close_alert(self) -> None:
expected_topic = u"Integration1" expected_topic = u"Integration1"
expected_message = (u"**OpsGenie: [Alert for Integration1.](https://app.opsgenie.com/alert/V2#/show/052652ac-5d1c-464a-812a-7dd18bbfba8c)**\n" expected_message = """
u"Type: *Close*\n" [OpsGenie Alert for Integration1](https://app.opsgenie.com/alert/V2#/show/052652ac-5d1c-464a-812a-7dd18bbfba8c):
u"Message: *test alert*" * **Type**: Close
) * **Message**: test alert
""".strip()
self.send_and_test_stream_message('close', expected_topic, expected_message, self.send_and_test_stream_message('close', expected_topic, expected_message,
content_type="application/x-www-form-urlencoded") content_type="application/x-www-form-urlencoded")
def test_create_alert(self) -> None: def test_create_alert(self) -> None:
expected_topic = u"Webhook" expected_topic = u"Webhook"
expected_message = (u"**OpsGenie: [Alert for Webhook.](https://app.opsgenie.com/alert/V2#/show/ec03dad6-62c8-4c94-b38b-d88f398e900f)**\n" expected_message = """
u"Type: *Create*\n" [OpsGenie Alert for Webhook](https://app.opsgenie.com/alert/V2#/show/ec03dad6-62c8-4c94-b38b-d88f398e900f):
u"Message: *another alert*\n" * **Type**: Create
u"`vip`" * **Message**: another alert
) * **Tags**: `vip`
""".strip()
self.send_and_test_stream_message('create', expected_topic, expected_message, self.send_and_test_stream_message('create', expected_topic, expected_message,
content_type="application/x-www-form-urlencoded") content_type="application/x-www-form-urlencoded")
def test_customaction_alert(self) -> None: def test_customaction_alert(self) -> None:
expected_topic = u"Integration1" expected_topic = u"Integration1"
expected_message = (u"**OpsGenie: [Alert for Integration1.](https://app.opsgenie.com/alert/V2#/show/052652ac-5d1c-464a-812a-7dd18bbfba8c)**\n" expected_message = """
u"Type: *TestAction*\n" [OpsGenie Alert for Integration1](https://app.opsgenie.com/alert/V2#/show/052652ac-5d1c-464a-812a-7dd18bbfba8c):
u"Message: *test alert*\n" * **Type**: TestAction
u"`tag1` `tag2`" * **Message**: test alert
) * **Tags**: `tag1`, `tag2`
""".strip()
self.send_and_test_stream_message('customaction', expected_topic, expected_message, self.send_and_test_stream_message('customaction', expected_topic, expected_message,
content_type="application/x-www-form-urlencoded") content_type="application/x-www-form-urlencoded")
def test_delete_alert(self) -> None: def test_delete_alert(self) -> None:
expected_topic = u"Integration1" expected_topic = u"Integration1"
expected_message = (u"**OpsGenie: [Alert for Integration1.](https://app.opsgenie.com/alert/V2#/show/052652ac-5d1c-464a-812a-7dd18bbfba8c)**\n" expected_message = """
u"Type: *Delete*\n" [OpsGenie Alert for Integration1](https://app.opsgenie.com/alert/V2#/show/052652ac-5d1c-464a-812a-7dd18bbfba8c):
u"Message: *test alert*" * **Type**: Delete
) * **Message**: test alert
""".strip()
self.send_and_test_stream_message('delete', expected_topic, expected_message, self.send_and_test_stream_message('delete', expected_topic, expected_message,
content_type="application/x-www-form-urlencoded") content_type="application/x-www-form-urlencoded")
def test_escalate_alert(self) -> None: def test_escalate_alert(self) -> None:
expected_topic = u"Webhook_Test" 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" expected_message = """
u"Type: *Escalate*\n" [OpsGenie Alert for Webhook_Test](https://app.opsgenie.com/alert/V2#/show/7ba97e3a-d328-4b5e-8f9a-39e945a3869a):
u"Escalation: *test_esc*" * **Type**: Escalate
) * **Escalation**: test_esc
""".strip()
self.send_and_test_stream_message('escalate', expected_topic, expected_message, self.send_and_test_stream_message('escalate', expected_topic, expected_message,
content_type="application/x-www-form-urlencoded") content_type="application/x-www-form-urlencoded")
def test_removetags_alert(self) -> None: def test_removetags_alert(self) -> None:
expected_topic = u"Integration1" expected_topic = u"Integration1"
expected_message = (u"**OpsGenie: [Alert for Integration1.](https://app.opsgenie.com/alert/V2#/show/052652ac-5d1c-464a-812a-7dd18bbfba8c)**\n" expected_message = """
u"Type: *RemoveTags*\n" [OpsGenie Alert for Integration1](https://app.opsgenie.com/alert/V2#/show/052652ac-5d1c-464a-812a-7dd18bbfba8c):
u"Removed tags: *tag3*\n" * **Type**: RemoveTags
u"Message: *test alert*\n" * **Tags removed**: tag3
u"`tag1` `tag2`" * **Message**: test alert
) * **Tags**: `tag1`, `tag2`
""".strip()
self.send_and_test_stream_message('removetags', expected_topic, expected_message, self.send_and_test_stream_message('removetags', expected_topic, expected_message,
content_type="application/x-www-form-urlencoded") content_type="application/x-www-form-urlencoded")
def test_takeownership_alert(self) -> None: def test_takeownership_alert(self) -> None:
expected_topic = u"Webhook" expected_topic = u"Webhook"
expected_message = (u"**OpsGenie: [Alert for Webhook.](https://app.opsgenie.com/alert/V2#/show/8a745a79-3ed3-4044-8427-98e067c0623c)**\n" expected_message = """
u"Type: *TakeOwnership*\n" [OpsGenie Alert for Webhook](https://app.opsgenie.com/alert/V2#/show/8a745a79-3ed3-4044-8427-98e067c0623c):
u"Message: *message test*\n" * **Type**: TakeOwnership
u"`tag1` `tag2`" * **Message**: message test
) * **Tags**: `tag1`, `tag2`
""".strip()
self.send_and_test_stream_message('takeownership', expected_topic, expected_message, self.send_and_test_stream_message('takeownership', expected_topic, expected_message,
content_type="application/x-www-form-urlencoded") content_type="application/x-www-form-urlencoded")
def test_unacknowledge_alert(self) -> None: def test_unacknowledge_alert(self) -> None:
expected_topic = u"Integration1" expected_topic = u"Integration1"
expected_message = (u"**OpsGenie: [Alert for Integration1.](https://app.opsgenie.com/alert/V2#/show/052652ac-5d1c-464a-812a-7dd18bbfba8c)**\n" expected_message = """
u"Type: *UnAcknowledge*\n" [OpsGenie Alert for Integration1](https://app.opsgenie.com/alert/V2#/show/052652ac-5d1c-464a-812a-7dd18bbfba8c):
u"Message: *test alert*\n" * **Type**: UnAcknowledge
u"`tag1` `tag2`" * **Message**: test alert
) * **Tags**: `tag1`, `tag2`
""".strip()
self.send_and_test_stream_message('unacknowledge', expected_topic, expected_message, self.send_and_test_stream_message('unacknowledge', expected_topic, expected_message,
content_type="application/x-www-form-urlencoded") content_type="application/x-www-form-urlencoded")

View File

@ -14,37 +14,70 @@ def api_opsgenie_webhook(request: HttpRequest, user_profile: UserProfile,
payload: Dict[str, Any]=REQ(argument_type='body')) -> HttpResponse: payload: Dict[str, Any]=REQ(argument_type='body')) -> HttpResponse:
# construct the body of the message # construct the body of the message
info = {"additional_info": '', info = {
"additional_info": '',
"alert_type": payload['action'], "alert_type": payload['action'],
"alert_id": payload['alert']['alertId'], "alert_id": payload['alert']['alertId'],
"integration_name": payload['integrationName'], "integration_name": payload['integrationName'],
"tags": ' '.join(['`' + tag + '`' for tag in payload['alert'].get('tags', [])]), "tags": ', '.join(['`' + tag + '`' for tag in payload['alert'].get('tags', [])]),
} }
topic = info['integration_name'] topic = info['integration_name']
bullet_template = "* **{key}**: {value}\n"
if 'note' in payload['alert']: 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']: 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']: 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']: 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']: 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: 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']: 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']: if 'message' in payload['alert']:
info['additional_info'] += "Message: *{}*\n".format(payload['alert']['message']) info['additional_info'] += bullet_template.format(
body = '' key='Message',
body_template = "**OpsGenie: [Alert for {integration_name}.]" \ value=payload['alert']['message']
"(https://app.opsgenie.com/alert/V2#/show/{alert_id})**\n" \ )
"Type: *{alert_type}*\n" \ if info['tags']:
"{additional_info}" \ info['additional_info'] += bullet_template.format(
"{tags}" key='Tags',
body += body_template.format(**info) value=info['tags']
# send the message )
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) check_send_webhook_message(request, user_profile, topic, body)
return json_success() return json_success()