From 0b7ef24a344af63298ede537a9bf1dbc2e99db85 Mon Sep 17 00:00:00 2001 From: acrefoot Date: Tue, 10 Feb 2015 23:36:29 -0800 Subject: [PATCH] Add option to explicitly add pagerduty topic (imported from commit 39ee1b37a807b19570d494f56bf7557228813ed5) --- zerver/test_hooks.py | 16 ++++++++++++++-- zerver/views/webhooks.py | 16 ++++++++-------- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/zerver/test_hooks.py b/zerver/test_hooks.py index 3b7d9bd81e..0a0faee6e6 100644 --- a/zerver/test_hooks.py +++ b/zerver/test_hooks.py @@ -737,11 +737,14 @@ class ZenDeskHookTests(AuthedTestCase): class PagerDutyHookTests(AuthedTestCase): - def send_webhook(self, data, stream_name): + def send_webhook(self, data, stream_name, topic=None): email = 'hamlet@zulip.com' self.subscribe_to_stream(email, stream_name) 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") 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' ) + 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): data = {'messages': [{'type': 'incident.triggered'}]} msg = self.send_webhook(data, 'pagerduty') diff --git a/zerver/views/webhooks.py b/zerver/views/webhooks.py index 0b3fdd5a5c..2c3e6d68c4 100644 --- a/zerver/views/webhooks.py +++ b/zerver/views/webhooks.py @@ -972,8 +972,8 @@ def build_pagerduty_formatdict(message): return format_dict -def send_raw_pagerduty_json(user_profile, stream, message): - subject = 'pagerduty' +def send_raw_pagerduty_json(user_profile, stream, message, topic): + subject = topic or 'pagerduty' body = ( u'Unknown pagerduty message\n' u'``` py\n' @@ -983,7 +983,7 @@ def send_raw_pagerduty_json(user_profile, stream, message): [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'): template = (u':unhealthy_heart: Incident ' 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}) ' 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) 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 @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) for message in payload['messages']: message_type = message['type'] 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: format_dict = build_pagerduty_formatdict(message) except: - send_raw_pagerduty_json(user_profile, stream, message) + send_raw_pagerduty_json(user_profile, stream, message, topic) 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()