2018-11-01 20:56:22 +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
|
2019-02-02 23:53:55 +01:00
|
|
|
from zerver.lib.response import json_success
|
2023-09-27 19:01:31 +02:00
|
|
|
from zerver.lib.typed_endpoint import JsonBodyPayload, typed_endpoint
|
2023-08-12 09:34:31 +02:00
|
|
|
from zerver.lib.validator import WildValue, check_int, check_none_or, check_string
|
2020-06-11 00:54:34 +02:00
|
|
|
from zerver.lib.webhooks.common import (
|
|
|
|
check_send_webhook_message,
|
|
|
|
get_http_headers_from_filename,
|
|
|
|
validate_extract_webhook_http_header,
|
|
|
|
)
|
2018-11-01 20:56:22 +01:00
|
|
|
from zerver.models import UserProfile
|
|
|
|
|
|
|
|
REVIEW_REQUEST_PUBLISHED = """
|
|
|
|
**{user_name}** opened [#{id}: {review_request_title}]({review_request_url}):
|
|
|
|
"""
|
|
|
|
|
|
|
|
REVIEW_REQUEST_REOPENED = """
|
|
|
|
**{user_name}** reopened [#{id}: {review_request_title}]({review_request_url}):
|
|
|
|
"""
|
|
|
|
|
|
|
|
REVIEW_REQUEST_CLOSED = """
|
|
|
|
**{user_name}** closed [#{id}: {review_request_title}]({review_request_url}):
|
|
|
|
"""
|
|
|
|
|
|
|
|
REVIEW_PUBLISHED = """
|
|
|
|
**{user_name}** [reviewed]({review_url}) [#{id}: {review_request_title}]({review_request_url}):
|
|
|
|
|
|
|
|
**Review**:
|
|
|
|
``` quote
|
|
|
|
{review_body_top}
|
|
|
|
```
|
|
|
|
"""
|
|
|
|
|
|
|
|
REVIEW_REQUEST_DETAILS = """
|
|
|
|
``` quote
|
|
|
|
**Description**: {description}
|
|
|
|
**Status**: {status}
|
|
|
|
**Target people**: {target_people}
|
|
|
|
{extra_info}
|
|
|
|
```
|
|
|
|
"""
|
|
|
|
|
|
|
|
REPLY_PUBLISHED = """
|
|
|
|
**{user_name}** [replied]({reply_url}) to [#{id}: {review_request_title}]({review_request_url}):
|
|
|
|
|
|
|
|
**Reply**:
|
|
|
|
``` quote
|
|
|
|
{reply_body_top}
|
|
|
|
```
|
|
|
|
"""
|
|
|
|
|
|
|
|
BRANCH_TEMPLATE = "**Branch**: {branch_name}"
|
|
|
|
|
2019-07-04 21:10:16 +02:00
|
|
|
fixture_to_headers = get_http_headers_from_filename("HTTP_X_REVIEWBOARD_EVENT")
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2022-06-27 10:36:48 +02:00
|
|
|
def get_target_people_string(payload: WildValue) -> str:
|
2018-11-01 20:56:22 +01:00
|
|
|
result = ""
|
2021-02-12 08:20:45 +01:00
|
|
|
target_people = payload["review_request"]["target_people"]
|
2018-11-01 20:56:22 +01:00
|
|
|
if len(target_people) == 1:
|
2022-06-27 10:36:48 +02:00
|
|
|
result = "**{title}**".format(title=target_people[0]["title"].tame(check_string))
|
2018-11-01 20:56:22 +01:00
|
|
|
else:
|
2022-06-27 10:36:48 +02:00
|
|
|
for target_index in range(len(target_people) - 1):
|
|
|
|
result += "**{title}**, ".format(
|
|
|
|
title=target_people[target_index]["title"].tame(check_string)
|
|
|
|
)
|
|
|
|
result += "and **{title}**".format(title=target_people[-1]["title"].tame(check_string))
|
2018-11-01 20:56:22 +01:00
|
|
|
|
|
|
|
return result
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2022-06-27 10:36:48 +02:00
|
|
|
def get_review_published_body(payload: WildValue) -> str:
|
2018-11-01 20:56:22 +01:00
|
|
|
kwargs = {
|
2022-06-27 10:36:48 +02:00
|
|
|
"review_url": payload["review"]["absolute_url"].tame(check_string),
|
|
|
|
"id": payload["review_request"]["id"].tame(check_int),
|
|
|
|
"review_request_title": payload["review_request"]["summary"].tame(check_string),
|
|
|
|
"review_request_url": payload["review_request"]["absolute_url"].tame(check_string),
|
|
|
|
"user_name": payload["review"]["links"]["user"]["title"].tame(check_string),
|
|
|
|
"review_body_top": payload["review"]["body_top"].tame(check_string),
|
2018-11-01 20:56:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return REVIEW_PUBLISHED.format(**kwargs).strip()
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2022-06-27 10:36:48 +02:00
|
|
|
def get_reply_published_body(payload: WildValue) -> str:
|
2018-11-01 20:56:22 +01:00
|
|
|
kwargs = {
|
2022-06-27 10:36:48 +02:00
|
|
|
"reply_url": payload["reply"]["links"]["self"]["href"].tame(check_string),
|
|
|
|
"id": payload["review_request"]["id"].tame(check_int),
|
|
|
|
"review_request_title": payload["review_request"]["summary"].tame(check_string),
|
|
|
|
"review_request_url": payload["review_request"]["links"]["self"]["href"].tame(check_string),
|
|
|
|
"user_name": payload["reply"]["links"]["user"]["title"].tame(check_string),
|
|
|
|
"user_url": payload["reply"]["links"]["user"]["href"].tame(check_string),
|
|
|
|
"reply_body_top": payload["reply"]["body_top"].tame(check_string),
|
2018-11-01 20:56:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return REPLY_PUBLISHED.format(**kwargs).strip()
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2022-06-27 10:36:48 +02:00
|
|
|
def get_review_request_published_body(payload: WildValue) -> str:
|
2018-11-01 20:56:22 +01:00
|
|
|
kwargs = {
|
2022-06-27 10:36:48 +02:00
|
|
|
"id": payload["review_request"]["id"].tame(check_int),
|
|
|
|
"review_request_title": payload["review_request"]["summary"].tame(check_string),
|
|
|
|
"review_request_url": payload["review_request"]["absolute_url"].tame(check_string),
|
|
|
|
"user_name": payload["review_request"]["links"]["submitter"]["title"].tame(check_string),
|
|
|
|
"description": payload["review_request"]["description"].tame(check_string),
|
|
|
|
"status": payload["review_request"]["status"].tame(check_string),
|
2021-02-12 08:20:45 +01:00
|
|
|
"target_people": get_target_people_string(payload),
|
|
|
|
"extra_info": "",
|
2018-11-01 20:56:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
message = REVIEW_REQUEST_PUBLISHED + REVIEW_REQUEST_DETAILS
|
2022-06-27 10:36:48 +02:00
|
|
|
branch = payload["review_request"].get("branch").tame(check_none_or(check_string))
|
2018-11-01 20:56:22 +01:00
|
|
|
if branch and branch is not None:
|
|
|
|
branch_info = BRANCH_TEMPLATE.format(branch_name=branch)
|
2021-02-12 08:20:45 +01:00
|
|
|
kwargs["extra_info"] = branch_info
|
2018-11-01 20:56:22 +01:00
|
|
|
|
|
|
|
return message.format(**kwargs).strip()
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2022-06-27 10:36:48 +02:00
|
|
|
def get_review_request_reopened_body(payload: WildValue) -> str:
|
2018-11-01 20:56:22 +01:00
|
|
|
kwargs = {
|
2022-06-27 10:36:48 +02:00
|
|
|
"id": payload["review_request"]["id"].tame(check_int),
|
|
|
|
"review_request_title": payload["review_request"]["summary"].tame(check_string),
|
|
|
|
"review_request_url": payload["review_request"]["absolute_url"].tame(check_string),
|
|
|
|
"user_name": payload["reopened_by"]["username"].tame(check_string),
|
|
|
|
"description": payload["review_request"]["description"].tame(check_string),
|
|
|
|
"status": payload["review_request"]["status"].tame(check_string),
|
2021-02-12 08:20:45 +01:00
|
|
|
"target_people": get_target_people_string(payload),
|
|
|
|
"extra_info": "",
|
2018-11-01 20:56:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
message = REVIEW_REQUEST_REOPENED + REVIEW_REQUEST_DETAILS
|
2022-06-27 10:36:48 +02:00
|
|
|
branch = payload["review_request"].get("branch").tame(check_none_or(check_string))
|
2018-11-01 20:56:22 +01:00
|
|
|
if branch and branch is not None:
|
|
|
|
branch_info = BRANCH_TEMPLATE.format(branch_name=branch)
|
2021-02-12 08:20:45 +01:00
|
|
|
kwargs["extra_info"] = branch_info
|
2018-11-01 20:56:22 +01:00
|
|
|
|
|
|
|
return message.format(**kwargs).strip()
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2022-06-27 10:36:48 +02:00
|
|
|
def get_review_request_closed_body(payload: WildValue) -> str:
|
2018-11-01 20:56:22 +01:00
|
|
|
kwargs = {
|
2022-06-27 10:36:48 +02:00
|
|
|
"id": payload["review_request"]["id"].tame(check_int),
|
|
|
|
"review_request_title": payload["review_request"]["summary"].tame(check_string),
|
|
|
|
"review_request_url": payload["review_request"]["absolute_url"].tame(check_string),
|
|
|
|
"user_name": payload["closed_by"]["username"].tame(check_string),
|
|
|
|
"description": payload["review_request"]["description"].tame(check_string),
|
|
|
|
"status": payload["review_request"]["status"].tame(check_string),
|
2021-02-12 08:20:45 +01:00
|
|
|
"target_people": get_target_people_string(payload),
|
2022-06-27 10:36:48 +02:00
|
|
|
"extra_info": "**Close type**: {}".format(payload["close_type"].tame(check_string)),
|
2018-11-01 20:56:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
message = REVIEW_REQUEST_CLOSED + REVIEW_REQUEST_DETAILS
|
2022-06-27 10:36:48 +02:00
|
|
|
branch = payload["review_request"].get("branch").tame(check_none_or(check_string))
|
2018-11-01 20:56:22 +01:00
|
|
|
if branch and branch is not None:
|
|
|
|
branch_info = BRANCH_TEMPLATE.format(branch_name=branch)
|
2021-02-12 08:20:45 +01:00
|
|
|
kwargs["extra_info"] = "{}\n{}".format(kwargs["extra_info"], branch_info)
|
2018-11-01 20:56:22 +01:00
|
|
|
|
|
|
|
return message.format(**kwargs).strip()
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2022-06-27 10:36:48 +02:00
|
|
|
def get_review_request_repo_title(payload: WildValue) -> str:
|
|
|
|
return payload["review_request"]["links"]["repository"]["title"].tame(check_string)
|
2018-11-01 20:56:22 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2018-11-01 20:56:22 +01:00
|
|
|
RB_MESSAGE_FUNCTIONS = {
|
2021-02-12 08:20:45 +01:00
|
|
|
"review_request_published": get_review_request_published_body,
|
|
|
|
"review_request_reopened": get_review_request_reopened_body,
|
|
|
|
"review_request_closed": get_review_request_closed_body,
|
|
|
|
"review_published": get_review_published_body,
|
|
|
|
"reply_published": get_reply_published_body,
|
2018-11-01 20:56:22 +01:00
|
|
|
}
|
|
|
|
|
2021-07-16 11:40:46 +02:00
|
|
|
ALL_EVENT_TYPES = list(RB_MESSAGE_FUNCTIONS.keys())
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2021-07-16 11:40:46 +02:00
|
|
|
|
|
|
|
@webhook_view("ReviewBoard", all_event_types=ALL_EVENT_TYPES)
|
2023-08-12 09:34:31 +02:00
|
|
|
@typed_endpoint
|
2018-11-01 20:56:22 +01:00
|
|
|
def api_reviewboard_webhook(
|
2021-02-12 08:19:30 +01:00
|
|
|
request: HttpRequest,
|
|
|
|
user_profile: UserProfile,
|
2023-08-12 09:34:31 +02:00
|
|
|
*,
|
2023-09-27 19:01:31 +02:00
|
|
|
payload: JsonBodyPayload[WildValue],
|
2018-11-01 20:56:22 +01:00
|
|
|
) -> HttpResponse:
|
2021-12-17 05:12:13 +01:00
|
|
|
event_type = validate_extract_webhook_http_header(
|
2022-05-12 06:54:12 +02:00
|
|
|
request, "X-ReviewBoard-Event", "Review Board"
|
2021-12-17 05:12:13 +01:00
|
|
|
)
|
2019-02-01 02:35:10 +01:00
|
|
|
assert event_type is not None
|
2018-11-01 20:56:22 +01:00
|
|
|
|
|
|
|
body_function = RB_MESSAGE_FUNCTIONS.get(event_type)
|
|
|
|
if body_function is not None:
|
|
|
|
body = body_function(payload)
|
|
|
|
topic = get_review_request_repo_title(payload)
|
2021-07-16 11:40:46 +02:00
|
|
|
check_send_webhook_message(request, user_profile, topic, body, event_type)
|
2018-11-01 20:56:22 +01:00
|
|
|
else:
|
2022-11-17 09:30:48 +01:00
|
|
|
raise UnsupportedWebhookEventTypeError(event_type)
|
2018-11-01 20:56:22 +01:00
|
|
|
|
2022-01-31 13:44:02 +01:00
|
|
|
return json_success(request)
|