2016-03-13 13:15:21 +01:00
|
|
|
# Webhooks for external integrations.
|
2020-11-19 01:19:08 +01:00
|
|
|
from typing import Any, Dict
|
2016-05-25 15:02:02 +02:00
|
|
|
|
2016-06-05 23:18:47 +02:00
|
|
|
from django.http import HttpRequest, HttpResponse
|
2021-04-16 00:57:30 +02:00
|
|
|
from django.utils.translation import gettext as _
|
2016-05-25 15:02:02 +02:00
|
|
|
|
2020-08-20 00:32:15 +02:00
|
|
|
from zerver.decorator import webhook_view
|
2021-06-30 18:35:50 +02:00
|
|
|
from zerver.lib.exceptions import JsonableError
|
2017-10-31 04:25:48 +01:00
|
|
|
from zerver.lib.request import REQ, has_request_variables
|
2021-06-30 18:35:50 +02:00
|
|
|
from zerver.lib.response import json_success
|
2020-11-24 21:26:05 +01:00
|
|
|
from zerver.lib.webhooks.common import check_send_webhook_message, unix_milliseconds_to_timestamp
|
2019-02-02 23:53:55 +01:00
|
|
|
from zerver.models import UserProfile
|
2016-03-13 13:15:21 +01:00
|
|
|
|
2020-11-19 01:19:08 +01:00
|
|
|
OPEN_TEMPLATE = """
|
|
|
|
[Incident]({incident_url}) **opened** for condition: **{condition_name}** at <time:{iso_timestamp}>
|
2019-04-17 23:26:31 +02:00
|
|
|
``` quote
|
2020-11-19 01:19:08 +01:00
|
|
|
{details}
|
2019-04-17 23:26:31 +02:00
|
|
|
```
|
2020-11-19 01:19:08 +01:00
|
|
|
""".strip()
|
2019-04-17 23:26:31 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
DEFAULT_TEMPLATE = (
|
|
|
|
"""[Incident]({incident_url}) **{status}** {owner}for condition: **{condition_name}**""".strip()
|
|
|
|
)
|
2019-04-17 23:26:31 +02:00
|
|
|
|
2020-11-19 01:19:08 +01:00
|
|
|
TOPIC_TEMPLATE = """{policy_name} ({incident_id})""".strip()
|
2019-04-17 23:26:31 +02:00
|
|
|
|
2021-07-16 11:40:46 +02:00
|
|
|
ALL_EVENT_TYPES = ["closed", "acknowledged", "open"]
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2021-07-16 11:40:46 +02:00
|
|
|
|
|
|
|
@webhook_view("NewRelic", all_event_types=ALL_EVENT_TYPES)
|
2016-03-13 13:15:21 +01:00
|
|
|
@has_request_variables
|
2020-11-19 01:19:08 +01:00
|
|
|
def api_newrelic_webhook(
|
|
|
|
request: HttpRequest,
|
|
|
|
user_profile: UserProfile,
|
2021-02-12 08:20:45 +01:00
|
|
|
payload: Dict[str, Any] = REQ(argument_type="body"),
|
2020-11-19 01:19:08 +01:00
|
|
|
) -> HttpResponse:
|
|
|
|
|
|
|
|
info = {
|
2021-02-12 08:20:45 +01:00
|
|
|
"condition_name": payload.get("condition_name", "Unknown condition"),
|
|
|
|
"details": payload.get("details", "No details."),
|
|
|
|
"incident_url": payload.get("incident_url", "https://alerts.newrelic.com"),
|
2021-02-12 08:19:30 +01:00
|
|
|
"incident_acknowledge_url": payload.get(
|
2021-02-12 08:20:45 +01:00
|
|
|
"incident_acknowledge_url", "https://alerts.newrelic.com"
|
2021-02-12 08:19:30 +01:00
|
|
|
),
|
2021-02-12 08:20:45 +01:00
|
|
|
"status": payload.get("current_state", "None"),
|
|
|
|
"iso_timestamp": "",
|
|
|
|
"owner": payload.get("owner", ""),
|
2020-11-19 01:19:08 +01:00
|
|
|
}
|
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
unix_time = payload.get("timestamp", None)
|
2020-11-19 01:19:08 +01:00
|
|
|
if unix_time is None:
|
2021-06-30 18:35:50 +02:00
|
|
|
raise JsonableError(_("The newrelic webhook requires timestamp in milliseconds"))
|
2020-11-19 01:19:08 +01:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
info["iso_timestamp"] = unix_milliseconds_to_timestamp(unix_time, "newrelic")
|
2020-11-19 01:19:08 +01:00
|
|
|
|
2020-12-13 21:52:38 +01:00
|
|
|
# Add formatting to the owner field if owner is present
|
2021-02-12 08:20:45 +01:00
|
|
|
if info["owner"] != "":
|
|
|
|
info["owner"] = "by **{}** ".format(info["owner"])
|
2020-12-13 21:52:38 +01:00
|
|
|
|
2020-11-19 01:19:08 +01:00
|
|
|
# These are the three promised current_state values
|
2021-02-12 08:20:45 +01:00
|
|
|
if "open" in info["status"]:
|
2020-11-19 01:19:08 +01:00
|
|
|
content = OPEN_TEMPLATE.format(**info)
|
2021-02-12 08:20:45 +01:00
|
|
|
elif "acknowledged" in info["status"]:
|
2020-11-19 01:19:08 +01:00
|
|
|
content = DEFAULT_TEMPLATE.format(**info)
|
2021-02-12 08:20:45 +01:00
|
|
|
elif "closed" in info["status"]:
|
2020-11-19 01:19:08 +01:00
|
|
|
content = DEFAULT_TEMPLATE.format(**info)
|
2016-03-13 13:15:21 +01:00
|
|
|
else:
|
2021-06-30 18:35:50 +02:00
|
|
|
raise JsonableError(
|
2021-02-12 08:19:30 +01:00
|
|
|
_("The newrelic webhook requires current_state be in [open|acknowledged|closed]")
|
|
|
|
)
|
2020-11-19 01:19:08 +01:00
|
|
|
|
|
|
|
topic_info = {
|
2021-02-12 08:20:45 +01:00
|
|
|
"policy_name": payload.get("policy_name", "Unknown Policy"),
|
|
|
|
"incident_id": payload.get("incident_id", "Unknown ID"),
|
2020-11-19 01:19:08 +01:00
|
|
|
}
|
|
|
|
topic = TOPIC_TEMPLATE.format(**topic_info)
|
2016-03-13 13:15:21 +01:00
|
|
|
|
2021-07-16 11:40:46 +02:00
|
|
|
check_send_webhook_message(request, user_profile, topic, content, info["status"])
|
2022-01-31 13:44:02 +01:00
|
|
|
return json_success(request)
|