2018-05-11 01:43:34 +02:00
|
|
|
from typing import Any, Mapping, 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
|
|
|
|
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 13:22:33 +01:00
|
|
|
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
|
2018-11-09 20:59:15 +01:00
|
|
|
from zerver.lib.webhooks.git import TOPIC_WITH_BRANCH_TEMPLATE, \
|
2017-11-16 00:43:10 +01:00
|
|
|
get_push_commits_event_message
|
2019-02-02 23:53:55 +01:00
|
|
|
from zerver.models import UserProfile
|
2016-03-13 13:22:33 +01:00
|
|
|
|
2020-01-14 22:06:24 +01:00
|
|
|
|
2018-03-16 23:37:32 +01:00
|
|
|
@authenticated_rest_api_view(webhook_client_name="Bitbucket")
|
2016-03-13 13:22:33 +01:00
|
|
|
@has_request_variables
|
2017-12-14 10:32:37 +01:00
|
|
|
def api_bitbucket_webhook(request: HttpRequest, user_profile: UserProfile,
|
2018-05-11 01:43:34 +02:00
|
|
|
payload: Mapping[str, Any]=REQ(validator=check_dict([])),
|
|
|
|
branches: Optional[str]=REQ(default=None)) -> HttpResponse:
|
2016-03-13 13:22:33 +01:00
|
|
|
repository = payload['repository']
|
2016-10-01 11:03:07 +02:00
|
|
|
|
|
|
|
commits = [
|
|
|
|
{
|
2019-03-16 18:39:03 +01:00
|
|
|
'name': commit.get('author') or 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']
|
2019-04-19 22:02:41 +02:00
|
|
|
content = (u"%s [force pushed](%s)."
|
2019-03-21 20:39:47 +01:00
|
|
|
% (payload.get('user', 'Someone'),
|
2016-03-13 13:22:33 +01:00
|
|
|
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()
|
2019-03-16 18:39:03 +01:00
|
|
|
|
|
|
|
committer = payload.get('user')
|
|
|
|
content = get_push_commits_event_message(
|
|
|
|
committer if committer is not None else 'Someone',
|
|
|
|
None, branch, commits)
|
2018-11-09 20:59:15 +01:00
|
|
|
subject = TOPIC_WITH_BRANCH_TEMPLATE.format(repo=repository['name'], branch=branch)
|
2016-03-13 13:22:33 +01:00
|
|
|
|
2018-12-28 21:28:52 +01:00
|
|
|
check_send_webhook_message(request, user_profile, subject, content,
|
|
|
|
unquote_url_parameters=True)
|
2016-03-13 13:22:33 +01:00
|
|
|
return json_success()
|