diff --git a/zerver/test_hooks.py b/zerver/test_hooks.py index e9529dcd75..4d0b021d0b 100644 --- a/zerver/test_hooks.py +++ b/zerver/test_hooks.py @@ -796,6 +796,15 @@ class PagerDutyHookTests(AuthedTestCase): ':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_no_subject(self): + data = ujson.loads(self.fixture_data('pagerduty', 'mp_fail')) + msg = self.send_webhook(data, 'pagerduty') + self.assertEqual(msg.subject, 'incident 48219') + self.assertEqual( + msg.content, + ':healthy_heart: Incident [48219](https://dropbox.pagerduty.com/incidents/PJKGZF9) resolved\n\n>mp_error_block_down_critical' + ) + 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 0fbf431cd5..59b5002915 100644 --- a/zerver/views/webhooks.py +++ b/zerver/views/webhooks.py @@ -961,7 +961,14 @@ def build_pagerdudy_formatdict(message): format_dict['resolved_by_username'] = 'nobody' format_dict['resolved_by_url'] = '' - format_dict['trigger_subject'] = message['data']['incident']['trigger_summary_data']['subject'] + trigger_message = [] + trigger_subject = message['data']['incident']['trigger_summary_data'].get('subject', '') + if trigger_subject: + trigger_message.append(trigger_subject) + trigger_description = message['data']['incident']['trigger_summary_data'].get('description', '') + if trigger_description: + trigger_message.append(trigger_description) + format_dict['trigger_message'] = '\n'.join(trigger_message) return format_dict @@ -981,18 +988,18 @@ def send_formated_pagerduty(user_profile, stream, message_type, format_dict): template = (':unhealthy_heart: Incident ' '[{incident_num}]({incident_url}) {action} by ' '[{service_name}]({service_url}) and assigned to ' - '[{assigned_to_username}@]({assigned_to_url})\n\n>{trigger_subject}') + '[{assigned_to_username}@]({assigned_to_url})\n\n>{trigger_message}') elif message_type == 'incident.resolve' and format_dict['resolved_by_url']: template = (':healthy_heart: Incident ' '[{incident_num}]({incident_url}) resolved by ' - '[{resolved_by_username}@]({resolved_by_url})\n\n>{trigger_subject}') + '[{resolved_by_username}@]({resolved_by_url})\n\n>{trigger_message}') elif message_type == 'incident.resolve' and not format_dict['resolved_by_url']: template = (':healthy_heart: Incident ' - '[{incident_num}]({incident_url}) resolved\n\n>{trigger_subject}') + '[{incident_num}]({incident_url}) resolved\n\n>{trigger_message}') else: template = (':average_heart: Incident [{incident_num}]({incident_url}) ' - '{action} by [{assigned_to_username}@]({assigned_to_url})\n\n>{trigger_subject}') + '{action} by [{assigned_to_username}@]({assigned_to_url})\n\n>{trigger_message}') subject = 'incident {incident_num}'.format(**format_dict) body = template.format(**format_dict)