2016-03-13 13:22:33 +01:00
|
|
|
from __future__ import absolute_import
|
2016-06-05 00:38:26 +02:00
|
|
|
|
2016-06-26 10:20:47 +02:00
|
|
|
from six import text_type
|
|
|
|
from typing import Any, Mapping
|
2016-06-05 00:38:26 +02:00
|
|
|
|
2016-06-05 23:13:50 +02:00
|
|
|
from django.http import HttpRequest, HttpResponse
|
2016-06-05 00:38:26 +02:00
|
|
|
|
|
|
|
from zerver.models import get_client, UserProfile
|
2016-03-13 13:22:33 +01:00
|
|
|
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
|
2016-10-01 11:03:07 +02:00
|
|
|
from zerver.lib.webhooks.git import get_push_commits_event_message
|
2016-03-13 13:22:33 +01:00
|
|
|
|
|
|
|
|
2016-05-18 20:35:35 +02:00
|
|
|
@authenticated_rest_api_view(is_webhook=True)
|
2016-03-13 13:22:33 +01:00
|
|
|
@has_request_variables
|
|
|
|
def api_bitbucket_webhook(request, user_profile, payload=REQ(validator=check_dict([])),
|
|
|
|
stream=REQ(default='commits')):
|
2016-06-26 10:20:47 +02:00
|
|
|
# type: (HttpRequest, UserProfile, Mapping[text_type, Any], text_type) -> HttpResponse
|
2016-03-13 13:22:33 +01:00
|
|
|
repository = payload['repository']
|
2016-10-01 11:03:07 +02:00
|
|
|
|
|
|
|
commits = [
|
|
|
|
{
|
|
|
|
'sha': commit.get('raw_node'),
|
|
|
|
'message': commit.get('message'),
|
|
|
|
'url': u'{}{}commits/{}'.format(
|
|
|
|
payload.get('canon_url'),
|
|
|
|
repository.get('absolute_url'),
|
|
|
|
commit.get('raw_node'))
|
|
|
|
}
|
|
|
|
for commit in payload.get('commits')
|
|
|
|
]
|
2016-03-13 13:22:33 +01:00
|
|
|
|
|
|
|
subject = repository['name']
|
|
|
|
if len(commits) == 0:
|
|
|
|
# Bitbucket doesn't give us enough information to really give
|
|
|
|
# a useful message :/
|
2016-06-26 10:19:12 +02:00
|
|
|
content = (u"%s [force pushed](%s)"
|
2016-03-13 13:22:33 +01:00
|
|
|
% (payload['user'],
|
|
|
|
payload['canon_url'] + repository['absolute_url']))
|
|
|
|
else:
|
|
|
|
branch = payload['commits'][-1]['branch']
|
2016-10-01 11:03:07 +02:00
|
|
|
content = get_push_commits_event_message(payload.get('user'), None, branch, commits)
|
2016-06-26 10:19:12 +02:00
|
|
|
subject += u'/%s' % (branch,)
|
2016-03-13 13:22:33 +01:00
|
|
|
|
|
|
|
check_send_message(user_profile, get_client("ZulipBitBucketWebhook"), "stream",
|
|
|
|
[stream], subject, content)
|
|
|
|
return json_success()
|