2019-11-18 03:28:15 +01:00
|
|
|
# vim:fenc=utf-8
|
2022-04-02 19:25:30 +02:00
|
|
|
from typing import Optional
|
2019-11-18 03:28:15 +01:00
|
|
|
|
|
|
|
from django.http import HttpRequest, HttpResponse
|
|
|
|
|
2020-08-20 00:32:15 +02:00
|
|
|
from zerver.decorator import webhook_view
|
2019-11-18 03:28:15 +01:00
|
|
|
from zerver.lib.request import REQ, has_request_variables
|
2022-04-02 19:25:30 +02:00
|
|
|
from zerver.lib.validator import WildValue, check_bool, check_int, check_string, to_wild_value
|
2019-11-18 21:07:30 +01:00
|
|
|
from zerver.lib.webhooks.common import get_http_headers_from_filename
|
|
|
|
from zerver.lib.webhooks.git import get_pull_request_event_message
|
2019-11-18 03:28:15 +01:00
|
|
|
from zerver.models import UserProfile
|
2020-09-02 05:21:28 +02:00
|
|
|
|
2019-11-18 21:07:30 +01:00
|
|
|
# Gitea is a fork of Gogs, and so the webhook implementation is nearly the same.
|
|
|
|
from zerver.webhooks.gogs.view import gogs_webhook_main
|
2019-11-18 03:28:15 +01:00
|
|
|
|
2019-11-18 21:07:30 +01:00
|
|
|
fixture_to_headers = get_http_headers_from_filename("HTTP_X_GITEA_EVENT")
|
2019-11-18 03:28:15 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2022-04-02 19:25:30 +02:00
|
|
|
def format_pull_request_event(payload: WildValue, include_title: bool = False) -> str:
|
2021-02-12 08:20:45 +01:00
|
|
|
assignee = payload["pull_request"]["assignee"]
|
2019-11-18 03:28:15 +01:00
|
|
|
|
2022-04-02 19:25:30 +02:00
|
|
|
if payload["pull_request"]["merged"].tame(check_bool):
|
|
|
|
user_name = payload["pull_request"]["merged_by"]["username"].tame(check_string)
|
|
|
|
action = "merged"
|
|
|
|
else:
|
|
|
|
user_name = payload["pull_request"]["user"]["username"].tame(check_string)
|
|
|
|
action = payload["action"].tame(check_string)
|
2019-11-18 03:28:15 +01:00
|
|
|
|
2022-04-02 19:25:30 +02:00
|
|
|
url = payload["pull_request"]["html_url"].tame(check_string)
|
|
|
|
number = payload["pull_request"]["number"].tame(check_int)
|
|
|
|
target_branch = payload["pull_request"]["head"]["ref"].tame(check_string)
|
|
|
|
base_branch = payload["pull_request"]["base"]["ref"].tame(check_string)
|
|
|
|
title = payload["pull_request"]["title"].tame(check_string) if include_title else None
|
|
|
|
stringified_assignee = assignee["login"].tame(check_string) if assignee else None
|
|
|
|
|
|
|
|
return get_pull_request_event_message(
|
|
|
|
user_name=user_name,
|
|
|
|
action=action,
|
|
|
|
url=url,
|
|
|
|
number=number,
|
|
|
|
target_branch=target_branch,
|
|
|
|
base_branch=base_branch,
|
|
|
|
title=title,
|
|
|
|
assignee=stringified_assignee,
|
|
|
|
)
|
2019-11-18 03:28:15 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
@webhook_view("Gitea")
|
2019-11-18 03:28:15 +01:00
|
|
|
@has_request_variables
|
2021-02-12 08:19:30 +01:00
|
|
|
def api_gitea_webhook(
|
|
|
|
request: HttpRequest,
|
|
|
|
user_profile: UserProfile,
|
2022-04-02 19:25:30 +02:00
|
|
|
payload: WildValue = REQ(argument_type="body", converter=to_wild_value),
|
2021-02-12 08:19:30 +01:00
|
|
|
branches: Optional[str] = REQ(default=None),
|
|
|
|
user_specified_topic: Optional[str] = REQ("topic", default=None),
|
|
|
|
) -> HttpResponse:
|
|
|
|
return gogs_webhook_main(
|
2021-02-12 08:20:45 +01:00
|
|
|
"Gitea",
|
2022-05-12 06:54:12 +02:00
|
|
|
"X-Gitea-Event",
|
2021-02-12 08:19:30 +01:00
|
|
|
format_pull_request_event,
|
|
|
|
request,
|
|
|
|
user_profile,
|
|
|
|
payload,
|
|
|
|
branches,
|
|
|
|
user_specified_topic,
|
|
|
|
)
|