2016-07-04 23:06:44 +02:00
|
|
|
# Webhooks for external integrations.
|
|
|
|
from __future__ import absolute_import
|
|
|
|
import re
|
|
|
|
from functools import partial
|
2016-11-09 16:05:45 +01:00
|
|
|
from six.moves import zip
|
2017-03-03 19:01:52 +01:00
|
|
|
from typing import Any, Callable, Dict, List, Optional, Text
|
2016-07-04 23:06:44 +02:00
|
|
|
from django.http import HttpRequest, HttpResponse
|
|
|
|
from django.utils.translation import ugettext as _
|
|
|
|
from zerver.lib.actions import check_send_message
|
|
|
|
from zerver.lib.response import json_success, json_error
|
|
|
|
from zerver.decorator import REQ, has_request_variables, api_key_only_webhook_view
|
|
|
|
from zerver.models import Client, UserProfile
|
2016-10-06 15:32:10 +02:00
|
|
|
from zerver.lib.webhooks.git import get_push_commits_event_message, SUBJECT_WITH_BRANCH_TEMPLATE,\
|
2016-10-11 18:55:39 +02:00
|
|
|
get_force_push_commits_event_message, get_remove_branch_event_message, get_pull_request_event_message,\
|
2016-11-09 16:05:45 +01:00
|
|
|
SUBJECT_WITH_PR_OR_ISSUE_INFO_TEMPLATE, get_issue_event_message, get_commits_comment_action_message,\
|
|
|
|
get_push_tag_event_message
|
2016-07-04 23:06:44 +02:00
|
|
|
|
|
|
|
|
|
|
|
BITBUCKET_SUBJECT_TEMPLATE = '{repository_name}'
|
2016-07-23 21:10:58 +02:00
|
|
|
USER_PART = 'User {display_name}(login: {username})'
|
2016-07-04 23:06:44 +02:00
|
|
|
|
2016-07-23 21:10:58 +02:00
|
|
|
BITBUCKET_FORK_BODY = USER_PART + ' forked the repository into [{fork_name}]({fork_url}).'
|
2016-07-04 23:06:44 +02:00
|
|
|
BITBUCKET_COMMIT_STATUS_CHANGED_BODY = '[System {key}]({system_url}) changed status of {commit_info} to {status}.'
|
2016-10-21 20:08:26 +02:00
|
|
|
|
2016-07-04 23:06:44 +02:00
|
|
|
|
|
|
|
PULL_REQUEST_SUPPORTED_ACTIONS = [
|
|
|
|
'approved',
|
|
|
|
'unapproved',
|
|
|
|
'created',
|
|
|
|
'updated',
|
|
|
|
'rejected',
|
2017-01-11 14:17:41 +01:00
|
|
|
'fulfilled',
|
2016-07-04 23:06:44 +02:00
|
|
|
'comment_created',
|
|
|
|
'comment_updated',
|
|
|
|
'comment_deleted',
|
|
|
|
]
|
|
|
|
|
|
|
|
class UnknownTriggerType(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
@api_key_only_webhook_view('Bitbucket2')
|
|
|
|
@has_request_variables
|
2016-07-23 21:10:58 +02:00
|
|
|
def api_bitbucket2_webhook(request, user_profile, client, payload=REQ(argument_type='body'),
|
|
|
|
stream=REQ(default='bitbucket')):
|
2016-07-04 23:06:44 +02:00
|
|
|
# type: (HttpRequest, UserProfile, Client, Dict[str, Any], str) -> HttpResponse
|
|
|
|
try:
|
|
|
|
type = get_type(request, payload)
|
2016-11-09 16:05:45 +01:00
|
|
|
if type != 'push':
|
|
|
|
subject = get_subject_based_on_type(payload, type)
|
|
|
|
body = get_body_based_on_type(type)(payload)
|
|
|
|
check_send_message(user_profile, client, 'stream', [stream], subject, body)
|
|
|
|
else:
|
|
|
|
subjects = get_push_subjects(payload)
|
|
|
|
bodies_list = get_push_bodies(payload)
|
|
|
|
for body, subject in zip(bodies_list, subjects):
|
|
|
|
check_send_message(user_profile, client, 'stream', [stream], subject, body)
|
|
|
|
|
2016-07-04 23:06:44 +02:00
|
|
|
except KeyError as e:
|
2016-07-13 17:09:49 +02:00
|
|
|
return json_error(_("Missing key {} in JSON").format(str(e)))
|
2016-07-04 23:06:44 +02:00
|
|
|
|
|
|
|
return json_success()
|
|
|
|
|
2016-11-11 13:32:41 +01:00
|
|
|
def get_subject_for_branch_specified_events(payload, branch_name=None):
|
2016-12-04 18:45:46 +01:00
|
|
|
# type: (Dict[str, Any], Optional[Text]) -> Text
|
2016-10-05 19:48:15 +02:00
|
|
|
return SUBJECT_WITH_BRANCH_TEMPLATE.format(
|
|
|
|
repo=get_repository_name(payload['repository']),
|
2016-11-11 13:32:41 +01:00
|
|
|
branch=get_branch_name_for_push_event(payload) if branch_name is None else branch_name
|
2016-10-05 19:48:15 +02:00
|
|
|
)
|
|
|
|
|
2016-11-09 16:05:45 +01:00
|
|
|
def get_push_subjects(payload):
|
|
|
|
# type: (Dict[str, Any]) -> List[str]
|
|
|
|
subjects_list = []
|
|
|
|
for change in payload['push']['changes']:
|
|
|
|
potential_tag = (change['new'] or change['old'] or {}).get('type')
|
|
|
|
if potential_tag == 'tag':
|
|
|
|
subjects_list.append(str(get_subject(payload)))
|
|
|
|
else:
|
2016-11-11 13:32:41 +01:00
|
|
|
if change.get('new'):
|
|
|
|
branch_name = change['new']['name']
|
|
|
|
else:
|
|
|
|
branch_name = change['old']['name']
|
|
|
|
subjects_list.append(str(get_subject_for_branch_specified_events(payload, branch_name)))
|
2016-11-09 16:05:45 +01:00
|
|
|
return subjects_list
|
|
|
|
|
2016-07-04 23:06:44 +02:00
|
|
|
def get_subject(payload):
|
|
|
|
# type: (Dict[str, Any]) -> str
|
2016-10-05 19:48:15 +02:00
|
|
|
return BITBUCKET_SUBJECT_TEMPLATE.format(repository_name=get_repository_name(payload['repository']))
|
|
|
|
|
|
|
|
def get_subject_based_on_type(payload, type):
|
2016-12-04 18:45:46 +01:00
|
|
|
# type: (Dict[str, Any], str) -> Text
|
2016-10-11 18:55:39 +02:00
|
|
|
if type.startswith('pull_request'):
|
2016-10-19 16:00:40 +02:00
|
|
|
return SUBJECT_WITH_PR_OR_ISSUE_INFO_TEMPLATE.format(
|
2016-10-11 18:55:39 +02:00
|
|
|
repo=get_repository_name(payload.get('repository')),
|
|
|
|
type='PR',
|
|
|
|
id=payload['pullrequest']['id'],
|
|
|
|
title=payload['pullrequest']['title']
|
|
|
|
)
|
2016-10-19 23:44:02 +02:00
|
|
|
if type.startswith('issue'):
|
|
|
|
return SUBJECT_WITH_PR_OR_ISSUE_INFO_TEMPLATE.format(
|
|
|
|
repo=get_repository_name(payload.get('repository')),
|
|
|
|
type='Issue',
|
|
|
|
id=payload['issue']['id'],
|
|
|
|
title=payload['issue']['title']
|
|
|
|
)
|
2016-10-05 19:48:15 +02:00
|
|
|
return get_subject(payload)
|
2016-07-04 23:06:44 +02:00
|
|
|
|
|
|
|
def get_type(request, payload):
|
|
|
|
# type: (HttpRequest, Dict[str, Any]) -> str
|
|
|
|
event_key = request.META.get("HTTP_X_EVENT_KEY")
|
|
|
|
if payload.get('push'):
|
|
|
|
return 'push'
|
|
|
|
elif payload.get('fork'):
|
|
|
|
return 'fork'
|
|
|
|
elif payload.get('comment') and payload.get('commit'):
|
|
|
|
return 'commit_comment'
|
|
|
|
elif payload.get('commit_status'):
|
|
|
|
return 'change_commit_status'
|
|
|
|
elif payload.get('issue'):
|
|
|
|
if payload.get('changes'):
|
|
|
|
return "issue_updated"
|
|
|
|
if payload.get('comment'):
|
|
|
|
return 'issue_commented'
|
|
|
|
return "issue_created"
|
|
|
|
elif payload.get('pullrequest'):
|
|
|
|
pull_request_template = 'pull_request_{}'
|
|
|
|
action = re.match('pullrequest:(?P<action>.*)$', event_key)
|
|
|
|
if action:
|
|
|
|
action = action.group('action')
|
|
|
|
if action in PULL_REQUEST_SUPPORTED_ACTIONS:
|
|
|
|
return pull_request_template.format(action)
|
2016-12-29 16:56:16 +01:00
|
|
|
raise UnknownTriggerType("We don't support {} event type".format(event_key))
|
2016-07-04 23:06:44 +02:00
|
|
|
|
|
|
|
def get_body_based_on_type(type):
|
|
|
|
# type: (str) -> Any
|
2016-11-09 16:05:45 +01:00
|
|
|
return GET_SINGLE_MESSAGE_BODY_DEPENDING_ON_TYPE_MAPPER.get(type)
|
|
|
|
|
|
|
|
def get_push_bodies(payload):
|
2016-12-04 18:45:46 +01:00
|
|
|
# type: (Dict[str, Any]) -> List[Text]
|
2016-11-09 16:05:45 +01:00
|
|
|
messages_list = []
|
|
|
|
for change in payload['push']['changes']:
|
|
|
|
potential_tag = (change['new'] or change['old'] or {}).get('type')
|
|
|
|
if potential_tag == 'tag':
|
|
|
|
messages_list.append(get_push_tag_body(payload, change))
|
|
|
|
elif change.get('closed'):
|
|
|
|
messages_list.append(get_remove_branch_push_body(payload, change))
|
|
|
|
elif change.get('forced'):
|
|
|
|
messages_list.append(get_force_push_body(payload, change))
|
|
|
|
else:
|
|
|
|
messages_list.append(get_normal_push_body(payload, change))
|
|
|
|
return messages_list
|
2016-10-06 15:32:10 +02:00
|
|
|
|
2016-10-06 16:14:51 +02:00
|
|
|
def get_remove_branch_push_body(payload, change):
|
2016-12-04 18:45:46 +01:00
|
|
|
# type: (Dict[str, Any], Dict[str, Any]) -> Text
|
2016-10-06 16:14:51 +02:00
|
|
|
return get_remove_branch_event_message(
|
|
|
|
get_user_username(payload),
|
|
|
|
change['old']['name'],
|
|
|
|
)
|
|
|
|
|
2016-10-06 15:32:10 +02:00
|
|
|
def get_force_push_body(payload, change):
|
2016-12-04 18:45:46 +01:00
|
|
|
# type: (Dict[str, Any], Dict[str, Any]) -> Text
|
2016-10-06 15:32:10 +02:00
|
|
|
return get_force_push_commits_event_message(
|
|
|
|
get_user_username(payload),
|
|
|
|
change['links']['html']['href'],
|
|
|
|
change['new']['name'],
|
|
|
|
change['new']['target']['hash']
|
|
|
|
)
|
2016-10-02 09:44:33 +02:00
|
|
|
|
2016-10-06 15:32:10 +02:00
|
|
|
def get_normal_push_body(payload, change):
|
2016-12-04 18:45:46 +01:00
|
|
|
# type: (Dict[str, Any], Dict[str, Any]) -> Text
|
2016-10-02 09:44:33 +02:00
|
|
|
commits_data = [{
|
|
|
|
'sha': commit.get('hash'),
|
|
|
|
'url': commit.get('links').get('html').get('href'),
|
|
|
|
'message': commit.get('message'),
|
|
|
|
} for commit in change['commits']]
|
|
|
|
|
|
|
|
return get_push_commits_event_message(
|
|
|
|
get_user_username(payload),
|
|
|
|
change['links']['html']['href'],
|
|
|
|
change['new']['name'],
|
2016-11-11 12:45:58 +01:00
|
|
|
commits_data,
|
|
|
|
is_truncated=change['truncated']
|
2016-07-04 23:06:44 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
def get_fork_body(payload):
|
|
|
|
# type: (Dict[str, Any]) -> str
|
|
|
|
return BITBUCKET_FORK_BODY.format(
|
|
|
|
display_name=get_user_display_name(payload),
|
|
|
|
username=get_user_username(payload),
|
2016-10-05 19:48:15 +02:00
|
|
|
fork_name=get_repository_full_name(payload['fork']),
|
2016-07-04 23:06:44 +02:00
|
|
|
fork_url=get_repository_url(payload['fork'])
|
|
|
|
)
|
|
|
|
|
|
|
|
def get_commit_comment_body(payload):
|
2016-12-04 18:45:46 +01:00
|
|
|
# type: (Dict[str, Any]) -> Text
|
2016-10-21 20:08:26 +02:00
|
|
|
comment = payload.get('comment')
|
2016-10-27 21:43:15 +02:00
|
|
|
action = u'[commented]({})'.format(comment['links']['html']['href'])
|
|
|
|
return get_commits_comment_action_message(
|
2016-10-21 20:08:26 +02:00
|
|
|
get_user_username(payload),
|
|
|
|
action,
|
|
|
|
comment['commit']['links']['html']['href'],
|
2016-10-27 21:43:15 +02:00
|
|
|
comment['commit']['hash'],
|
|
|
|
comment['content']['raw'],
|
2016-07-04 23:06:44 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
def get_commit_status_changed_body(payload):
|
|
|
|
# type: (Dict[str, Any]) -> str
|
|
|
|
commit_id = re.match('.*/commit/(?P<commit_id>[A-Za-z0-9]*$)', payload['commit_status']['links']['commit']['href'])
|
|
|
|
if commit_id:
|
|
|
|
commit_info = "{}/{}".format(get_repository_url(payload['repository']), commit_id.group('commit_id'))
|
|
|
|
else:
|
|
|
|
commit_info = 'commit'
|
|
|
|
|
|
|
|
return BITBUCKET_COMMIT_STATUS_CHANGED_BODY.format(
|
|
|
|
key=payload['commit_status']['key'],
|
|
|
|
system_url=payload['commit_status']['url'],
|
|
|
|
commit_info=commit_info,
|
|
|
|
status=payload['commit_status']['state']
|
|
|
|
)
|
|
|
|
|
2016-10-21 20:08:26 +02:00
|
|
|
def get_issue_commented_body(payload):
|
2016-12-04 18:45:46 +01:00
|
|
|
# type: (Dict[str, Any]) -> Text
|
2016-10-26 20:47:12 +02:00
|
|
|
action = '[commented]({}) on'.format(payload['comment']['links']['html']['href'])
|
2016-10-21 20:08:26 +02:00
|
|
|
return get_issue_action_body(payload, action)
|
|
|
|
|
2016-07-04 23:06:44 +02:00
|
|
|
def get_issue_action_body(payload, action):
|
2016-12-04 18:45:46 +01:00
|
|
|
# type: (Dict[str, Any], str) -> Text
|
2016-10-19 23:44:02 +02:00
|
|
|
issue = payload['issue']
|
|
|
|
assignee = None
|
|
|
|
message = None
|
|
|
|
if action == 'created':
|
|
|
|
if issue['assignee']:
|
|
|
|
assignee = issue['assignee'].get('username')
|
|
|
|
message = issue['content']['raw']
|
|
|
|
|
|
|
|
return get_issue_event_message(
|
|
|
|
get_user_username(payload),
|
|
|
|
action,
|
|
|
|
issue['links']['html']['href'],
|
2016-10-26 21:13:00 +02:00
|
|
|
issue['id'],
|
2016-10-19 23:44:02 +02:00
|
|
|
message,
|
|
|
|
assignee
|
2016-07-04 23:06:44 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
def get_pull_request_action_body(payload, action):
|
2016-12-04 18:45:46 +01:00
|
|
|
# type: (Dict[str, Any], str) -> Text
|
2016-10-11 18:55:39 +02:00
|
|
|
pull_request = payload['pullrequest']
|
|
|
|
return get_pull_request_event_message(
|
|
|
|
get_user_username(payload),
|
|
|
|
action,
|
|
|
|
get_pull_request_url(pull_request),
|
2016-10-26 21:13:00 +02:00
|
|
|
pull_request.get('id')
|
2016-10-11 18:55:39 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
def get_pull_request_created_or_updated_body(payload, action):
|
2016-12-04 18:45:46 +01:00
|
|
|
# type: (Dict[str, Any], str) -> Text
|
2016-10-11 18:55:39 +02:00
|
|
|
pull_request = payload['pullrequest']
|
|
|
|
assignee = None
|
|
|
|
if pull_request.get('reviewers'):
|
|
|
|
assignee = pull_request.get('reviewers')[0]['username']
|
|
|
|
|
|
|
|
return get_pull_request_event_message(
|
|
|
|
get_user_username(payload),
|
|
|
|
action,
|
|
|
|
get_pull_request_url(pull_request),
|
2016-10-26 21:13:00 +02:00
|
|
|
pull_request.get('id'),
|
2016-10-11 18:55:39 +02:00
|
|
|
target_branch=pull_request['source']['branch']['name'],
|
|
|
|
base_branch=pull_request['destination']['branch']['name'],
|
2016-10-19 15:57:57 +02:00
|
|
|
message=pull_request['description'],
|
2016-10-11 18:55:39 +02:00
|
|
|
assignee=assignee
|
2016-07-04 23:06:44 +02:00
|
|
|
)
|
|
|
|
|
2016-10-21 20:08:26 +02:00
|
|
|
def get_pull_request_comment_created_action_body(payload):
|
2016-12-04 18:45:46 +01:00
|
|
|
# type: (Dict[str, Any]) -> Text
|
2016-10-21 20:08:26 +02:00
|
|
|
action = '[commented]({})'.format(payload['comment']['links']['html']['href'])
|
|
|
|
return get_pull_request_comment_action_body(payload, action)
|
|
|
|
|
|
|
|
def get_pull_request_deleted_or_updated_comment_action_body(payload, action):
|
2016-12-04 18:45:46 +01:00
|
|
|
# type: (Dict[str, Any], Text) -> Text
|
2016-11-03 17:37:20 +01:00
|
|
|
action = "{} a [comment]({})".format(action, payload['comment']['links']['html']['href'])
|
2016-10-21 20:08:26 +02:00
|
|
|
return get_pull_request_comment_action_body(payload, action)
|
|
|
|
|
2016-07-04 23:06:44 +02:00
|
|
|
def get_pull_request_comment_action_body(payload, action):
|
2016-12-04 18:45:46 +01:00
|
|
|
# type: (Dict[str, Any], str) -> Text
|
2016-10-26 20:47:12 +02:00
|
|
|
action += ' on'
|
2016-10-21 20:08:26 +02:00
|
|
|
return get_pull_request_event_message(
|
|
|
|
get_user_username(payload),
|
|
|
|
action,
|
|
|
|
payload['pullrequest']['links']['html']['href'],
|
2016-10-26 21:13:00 +02:00
|
|
|
payload['pullrequest']['id'],
|
2016-10-21 20:08:26 +02:00
|
|
|
message=payload['comment']['content']['raw']
|
2016-07-04 23:06:44 +02:00
|
|
|
)
|
|
|
|
|
2016-11-09 16:05:45 +01:00
|
|
|
def get_push_tag_body(payload, change):
|
2016-12-04 18:45:46 +01:00
|
|
|
# type: (Dict[str, Any], Dict[str, Any]) -> Text
|
2016-11-09 16:05:45 +01:00
|
|
|
if change.get('created'):
|
|
|
|
tag = change.get('new')
|
|
|
|
action = 'pushed'
|
|
|
|
elif change.get('closed'):
|
|
|
|
tag = change.get('old')
|
|
|
|
action = 'removed'
|
|
|
|
else:
|
|
|
|
tag = change.get('new')
|
|
|
|
action = None
|
|
|
|
return get_push_tag_event_message(
|
|
|
|
get_user_username(payload),
|
|
|
|
tag.get('name'),
|
|
|
|
tag_url=tag.get('links').get('html').get('href'),
|
|
|
|
action=action
|
|
|
|
)
|
|
|
|
|
2016-07-04 23:06:44 +02:00
|
|
|
def get_pull_request_title(pullrequest_payload):
|
|
|
|
# type: (Dict[str, Any]) -> str
|
|
|
|
return pullrequest_payload['title']
|
|
|
|
|
|
|
|
def get_pull_request_url(pullrequest_payload):
|
|
|
|
# type: (Dict[str, Any]) -> str
|
|
|
|
return pullrequest_payload['links']['html']['href']
|
|
|
|
|
|
|
|
def get_repository_url(repository_payload):
|
|
|
|
# type: (Dict[str, Any]) -> str
|
|
|
|
return repository_payload['links']['html']['href']
|
|
|
|
|
|
|
|
def get_repository_name(repository_payload):
|
2016-10-05 19:48:15 +02:00
|
|
|
# type: (Dict[str, Any]) -> str
|
|
|
|
return repository_payload['name']
|
|
|
|
|
|
|
|
def get_repository_full_name(repository_payload):
|
2016-07-04 23:06:44 +02:00
|
|
|
# type: (Dict[str, Any]) -> str
|
|
|
|
return repository_payload['full_name']
|
|
|
|
|
|
|
|
def get_user_display_name(payload):
|
|
|
|
# type: (Dict[str, Any]) -> str
|
|
|
|
return payload['actor']['display_name']
|
|
|
|
|
|
|
|
def get_user_username(payload):
|
|
|
|
# type: (Dict[str, Any]) -> str
|
|
|
|
return payload['actor']['username']
|
|
|
|
|
2016-10-05 19:48:15 +02:00
|
|
|
def get_branch_name_for_push_event(payload):
|
|
|
|
# type: (Dict[str, Any]) -> str
|
2016-10-06 16:14:51 +02:00
|
|
|
change = payload['push']['changes'][-1]
|
|
|
|
if change.get('new'):
|
|
|
|
return change['new']['name']
|
|
|
|
else:
|
|
|
|
return change['old']['name']
|
2016-10-05 19:48:15 +02:00
|
|
|
|
2016-11-09 16:05:45 +01:00
|
|
|
GET_SINGLE_MESSAGE_BODY_DEPENDING_ON_TYPE_MAPPER = {
|
2016-07-04 23:06:44 +02:00
|
|
|
'fork': get_fork_body,
|
|
|
|
'commit_comment': get_commit_comment_body,
|
|
|
|
'change_commit_status': get_commit_status_changed_body,
|
|
|
|
'issue_updated': partial(get_issue_action_body, action='updated'),
|
|
|
|
'issue_created': partial(get_issue_action_body, action='created'),
|
2016-10-21 20:08:26 +02:00
|
|
|
'issue_commented': get_issue_commented_body,
|
2016-10-11 18:55:39 +02:00
|
|
|
'pull_request_created': partial(get_pull_request_created_or_updated_body, action='created'),
|
|
|
|
'pull_request_updated': partial(get_pull_request_created_or_updated_body, action='updated'),
|
2016-07-04 23:06:44 +02:00
|
|
|
'pull_request_approved': partial(get_pull_request_action_body, action='approved'),
|
|
|
|
'pull_request_unapproved': partial(get_pull_request_action_body, action='unapproved'),
|
2017-01-11 14:17:41 +01:00
|
|
|
'pull_request_fulfilled': partial(get_pull_request_action_body, action='merged'),
|
2016-07-04 23:06:44 +02:00
|
|
|
'pull_request_rejected': partial(get_pull_request_action_body, action='rejected'),
|
2016-10-21 20:08:26 +02:00
|
|
|
'pull_request_comment_created': get_pull_request_comment_created_action_body,
|
|
|
|
'pull_request_comment_updated': partial(get_pull_request_deleted_or_updated_comment_action_body, action='updated'),
|
|
|
|
'pull_request_comment_deleted': partial(get_pull_request_deleted_or_updated_comment_action_body, action='deleted')
|
2016-07-04 23:06:44 +02:00
|
|
|
}
|