2016-03-13 12:38:57 +01:00
|
|
|
# Webhooks for external integrations.
|
2017-11-16 00:43:10 +01:00
|
|
|
import base64
|
2019-04-26 01:16:38 +02:00
|
|
|
import re
|
2020-01-14 22:06:24 +01:00
|
|
|
from functools import wraps
|
2020-06-24 02:10:50 +02:00
|
|
|
from typing import Any, Dict, List, Optional, Tuple, cast
|
2017-11-16 00:43:10 +01:00
|
|
|
|
2016-06-05 19:29:21 +02:00
|
|
|
from django.http import HttpRequest, HttpResponse
|
2017-11-16 00:43:10 +01:00
|
|
|
|
2017-10-31 04:25:48 +01:00
|
|
|
from zerver.decorator import authenticated_rest_api_view
|
|
|
|
from zerver.lib.request import REQ, has_request_variables
|
2017-11-16 00:43:10 +01:00
|
|
|
from zerver.lib.response import json_success
|
2020-01-14 22:06:24 +01:00
|
|
|
from zerver.lib.types import ViewFuncT
|
|
|
|
from zerver.lib.validator import check_dict
|
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,
|
|
|
|
commits: List[Dict[str, str]],
|
|
|
|
before: str,
|
|
|
|
after: str,
|
|
|
|
url: str,
|
|
|
|
pusher: str,
|
|
|
|
forced: Optional[str] = None,
|
|
|
|
created: Optional[str] = None,
|
|
|
|
deleted: bool = False,
|
|
|
|
) -> Tuple[str, str]:
|
2021-02-12 08:20:45 +01:00
|
|
|
short_ref = re.sub(r"^refs/heads/", "", ref)
|
2019-04-26 01:16:38 +02:00
|
|
|
subject = TOPIC_WITH_BRANCH_TEMPLATE.format(repo=name, branch=short_ref)
|
|
|
|
|
|
|
|
commits = _transform_commits_list_to_common_format(commits)
|
|
|
|
content = get_push_commits_event_message(pusher, url, short_ref, commits, deleted=deleted)
|
|
|
|
|
|
|
|
return subject, content
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2019-04-26 01:16:38 +02:00
|
|
|
def _transform_commits_list_to_common_format(commits: List[Dict[str, Any]]) -> List[Dict[str, str]]:
|
|
|
|
new_commits_list = []
|
|
|
|
for commit in commits:
|
2021-02-12 08:19:30 +01:00
|
|
|
new_commits_list.append(
|
|
|
|
{
|
2021-02-12 08:20:45 +01:00
|
|
|
"name": commit["author"].get("username"),
|
|
|
|
"sha": commit.get("id"),
|
|
|
|
"url": commit.get("url"),
|
|
|
|
"message": commit.get("message"),
|
2021-02-12 08:19:30 +01:00
|
|
|
}
|
|
|
|
)
|
2019-04-26 01:16:38 +02:00
|
|
|
return new_commits_list
|
2016-03-13 12:38:57 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2020-10-23 02:43:28 +02:00
|
|
|
# Beanstalk's web hook UI rejects URL with a @ in the username section
|
2016-03-13 12:38:57 +01:00
|
|
|
# So we ask the user to replace them with %40
|
|
|
|
# We manually fix the username here before passing it along to @authenticated_rest_api_view
|
2017-11-04 07:47:46 +01:00
|
|
|
def beanstalk_decoder(view_func: ViewFuncT) -> ViewFuncT:
|
2016-03-13 12:38:57 +01:00
|
|
|
@wraps(view_func)
|
2020-06-24 02:10:50 +02:00
|
|
|
def _wrapped_view_func(request: HttpRequest, *args: object, **kwargs: object) -> HttpResponse:
|
2020-05-09 00:10:17 +02:00
|
|
|
auth_type: str
|
|
|
|
encoded_value: str
|
2021-02-12 08:20:45 +01:00
|
|
|
auth_type, encoded_value = request.META["HTTP_AUTHORIZATION"].split()
|
2018-09-22 22:09:41 +02:00
|
|
|
if auth_type.lower() == "basic":
|
2021-08-02 23:20:39 +02:00
|
|
|
email, api_key = base64.b64decode(encoded_value).decode().split(":")
|
2021-02-12 08:20:45 +01:00
|
|
|
email = email.replace("%40", "@")
|
2020-06-10 06:41:04 +02:00
|
|
|
credentials = f"{email}:{api_key}"
|
2021-08-02 23:20:39 +02:00
|
|
|
encoded_credentials: str = base64.b64encode(credentials.encode()).decode()
|
2021-02-12 08:20:45 +01:00
|
|
|
request.META["HTTP_AUTHORIZATION"] = "Basic " + encoded_credentials
|
2016-03-13 12:38:57 +01:00
|
|
|
|
|
|
|
return view_func(request, *args, **kwargs)
|
|
|
|
|
2020-06-24 02:10:50 +02:00
|
|
|
return cast(ViewFuncT, _wrapped_view_func) # https://github.com/python/mypy/issues/1927
|
2016-03-13 12:38:57 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2016-03-13 12:38:57 +01:00
|
|
|
@beanstalk_decoder
|
2018-03-16 23:37:32 +01:00
|
|
|
@authenticated_rest_api_view(webhook_client_name="Beanstalk")
|
2016-03-13 12:38:57 +01:00
|
|
|
@has_request_variables
|
2021-02-12 08:19:30 +01:00
|
|
|
def api_beanstalk_webhook(
|
|
|
|
request: HttpRequest,
|
|
|
|
user_profile: UserProfile,
|
2021-04-07 22:00:44 +02:00
|
|
|
payload: Dict[str, Any] = REQ(json_validator=check_dict([])),
|
2021-02-12 08:19:30 +01:00
|
|
|
branches: Optional[str] = REQ(default=None),
|
|
|
|
) -> 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:
|
2021-02-12 08:20:45 +01:00
|
|
|
if branches is not None and branches.find(payload["branch"]) == -1:
|
2017-04-22 02:34:53 +02:00
|
|
|
return json_success()
|
2016-03-13 12:38:57 +01:00
|
|
|
# To get a linkable url,
|
2021-02-12 08:20:45 +01:00
|
|
|
for commit in payload["commits"]:
|
|
|
|
commit["author"] = {"username": commit["author"]["name"]}
|
2017-04-05 09:12:19 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
subject, content = build_message_from_gitlog(
|
|
|
|
user_profile,
|
2021-02-12 08:20:45 +01:00
|
|
|
payload["repository"]["name"],
|
|
|
|
payload["ref"],
|
|
|
|
payload["commits"],
|
|
|
|
payload["before"],
|
|
|
|
payload["after"],
|
|
|
|
payload["repository"]["url"],
|
|
|
|
payload["pusher_name"],
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2016-03-13 12:38:57 +01:00
|
|
|
else:
|
2021-02-12 08:20:45 +01:00
|
|
|
author = payload.get("author_full_name")
|
|
|
|
url = payload.get("changeset_url")
|
|
|
|
revision = payload.get("revision")
|
|
|
|
(short_commit_msg, _, _) = payload["message"].partition("\n")
|
2016-03-13 12:38:57 +01:00
|
|
|
|
2020-06-10 06:41:04 +02:00
|
|
|
subject = f"svn r{revision}"
|
|
|
|
content = f"{author} pushed [revision {revision}]({url}):\n\n> {short_commit_msg}"
|
2016-03-13 12:38:57 +01:00
|
|
|
|
2018-03-13 23:43:02 +01:00
|
|
|
check_send_webhook_message(request, user_profile, subject, content)
|
2016-03-13 12:38:57 +01:00
|
|
|
return json_success()
|