2018-01-15 17:50:12 +01:00
|
|
|
# Webhooks for external integrations.
|
2020-01-14 22:06:24 +01:00
|
|
|
from typing import Any, Dict, List
|
2018-01-15 17:50:12 +01:00
|
|
|
|
|
|
|
from django.http import HttpRequest, HttpResponse
|
|
|
|
|
2020-08-20 00:32:15 +02:00
|
|
|
from zerver.decorator import webhook_view
|
2018-01-15 17:50:12 +01:00
|
|
|
from zerver.lib.request import REQ, has_request_variables
|
|
|
|
from zerver.lib.response import json_success
|
2018-03-16 22:53:50 +01:00
|
|
|
from zerver.lib.webhooks.common import check_send_webhook_message
|
2019-02-02 23:53:55 +01:00
|
|
|
from zerver.models import UserProfile
|
2018-01-15 17:50:12 +01:00
|
|
|
|
python: Convert assignment type annotations to Python 3.6 style.
This commit was split by tabbott; this piece covers the vast majority
of files in Zulip, but excludes scripts/, tools/, and puppet/ to help
ensure we at least show the right error messages for Xenial systems.
We can likely further refine the remaining pieces with some testing.
Generated by com2ann, with whitespace fixes and various manual fixes
for runtime issues:
- invoiced_through: Optional[LicenseLedger] = models.ForeignKey(
+ invoiced_through: Optional["LicenseLedger"] = models.ForeignKey(
-_apns_client: Optional[APNsClient] = None
+_apns_client: Optional["APNsClient"] = None
- notifications_stream: Optional[Stream] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
- signup_notifications_stream: Optional[Stream] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
+ notifications_stream: Optional["Stream"] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
+ signup_notifications_stream: Optional["Stream"] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
- author: Optional[UserProfile] = models.ForeignKey('UserProfile', blank=True, null=True, on_delete=CASCADE)
+ author: Optional["UserProfile"] = models.ForeignKey('UserProfile', blank=True, null=True, on_delete=CASCADE)
- bot_owner: Optional[UserProfile] = models.ForeignKey('self', null=True, on_delete=models.SET_NULL)
+ bot_owner: Optional["UserProfile"] = models.ForeignKey('self', null=True, on_delete=models.SET_NULL)
- default_sending_stream: Optional[Stream] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
- default_events_register_stream: Optional[Stream] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
+ default_sending_stream: Optional["Stream"] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
+ default_events_register_stream: Optional["Stream"] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
-descriptors_by_handler_id: Dict[int, ClientDescriptor] = {}
+descriptors_by_handler_id: Dict[int, "ClientDescriptor"] = {}
-worker_classes: Dict[str, Type[QueueProcessingWorker]] = {}
-queues: Dict[str, Dict[str, Type[QueueProcessingWorker]]] = {}
+worker_classes: Dict[str, Type["QueueProcessingWorker"]] = {}
+queues: Dict[str, Dict[str, Type["QueueProcessingWorker"]]] = {}
-AUTH_LDAP_REVERSE_EMAIL_SEARCH: Optional[LDAPSearch] = None
+AUTH_LDAP_REVERSE_EMAIL_SEARCH: Optional["LDAPSearch"] = None
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-22 01:09:50 +02:00
|
|
|
subject_types: Dict[str, List[List[str]]] = {
|
2021-02-12 08:20:45 +01:00
|
|
|
"app": [ # Object type name
|
|
|
|
["name"], # Title
|
|
|
|
["html_url"], # Automatically put into title
|
|
|
|
["language"], # Other properties.
|
|
|
|
["framework"],
|
2018-01-15 17:50:12 +01:00
|
|
|
],
|
2021-02-12 08:20:45 +01:00
|
|
|
"base": [
|
|
|
|
["title"],
|
|
|
|
["html_url"],
|
|
|
|
["#summary"],
|
|
|
|
["subject"],
|
2018-01-15 17:50:12 +01:00
|
|
|
],
|
2021-02-12 08:20:45 +01:00
|
|
|
"comment": [
|
|
|
|
[""],
|
|
|
|
["subject"],
|
2018-01-15 17:50:12 +01:00
|
|
|
],
|
2021-02-12 08:20:45 +01:00
|
|
|
"errorgroup": [
|
|
|
|
["E#{}", "number"],
|
|
|
|
["html_url"],
|
|
|
|
["last_occurrence:error"],
|
2018-01-15 17:50:12 +01:00
|
|
|
],
|
2021-02-12 08:20:45 +01:00
|
|
|
"error": [
|
|
|
|
[""],
|
2018-01-15 17:50:12 +01:00
|
|
|
['">**Most recent Occurrence**'],
|
2021-02-12 08:20:45 +01:00
|
|
|
["in {}", "extra/pathname"],
|
|
|
|
["!message"],
|
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
|
|
|
],
|
python: Convert assignment type annotations to Python 3.6 style.
This commit was split by tabbott; this piece covers the vast majority
of files in Zulip, but excludes scripts/, tools/, and puppet/ to help
ensure we at least show the right error messages for Xenial systems.
We can likely further refine the remaining pieces with some testing.
Generated by com2ann, with whitespace fixes and various manual fixes
for runtime issues:
- invoiced_through: Optional[LicenseLedger] = models.ForeignKey(
+ invoiced_through: Optional["LicenseLedger"] = models.ForeignKey(
-_apns_client: Optional[APNsClient] = None
+_apns_client: Optional["APNsClient"] = None
- notifications_stream: Optional[Stream] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
- signup_notifications_stream: Optional[Stream] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
+ notifications_stream: Optional["Stream"] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
+ signup_notifications_stream: Optional["Stream"] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
- author: Optional[UserProfile] = models.ForeignKey('UserProfile', blank=True, null=True, on_delete=CASCADE)
+ author: Optional["UserProfile"] = models.ForeignKey('UserProfile', blank=True, null=True, on_delete=CASCADE)
- bot_owner: Optional[UserProfile] = models.ForeignKey('self', null=True, on_delete=models.SET_NULL)
+ bot_owner: Optional["UserProfile"] = models.ForeignKey('self', null=True, on_delete=models.SET_NULL)
- default_sending_stream: Optional[Stream] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
- default_events_register_stream: Optional[Stream] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
+ default_sending_stream: Optional["Stream"] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
+ default_events_register_stream: Optional["Stream"] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
-descriptors_by_handler_id: Dict[int, ClientDescriptor] = {}
+descriptors_by_handler_id: Dict[int, "ClientDescriptor"] = {}
-worker_classes: Dict[str, Type[QueueProcessingWorker]] = {}
-queues: Dict[str, Dict[str, Type[QueueProcessingWorker]]] = {}
+worker_classes: Dict[str, Type["QueueProcessingWorker"]] = {}
+queues: Dict[str, Dict[str, Type["QueueProcessingWorker"]]] = {}
-AUTH_LDAP_REVERSE_EMAIL_SEARCH: Optional[LDAPSearch] = None
+AUTH_LDAP_REVERSE_EMAIL_SEARCH: Optional["LDAPSearch"] = None
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-22 01:09:50 +02:00
|
|
|
}
|
2018-01-15 17:50:12 +01:00
|
|
|
|
|
|
|
|
|
|
|
def get_value(_obj: Dict[str, Any], key: str) -> str:
|
2021-02-12 08:20:45 +01:00
|
|
|
for _key in key.lstrip("!").split("/"):
|
2018-01-15 17:50:12 +01:00
|
|
|
if _key in _obj.keys():
|
|
|
|
_obj = _obj[_key]
|
|
|
|
else:
|
2021-02-12 08:20:45 +01:00
|
|
|
return ""
|
2018-01-15 17:50:12 +01:00
|
|
|
return str(_obj)
|
|
|
|
|
|
|
|
|
|
|
|
def format_object(
|
|
|
|
obj: Dict[str, Any],
|
|
|
|
subject_type: str,
|
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
|
|
|
message: str,
|
2018-01-15 17:50:12 +01:00
|
|
|
) -> str:
|
|
|
|
if subject_type not in subject_types.keys():
|
|
|
|
return message
|
python: Convert assignment type annotations to Python 3.6 style.
This commit was split by tabbott; this piece covers the vast majority
of files in Zulip, but excludes scripts/, tools/, and puppet/ to help
ensure we at least show the right error messages for Xenial systems.
We can likely further refine the remaining pieces with some testing.
Generated by com2ann, with whitespace fixes and various manual fixes
for runtime issues:
- invoiced_through: Optional[LicenseLedger] = models.ForeignKey(
+ invoiced_through: Optional["LicenseLedger"] = models.ForeignKey(
-_apns_client: Optional[APNsClient] = None
+_apns_client: Optional["APNsClient"] = None
- notifications_stream: Optional[Stream] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
- signup_notifications_stream: Optional[Stream] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
+ notifications_stream: Optional["Stream"] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
+ signup_notifications_stream: Optional["Stream"] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
- author: Optional[UserProfile] = models.ForeignKey('UserProfile', blank=True, null=True, on_delete=CASCADE)
+ author: Optional["UserProfile"] = models.ForeignKey('UserProfile', blank=True, null=True, on_delete=CASCADE)
- bot_owner: Optional[UserProfile] = models.ForeignKey('self', null=True, on_delete=models.SET_NULL)
+ bot_owner: Optional["UserProfile"] = models.ForeignKey('self', null=True, on_delete=models.SET_NULL)
- default_sending_stream: Optional[Stream] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
- default_events_register_stream: Optional[Stream] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
+ default_sending_stream: Optional["Stream"] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
+ default_events_register_stream: Optional["Stream"] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
-descriptors_by_handler_id: Dict[int, ClientDescriptor] = {}
+descriptors_by_handler_id: Dict[int, "ClientDescriptor"] = {}
-worker_classes: Dict[str, Type[QueueProcessingWorker]] = {}
-queues: Dict[str, Dict[str, Type[QueueProcessingWorker]]] = {}
+worker_classes: Dict[str, Type["QueueProcessingWorker"]] = {}
+queues: Dict[str, Dict[str, Type["QueueProcessingWorker"]]] = {}
-AUTH_LDAP_REVERSE_EMAIL_SEARCH: Optional[LDAPSearch] = None
+AUTH_LDAP_REVERSE_EMAIL_SEARCH: Optional["LDAPSearch"] = None
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-22 01:09:50 +02:00
|
|
|
keys: List[List[str]] = subject_types[subject_type][1:]
|
2018-01-15 17:50:12 +01:00
|
|
|
title = subject_types[subject_type][0]
|
2021-02-12 08:20:45 +01:00
|
|
|
if title[0] != "":
|
|
|
|
title_str = ""
|
2018-01-15 17:50:12 +01:00
|
|
|
if len(title) > 1:
|
|
|
|
title_str = title[0].format(get_value(obj, title[1]))
|
|
|
|
else:
|
|
|
|
title_str = obj[title[0]]
|
2021-02-12 08:20:45 +01:00
|
|
|
if obj["html_url"] is not None:
|
|
|
|
url: str = obj["html_url"]
|
|
|
|
if "opbeat.com" not in url:
|
|
|
|
url = "https://opbeat.com/" + url.lstrip("/")
|
|
|
|
message += f"\n**[{title_str}]({url})**"
|
2018-01-15 17:50:12 +01:00
|
|
|
else:
|
2021-02-12 08:20:45 +01:00
|
|
|
message += f"\n**{title_str}**"
|
2018-01-15 17:50:12 +01:00
|
|
|
for key_list in keys:
|
|
|
|
if len(key_list) > 1:
|
|
|
|
value = key_list[0].format(get_value(obj, key_list[1]))
|
2021-02-12 08:20:45 +01:00
|
|
|
message += f"\n>{value}"
|
2018-01-15 17:50:12 +01:00
|
|
|
else:
|
|
|
|
key = key_list[0]
|
2021-02-12 08:20:45 +01:00
|
|
|
key_raw = key.lstrip("!").lstrip("#").lstrip('"')
|
|
|
|
if key_raw != "html_url" and key_raw != "subject" and ":" not in key_raw:
|
2018-01-15 17:50:12 +01:00
|
|
|
value = get_value(obj, key_raw)
|
2021-02-12 08:20:45 +01:00
|
|
|
if key.startswith("!"):
|
|
|
|
message += f"\n>{value}"
|
|
|
|
elif key.startswith("#"):
|
|
|
|
message += f"\n{value}"
|
2018-01-15 17:50:12 +01:00
|
|
|
elif key.startswith('"'):
|
2021-02-12 08:20:45 +01:00
|
|
|
message += f"\n{key_raw}"
|
2018-01-15 17:50:12 +01:00
|
|
|
else:
|
2021-02-12 08:20:45 +01:00
|
|
|
message += f"\n>{key}: {value}"
|
|
|
|
if key == "subject":
|
|
|
|
message = format_object(obj["subject"], obj["subject_type"], message + "\n")
|
|
|
|
if ":" in key:
|
|
|
|
value, value_type = key.split(":")
|
|
|
|
message = format_object(obj[value], value_type, message + "\n")
|
2018-01-15 17:50:12 +01:00
|
|
|
return message
|
|
|
|
|
|
|
|
|
2020-08-20 00:32:15 +02:00
|
|
|
@webhook_view("Opbeat")
|
2018-01-15 17:50:12 +01:00
|
|
|
@has_request_variables
|
2021-02-12 08:19:30 +01:00
|
|
|
def api_opbeat_webhook(
|
|
|
|
request: HttpRequest,
|
|
|
|
user_profile: UserProfile,
|
2021-02-12 08:20:45 +01:00
|
|
|
payload: Dict[str, Any] = REQ(argument_type="body"),
|
2021-02-12 08:19:30 +01:00
|
|
|
) -> HttpResponse:
|
2018-01-15 17:50:12 +01:00
|
|
|
"""
|
|
|
|
This uses the subject name from opbeat to make the subject,
|
|
|
|
and the summary from Opbeat as the message body, with
|
|
|
|
details about the object mentioned.
|
|
|
|
"""
|
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
message_subject = payload["title"]
|
2018-01-15 17:50:12 +01:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
message = format_object(payload, "base", "")
|
2018-01-15 17:50:12 +01:00
|
|
|
|
2018-03-16 22:53:50 +01:00
|
|
|
check_send_webhook_message(request, user_profile, message_subject, message)
|
2018-01-15 17:50:12 +01:00
|
|
|
return json_success()
|