2020-01-14 22:06:24 +01:00
|
|
|
import time
|
2018-01-06 16:48:01 +01:00
|
|
|
|
|
|
|
from django.http import HttpRequest, HttpResponse
|
|
|
|
|
2020-08-20 00:32:15 +02:00
|
|
|
from zerver.decorator import webhook_view
|
2022-11-17 09:30:48 +01:00
|
|
|
from zerver.lib.exceptions import UnsupportedWebhookEventTypeError
|
2018-01-06 16:48:01 +01:00
|
|
|
from zerver.lib.request import REQ, has_request_variables
|
|
|
|
from zerver.lib.response import json_success
|
2022-06-27 11:08:02 +02:00
|
|
|
from zerver.lib.validator import (
|
|
|
|
WildValue,
|
|
|
|
check_anything,
|
|
|
|
check_int,
|
|
|
|
check_list,
|
|
|
|
check_string,
|
|
|
|
to_wild_value,
|
|
|
|
)
|
2020-08-19 22:14:40 +02:00
|
|
|
from zerver.lib.webhooks.common import check_send_webhook_message
|
2018-01-06 16:48:01 +01:00
|
|
|
from zerver.models import UserProfile
|
|
|
|
|
2021-07-16 11:40:46 +02:00
|
|
|
ALL_EVENT_TYPES = ["error_notification", "error_activity"]
|
2018-01-06 16:48:01 +01:00
|
|
|
|
2021-07-16 11:40:46 +02:00
|
|
|
|
|
|
|
@webhook_view("Raygun", all_event_types=ALL_EVENT_TYPES)
|
2018-01-06 16:48:01 +01:00
|
|
|
@has_request_variables
|
2021-02-12 08:19:30 +01:00
|
|
|
def api_raygun_webhook(
|
|
|
|
request: HttpRequest,
|
|
|
|
user_profile: UserProfile,
|
2022-06-27 11:08:02 +02:00
|
|
|
payload: WildValue = REQ(argument_type="body", converter=to_wild_value),
|
2021-02-12 08:19:30 +01:00
|
|
|
) -> HttpResponse:
|
2018-01-06 16:48:01 +01:00
|
|
|
# The payload contains 'event' key. This 'event' key has a value of either
|
|
|
|
# 'error_notification' or 'error_activity'. 'error_notification' happens
|
|
|
|
# when an error is caught in an application, where as 'error_activity'
|
|
|
|
# happens when an action is being taken for the error itself
|
|
|
|
# (ignored/resolved/assigned/etc.).
|
2022-06-27 11:08:02 +02:00
|
|
|
event = payload["event"].tame(check_string)
|
2018-01-06 16:48:01 +01:00
|
|
|
|
|
|
|
# Because we wanted to create a message for all of the payloads, it is best
|
|
|
|
# to handle them separately. This is because some payload keys don't exist
|
|
|
|
# in the other event.
|
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
if event == "error_notification":
|
2018-01-06 16:48:01 +01:00
|
|
|
message = compose_notification_message(payload)
|
2021-02-12 08:20:45 +01:00
|
|
|
elif event == "error_activity":
|
2018-01-06 16:48:01 +01:00
|
|
|
message = compose_activity_message(payload)
|
|
|
|
else:
|
2022-11-17 09:30:48 +01:00
|
|
|
raise UnsupportedWebhookEventTypeError(event)
|
2018-01-06 16:48:01 +01:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
topic = "test"
|
2018-03-16 22:53:50 +01:00
|
|
|
|
2021-07-16 11:40:46 +02:00
|
|
|
check_send_webhook_message(request, user_profile, topic, message, event)
|
2018-01-06 16:48:01 +01:00
|
|
|
|
2022-01-31 13:44:02 +01:00
|
|
|
return json_success(request)
|
2018-01-06 16:48:01 +01:00
|
|
|
|
|
|
|
|
2022-06-27 11:08:02 +02:00
|
|
|
def make_user_stats_chunk(error_dict: WildValue) -> str:
|
2018-01-06 16:48:01 +01:00
|
|
|
"""Creates a stat chunk about total occurrences and users affected for the
|
|
|
|
error.
|
|
|
|
|
|
|
|
Example: usersAffected: 2, totalOccurrences: 10
|
|
|
|
Output: 2 users affected with 10 total occurrences
|
|
|
|
|
|
|
|
:param error_dict: The error dictionary containing the error keys and
|
|
|
|
values
|
|
|
|
:returns: A message chunk that will be added to the main message
|
|
|
|
"""
|
2022-06-27 11:08:02 +02:00
|
|
|
users_affected = error_dict["usersAffected"].tame(check_int)
|
|
|
|
total_occurrences = error_dict["totalOccurrences"].tame(check_int)
|
2018-01-06 16:48:01 +01:00
|
|
|
|
|
|
|
# One line is subjectively better than two lines for this.
|
2020-06-10 06:40:53 +02:00
|
|
|
return f"* {users_affected} users affected with {total_occurrences} total occurrences\n"
|
2018-01-06 16:48:01 +01:00
|
|
|
|
|
|
|
|
2022-06-27 11:08:02 +02:00
|
|
|
def make_time_chunk(error_dict: WildValue) -> str:
|
2018-01-06 16:48:01 +01:00
|
|
|
"""Creates a time message chunk.
|
|
|
|
|
|
|
|
Example: firstOccurredOn: "X", lastOccurredOn: "Y"
|
|
|
|
Output:
|
|
|
|
First occurred: X
|
|
|
|
Last occurred: Y
|
|
|
|
|
|
|
|
:param error_dict: The error dictionary containing the error keys and
|
|
|
|
values
|
|
|
|
:returns: A message chunk that will be added to the main message
|
|
|
|
"""
|
|
|
|
# Make the timestamp more readable to a human.
|
2022-06-27 11:08:02 +02:00
|
|
|
time_first = parse_time(error_dict["firstOccurredOn"].tame(check_string))
|
|
|
|
time_last = parse_time(error_dict["lastOccurredOn"].tame(check_string))
|
2018-01-06 16:48:01 +01:00
|
|
|
|
|
|
|
# Provide time information about this error,
|
2020-06-10 06:40:53 +02:00
|
|
|
return f"* **First occurred**: {time_first}\n* **Last occurred**: {time_last}\n"
|
2018-01-06 16:48:01 +01:00
|
|
|
|
|
|
|
|
2018-05-10 19:34:01 +02:00
|
|
|
def make_message_chunk(message: str) -> str:
|
2018-01-06 16:48:01 +01:00
|
|
|
"""Creates a message chunk if exists.
|
|
|
|
|
|
|
|
Example: message: "This is an example message" returns "Message: This is an
|
|
|
|
example message". Whereas message: "" returns "".
|
|
|
|
|
|
|
|
:param message: The value of message inside of the error dictionary
|
|
|
|
:returns: A message chunk if there exists an additional message, otherwise
|
|
|
|
returns an empty string.
|
|
|
|
"""
|
|
|
|
# "Message" shouldn't be included if there is none supplied.
|
2020-06-09 00:25:09 +02:00
|
|
|
return f"* **Message**: {message}\n" if message != "" else ""
|
2018-01-06 16:48:01 +01:00
|
|
|
|
|
|
|
|
2022-06-27 11:08:02 +02:00
|
|
|
def make_app_info_chunk(app_dict: WildValue) -> str:
|
2018-01-06 16:48:01 +01:00
|
|
|
"""Creates a message chunk that contains the application info and the link
|
|
|
|
to the Raygun dashboard about the application.
|
|
|
|
|
|
|
|
:param app_dict: The application dictionary obtained from the payload
|
|
|
|
:returns: A message chunk that will be added to the main message
|
|
|
|
"""
|
2022-06-27 11:08:02 +02:00
|
|
|
app_name = app_dict["name"].tame(check_string)
|
|
|
|
app_url = app_dict["url"].tame(check_string)
|
2020-06-09 00:25:09 +02:00
|
|
|
return f"* **Application details**: [{app_name}]({app_url})\n"
|
2018-01-06 16:48:01 +01:00
|
|
|
|
|
|
|
|
2022-06-27 11:08:02 +02:00
|
|
|
def notification_message_follow_up(payload: WildValue) -> str:
|
2018-01-06 16:48:01 +01:00
|
|
|
"""Creates a message for a repeating error follow up
|
|
|
|
|
|
|
|
:param payload: Raygun payload
|
|
|
|
:return: Returns the message, somewhat beautifully formatted
|
|
|
|
"""
|
|
|
|
message = ""
|
|
|
|
|
|
|
|
# Link to Raygun about the follow up
|
2022-06-27 11:08:02 +02:00
|
|
|
followup_link_md = "[follow-up error]({})".format(payload["error"]["url"].tame(check_string))
|
2018-01-06 16:48:01 +01:00
|
|
|
|
2022-06-27 11:08:02 +02:00
|
|
|
followup_type = payload["eventType"].tame(check_string)
|
2018-01-06 16:48:01 +01:00
|
|
|
|
|
|
|
if followup_type == "HourlyFollowUp":
|
|
|
|
prefix = "Hourly"
|
|
|
|
else:
|
|
|
|
# Cut the "MinuteFollowUp" from the possible event types, then add "
|
|
|
|
# minute" after that. So prefix for "OneMinuteFollowUp" is "One
|
|
|
|
# minute", where "FiveMinuteFollowUp" is "Five minute".
|
2021-02-12 08:19:30 +01:00
|
|
|
prefix = followup_type[: len(followup_type) - 14] + " minute"
|
2018-01-06 16:48:01 +01:00
|
|
|
|
2020-06-09 00:25:09 +02:00
|
|
|
message += f"{prefix} {followup_link_md}:\n"
|
2018-01-06 16:48:01 +01:00
|
|
|
|
|
|
|
# Get the message of the error.
|
2022-06-27 11:08:02 +02:00
|
|
|
payload_msg = payload["error"]["message"].tame(check_string)
|
2018-01-06 16:48:01 +01:00
|
|
|
|
|
|
|
message += make_message_chunk(payload_msg)
|
2021-02-12 08:20:45 +01:00
|
|
|
message += make_time_chunk(payload["error"])
|
|
|
|
message += make_user_stats_chunk(payload["error"])
|
|
|
|
message += make_app_info_chunk(payload["application"])
|
2018-01-06 16:48:01 +01:00
|
|
|
|
|
|
|
return message
|
|
|
|
|
|
|
|
|
2022-06-27 11:08:02 +02:00
|
|
|
def notification_message_error_occurred(payload: WildValue) -> str:
|
2018-01-06 16:48:01 +01:00
|
|
|
"""Creates a message for a new error or reoccurred error
|
|
|
|
|
|
|
|
:param payload: Raygun payload
|
|
|
|
:return: Returns the message, somewhat beautifully formatted
|
|
|
|
"""
|
|
|
|
message = ""
|
|
|
|
|
|
|
|
# Provide a clickable link that goes to Raygun about this error.
|
2022-06-27 11:08:02 +02:00
|
|
|
error_link_md = "[Error]({})".format(payload["error"]["url"].tame(check_string))
|
2018-01-06 16:48:01 +01:00
|
|
|
|
|
|
|
# Stylize the message based on the event type of the error.
|
2022-06-27 11:08:02 +02:00
|
|
|
if payload["eventType"].tame(check_string) == "NewErrorOccurred":
|
2020-06-09 00:25:09 +02:00
|
|
|
message += "{}:\n".format(f"New {error_link_md} occurred")
|
2022-06-27 11:08:02 +02:00
|
|
|
elif payload["eventType"].tame(check_string) == "ErrorReoccurred":
|
2020-06-09 00:25:09 +02:00
|
|
|
message += "{}:\n".format(f"{error_link_md} reoccurred")
|
2018-01-06 16:48:01 +01:00
|
|
|
|
|
|
|
# Get the message of the error. This value can be empty (as in "").
|
2022-06-27 11:08:02 +02:00
|
|
|
payload_msg = payload["error"]["message"].tame(check_string)
|
2018-01-06 16:48:01 +01:00
|
|
|
|
|
|
|
message += make_message_chunk(payload_msg)
|
2021-02-12 08:20:45 +01:00
|
|
|
message += make_time_chunk(payload["error"])
|
|
|
|
message += make_user_stats_chunk(payload["error"])
|
2018-01-06 16:48:01 +01:00
|
|
|
|
|
|
|
# Only NewErrorOccurred and ErrorReoccurred contain an error instance.
|
2021-02-12 08:20:45 +01:00
|
|
|
error_instance = payload["error"]["instance"]
|
2018-01-06 16:48:01 +01:00
|
|
|
|
|
|
|
# Extract each of the keys and values in error_instance for easier handle
|
|
|
|
|
|
|
|
# Contains list of tags for the error. Can be empty (null)
|
2021-02-12 08:20:45 +01:00
|
|
|
tags = error_instance["tags"]
|
2018-01-06 16:48:01 +01:00
|
|
|
|
|
|
|
# Contains the identity of affected user at the moment this error
|
|
|
|
# happened. This surprisingly can be null. Somehow.
|
2021-02-12 08:20:45 +01:00
|
|
|
affected_user = error_instance["affectedUser"]
|
2018-01-06 16:48:01 +01:00
|
|
|
|
|
|
|
# Contains custom data for this particular error (if supplied). Can be
|
|
|
|
# null.
|
2021-02-12 08:20:45 +01:00
|
|
|
custom_data = error_instance["customData"]
|
2018-01-06 16:48:01 +01:00
|
|
|
|
|
|
|
if tags is not None:
|
2022-06-27 11:08:02 +02:00
|
|
|
message += "* **Tags**: {}\n".format(", ".join(tags.tame(check_list(check_string))))
|
2018-01-06 16:48:01 +01:00
|
|
|
|
|
|
|
if affected_user is not None:
|
2022-06-27 11:08:02 +02:00
|
|
|
user_uuid = affected_user["UUID"].tame(check_string)
|
2020-06-10 06:40:53 +02:00
|
|
|
message += f"* **Affected user**: {user_uuid[:6]}...{user_uuid[-5:]}\n"
|
2018-01-06 16:48:01 +01:00
|
|
|
|
|
|
|
if custom_data is not None:
|
|
|
|
# We don't know what the keys and values beforehand, so we are forced
|
|
|
|
# to iterate.
|
|
|
|
for key in sorted(custom_data.keys()):
|
2022-06-27 11:08:02 +02:00
|
|
|
message += f"* **{key}**: {custom_data[key].tame(check_anything)}\n"
|
2018-01-06 16:48:01 +01:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
message += make_app_info_chunk(payload["application"])
|
2018-01-06 16:48:01 +01:00
|
|
|
|
|
|
|
return message
|
|
|
|
|
|
|
|
|
2022-06-27 11:08:02 +02:00
|
|
|
def compose_notification_message(payload: WildValue) -> str:
|
2018-01-06 16:48:01 +01:00
|
|
|
"""Composes a message that contains information on the error
|
|
|
|
|
|
|
|
:param payload: Raygun payload
|
|
|
|
:return: Returns a response message
|
|
|
|
"""
|
|
|
|
|
|
|
|
# Get the event type of the error. This can be "NewErrorOccurred",
|
|
|
|
# "ErrorReoccurred", "OneMinuteFollowUp", "FiveMinuteFollowUp", ...,
|
|
|
|
# "HourlyFollowUp" for notification error.
|
2022-06-27 11:08:02 +02:00
|
|
|
event_type = payload["eventType"].tame(check_string)
|
2018-01-06 16:48:01 +01:00
|
|
|
|
|
|
|
# "NewErrorOccurred" and "ErrorReoccurred" contain error instance
|
|
|
|
# information, meaning that it has payload['error']['instance']. The other
|
|
|
|
# event type (the follow ups) doesn't have this instance.
|
|
|
|
|
|
|
|
# We now split this main function again into two functions. One is for
|
|
|
|
# "NewErrorOccurred" and "ErrorReoccurred", and one is for the rest. Both
|
|
|
|
# functions will return a text message that is formatted for the chat.
|
|
|
|
if event_type == "NewErrorOccurred" or event_type == "ErrorReoccurred":
|
|
|
|
return notification_message_error_occurred(payload)
|
|
|
|
elif "FollowUp" in event_type:
|
|
|
|
return notification_message_follow_up(payload)
|
|
|
|
else:
|
2022-11-17 09:30:48 +01:00
|
|
|
raise UnsupportedWebhookEventTypeError(event_type)
|
2018-01-06 16:48:01 +01:00
|
|
|
|
|
|
|
|
2022-06-27 11:08:02 +02:00
|
|
|
def activity_message(payload: WildValue) -> str:
|
2018-01-06 16:48:01 +01:00
|
|
|
"""Creates a message from an activity that is being taken for an error
|
|
|
|
|
|
|
|
:param payload: Raygun payload
|
|
|
|
:return: Returns the message, somewhat beautifully formatted
|
|
|
|
"""
|
|
|
|
message = ""
|
|
|
|
|
2022-06-27 11:08:02 +02:00
|
|
|
error_link_md = "[Error]({})".format(payload["error"]["url"].tame(check_string))
|
2018-01-06 16:48:01 +01:00
|
|
|
|
2022-06-27 11:08:02 +02:00
|
|
|
event_type = payload["eventType"].tame(check_string)
|
2018-01-06 16:48:01 +01:00
|
|
|
|
2022-06-27 11:08:02 +02:00
|
|
|
user = payload["error"]["user"].tame(check_string)
|
2018-01-06 16:48:01 +01:00
|
|
|
if event_type == "StatusChanged":
|
2022-06-27 11:08:02 +02:00
|
|
|
error_status = payload["error"]["status"].tame(check_string)
|
2020-06-10 06:40:53 +02:00
|
|
|
message += f"{error_link_md} status changed to **{error_status}** by {user}:\n"
|
2018-01-06 16:48:01 +01:00
|
|
|
elif event_type == "CommentAdded":
|
2022-06-27 11:08:02 +02:00
|
|
|
comment = payload["error"]["comment"].tame(check_string)
|
2020-06-10 06:40:53 +02:00
|
|
|
message += f"{user} commented on {error_link_md}:\n\n``` quote\n{comment}\n```\n"
|
2018-01-06 16:48:01 +01:00
|
|
|
elif event_type == "AssignedToUser":
|
2022-06-27 11:08:02 +02:00
|
|
|
assigned_to = payload["error"]["assignedTo"].tame(check_string)
|
2020-06-10 06:40:53 +02:00
|
|
|
message += f"{user} assigned {error_link_md} to {assigned_to}:\n"
|
2018-01-06 16:48:01 +01:00
|
|
|
|
2022-06-27 11:08:02 +02:00
|
|
|
message += "* **Timestamp**: {}\n".format(
|
|
|
|
parse_time(payload["error"]["activityDate"].tame(check_string))
|
|
|
|
)
|
2018-01-06 16:48:01 +01:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
message += make_app_info_chunk(payload["application"])
|
2018-01-06 16:48:01 +01:00
|
|
|
|
|
|
|
return message
|
|
|
|
|
|
|
|
|
2022-06-27 11:08:02 +02:00
|
|
|
def compose_activity_message(payload: WildValue) -> str:
|
2018-01-06 16:48:01 +01:00
|
|
|
"""Composes a message that contains an activity that is being taken to
|
|
|
|
an error, such as commenting, assigning an error to a user, ignoring the
|
|
|
|
error, etc.
|
|
|
|
|
|
|
|
:param payload: Raygun payload
|
|
|
|
:return: Returns a response message
|
|
|
|
"""
|
|
|
|
|
2022-06-27 11:08:02 +02:00
|
|
|
event_type = payload["eventType"].tame(check_string)
|
2018-01-06 16:48:01 +01:00
|
|
|
|
|
|
|
# Activity is separated into three main categories: status changes (
|
|
|
|
# ignores, resolved), error is assigned to user, and comment added to
|
|
|
|
# an error,
|
|
|
|
|
|
|
|
# But, they all are almost identical and the only differences between them
|
|
|
|
# are the keys at line 9 (check fixtures). So there's no need to split
|
|
|
|
# the function like the notification one.
|
2021-02-12 08:19:30 +01:00
|
|
|
if (
|
|
|
|
event_type == "StatusChanged"
|
|
|
|
or event_type == "AssignedToUser"
|
|
|
|
or event_type == "CommentAdded"
|
|
|
|
):
|
2018-01-06 16:48:01 +01:00
|
|
|
return activity_message(payload)
|
|
|
|
else:
|
2022-11-17 09:30:48 +01:00
|
|
|
raise UnsupportedWebhookEventTypeError(event_type)
|
2018-01-06 16:48:01 +01:00
|
|
|
|
|
|
|
|
2018-05-10 19:34:01 +02:00
|
|
|
def parse_time(timestamp: str) -> str:
|
2018-01-06 16:48:01 +01:00
|
|
|
"""Parses and returns the timestamp provided
|
|
|
|
|
|
|
|
:param timestamp: The timestamp provided by the payload
|
|
|
|
:returns: A string containing the time
|
|
|
|
"""
|
|
|
|
|
|
|
|
# Raygun provides two timestamp format, one with the Z at the end,
|
|
|
|
# and one without the Z.
|
|
|
|
|
|
|
|
format = "%Y-%m-%dT%H:%M:%S"
|
|
|
|
format += "Z" if timestamp[-1:] == "Z" else ""
|
2021-02-12 08:19:30 +01:00
|
|
|
parsed_time = time.strftime("%c", time.strptime(timestamp, format))
|
2018-01-06 16:48:01 +01:00
|
|
|
return parsed_time
|