2016-03-13 12:38:57 +01:00
|
|
|
# Webhooks for external integrations.
|
|
|
|
|
2017-11-16 00:43:10 +01:00
|
|
|
import base64
|
|
|
|
from functools import wraps
|
|
|
|
from typing import Any, Callable, Dict, Optional, Text, TypeVar
|
|
|
|
|
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
|
2017-09-30 04:18:16 +02:00
|
|
|
from zerver.lib.actions import check_send_stream_message
|
2018-03-13 17:44:46 +01:00
|
|
|
from zerver.lib.types import ViewFuncT
|
2017-10-31 04:25:48 +01:00
|
|
|
from zerver.lib.request import REQ, has_request_variables
|
2017-11-16 00:43:10 +01:00
|
|
|
from zerver.lib.response import json_success
|
2016-03-13 12:38:57 +01:00
|
|
|
from zerver.lib.validator import check_dict
|
2017-11-16 00:43:10 +01:00
|
|
|
from zerver.models import UserProfile, get_client
|
2016-11-23 20:15:23 +01:00
|
|
|
from zerver.webhooks.github.view import build_message_from_gitlog
|
2016-03-13 12:38:57 +01:00
|
|
|
|
|
|
|
# Beanstalk's web hook UI rejects url with a @ in the username section of a url
|
|
|
|
# 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)
|
2017-11-04 07:47:46 +01:00
|
|
|
def _wrapped_view_func(request: HttpRequest, *args: Any, **kwargs: Any) -> HttpResponse:
|
2016-03-13 12:38:57 +01:00
|
|
|
try:
|
2017-05-07 20:05:35 +02:00
|
|
|
auth_type, encoded_value = request.META['HTTP_AUTHORIZATION'].split() # type: str, str
|
2016-03-13 12:38:57 +01:00
|
|
|
if auth_type.lower() == "basic":
|
2017-12-21 20:59:50 +01:00
|
|
|
email, api_key = base64.b64decode(encoded_value).decode('utf-8').split(":")
|
2016-03-13 12:38:57 +01:00
|
|
|
email = email.replace('%40', '@')
|
2016-07-05 05:48:40 +02:00
|
|
|
credentials = u"%s:%s" % (email, api_key)
|
2017-12-21 20:59:50 +01:00
|
|
|
encoded_credentials = base64.b64encode(credentials.encode('utf-8')).decode('utf8') # type: str
|
2016-07-17 19:43:36 +02:00
|
|
|
request.META['HTTP_AUTHORIZATION'] = "Basic " + encoded_credentials
|
2016-07-05 05:48:40 +02:00
|
|
|
except Exception:
|
2016-03-13 12:38:57 +01:00
|
|
|
pass
|
|
|
|
|
|
|
|
return view_func(request, *args, **kwargs)
|
|
|
|
|
2017-05-07 20:05:35 +02:00
|
|
|
return _wrapped_view_func # type: ignore # https://github.com/python/mypy/issues/1927
|
2016-03-13 12:38:57 +01:00
|
|
|
|
|
|
|
@beanstalk_decoder
|
2016-05-18 20:35:35 +02:00
|
|
|
@authenticated_rest_api_view(is_webhook=True)
|
2016-03-13 12:38:57 +01:00
|
|
|
@has_request_variables
|
2017-12-18 15:08:27 +01:00
|
|
|
def api_beanstalk_webhook(request: HttpRequest, user_profile: UserProfile,
|
|
|
|
payload: Dict[str, Any]=REQ(validator=check_dict([])),
|
|
|
|
branches: Optional[Text]=REQ(default=None)) -> HttpResponse:
|
2016-03-13 12:38:57 +01:00
|
|
|
# Beanstalk supports both SVN and git repositories
|
|
|
|
# We distinguish between the two by checking for a
|
|
|
|
# 'uri' key that is only present for git repos
|
|
|
|
git_repo = 'uri' in payload
|
|
|
|
if git_repo:
|
2017-04-22 02:34:53 +02:00
|
|
|
if branches is not None and branches.find(payload['branch']) == -1:
|
|
|
|
return json_success()
|
2016-03-13 12:38:57 +01:00
|
|
|
# To get a linkable url,
|
2017-04-05 09:12:19 +02:00
|
|
|
for commit in payload['commits']:
|
2017-04-25 22:34:07 +02:00
|
|
|
commit['author'] = {'username': commit['author']['name']}
|
2017-04-05 09:12:19 +02:00
|
|
|
|
2016-03-13 12:38:57 +01:00
|
|
|
subject, content = build_message_from_gitlog(user_profile, payload['repository']['name'],
|
|
|
|
payload['ref'], payload['commits'],
|
|
|
|
payload['before'], payload['after'],
|
|
|
|
payload['repository']['url'],
|
|
|
|
payload['pusher_name'])
|
|
|
|
else:
|
|
|
|
author = payload.get('author_full_name')
|
|
|
|
url = payload.get('changeset_url')
|
|
|
|
revision = payload.get('revision')
|
2017-05-24 23:03:06 +02:00
|
|
|
(short_commit_msg, _, _) = payload['message'].partition("\n")
|
2016-03-13 12:38:57 +01:00
|
|
|
|
|
|
|
subject = "svn r%s" % (revision,)
|
|
|
|
content = "%s pushed [revision %s](%s):\n\n> %s" % (author, revision, url, short_commit_msg)
|
|
|
|
|
2017-09-30 04:18:16 +02:00
|
|
|
check_send_stream_message(user_profile, get_client("ZulipBeanstalkWebhook"),
|
|
|
|
"commits", subject, content)
|
2016-03-13 12:38:57 +01:00
|
|
|
return json_success()
|