2016-03-13 12:38:57 +01:00
|
|
|
# Webhooks for external integrations.
|
2019-04-26 01:16:38 +02:00
|
|
|
import re
|
2017-11-16 00:43:10 +01:00
|
|
|
|
2016-06-05 19:29:21 +02:00
|
|
|
from django.http import HttpRequest, HttpResponse
|
2023-08-12 09:34:31 +02:00
|
|
|
from pydantic import Json
|
2017-11-16 00:43:10 +01:00
|
|
|
|
2017-10-31 04:25:48 +01:00
|
|
|
from zerver.decorator import authenticated_rest_api_view
|
2017-11-16 00:43:10 +01:00
|
|
|
from zerver.lib.response import json_success
|
2023-08-12 09:34:31 +02:00
|
|
|
from zerver.lib.typed_endpoint import typed_endpoint
|
|
|
|
from zerver.lib.validator import WildValue, check_int, check_string
|
2018-03-13 23:43:02 +01:00
|
|
|
from zerver.lib.webhooks.common import check_send_webhook_message
|
2020-06-11 00:54:34 +02:00
|
|
|
from zerver.lib.webhooks.git import TOPIC_WITH_BRANCH_TEMPLATE, get_push_commits_event_message
|
2019-02-02 23:53:55 +01:00
|
|
|
from zerver.models import UserProfile
|
2019-04-26 01:16:38 +02:00
|
|
|
|
2020-01-14 22:06:24 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
def build_message_from_gitlog(
|
|
|
|
user_profile: UserProfile,
|
|
|
|
name: str,
|
|
|
|
ref: str,
|
2022-01-11 09:04:43 +01:00
|
|
|
commits: WildValue,
|
2021-02-12 08:19:30 +01:00
|
|
|
before: str,
|
|
|
|
after: str,
|
|
|
|
url: str,
|
|
|
|
pusher: str,
|
2024-07-12 02:30:23 +02:00
|
|
|
forced: str | None = None,
|
|
|
|
created: str | None = None,
|
2021-02-12 08:19:30 +01:00
|
|
|
deleted: bool = False,
|
2024-07-12 02:30:17 +02:00
|
|
|
) -> tuple[str, str]:
|
2021-02-12 08:20:45 +01:00
|
|
|
short_ref = re.sub(r"^refs/heads/", "", ref)
|
2024-01-17 15:53:30 +01:00
|
|
|
topic_name = TOPIC_WITH_BRANCH_TEMPLATE.format(repo=name, branch=short_ref)
|
2019-04-26 01:16:38 +02:00
|
|
|
|
2022-01-11 09:04:43 +01:00
|
|
|
commits_data = _transform_commits_list_to_common_format(commits)
|
|
|
|
content = get_push_commits_event_message(pusher, url, short_ref, commits_data, deleted=deleted)
|
2019-04-26 01:16:38 +02:00
|
|
|
|
2024-01-17 15:53:30 +01:00
|
|
|
return topic_name, content
|
2019-04-26 01:16:38 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2024-07-12 02:30:17 +02:00
|
|
|
def _transform_commits_list_to_common_format(commits: WildValue) -> list[dict[str, str]]:
|
2023-07-31 22:52:35 +02:00
|
|
|
return [
|
|
|
|
{
|
|
|
|
"name": commit["author"]["name"].tame(check_string),
|
|
|
|
"sha": commit["id"].tame(check_string),
|
|
|
|
"url": commit["url"].tame(check_string),
|
|
|
|
"message": commit["message"].tame(check_string),
|
|
|
|
}
|
|
|
|
for commit in commits
|
|
|
|
]
|
2016-03-13 12:38:57 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2022-05-12 07:53:37 +02:00
|
|
|
@authenticated_rest_api_view(
|
|
|
|
webhook_client_name="Beanstalk",
|
|
|
|
# Beanstalk's web hook UI rejects URL with a @ in the username section
|
|
|
|
# So we ask the user to replace them with %40
|
|
|
|
beanstalk_email_decode=True,
|
|
|
|
)
|
2023-08-12 09:34:31 +02:00
|
|
|
@typed_endpoint
|
2021-02-12 08:19:30 +01:00
|
|
|
def api_beanstalk_webhook(
|
|
|
|
request: HttpRequest,
|
|
|
|
user_profile: UserProfile,
|
2023-08-12 09:34:31 +02:00
|
|
|
*,
|
|
|
|
payload: Json[WildValue],
|
2024-07-12 02:30:23 +02:00
|
|
|
branches: str | None = None,
|
2021-02-12 08:19:30 +01:00
|
|
|
) -> HttpResponse:
|
2020-10-23 02:43:28 +02:00
|
|
|
# Beanstalk supports both SVN and Git repositories
|
2016-03-13 12:38:57 +01:00
|
|
|
# We distinguish between the two by checking for a
|
2020-10-23 02:43:28 +02:00
|
|
|
# 'uri' key that is only present for Git repos
|
2021-02-12 08:20:45 +01:00
|
|
|
git_repo = "uri" in payload
|
2016-03-13 12:38:57 +01:00
|
|
|
if git_repo:
|
2022-01-11 09:04:43 +01:00
|
|
|
if branches is not None and branches.find(payload["branch"].tame(check_string)) == -1:
|
2022-01-31 13:44:02 +01:00
|
|
|
return json_success(request)
|
2017-04-05 09:12:19 +02:00
|
|
|
|
2024-01-17 15:53:30 +01:00
|
|
|
topic_name, content = build_message_from_gitlog(
|
2021-02-12 08:19:30 +01:00
|
|
|
user_profile,
|
2022-01-11 09:04:43 +01:00
|
|
|
payload["repository"]["name"].tame(check_string),
|
|
|
|
payload["ref"].tame(check_string),
|
2021-02-12 08:20:45 +01:00
|
|
|
payload["commits"],
|
2022-01-11 09:04:43 +01:00
|
|
|
payload["before"].tame(check_string),
|
|
|
|
payload["after"].tame(check_string),
|
|
|
|
payload["repository"]["url"].tame(check_string),
|
|
|
|
payload["pusher_name"].tame(check_string),
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2016-03-13 12:38:57 +01:00
|
|
|
else:
|
2022-01-11 09:04:43 +01:00
|
|
|
author = payload["author_full_name"].tame(check_string)
|
|
|
|
url = payload["changeset_url"].tame(check_string)
|
|
|
|
revision = payload["revision"].tame(check_int)
|
|
|
|
(short_commit_msg, _, _) = payload["message"].tame(check_string).partition("\n")
|
2016-03-13 12:38:57 +01:00
|
|
|
|
2024-01-17 15:53:30 +01:00
|
|
|
topic_name = f"svn r{revision}"
|
2020-06-10 06:41:04 +02:00
|
|
|
content = f"{author} pushed [revision {revision}]({url}):\n\n> {short_commit_msg}"
|
2016-03-13 12:38:57 +01:00
|
|
|
|
2024-01-17 15:53:30 +01:00
|
|
|
check_send_webhook_message(request, user_profile, topic_name, content)
|
2022-01-31 13:44:02 +01:00
|
|
|
return json_success(request)
|