2017-04-05 02:52:31 +02:00
|
|
|
from typing import Any, Mapping, Text, Optional
|
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
|
|
|
|
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
|
2016-03-13 13:22:33 +01:00
|
|
|
from zerver.lib.response import json_success
|
2017-10-31 04:25:48 +01:00
|
|
|
from zerver.lib.request import REQ, has_request_variables
|
2016-03-13 13:22:33 +01:00
|
|
|
from zerver.lib.validator import check_dict
|
2016-10-05 19:28:04 +02:00
|
|
|
from zerver.lib.webhooks.git import get_push_commits_event_message, SUBJECT_WITH_BRANCH_TEMPLATE
|
2017-10-31 04:25:48 +01:00
|
|
|
from zerver.models import get_client, UserProfile
|
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([])),
|
2017-04-05 02:52:31 +02:00
|
|
|
stream=REQ(default='commits'), branches=REQ(default=None)):
|
|
|
|
# type: (HttpRequest, UserProfile, Mapping[Text, Any], Text, Optional[Text]) -> HttpResponse
|
2016-03-13 13:22:33 +01:00
|
|
|
repository = payload['repository']
|
2016-10-01 11:03:07 +02:00
|
|
|
|
|
|
|
commits = [
|
|
|
|
{
|
2017-04-05 09:12:19 +02:00
|
|
|
'name': payload.get('user'),
|
2016-10-01 11:03:07 +02:00
|
|
|
'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'))
|
|
|
|
}
|
2017-05-24 23:03:06 +02:00
|
|
|
for commit in payload['commits']
|
2016-10-01 11:03:07 +02:00
|
|
|
]
|
2016-03-13 13:22:33 +01:00
|
|
|
|
|
|
|
if len(commits) == 0:
|
|
|
|
# Bitbucket doesn't give us enough information to really give
|
|
|
|
# a useful message :/
|
2016-10-05 19:28:04 +02:00
|
|
|
subject = repository['name']
|
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']
|
2017-04-05 02:52:31 +02:00
|
|
|
if branches is not None and branches.find(branch) == -1:
|
|
|
|
return json_success()
|
2017-05-24 23:03:06 +02:00
|
|
|
content = get_push_commits_event_message(payload['user'], None, branch, commits)
|
2016-10-05 19:28:04 +02:00
|
|
|
subject = SUBJECT_WITH_BRANCH_TEMPLATE.format(repo=repository['name'], branch=branch)
|
2016-03-13 13:22:33 +01:00
|
|
|
|
2017-09-30 04:18:16 +02:00
|
|
|
check_send_stream_message(user_profile, get_client("ZulipBitBucketWebhook"),
|
|
|
|
stream, subject, content)
|
2016-03-13 13:22:33 +01:00
|
|
|
return json_success()
|