2016-03-13 12:50:20 +01:00
|
|
|
# Webhooks for external integrations.
|
2017-11-16 00:43:10 +01:00
|
|
|
import re
|
2019-05-09 03:40:38 +02:00
|
|
|
import string
|
2020-01-14 22:06:24 +01:00
|
|
|
from typing import Any, Callable, Dict, List, Optional
|
2016-05-25 15:02:02 +02:00
|
|
|
|
2017-11-16 00:43:10 +01:00
|
|
|
from django.db.models import Q
|
2016-06-05 23:09:32 +02:00
|
|
|
from django.http import HttpRequest, HttpResponse
|
2016-03-13 12:50:20 +01:00
|
|
|
|
2020-08-20 00:32:15 +02:00
|
|
|
from zerver.decorator import webhook_view
|
2021-09-13 20:23:54 +02:00
|
|
|
from zerver.lib.exceptions import AnomalousWebhookPayload, UnsupportedWebhookEventType
|
2017-10-31 04:25:48 +01:00
|
|
|
from zerver.lib.request import REQ, has_request_variables
|
2019-02-02 23:53:55 +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
|
2018-12-07 00:05:57 +01:00
|
|
|
from zerver.models import Realm, UserProfile, get_user_by_delivery_email
|
2017-01-03 18:44:13 +01:00
|
|
|
|
|
|
|
IGNORED_EVENTS = [
|
2020-08-22 14:39:00 +02:00
|
|
|
"attachment_created",
|
|
|
|
"issuelink_created",
|
|
|
|
"issuelink_deleted",
|
2020-08-22 14:37:45 +02:00
|
|
|
"jira:version_released",
|
2020-08-22 15:15:07 +02:00
|
|
|
"jira:worklog_updated",
|
2020-08-22 14:39:00 +02:00
|
|
|
"sprint_closed",
|
|
|
|
"sprint_started",
|
|
|
|
"worklog_created",
|
|
|
|
"worklog_updated",
|
2017-01-03 18:44:13 +01:00
|
|
|
]
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2018-05-10 19:34:01 +02:00
|
|
|
def guess_zulip_user_from_jira(jira_username: str, realm: Realm) -> Optional[UserProfile]:
|
2016-03-13 12:50:20 +01:00
|
|
|
try:
|
|
|
|
# Try to find a matching user in Zulip
|
|
|
|
# We search a user's full name, short name,
|
|
|
|
# and beginning of email address
|
|
|
|
user = UserProfile.objects.filter(
|
2021-02-12 08:19:30 +01:00
|
|
|
Q(full_name__iexact=jira_username) | Q(email__istartswith=jira_username),
|
2017-01-24 07:06:13 +01:00
|
|
|
is_active=True,
|
2021-02-12 08:19:30 +01:00
|
|
|
realm=realm,
|
|
|
|
).order_by("id")[0]
|
2016-03-13 12:50:20 +01:00
|
|
|
return user
|
|
|
|
except IndexError:
|
|
|
|
return None
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2018-05-10 19:34:01 +02:00
|
|
|
def convert_jira_markup(content: str, realm: Realm) -> str:
|
2021-05-10 07:02:14 +02:00
|
|
|
# Attempt to do some simplistic conversion of Jira
|
2016-03-13 12:50:20 +01:00
|
|
|
# formatting to Markdown, for consumption in Zulip
|
|
|
|
|
|
|
|
# Jira uses *word* for bold, we use **word**
|
2021-02-12 08:20:45 +01:00
|
|
|
content = re.sub(r"\*([^\*]+)\*", r"**\1**", content)
|
2016-03-13 12:50:20 +01:00
|
|
|
|
|
|
|
# Jira uses {{word}} for monospacing, we use `word`
|
2021-02-12 08:20:45 +01:00
|
|
|
content = re.sub(r"{{([^\*]+?)}}", r"`\1`", content)
|
2016-03-13 12:50:20 +01:00
|
|
|
|
|
|
|
# Starting a line with bq. block quotes that line
|
2021-02-12 08:20:45 +01:00
|
|
|
content = re.sub(r"bq\. (.*)", r"> \1", content)
|
2016-03-13 12:50:20 +01:00
|
|
|
|
|
|
|
# Wrapping a block of code in {quote}stuff{quote} also block-quotes it
|
2021-02-12 08:20:45 +01:00
|
|
|
quote_re = re.compile(r"{quote}(.*?){quote}", re.DOTALL)
|
|
|
|
content = re.sub(quote_re, r"~~~ quote\n\1\n~~~", content)
|
2016-03-13 12:50:20 +01:00
|
|
|
|
|
|
|
# {noformat}stuff{noformat} blocks are just code blocks with no
|
|
|
|
# syntax highlighting
|
2021-02-12 08:20:45 +01:00
|
|
|
noformat_re = re.compile(r"{noformat}(.*?){noformat}", re.DOTALL)
|
|
|
|
content = re.sub(noformat_re, r"~~~\n\1\n~~~", content)
|
2016-03-13 12:50:20 +01:00
|
|
|
|
|
|
|
# Code blocks are delineated by {code[: lang]} {code}
|
2021-02-12 08:20:45 +01:00
|
|
|
code_re = re.compile(r"{code[^\n]*}(.*?){code}", re.DOTALL)
|
|
|
|
content = re.sub(code_re, r"~~~\n\1\n~~~", content)
|
2016-03-13 12:50:20 +01:00
|
|
|
|
|
|
|
# Links are of form: [https://www.google.com] or [Link Title|https://www.google.com]
|
|
|
|
# In order to support both forms, we don't match a | in bare links
|
2021-02-12 08:20:45 +01:00
|
|
|
content = re.sub(r"\[([^\|~]+?)\]", r"[\1](\1)", content)
|
2016-03-13 12:50:20 +01:00
|
|
|
|
2020-08-11 01:47:49 +02:00
|
|
|
# Full links which have a | are converted into a better Markdown link
|
2021-02-12 08:20:45 +01:00
|
|
|
full_link_re = re.compile(r"\[(?:(?P<title>[^|~]+)\|)(?P<url>[^\]]*)\]")
|
|
|
|
content = re.sub(full_link_re, r"[\g<title>](\g<url>)", content)
|
2016-03-13 12:50:20 +01:00
|
|
|
|
2021-05-10 07:02:14 +02:00
|
|
|
# Try to convert a Jira user mention of format [~username] into a
|
|
|
|
# Zulip user mention. We don't know the email, just the Jira username,
|
2016-03-13 12:50:20 +01:00
|
|
|
# so we naively guess at their Zulip account using this
|
|
|
|
if realm:
|
2021-02-12 08:20:45 +01:00
|
|
|
mention_re = re.compile("\\[~(.*?)\\]")
|
2016-03-13 12:50:20 +01:00
|
|
|
for username in mention_re.findall(content):
|
|
|
|
# Try to look up username
|
|
|
|
user_profile = guess_zulip_user_from_jira(username, realm)
|
|
|
|
if user_profile:
|
2020-06-09 00:25:09 +02:00
|
|
|
replacement = f"**{user_profile.full_name}**"
|
2016-03-13 12:50:20 +01:00
|
|
|
else:
|
2020-06-09 00:25:09 +02:00
|
|
|
replacement = f"**{username}**"
|
2016-03-13 12:50:20 +01:00
|
|
|
|
2020-06-09 00:25:09 +02:00
|
|
|
content = content.replace(f"[~{username}]", replacement)
|
2016-03-13 12:50:20 +01:00
|
|
|
|
|
|
|
return content
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
def get_in(payload: Dict[str, Any], keys: List[str], default: str = "") -> Any:
|
2016-12-31 15:57:50 +01:00
|
|
|
try:
|
|
|
|
for key in keys:
|
|
|
|
payload = payload[key]
|
|
|
|
except (AttributeError, KeyError, TypeError):
|
|
|
|
return default
|
|
|
|
return payload
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
|
|
|
def get_issue_string(
|
|
|
|
payload: Dict[str, Any], issue_id: Optional[str] = None, with_title: bool = False
|
|
|
|
) -> str:
|
2016-03-13 12:50:20 +01:00
|
|
|
# Guess the URL as it is not specified in the payload
|
|
|
|
# We assume that there is a /browse/BUG-### page
|
2020-10-23 02:43:28 +02:00
|
|
|
# from the REST URL of the issue itself
|
2016-12-31 15:57:50 +01:00
|
|
|
if issue_id is None:
|
|
|
|
issue_id = get_issue_id(payload)
|
|
|
|
|
2019-05-03 12:55:28 +02:00
|
|
|
if with_title:
|
2020-06-09 00:25:09 +02:00
|
|
|
text = f"{issue_id}: {get_issue_title(payload)}"
|
2019-05-03 12:55:28 +02:00
|
|
|
else:
|
|
|
|
text = issue_id
|
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
base_url = re.match(r"(.*)\/rest\/api/.*", get_in(payload, ["issue", "self"]))
|
2016-12-31 15:57:50 +01:00
|
|
|
if base_url and len(base_url.groups()):
|
2020-06-09 00:25:09 +02:00
|
|
|
return f"[{text}]({base_url.group(1)}/browse/{issue_id})"
|
2016-03-13 12:50:20 +01:00
|
|
|
else:
|
2019-05-03 12:55:28 +02:00
|
|
|
return text
|
2016-12-31 15:57:50 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2018-05-10 19:34:01 +02:00
|
|
|
def get_assignee_mention(assignee_email: str, realm: Realm) -> str:
|
2021-02-12 08:20:45 +01:00
|
|
|
if assignee_email != "":
|
2016-03-13 12:50:20 +01:00
|
|
|
try:
|
2018-12-07 00:05:57 +01:00
|
|
|
assignee_name = get_user_by_delivery_email(assignee_email, realm).full_name
|
2016-03-13 12:50:20 +01:00
|
|
|
except UserProfile.DoesNotExist:
|
2016-12-31 15:57:50 +01:00
|
|
|
assignee_name = assignee_email
|
2020-06-09 00:25:09 +02:00
|
|
|
return f"**{assignee_name}**"
|
2021-02-12 08:20:45 +01:00
|
|
|
return ""
|
2016-12-31 15:57:50 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2018-05-10 19:34:01 +02:00
|
|
|
def get_issue_author(payload: Dict[str, Any]) -> str:
|
2021-02-12 08:20:45 +01:00
|
|
|
return get_in(payload, ["user", "displayName"])
|
2016-12-31 15:57:50 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2018-05-10 19:34:01 +02:00
|
|
|
def get_issue_id(payload: Dict[str, Any]) -> str:
|
2021-02-12 08:20:45 +01:00
|
|
|
if "issue" not in payload:
|
2020-02-26 22:10:25 +01:00
|
|
|
# Some ancient version of Jira or one of its extensions posts
|
|
|
|
# comment_created events without an "issue" element. For
|
|
|
|
# these, the best we can do is extract the Jira-intenral
|
|
|
|
# issue number and use that in the topic.
|
|
|
|
#
|
|
|
|
# Users who want better formatting can upgrade Jira.
|
2021-02-12 08:20:45 +01:00
|
|
|
return payload["comment"]["self"].split("/")[-3]
|
2020-02-26 22:10:25 +01:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
return get_in(payload, ["issue", "key"])
|
2016-12-31 15:57:50 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2018-05-10 19:34:01 +02:00
|
|
|
def get_issue_title(payload: Dict[str, Any]) -> str:
|
2021-02-12 08:20:45 +01:00
|
|
|
if "issue" not in payload:
|
2020-02-26 22:10:25 +01:00
|
|
|
# Some ancient version of Jira or one of its extensions posts
|
|
|
|
# comment_created events without an "issue" element. For
|
|
|
|
# these, the best we can do is extract the Jira-intenral
|
|
|
|
# issue number and use that in the topic.
|
|
|
|
#
|
|
|
|
# Users who want better formatting can upgrade Jira.
|
2021-02-12 08:20:45 +01:00
|
|
|
return "Upgrade Jira to get the issue title here."
|
2020-02-26 22:10:25 +01:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
return get_in(payload, ["issue", "fields", "summary"])
|
2016-12-31 15:57:50 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2018-05-10 19:34:01 +02:00
|
|
|
def get_issue_subject(payload: Dict[str, Any]) -> str:
|
2020-06-09 00:25:09 +02:00
|
|
|
return f"{get_issue_id(payload)}: {get_issue_title(payload)}"
|
2016-12-31 15:57:50 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2018-05-10 19:34:01 +02:00
|
|
|
def get_sub_event_for_update_issue(payload: Dict[str, Any]) -> str:
|
2021-02-12 08:20:45 +01:00
|
|
|
sub_event = payload.get("issue_event_type_name", "")
|
|
|
|
if sub_event == "":
|
|
|
|
if payload.get("comment"):
|
|
|
|
return "issue_commented"
|
|
|
|
elif payload.get("transition"):
|
|
|
|
return "issue_transited"
|
2017-01-30 19:26:48 +01:00
|
|
|
return sub_event
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2018-05-10 19:34:01 +02:00
|
|
|
def get_event_type(payload: Dict[str, Any]) -> Optional[str]:
|
2021-02-12 08:20:45 +01:00
|
|
|
event = payload.get("webhookEvent")
|
|
|
|
if event is None and payload.get("transition"):
|
|
|
|
event = "jira:issue_updated"
|
2017-01-30 19:26:48 +01:00
|
|
|
return event
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2018-05-10 19:34:01 +02:00
|
|
|
def add_change_info(content: str, field: str, from_field: str, to_field: str) -> str:
|
2020-06-09 00:25:09 +02:00
|
|
|
content += f"* Changed {field}"
|
2017-01-30 19:26:48 +01:00
|
|
|
if from_field:
|
2020-06-09 00:25:09 +02:00
|
|
|
content += f" from **{from_field}**"
|
2017-01-30 19:26:48 +01:00
|
|
|
if to_field:
|
2020-06-09 00:25:09 +02:00
|
|
|
content += f" to {to_field}\n"
|
2017-01-30 19:26:48 +01:00
|
|
|
return content
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2018-05-10 19:34:01 +02:00
|
|
|
def handle_updated_issue_event(payload: Dict[str, Any], user_profile: UserProfile) -> str:
|
2016-12-31 15:57:50 +01:00
|
|
|
# Reassigned, commented, reopened, and resolved events are all bundled
|
|
|
|
# into this one 'updated' event type, so we try to extract the meaningful
|
|
|
|
# event that happened
|
2021-02-12 08:20:45 +01:00
|
|
|
issue_id = get_in(payload, ["issue", "key"])
|
2019-05-03 12:55:28 +02:00
|
|
|
issue = get_issue_string(payload, issue_id, True)
|
2016-03-13 12:50:20 +01:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
assignee_email = get_in(payload, ["issue", "fields", "assignee", "emailAddress"], "")
|
2017-07-14 01:29:03 +02:00
|
|
|
assignee_mention = get_assignee_mention(assignee_email, user_profile.realm)
|
2016-03-13 12:50:20 +01:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
if assignee_mention != "":
|
2020-06-09 00:25:09 +02:00
|
|
|
assignee_blurb = f" (assigned to {assignee_mention})"
|
2016-12-31 15:57:50 +01:00
|
|
|
else:
|
2021-02-12 08:20:45 +01:00
|
|
|
assignee_blurb = ""
|
2016-12-31 15:57:50 +01:00
|
|
|
|
2017-01-30 19:26:48 +01:00
|
|
|
sub_event = get_sub_event_for_update_issue(payload)
|
2021-02-12 08:20:45 +01:00
|
|
|
if "comment" in sub_event:
|
|
|
|
if sub_event == "issue_commented":
|
|
|
|
verb = "commented on"
|
|
|
|
elif sub_event == "issue_comment_edited":
|
|
|
|
verb = "edited a comment on"
|
2017-01-03 18:44:13 +01:00
|
|
|
else:
|
2021-02-12 08:20:45 +01:00
|
|
|
verb = "deleted a comment from"
|
2019-01-07 21:58:39 +01:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
if payload.get("webhookEvent") == "comment_created":
|
|
|
|
author = payload["comment"]["author"]["displayName"]
|
2019-01-07 21:58:39 +01:00
|
|
|
else:
|
|
|
|
author = get_issue_author(payload)
|
|
|
|
|
2020-06-09 00:25:09 +02:00
|
|
|
content = f"{author} {verb} {issue}{assignee_blurb}"
|
2021-02-12 08:20:45 +01:00
|
|
|
comment = get_in(payload, ["comment", "body"])
|
2017-01-03 18:44:13 +01:00
|
|
|
if comment:
|
|
|
|
comment = convert_jira_markup(comment, user_profile.realm)
|
2020-06-09 00:25:09 +02:00
|
|
|
content = f"{content}:\n\n``` quote\n{comment}\n```"
|
2019-05-09 03:40:38 +02:00
|
|
|
else:
|
2020-06-09 00:25:09 +02:00
|
|
|
content = f"{content}."
|
2017-01-03 18:44:13 +01:00
|
|
|
else:
|
2020-06-09 00:25:09 +02:00
|
|
|
content = f"{get_issue_author(payload)} updated {issue}{assignee_blurb}:\n\n"
|
2021-02-12 08:20:45 +01:00
|
|
|
changelog = get_in(payload, ["changelog"])
|
2016-03-13 12:50:20 +01:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
if changelog != "":
|
2017-01-03 18:44:13 +01:00
|
|
|
# Use the changelog to display the changes, whitelist types we accept
|
2021-02-12 08:20:45 +01:00
|
|
|
items = changelog.get("items")
|
2017-01-03 18:44:13 +01:00
|
|
|
for item in items:
|
2021-02-12 08:20:45 +01:00
|
|
|
field = item.get("field")
|
2016-03-13 12:50:20 +01:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
if field == "assignee" and assignee_mention != "":
|
2017-01-03 18:44:13 +01:00
|
|
|
target_field_string = assignee_mention
|
|
|
|
else:
|
|
|
|
# Convert a user's target to a @-mention if possible
|
2021-02-12 08:20:45 +01:00
|
|
|
target_field_string = "**{}**".format(item.get("toString"))
|
2016-12-31 15:57:50 +01:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
from_field_string = item.get("fromString")
|
2017-01-03 18:44:13 +01:00
|
|
|
if target_field_string or from_field_string:
|
2021-02-12 08:19:30 +01:00
|
|
|
content = add_change_info(
|
|
|
|
content, field, from_field_string, target_field_string
|
|
|
|
)
|
2017-01-30 19:26:48 +01:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
elif sub_event == "issue_transited":
|
|
|
|
from_field_string = get_in(payload, ["transition", "from_status"])
|
|
|
|
target_field_string = "**{}**".format(get_in(payload, ["transition", "to_status"]))
|
2017-01-30 19:26:48 +01:00
|
|
|
if target_field_string or from_field_string:
|
2021-02-12 08:20:45 +01:00
|
|
|
content = add_change_info(content, "status", from_field_string, target_field_string)
|
2016-12-31 15:57:50 +01:00
|
|
|
|
|
|
|
return content
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2019-02-17 21:10:10 +01:00
|
|
|
def handle_created_issue_event(payload: Dict[str, Any], user_profile: UserProfile) -> str:
|
2019-05-09 03:40:38 +02:00
|
|
|
template = """
|
|
|
|
{author} created {issue_string}:
|
|
|
|
|
|
|
|
* **Priority**: {priority}
|
|
|
|
* **Assignee**: {assignee}
|
|
|
|
""".strip()
|
|
|
|
|
|
|
|
return template.format(
|
|
|
|
author=get_issue_author(payload),
|
|
|
|
issue_string=get_issue_string(payload, with_title=True),
|
2021-02-12 08:20:45 +01:00
|
|
|
priority=get_in(payload, ["issue", "fields", "priority", "name"]),
|
|
|
|
assignee=get_in(payload, ["issue", "fields", "assignee", "displayName"], "no one"),
|
2016-12-31 15:57:50 +01:00
|
|
|
)
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2019-02-17 21:10:10 +01:00
|
|
|
def handle_deleted_issue_event(payload: Dict[str, Any], user_profile: UserProfile) -> str:
|
2019-05-09 03:40:38 +02:00
|
|
|
template = "{author} deleted {issue_string}{punctuation}"
|
|
|
|
title = get_issue_title(payload)
|
2021-02-12 08:20:45 +01:00
|
|
|
punctuation = "." if title[-1] not in string.punctuation else ""
|
2019-05-09 03:40:38 +02:00
|
|
|
return template.format(
|
|
|
|
author=get_issue_author(payload),
|
|
|
|
issue_string=get_issue_string(payload, with_title=True),
|
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
|
|
|
punctuation=punctuation,
|
2019-05-09 03:40:38 +02:00
|
|
|
)
|
2016-12-31 15:57:50 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2019-08-16 17:20:07 +02:00
|
|
|
def normalize_comment(comment: str) -> str:
|
|
|
|
# Here's how Jira escapes special characters in their payload:
|
|
|
|
# ,.?\\!\n\"'\n\\[]\\{}()\n@#$%^&*\n~`|/\\\\
|
|
|
|
# for some reason, as of writing this, ! has two '\' before it.
|
|
|
|
normalized_comment = comment.replace("\\!", "!")
|
|
|
|
return normalized_comment
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2019-08-16 17:20:07 +02:00
|
|
|
def handle_comment_created_event(payload: Dict[str, Any], user_profile: UserProfile) -> str:
|
2020-02-26 22:10:25 +01:00
|
|
|
title = get_issue_title(payload)
|
2021-02-12 08:20:45 +01:00
|
|
|
return '{author} commented on issue: *"{title}"\
|
|
|
|
*\n``` quote\n{comment}\n```\n'.format(
|
2021-02-12 08:19:30 +01:00
|
|
|
author=payload["comment"]["author"]["displayName"],
|
|
|
|
title=title,
|
|
|
|
comment=normalize_comment(payload["comment"]["body"]),
|
2019-08-16 17:20:07 +02:00
|
|
|
)
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2019-08-16 17:20:07 +02:00
|
|
|
def handle_comment_updated_event(payload: Dict[str, Any], user_profile: UserProfile) -> str:
|
2020-03-27 00:38:33 +01:00
|
|
|
title = get_issue_title(payload)
|
2021-02-12 08:20:45 +01:00
|
|
|
return '{author} updated their comment on issue: *"{title}"\
|
|
|
|
*\n``` quote\n{comment}\n```\n'.format(
|
2021-02-12 08:19:30 +01:00
|
|
|
author=payload["comment"]["author"]["displayName"],
|
|
|
|
title=title,
|
|
|
|
comment=normalize_comment(payload["comment"]["body"]),
|
2019-08-16 17:20:07 +02:00
|
|
|
)
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2019-08-16 17:20:07 +02:00
|
|
|
def handle_comment_deleted_event(payload: Dict[str, Any], user_profile: UserProfile) -> str:
|
2020-03-27 00:38:33 +01:00
|
|
|
title = get_issue_title(payload)
|
2021-02-12 08:20:45 +01:00
|
|
|
return '{author} deleted their comment on issue: *"{title}"\
|
|
|
|
*\n``` quote\n~~{comment}~~\n```\n'.format(
|
2021-02-12 08:19:30 +01:00
|
|
|
author=payload["comment"]["author"]["displayName"],
|
|
|
|
title=title,
|
|
|
|
comment=normalize_comment(payload["comment"]["body"]),
|
2019-08-16 17:20:07 +02:00
|
|
|
)
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2021-07-16 11:40:46 +02:00
|
|
|
JIRA_CONTENT_FUNCTION_MAPPER: Dict[str, Optional[Callable[[Dict[str, Any], UserProfile], str]]] = {
|
2019-02-17 21:10:10 +01:00
|
|
|
"jira:issue_created": handle_created_issue_event,
|
|
|
|
"jira:issue_deleted": handle_deleted_issue_event,
|
|
|
|
"jira:issue_updated": handle_updated_issue_event,
|
2019-08-16 17:20:07 +02:00
|
|
|
"comment_created": handle_comment_created_event,
|
|
|
|
"comment_updated": handle_comment_updated_event,
|
|
|
|
"comment_deleted": handle_comment_deleted_event,
|
2019-02-17 21:10:10 +01:00
|
|
|
}
|
|
|
|
|
2021-07-16 11:40:46 +02:00
|
|
|
ALL_EVENT_TYPES = list(JIRA_CONTENT_FUNCTION_MAPPER.keys())
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2021-07-16 11:40:46 +02:00
|
|
|
|
|
|
|
@webhook_view("Jira", all_event_types=ALL_EVENT_TYPES)
|
2016-12-31 15:57:50 +01:00
|
|
|
@has_request_variables
|
2021-02-12 08:19:30 +01:00
|
|
|
def api_jira_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:
|
2016-12-31 15:57:50 +01:00
|
|
|
|
2017-01-30 19:26:48 +01:00
|
|
|
event = get_event_type(payload)
|
2019-02-17 21:10:10 +01:00
|
|
|
if event in IGNORED_EVENTS:
|
2017-01-03 18:44:13 +01:00
|
|
|
return json_success()
|
2019-02-17 21:10:10 +01:00
|
|
|
|
2021-09-13 20:23:54 +02:00
|
|
|
if event is None:
|
|
|
|
raise AnomalousWebhookPayload()
|
|
|
|
|
2021-07-16 11:40:46 +02:00
|
|
|
if event is not None:
|
|
|
|
content_func = JIRA_CONTENT_FUNCTION_MAPPER.get(event)
|
2019-02-17 21:10:10 +01:00
|
|
|
|
2019-07-28 22:35:50 +02:00
|
|
|
if content_func is None:
|
2020-08-20 00:50:06 +02:00
|
|
|
raise UnsupportedWebhookEventType(event)
|
2016-03-13 12:50:20 +01:00
|
|
|
|
2020-03-27 14:19:00 +01:00
|
|
|
subject = get_issue_subject(payload)
|
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
|
|
|
content: str = content_func(payload, user_profile)
|
2019-02-17 21:10:10 +01:00
|
|
|
|
2021-07-16 11:40:46 +02:00
|
|
|
check_send_webhook_message(
|
|
|
|
request, user_profile, subject, content, event, unquote_url_parameters=True
|
|
|
|
)
|
2016-03-13 12:50:20 +01:00
|
|
|
return json_success()
|