diff --git a/zerver/views/webhooks/__init__.py b/zerver/views/webhooks/__init__.py index 0cc4d5f23e..5b16491348 100644 --- a/zerver/views/webhooks/__init__.py +++ b/zerver/views/webhooks/__init__.py @@ -353,58 +353,6 @@ def api_pivotal_webhook(request, user_profile): [stream], subject, content) return json_success() -# 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 -def beanstalk_decoder(view_func): - @wraps(view_func) - def _wrapped_view_func(request, *args, **kwargs): - try: - auth_type, encoded_value = request.META['HTTP_AUTHORIZATION'].split() - if auth_type.lower() == "basic": - email, api_key = base64.b64decode(encoded_value).split(":") - email = email.replace('%40', '@') - request.META['HTTP_AUTHORIZATION'] = "Basic %s" % (base64.b64encode("%s:%s" % (email, api_key))) - except: - pass - - return view_func(request, *args, **kwargs) - - return _wrapped_view_func - -@beanstalk_decoder -@authenticated_rest_api_view -@has_request_variables -def api_beanstalk_webhook(request, user_profile, - payload=REQ(validator=check_dict([]))): - # 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: - # To get a linkable url, - name = payload.get('repository').get('name') - 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') - (short_commit_msg, _, _) = payload.get('message').partition("\n") - - subject = "svn r%s" % (revision,) - content = "%s pushed [revision %s](%s):\n\n> %s" % (author, revision, url, short_commit_msg) - - check_send_message(user_profile, get_client("ZulipBeanstalkWebhook"), "stream", - ["commits"], subject, content) - return json_success() - # Desk.com's integrations all make the user supply a template, where it fills # in stuff like {{customer.name}} and posts the result as a "data" parameter. # There's no raw JSON for us to work from. Thus, it makes sense to just write diff --git a/zerver/views/webhooks/beanstalk.py b/zerver/views/webhooks/beanstalk.py new file mode 100644 index 0000000000..d0a6bc7875 --- /dev/null +++ b/zerver/views/webhooks/beanstalk.py @@ -0,0 +1,63 @@ +# Webhooks for external integrations. + +from __future__ import absolute_import +from zerver.models import get_client +from zerver.lib.actions import check_send_message +from zerver.lib.response import json_success +from zerver.lib.validator import check_dict +from zerver.decorator import REQ, has_request_variables, authenticated_rest_api_view + +import base64 +from functools import wraps + +from .github import build_message_from_gitlog + + + +# 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 +def beanstalk_decoder(view_func): + @wraps(view_func) + def _wrapped_view_func(request, *args, **kwargs): + try: + auth_type, encoded_value = request.META['HTTP_AUTHORIZATION'].split() + if auth_type.lower() == "basic": + email, api_key = base64.b64decode(encoded_value).split(":") + email = email.replace('%40', '@') + request.META['HTTP_AUTHORIZATION'] = "Basic %s" % (base64.b64encode("%s:%s" % (email, api_key))) + except: + pass + + return view_func(request, *args, **kwargs) + + return _wrapped_view_func + +@beanstalk_decoder +@authenticated_rest_api_view +@has_request_variables +def api_beanstalk_webhook(request, user_profile, + payload=REQ(validator=check_dict([]))): + # 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: + # To get a linkable url, + 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') + (short_commit_msg, _, _) = payload.get('message').partition("\n") + + subject = "svn r%s" % (revision,) + content = "%s pushed [revision %s](%s):\n\n> %s" % (author, revision, url, short_commit_msg) + + check_send_message(user_profile, get_client("ZulipBeanstalkWebhook"), "stream", + ["commits"], subject, content) + return json_success() diff --git a/zproject/urls.py b/zproject/urls.py index 49887f7d2b..1f210b928c 100644 --- a/zproject/urls.py +++ b/zproject/urls.py @@ -154,7 +154,7 @@ urlpatterns += patterns('zerver.views', url(r'^api/v1/fetch_google_client_id$', 'api_fetch_google_client_id'), # These are integration-specific web hook callbacks - url(r'^api/v1/external/beanstalk$', 'webhooks.api_beanstalk_webhook'), + url(r'^api/v1/external/beanstalk$', 'webhooks.beanstalk.api_beanstalk_webhook'), url(r'^api/v1/external/github$', 'webhooks.github.api_github_landing'), url(r'^api/v1/external/jira$', 'webhooks.api_jira_webhook'), url(r'^api/v1/external/pivotal$', 'webhooks.api_pivotal_webhook'),