The subject is an optional field in the pagerduty API

Pages from MP are using the description field not the subject field.
Include both in the page if given and don't fail if the key is missing.

(imported from commit 4351e5656d4ea025a03c07c8bb3bb5d406ef2d3d)
This commit is contained in:
Jason Michalski 2015-02-08 21:30:19 -08:00
parent 15713964a5
commit 179bf06940
2 changed files with 21 additions and 5 deletions

View File

@ -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')

View File

@ -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)