2019-11-18 03:28:15 +01:00
|
|
|
# vim:fenc=utf-8
|
|
|
|
|
|
|
|
from django.http import HttpRequest, HttpResponse
|
|
|
|
|
2020-08-20 00:32:15 +02:00
|
|
|
from zerver.decorator import webhook_view
|
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_bool, check_int, check_string
|
|
|
|
from zerver.lib.webhooks.common import OptionalUserSpecifiedTopicStr, get_http_headers_from_filename
|
2019-11-18 21:07:30 +01:00
|
|
|
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)
|
2023-04-07 23:41:47 +02:00
|
|
|
target_branch = None
|
|
|
|
base_branch = None
|
|
|
|
if action != "edited":
|
2023-07-21 15:36:19 +02:00
|
|
|
if "head" in payload["pull_request"]:
|
|
|
|
target_branch = payload["pull_request"]["head"]["ref"].tame(check_string)
|
|
|
|
if "base" in payload["pull_request"]:
|
|
|
|
base_branch = payload["pull_request"]["base"]["ref"].tame(check_string)
|
2022-04-02 19:25:30 +02:00
|
|
|
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")
|
2023-08-12 09:34:31 +02:00
|
|
|
@typed_endpoint
|
2021-02-12 08:19:30 +01:00
|
|
|
def api_gitea_webhook(
|
|
|
|
request: HttpRequest,
|
|
|
|
user_profile: UserProfile,
|
2023-08-12 09:34:31 +02:00
|
|
|
*,
|
2023-09-27 19:01:31 +02:00
|
|
|
payload: JsonBodyPayload[WildValue],
|
2024-07-12 02:30:23 +02:00
|
|
|
branches: str | None = None,
|
2023-08-12 09:34:31 +02:00
|
|
|
user_specified_topic: OptionalUserSpecifiedTopicStr = None,
|
2021-02-12 08:19:30 +01:00
|
|
|
) -> 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,
|
|
|
|
)
|