Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
integrations: Change format of templates for PagerDuty V3.
PIG20832ca26391465
diff --git a/zerver/webhooks/pagerduty/tests.py b/zerver/webhooks/pagerduty/tests.pyindex ac25db6ede6..6354e38c8e0 100644--- a/zerver/webhooks/pagerduty/tests.py+++ b/zerver/webhooks/pagerduty/tests.py@@ -7,51 +7,51 @@ class PagerDutyHookTests(WebhookTestCase):FIXTURE_DIR_NAME = "pagerduty"def test_trigger(self) -> None:- expected_message = "Incident [3](https://zulip-test.pagerduty.com/incidents/P140S4Y) triggered by [Test service](https://zulip-test.pagerduty.com/services/PIL5CUQ) (assigned to [armooo](https://zulip-test.pagerduty.com/users/POBCFRJ)):\n\n``` quote\nfoo\n```"+ expected_message = "Incident [3](https://zulip-test.pagerduty.com/incidents/P140S4Y) triggered by [Test service](https://zulip-test.pagerduty.com/services/PIL5CUQ) (assigned to [armooo](https://zulip-test.pagerduty.com/users/POBCFRJ)).\n\n``` quote\nfoo\n```"self.check_webhook("trigger", "Incident 3", expected_message)def test_trigger_v2(self) -> None:- expected_message = "Incident [33](https://webdemo.pagerduty.com/incidents/PRORDTY) triggered by [Production XDB Cluster](https://webdemo.pagerduty.com/services/PN49J75) (assigned to [Laura Haley](https://webdemo.pagerduty.com/users/P553OPV)):\n\n``` quote\nMy new incident\n```"+ expected_message = "Incident [33](https://webdemo.pagerduty.com/incidents/PRORDTY) triggered by [Production XDB Cluster](https://webdemo.pagerduty.com/services/PN49J75) (assigned to [Laura Haley](https://webdemo.pagerduty.com/users/P553OPV)).\n\n``` quote\nMy new incident\n```"self.check_webhook("trigger_v2", "Incident 33", expected_message)def test_trigger_without_assignee_v2(self) -> None:- expected_message = "Incident [33](https://webdemo.pagerduty.com/incidents/PRORDTY) triggered by [Production XDB Cluster](https://webdemo.pagerduty.com/services/PN49J75) (assigned to nobody):\n\n``` quote\nMy new incident\n```"+ expected_message = "Incident [33](https://webdemo.pagerduty.com/incidents/PRORDTY) triggered by [Production XDB Cluster](https://webdemo.pagerduty.com/services/PN49J75) (assigned to nobody).\n\n``` quote\nMy new incident\n```"self.check_webhook("trigger_without_assignee_v2", "Incident 33", expected_message)def test_unacknowledge(self) -> None:- expected_message = "Incident [3](https://zulip-test.pagerduty.com/incidents/P140S4Y) unacknowledged by [Test service](https://zulip-test.pagerduty.com/services/PIL5CUQ) (assigned to [armooo](https://zulip-test.pagerduty.com/users/POBCFRJ)):\n\n``` quote\nfoo\n```"+ expected_message = "Incident [3](https://zulip-test.pagerduty.com/incidents/P140S4Y) unacknowledged by [Test service](https://zulip-test.pagerduty.com/services/PIL5CUQ) (assigned to [armooo](https://zulip-test.pagerduty.com/users/POBCFRJ)).\n\n``` quote\nfoo\n```"self.check_webhook("unacknowledge", "Incident 3", expected_message)def test_resolved(self) -> None:- expected_message = "Incident [1](https://zulip-test.pagerduty.com/incidents/PO1XIJ5) resolved by [armooo](https://zulip-test.pagerduty.com/users/POBCFRJ):\n\n``` quote\nIt is on fire\n```"+ expected_message = "Incident [1](https://zulip-test.pagerduty.com/incidents/PO1XIJ5) resolved by [armooo](https://zulip-test.pagerduty.com/users/POBCFRJ).\n\n``` quote\nIt is on fire\n```"self.check_webhook("resolved", "Incident 1", expected_message)def test_resolved_v2(self) -> None:- expected_message = "Incident [33](https://webdemo.pagerduty.com/incidents/PRORDTY) resolved by [Laura Haley](https://webdemo.pagerduty.com/users/P553OPV):\n\n``` quote\nMy new incident\n```"+ expected_message = "Incident [33](https://webdemo.pagerduty.com/incidents/PRORDTY) resolved by [Laura Haley](https://webdemo.pagerduty.com/users/P553OPV).\n\n``` quote\nMy new incident\n```"self.check_webhook("resolve_v2", "Incident 33", expected_message)def test_auto_resolved(self) -> None:- expected_message = "Incident [2](https://zulip-test.pagerduty.com/incidents/PX7K9J2) resolved:\n\n``` quote\nnew\n```"+ expected_message = "Incident [2](https://zulip-test.pagerduty.com/incidents/PX7K9J2) resolved.\n\n``` quote\nnew\n```"self.check_webhook("auto_resolved", "Incident 2", expected_message)def test_acknowledge(self) -> None:- expected_message = "Incident [1](https://zulip-test.pagerduty.com/incidents/PO1XIJ5) acknowledged by [armooo](https://zulip-test.pagerduty.com/users/POBCFRJ):\n\n``` quote\nIt is on fire\n```"+ expected_message = "Incident [1](https://zulip-test.pagerduty.com/incidents/PO1XIJ5) acknowledged by [armooo](https://zulip-test.pagerduty.com/users/POBCFRJ).\n\n``` quote\nIt is on fire\n```"self.check_webhook("acknowledge", "Incident 1", expected_message)def test_acknowledge_without_trigger_summary_data(self) -> None:- expected_message = "Incident [1](https://zulip-test.pagerduty.com/incidents/PO1XIJ5) acknowledged by [armooo](https://zulip-test.pagerduty.com/users/POBCFRJ):\n\n``` quote\n\n```"+ expected_message = "Incident [1](https://zulip-test.pagerduty.com/incidents/PO1XIJ5) acknowledged by [armooo](https://zulip-test.pagerduty.com/users/POBCFRJ).\n\n``` quote\n\n```"self.check_webhook("acknowledge_without_trigger_summary_data", "Incident 1", expected_message)def test_acknowledge_v2(self) -> None:- expected_message = "Incident [33](https://webdemo.pagerduty.com/incidents/PRORDTY) acknowledged by [Laura Haley](https://webdemo.pagerduty.com/users/P553OPV):\n\n``` quote\nMy new incident\n```"+ expected_message = "Incident [33](https://webdemo.pagerduty.com/incidents/PRORDTY) acknowledged by [Laura Haley](https://webdemo.pagerduty.com/users/P553OPV).\n\n``` quote\nMy new incident\n```"self.check_webhook("acknowledge_v2", "Incident 33", expected_message)def test_incident_assigned_v2(self) -> None:- expected_message = "Incident [33](https://webdemo.pagerduty.com/incidents/PRORDTY) assigned to [Wiley Jacobson](https://webdemo.pagerduty.com/users/PFBSJ2Z):\n\n``` quote\nMy new incident\n```"+ expected_message = "Incident [33](https://webdemo.pagerduty.com/incidents/PRORDTY) assigned to [Wiley Jacobson](https://webdemo.pagerduty.com/users/PFBSJ2Z).\n\n``` quote\nMy new incident\n```"self.check_webhook("assign_v2", "Incident 33", expected_message)def test_no_subject(self) -> None:- expected_message = "Incident [48219](https://dropbox.pagerduty.com/incidents/PJKGZF9) resolved:\n\n``` quote\nmp_error_block_down_critical\u2119\u01b4\n```"+ expected_message = "Incident [48219](https://dropbox.pagerduty.com/incidents/PJKGZF9) resolved.\n\n``` quote\nmp_error_block_down_critical\u2119\u01b4\n```"self.check_webhook("mp_fail", "Incident 48219", expected_message)diff --git a/zerver/webhooks/pagerduty/view.py b/zerver/webhooks/pagerduty/view.pyindex db446059826..defb3b4639a 100644--- a/zerver/webhooks/pagerduty/view.py+++ b/zerver/webhooks/pagerduty/view.py@@ -27,43 +27,37 @@ PAGER_DUTY_EVENT_NAMES_V2 = {"incident.assign": "assigned",}-ASSIGNEE_TEMPLATE = "[{username}]({url})"+AGENT_TEMPLATE = "[{username}]({url})"INCIDENT_WITH_SERVICE_AND_ASSIGNEE = ("Incident [{incident_num}]({incident_url}) {action} by [{service_name}]"- "({service_url}) (assigned to {assignee_info}):\n\n``` quote\n{trigger_message}\n```"+ "({service_url}) (assigned to {assignee_info}).\n\n{trigger_message}")+TRIGGER_MESSAGE = "``` quote\n{message}\n```"+INCIDENT_WITH_ASSIGNEE = """-Incident [{incident_num}]({incident_url}) {action} by {assignee_info}:+Incident [{incident_num}]({incident_url}) {action} by {assignee_info}.-``` quote{trigger_message}-```""".strip()INCIDENT_ASSIGNED = """-Incident [{incident_num}]({incident_url}) {action} to {assignee_info}:+Incident [{incident_num}]({incident_url}) {action} to {assignee_info}.-``` quote{trigger_message}-```""".strip()INCIDENT_RESOLVED_WITH_AGENT = """-Incident [{incident_num}]({incident_url}) resolved by {resolving_agent_info}:+Incident [{incident_num}]({incident_url}) resolved by {agent_info}.-``` quote{trigger_message}-```""".strip()INCIDENT_RESOLVED = """-Incident [{incident_num}]({incident_url}) resolved:+Incident [{incident_num}]({incident_url}) resolved.-``` quote{trigger_message}-```""".strip()@@ -80,7 +74,7 @@ def build_pagerduty_formatdict(message: Dict[str, Any]) -> Dict[str, Any]:if message["data"]["incident"].get("assigned_to_user", None):assigned_to_user = message["data"]["incident"]["assigned_to_user"]- format_dict["assignee_info"] = ASSIGNEE_TEMPLATE.format(+ format_dict["assignee_info"] = AGENT_TEMPLATE.format(username=assigned_to_user["email"].split("@")[0],url=assigned_to_user["html_url"],)@@ -89,7 +83,7 @@ def build_pagerduty_formatdict(message: Dict[str, Any]) -> Dict[str, Any]:if message["data"]["incident"].get("resolved_by_user", None):resolved_by_user = message["data"]["incident"]["resolved_by_user"]- format_dict["resolving_agent_info"] = ASSIGNEE_TEMPLATE.format(+ format_dict["agent_info"] = AGENT_TEMPLATE.format(username=resolved_by_user["email"].split("@")[0],url=resolved_by_user["html_url"],)@@ -105,7 +99,7 @@ def build_pagerduty_formatdict(message: Dict[str, Any]) -> Dict[str, Any]:if trigger_description:trigger_message.append(trigger_description)- format_dict["trigger_message"] = "\n".join(trigger_message)+ format_dict["trigger_message"] = TRIGGER_MESSAGE.format(message="\n".join(trigger_message))return format_dict@@ -123,7 +117,7 @@ def build_pagerduty_formatdict_v2(message: Dict[str, Any]) -> Dict[str, Any]:assignments = message["incident"]["assignments"]if assignments:assignee = assignments[0]["assignee"]- format_dict["assignee_info"] = ASSIGNEE_TEMPLATE.format(+ format_dict["assignee_info"] = AGENT_TEMPLATE.format(username=assignee["summary"], url=assignee["html_url"])else:@@ -131,14 +125,14 @@ def build_pagerduty_formatdict_v2(message: Dict[str, Any]) -> Dict[str, Any]:last_status_change_by = message["incident"].get("last_status_change_by")if last_status_change_by is not None:- format_dict["resolving_agent_info"] = ASSIGNEE_TEMPLATE.format(+ format_dict["agent_info"] = AGENT_TEMPLATE.format(username=last_status_change_by["summary"],url=last_status_change_by["html_url"],)trigger_description = message["incident"].get("description")if trigger_description is not None:- format_dict["trigger_message"] = trigger_description+ format_dict["trigger_message"] = TRIGGER_MESSAGE.format(message=trigger_description)return format_dict@@ -147,9 +141,9 @@ def send_formated_pagerduty() -> None:if message_type in ("incident.trigger", "incident.unacknowledge"):template = INCIDENT_WITH_SERVICE_AND_ASSIGNEE- elif message_type == "incident.resolve" and format_dict.get("resolving_agent_info") is not None:+ elif message_type == "incident.resolve" and format_dict.get("agent_info") is not None:template = INCIDENT_RESOLVED_WITH_AGENT- elif message_type == "incident.resolve" and format_dict.get("resolving_agent_info") is None:+ elif message_type == "incident.resolve" and format_dict.get("agent_info") is None:template = INCIDENT_RESOLVEDelif message_type == "incident.assign":template = INCIDENT_ASSIGNED