Add option to explicitly add pagerduty topic

(imported from commit 39ee1b37a807b19570d494f56bf7557228813ed5)
This commit is contained in:
acrefoot 2015-02-10 23:36:29 -08:00
parent 4eaf88e8d1
commit 0b7ef24a34
2 changed files with 22 additions and 10 deletions

View File

@ -737,11 +737,14 @@ class ZenDeskHookTests(AuthedTestCase):
class PagerDutyHookTests(AuthedTestCase): class PagerDutyHookTests(AuthedTestCase):
def send_webhook(self, data, stream_name): def send_webhook(self, data, stream_name, topic=None):
email = 'hamlet@zulip.com' email = 'hamlet@zulip.com'
self.subscribe_to_stream(email, stream_name) self.subscribe_to_stream(email, stream_name)
api_key = self.get_api_key(email) api_key = self.get_api_key(email)
url = '/api/v1/external/pagerduty?api_key=%s&stream=%s' % (api_key, stream_name) if topic:
url = '/api/v1/external/pagerduty?api_key=%s&stream=%s&topic=%s' % (api_key, stream_name, topic)
else:
url = '/api/v1/external/pagerduty?api_key=%s&stream=%s' % (api_key, stream_name)
result = self.client.post(url, ujson.dumps(data), content_type="application/json") result = self.client.post(url, ujson.dumps(data), content_type="application/json")
self.assert_json_success(result) self.assert_json_success(result)
@ -805,6 +808,15 @@ class PagerDutyHookTests(AuthedTestCase):
u':healthy_heart: Incident [48219](https://dropbox.pagerduty.com/incidents/PJKGZF9) resolved\n\n>mp_error_block_down_critical\u2119\u01b4' u':healthy_heart: Incident [48219](https://dropbox.pagerduty.com/incidents/PJKGZF9) resolved\n\n>mp_error_block_down_critical\u2119\u01b4'
) )
def test_explicit_subject(self):
data = ujson.loads(self.fixture_data('pagerduty', 'acknowledge'))
msg = self.send_webhook(data, 'pagerduty', topic="my+cool+topic")
self.assertEqual(msg.subject, 'my cool topic')
self.assertEqual(
msg.content,
':average_heart: Incident [1](https://zulip-test.pagerduty.com/incidents/PO1XIJ5) acknowledged by [armooo@](https://zulip-test.pagerduty.com/users/POBCFRJ)\n\n>It is on fire'
)
def test_bad_message(self): def test_bad_message(self):
data = {'messages': [{'type': 'incident.triggered'}]} data = {'messages': [{'type': 'incident.triggered'}]}
msg = self.send_webhook(data, 'pagerduty') msg = self.send_webhook(data, 'pagerduty')

View File

@ -972,8 +972,8 @@ def build_pagerduty_formatdict(message):
return format_dict return format_dict
def send_raw_pagerduty_json(user_profile, stream, message): def send_raw_pagerduty_json(user_profile, stream, message, topic):
subject = 'pagerduty' subject = topic or 'pagerduty'
body = ( body = (
u'Unknown pagerduty message\n' u'Unknown pagerduty message\n'
u'``` py\n' u'``` py\n'
@ -983,7 +983,7 @@ def send_raw_pagerduty_json(user_profile, stream, message):
[stream], subject, body) [stream], subject, body)
def send_formated_pagerduty(user_profile, stream, message_type, format_dict): def send_formated_pagerduty(user_profile, stream, message_type, format_dict, topic):
if message_type in ('incident.trigger', 'incident.unacknowledge'): if message_type in ('incident.trigger', 'incident.unacknowledge'):
template = (u':unhealthy_heart: Incident ' template = (u':unhealthy_heart: Incident '
u'[{incident_num}]({incident_url}) {action} by ' u'[{incident_num}]({incident_url}) {action} by '
@ -1001,7 +1001,7 @@ def send_formated_pagerduty(user_profile, stream, message_type, format_dict):
template = (u':average_heart: Incident [{incident_num}]({incident_url}) ' template = (u':average_heart: Incident [{incident_num}]({incident_url}) '
u'{action} by [{assigned_to_username}@]({assigned_to_url})\n\n>{trigger_message}') u'{action} by [{assigned_to_username}@]({assigned_to_url})\n\n>{trigger_message}')
subject = u'incident {incident_num}'.format(**format_dict) subject = topic or u'incident {incident_num}'.format(**format_dict)
body = template.format(**format_dict) body = template.format(**format_dict)
check_send_message(user_profile, get_client('ZulipPagerDutyWebhook'), 'stream', check_send_message(user_profile, get_client('ZulipPagerDutyWebhook'), 'stream',
@ -1010,20 +1010,20 @@ def send_formated_pagerduty(user_profile, stream, message_type, format_dict):
@api_key_only_webhook_view @api_key_only_webhook_view
@has_request_variables @has_request_variables
def api_pagerduty_webhook(request, user_profile, stream=REQ(default='pagerduty')): def api_pagerduty_webhook(request, user_profile, stream=REQ(default='pagerduty'), topic=REQ(default=None)):
payload = ujson.loads(request.body) payload = ujson.loads(request.body)
for message in payload['messages']: for message in payload['messages']:
message_type = message['type'] message_type = message['type']
if message_type not in PAGER_DUTY_EVENT_NAMES: if message_type not in PAGER_DUTY_EVENT_NAMES:
send_raw_pagerduty_json(user_profile, stream, message) send_raw_pagerduty_json(user_profile, stream, message, topic)
try: try:
format_dict = build_pagerduty_formatdict(message) format_dict = build_pagerduty_formatdict(message)
except: except:
send_raw_pagerduty_json(user_profile, stream, message) send_raw_pagerduty_json(user_profile, stream, message, topic)
else: else:
send_formated_pagerduty(user_profile, stream, message_type, format_dict) send_formated_pagerduty(user_profile, stream, message_type, format_dict, topic)
return json_success() return json_success()