2020-08-11 03:45:55 +02:00
|
|
|
import logging
|
2020-10-23 08:20:24 +02:00
|
|
|
from datetime import datetime
|
2020-06-11 00:54:34 +02:00
|
|
|
from typing import Any, Dict, List, Optional, Tuple
|
2020-10-23 08:20:24 +02:00
|
|
|
from urllib.parse import urljoin
|
2017-11-16 00:43:10 +01:00
|
|
|
|
2016-09-06 21:17:18 +02:00
|
|
|
from django.http import HttpRequest, HttpResponse
|
2017-10-31 04:25:48 +01:00
|
|
|
|
2020-08-20 00:32:15 +02:00
|
|
|
from zerver.decorator import webhook_view
|
2020-08-19 22:26:38 +02:00
|
|
|
from zerver.lib.exceptions import UnsupportedWebhookEventType
|
2017-10-31 04:25:48 +01:00
|
|
|
from zerver.lib.request import REQ, has_request_variables
|
2017-11-16 00:43:10 +01:00
|
|
|
from zerver.lib.response import json_success
|
2020-08-19 22:14:40 +02:00
|
|
|
from zerver.lib.webhooks.common import check_send_webhook_message
|
2017-10-31 04:25:48 +01:00
|
|
|
from zerver.models import UserProfile
|
2016-09-06 21:17:18 +02:00
|
|
|
|
2020-05-07 19:52:30 +02:00
|
|
|
DEPRECATED_EXCEPTION_MESSAGE_TEMPLATE = """
|
|
|
|
New [issue]({url}) (level: {level}):
|
|
|
|
|
|
|
|
``` quote
|
|
|
|
{message}
|
|
|
|
```
|
|
|
|
"""
|
|
|
|
|
2020-04-29 04:03:36 +02:00
|
|
|
MESSAGE_EVENT_TEMPLATE = """
|
|
|
|
**New message event:** [{title}]({web_link})
|
|
|
|
```quote
|
|
|
|
**level:** {level}
|
|
|
|
**timestamp:** {datetime}
|
2019-04-17 23:09:05 +02:00
|
|
|
```
|
2020-04-29 04:03:36 +02:00
|
|
|
"""
|
|
|
|
|
|
|
|
EXCEPTION_EVENT_TEMPLATE = """
|
|
|
|
**New exception:** [{title}]({web_link})
|
|
|
|
```quote
|
|
|
|
**level:** {level}
|
|
|
|
**timestamp:** {datetime}
|
|
|
|
**filename:** {filename}
|
|
|
|
```
|
|
|
|
"""
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
EXCEPTION_EVENT_TEMPLATE_WITH_TRACEBACK = (
|
|
|
|
EXCEPTION_EVENT_TEMPLATE
|
|
|
|
+ """
|
2020-04-29 04:03:36 +02:00
|
|
|
Traceback:
|
2020-08-11 03:44:48 +02:00
|
|
|
```{syntax_highlight_as}
|
2020-04-29 04:03:36 +02:00
|
|
|
{pre_context}---> {context_line}{post_context}\
|
|
|
|
```
|
|
|
|
"""
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2020-04-29 04:03:36 +02:00
|
|
|
# Because of the \n added at the end of each context element,
|
|
|
|
# this will actually look better in the traceback.
|
|
|
|
|
|
|
|
ISSUE_CREATED_MESSAGE_TEMPLATE = """
|
|
|
|
**New issue created:** {title}
|
|
|
|
```quote
|
|
|
|
**level:** {level}
|
|
|
|
**timestamp:** {datetime}
|
|
|
|
**assignee:** {assignee}
|
|
|
|
```
|
|
|
|
"""
|
|
|
|
|
|
|
|
ISSUE_ASSIGNED_MESSAGE_TEMPLATE = """
|
|
|
|
Issue **{title}** has now been assigned to **{assignee}** by **{actor}**.
|
|
|
|
"""
|
|
|
|
|
|
|
|
ISSUE_RESOLVED_MESSAGE_TEMPLATE = """
|
|
|
|
Issue **{title}** was marked as resolved by **{actor}**.
|
|
|
|
"""
|
|
|
|
|
|
|
|
ISSUE_IGNORED_MESSAGE_TEMPLATE = """
|
|
|
|
Issue **{title}** was ignored by **{actor}**.
|
|
|
|
"""
|
|
|
|
|
2020-08-11 03:44:48 +02:00
|
|
|
# Maps "platform" name provided by Sentry to the Pygments lexer name
|
|
|
|
syntax_highlight_as_map = {
|
2020-04-29 04:03:36 +02:00
|
|
|
"go": "go",
|
2020-08-11 03:48:43 +02:00
|
|
|
"java": "java",
|
|
|
|
"javascript": "javascript",
|
2020-04-29 04:03:36 +02:00
|
|
|
"node": "javascript",
|
|
|
|
"python": "python3",
|
2020-08-11 03:44:48 +02:00
|
|
|
}
|
2020-04-29 04:03:36 +02:00
|
|
|
|
|
|
|
|
|
|
|
def convert_lines_to_traceback_string(lines: Optional[List[str]]) -> str:
|
|
|
|
traceback = ""
|
|
|
|
if lines is not None:
|
|
|
|
for line in lines:
|
2021-02-12 08:19:30 +01:00
|
|
|
if line == "":
|
2020-04-29 04:03:36 +02:00
|
|
|
traceback += "\n"
|
|
|
|
else:
|
2020-06-09 00:25:09 +02:00
|
|
|
traceback += f" {line}\n"
|
2020-04-29 04:03:36 +02:00
|
|
|
return traceback
|
|
|
|
|
|
|
|
|
|
|
|
def handle_event_payload(event: Dict[str, Any]) -> Tuple[str, str]:
|
2021-05-08 02:36:30 +02:00
|
|
|
"""Handle either an exception type event or a message type event payload."""
|
2020-04-29 04:03:36 +02:00
|
|
|
# We shouldn't support the officially deprecated Raven series of SDKs.
|
|
|
|
if int(event["version"]) < 7:
|
2020-08-20 00:50:06 +02:00
|
|
|
raise UnsupportedWebhookEventType("Raven SDK")
|
2020-04-29 04:03:36 +02:00
|
|
|
|
2021-01-13 02:26:13 +01:00
|
|
|
subject = event["title"]
|
2020-06-17 01:48:38 +02:00
|
|
|
platform_name = event["platform"]
|
2020-08-11 03:45:55 +02:00
|
|
|
syntax_highlight_as = syntax_highlight_as_map.get(platform_name, "")
|
|
|
|
if syntax_highlight_as == "": # nocoverage
|
2020-09-13 05:12:46 +02:00
|
|
|
logging.info("Unknown Sentry platform: %s", platform_name)
|
2020-06-17 01:48:38 +02:00
|
|
|
|
2020-04-29 04:03:36 +02:00
|
|
|
context = {
|
|
|
|
"title": subject,
|
|
|
|
"level": event["level"],
|
|
|
|
"web_link": event["web_url"],
|
python: Use trailing commas consistently.
Automatically generated by the following script, based on the output
of lint with flake8-comma:
import re
import sys
last_filename = None
last_row = None
lines = []
for msg in sys.stdin:
m = re.match(
r"\x1b\[35mflake8 \|\x1b\[0m \x1b\[1;31m(.+):(\d+):(\d+): (\w+)", msg
)
if m:
filename, row_str, col_str, err = m.groups()
row, col = int(row_str), int(col_str)
if filename == last_filename:
assert last_row != row
else:
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
with open(filename) as f:
lines = f.readlines()
last_filename = filename
last_row = row
line = lines[row - 1]
if err in ["C812", "C815"]:
lines[row - 1] = line[: col - 1] + "," + line[col - 1 :]
elif err in ["C819"]:
assert line[col - 2] == ","
lines[row - 1] = line[: col - 2] + line[col - 1 :].lstrip(" ")
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-10 05:23:40 +02:00
|
|
|
"datetime": event["datetime"].split(".")[0].replace("T", " "),
|
2020-04-29 04:03:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if "exception" in event:
|
|
|
|
# The event was triggered by a sentry.capture_exception() call
|
|
|
|
# (in the Python Sentry SDK) or something similar.
|
|
|
|
|
2020-08-17 23:56:53 +02:00
|
|
|
filename = event["metadata"].get("filename", None)
|
2020-04-29 04:03:36 +02:00
|
|
|
|
|
|
|
stacktrace = None
|
2020-08-31 21:12:13 +02:00
|
|
|
for value in reversed(event["exception"]["values"]):
|
2020-04-29 04:03:36 +02:00
|
|
|
if "stacktrace" in value:
|
|
|
|
stacktrace = value["stacktrace"]
|
|
|
|
break
|
|
|
|
|
2020-08-17 23:56:53 +02:00
|
|
|
if stacktrace and filename:
|
2020-04-29 04:03:36 +02:00
|
|
|
exception_frame = None
|
2020-08-17 22:40:14 +02:00
|
|
|
for frame in reversed(stacktrace["frames"]):
|
2020-08-17 23:56:53 +02:00
|
|
|
if frame.get("filename", None) == filename:
|
2020-04-29 04:03:36 +02:00
|
|
|
exception_frame = frame
|
|
|
|
break
|
|
|
|
|
2020-09-22 23:01:40 +02:00
|
|
|
if exception_frame and "context_line" in exception_frame:
|
|
|
|
pre_context = convert_lines_to_traceback_string(
|
|
|
|
exception_frame.get("pre_context", None)
|
|
|
|
)
|
2020-04-29 04:03:36 +02:00
|
|
|
context_line = exception_frame["context_line"] + "\n"
|
2020-09-22 23:01:40 +02:00
|
|
|
post_context = convert_lines_to_traceback_string(
|
|
|
|
exception_frame.get("post_context", None)
|
|
|
|
)
|
2020-04-29 04:03:36 +02:00
|
|
|
|
2020-09-03 05:32:15 +02:00
|
|
|
context.update(
|
|
|
|
syntax_highlight_as=syntax_highlight_as,
|
|
|
|
filename=filename,
|
|
|
|
pre_context=pre_context,
|
|
|
|
context_line=context_line,
|
|
|
|
post_context=post_context,
|
|
|
|
)
|
2020-04-29 04:03:36 +02:00
|
|
|
|
|
|
|
body = EXCEPTION_EVENT_TEMPLATE_WITH_TRACEBACK.format(**context)
|
|
|
|
return (subject, body)
|
|
|
|
|
2020-09-03 05:32:15 +02:00
|
|
|
context.update(filename=filename) # nocoverage
|
2020-04-29 04:03:36 +02:00
|
|
|
body = EXCEPTION_EVENT_TEMPLATE.format(**context) # nocoverage
|
|
|
|
return (subject, body) # nocoverage
|
|
|
|
|
|
|
|
elif "logentry" in event:
|
|
|
|
# The event was triggered by a sentry.capture_message() call
|
|
|
|
# (in the Python Sentry SDK) or something similar.
|
|
|
|
body = MESSAGE_EVENT_TEMPLATE.format(**context)
|
|
|
|
|
|
|
|
else:
|
2020-08-20 00:50:06 +02:00
|
|
|
raise UnsupportedWebhookEventType("unknown-event type")
|
2020-04-29 04:03:36 +02:00
|
|
|
|
|
|
|
return (subject, body)
|
|
|
|
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
def handle_issue_payload(
|
|
|
|
action: str, issue: Dict[str, Any], actor: Dict[str, Any]
|
|
|
|
) -> Tuple[str, str]:
|
2021-05-08 02:36:30 +02:00
|
|
|
"""Handle either an issue type event."""
|
2020-04-29 04:03:36 +02:00
|
|
|
subject = issue["title"]
|
|
|
|
datetime = issue["lastSeen"].split(".")[0].replace("T", " ")
|
|
|
|
|
|
|
|
if issue["assignedTo"]:
|
|
|
|
if issue["assignedTo"]["type"] == "team":
|
|
|
|
assignee = "team {}".format(issue["assignedTo"]["name"])
|
|
|
|
else:
|
|
|
|
assignee = issue["assignedTo"]["name"]
|
|
|
|
else:
|
|
|
|
assignee = "No one"
|
|
|
|
|
|
|
|
if action == "created":
|
|
|
|
context = {
|
|
|
|
"title": subject,
|
|
|
|
"level": issue["level"],
|
|
|
|
"datetime": datetime,
|
python: Use trailing commas consistently.
Automatically generated by the following script, based on the output
of lint with flake8-comma:
import re
import sys
last_filename = None
last_row = None
lines = []
for msg in sys.stdin:
m = re.match(
r"\x1b\[35mflake8 \|\x1b\[0m \x1b\[1;31m(.+):(\d+):(\d+): (\w+)", msg
)
if m:
filename, row_str, col_str, err = m.groups()
row, col = int(row_str), int(col_str)
if filename == last_filename:
assert last_row != row
else:
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
with open(filename) as f:
lines = f.readlines()
last_filename = filename
last_row = row
line = lines[row - 1]
if err in ["C812", "C815"]:
lines[row - 1] = line[: col - 1] + "," + line[col - 1 :]
elif err in ["C819"]:
assert line[col - 2] == ","
lines[row - 1] = line[: col - 2] + line[col - 1 :].lstrip(" ")
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-10 05:23:40 +02:00
|
|
|
"assignee": assignee,
|
2020-04-29 04:03:36 +02:00
|
|
|
}
|
|
|
|
body = ISSUE_CREATED_MESSAGE_TEMPLATE.format(**context)
|
|
|
|
|
|
|
|
elif action == "resolved":
|
|
|
|
context = {
|
|
|
|
"title": subject,
|
python: Use trailing commas consistently.
Automatically generated by the following script, based on the output
of lint with flake8-comma:
import re
import sys
last_filename = None
last_row = None
lines = []
for msg in sys.stdin:
m = re.match(
r"\x1b\[35mflake8 \|\x1b\[0m \x1b\[1;31m(.+):(\d+):(\d+): (\w+)", msg
)
if m:
filename, row_str, col_str, err = m.groups()
row, col = int(row_str), int(col_str)
if filename == last_filename:
assert last_row != row
else:
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
with open(filename) as f:
lines = f.readlines()
last_filename = filename
last_row = row
line = lines[row - 1]
if err in ["C812", "C815"]:
lines[row - 1] = line[: col - 1] + "," + line[col - 1 :]
elif err in ["C819"]:
assert line[col - 2] == ","
lines[row - 1] = line[: col - 2] + line[col - 1 :].lstrip(" ")
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-10 05:23:40 +02:00
|
|
|
"actor": actor["name"],
|
2020-04-29 04:03:36 +02:00
|
|
|
}
|
|
|
|
body = ISSUE_RESOLVED_MESSAGE_TEMPLATE.format(**context)
|
|
|
|
|
|
|
|
elif action == "assigned":
|
|
|
|
context = {
|
|
|
|
"title": subject,
|
|
|
|
"assignee": assignee,
|
python: Use trailing commas consistently.
Automatically generated by the following script, based on the output
of lint with flake8-comma:
import re
import sys
last_filename = None
last_row = None
lines = []
for msg in sys.stdin:
m = re.match(
r"\x1b\[35mflake8 \|\x1b\[0m \x1b\[1;31m(.+):(\d+):(\d+): (\w+)", msg
)
if m:
filename, row_str, col_str, err = m.groups()
row, col = int(row_str), int(col_str)
if filename == last_filename:
assert last_row != row
else:
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
with open(filename) as f:
lines = f.readlines()
last_filename = filename
last_row = row
line = lines[row - 1]
if err in ["C812", "C815"]:
lines[row - 1] = line[: col - 1] + "," + line[col - 1 :]
elif err in ["C819"]:
assert line[col - 2] == ","
lines[row - 1] = line[: col - 2] + line[col - 1 :].lstrip(" ")
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-10 05:23:40 +02:00
|
|
|
"actor": actor["name"],
|
2020-04-29 04:03:36 +02:00
|
|
|
}
|
|
|
|
body = ISSUE_ASSIGNED_MESSAGE_TEMPLATE.format(**context)
|
|
|
|
|
|
|
|
elif action == "ignored":
|
|
|
|
context = {
|
|
|
|
"title": subject,
|
python: Use trailing commas consistently.
Automatically generated by the following script, based on the output
of lint with flake8-comma:
import re
import sys
last_filename = None
last_row = None
lines = []
for msg in sys.stdin:
m = re.match(
r"\x1b\[35mflake8 \|\x1b\[0m \x1b\[1;31m(.+):(\d+):(\d+): (\w+)", msg
)
if m:
filename, row_str, col_str, err = m.groups()
row, col = int(row_str), int(col_str)
if filename == last_filename:
assert last_row != row
else:
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
with open(filename) as f:
lines = f.readlines()
last_filename = filename
last_row = row
line = lines[row - 1]
if err in ["C812", "C815"]:
lines[row - 1] = line[: col - 1] + "," + line[col - 1 :]
elif err in ["C819"]:
assert line[col - 2] == ","
lines[row - 1] = line[: col - 2] + line[col - 1 :].lstrip(" ")
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-10 05:23:40 +02:00
|
|
|
"actor": actor["name"],
|
2020-04-29 04:03:36 +02:00
|
|
|
}
|
|
|
|
body = ISSUE_IGNORED_MESSAGE_TEMPLATE.format(**context)
|
|
|
|
|
|
|
|
else:
|
2020-08-20 00:50:06 +02:00
|
|
|
raise UnsupportedWebhookEventType("unknown-issue-action type")
|
2020-04-29 04:03:36 +02:00
|
|
|
|
|
|
|
return (subject, body)
|
|
|
|
|
2019-04-17 23:09:05 +02:00
|
|
|
|
2020-05-07 19:52:30 +02:00
|
|
|
def handle_deprecated_payload(payload: Dict[str, Any]) -> Tuple[str, str]:
|
2021-02-12 08:20:45 +01:00
|
|
|
subject = "{}".format(payload.get("project_name"))
|
2020-05-07 19:52:30 +02:00
|
|
|
body = DEPRECATED_EXCEPTION_MESSAGE_TEMPLATE.format(
|
2021-02-12 08:20:45 +01:00
|
|
|
level=payload["level"].upper(),
|
|
|
|
url=payload.get("url"),
|
|
|
|
message=payload.get("message"),
|
2020-05-07 19:52:30 +02:00
|
|
|
)
|
|
|
|
return (subject, body)
|
|
|
|
|
|
|
|
|
2020-10-23 08:20:24 +02:00
|
|
|
def transform_webhook_payload(payload: Dict[str, Any]) -> Optional[Dict[str, Any]]:
|
|
|
|
"""Attempt to use webhook payload for the notification.
|
|
|
|
|
|
|
|
When the integration is configured as a webhook, instead of being added as
|
2021-05-10 07:02:14 +02:00
|
|
|
an internal integration, the payload is slightly different, but has all the
|
2020-10-23 08:20:24 +02:00
|
|
|
required information for sending a notification. We transform this payload to
|
|
|
|
look like the payload from a "properly configured" integration.
|
|
|
|
"""
|
2021-02-12 08:20:45 +01:00
|
|
|
event = payload.get("event", {})
|
2020-10-23 08:20:24 +02:00
|
|
|
# deprecated payloads don't have event_id
|
2021-02-12 08:20:45 +01:00
|
|
|
event_id = event.get("event_id")
|
2020-10-23 08:20:24 +02:00
|
|
|
if not event_id:
|
|
|
|
return None
|
|
|
|
|
2020-12-02 06:22:44 +01:00
|
|
|
event_path = f"events/{event_id}/"
|
2021-02-12 08:20:45 +01:00
|
|
|
event["web_url"] = urljoin(payload["url"], event_path)
|
|
|
|
timestamp = event.get("timestamp", event["received"])
|
|
|
|
event["datetime"] = datetime.fromtimestamp(timestamp).isoformat()
|
2020-10-23 08:20:24 +02:00
|
|
|
return payload
|
|
|
|
|
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
@webhook_view("Sentry")
|
2016-09-06 21:17:18 +02:00
|
|
|
@has_request_variables
|
2021-02-12 08:19:30 +01:00
|
|
|
def api_sentry_webhook(
|
|
|
|
request: HttpRequest,
|
|
|
|
user_profile: UserProfile,
|
|
|
|
payload: Dict[str, Any] = REQ(argument_type="body"),
|
|
|
|
) -> HttpResponse:
|
2020-05-07 19:52:30 +02:00
|
|
|
data = payload.get("data", None)
|
2020-04-29 04:03:36 +02:00
|
|
|
|
2020-10-23 08:20:24 +02:00
|
|
|
if data is None:
|
|
|
|
data = transform_webhook_payload(payload)
|
|
|
|
|
2020-04-29 04:03:36 +02:00
|
|
|
# We currently support two types of payloads: events and issues.
|
2020-05-07 19:52:30 +02:00
|
|
|
if data:
|
|
|
|
if "event" in data:
|
|
|
|
subject, body = handle_event_payload(data["event"])
|
|
|
|
elif "issue" in data:
|
|
|
|
subject, body = handle_issue_payload(payload["action"], data["issue"], payload["actor"])
|
|
|
|
else:
|
2020-08-20 00:50:06 +02:00
|
|
|
raise UnsupportedWebhookEventType(str(list(data.keys())))
|
2020-04-29 04:03:36 +02:00
|
|
|
else:
|
2020-05-07 19:52:30 +02:00
|
|
|
subject, body = handle_deprecated_payload(payload)
|
2019-04-17 23:09:05 +02:00
|
|
|
|
2018-03-16 22:53:50 +01:00
|
|
|
check_send_webhook_message(request, user_profile, subject, body)
|
2022-01-31 13:44:02 +01:00
|
|
|
return json_success(request)
|