Normalize pushing tags event messages in Bitbucket2 integration.

This commit is contained in:
Tomasz Kolek 2016-11-09 16:05:45 +01:00 committed by Tim Abbott
parent 5980a75e07
commit 81f18b829d
5 changed files with 504 additions and 18 deletions

View File

@ -0,0 +1,179 @@
{
"actor":{
"links":{
"avatar":{
"href":"https://bitbucket.org/account/kolaszek/avatar/32/"
},
"html":{
"href":"https://bitbucket.org/kolaszek/"
},
"self":{
"href":"https://api.bitbucket.org/2.0/users/kolaszek"
}
},
"website":null,
"display_name":"Tomasz",
"created_on":"2016-06-29T17:51:47.001868+00:00",
"type":"user",
"uuid":"{678ab31f-9f88-4d7a-b343-1bdf9f024917}",
"location":null,
"username":"kolaszek"
},
"repository":{
"links":{
"avatar":{
"href":"https://bitbucket.org/kolaszek/repository-name/avatar/32/"
},
"html":{
"href":"https://bitbucket.org/kolaszek/repository-name"
},
"self":{
"href":"https://api.bitbucket.org/2.0/repositories/kolaszek/repository-name"
}
},
"full_name":"kolaszek/repository-name",
"uuid":"{59005838-3978-410b-84fd-8b4ce1b41089}",
"owner":{
"links":{
"avatar":{
"href":"https://bitbucket.org/account/kolaszek/avatar/32/"
},
"html":{
"href":"https://bitbucket.org/kolaszek/"
},
"self":{
"href":"https://api.bitbucket.org/2.0/users/kolaszek"
}
},
"uuid":"{678ab31f-9f88-4d7a-b343-1bdf9f024917}",
"display_name":"Tomasz",
"type":"user",
"username":"kolaszek"
},
"name":"Repository name",
"is_private":false,
"type":"repository",
"website":"",
"scm":"git"
},
"push":{
"changes":[
{
"links":{
"commits":{
"href":"https://api.bitbucket.org/2.0/repositories/kolaszek/repository-name/commits?include=c47631f097fd69c294486b5f9b99deb4a05cb285"
}
},
"forced":false,
"created":true,
"closed":false,
"truncated":false,
"new":{
"links":{
"commits":{
"href":"https://api.bitbucket.org/2.0/repositories/kolaszek/repository-name/commits/a"
},
"self":{
"href":"https://api.bitbucket.org/2.0/repositories/kolaszek/repository-name/refs/tags/a"
},
"html":{
"href":"https://bitbucket.org/kolaszek/repository-name/commits/tag/a"
}
},
"target":{
"links":{
"html":{
"href":"https://bitbucket.org/kolaszek/repository-name/commits/450118afff93569d1ce362174e9f1a7f104f9166"
},
"self":{
"href":"https://api.bitbucket.org/2.0/repositories/kolaszek/repository-name/commit/450118afff93569d1ce362174e9f1a7f104f9166"
}
},
"parents":[
{
"links":{
"html":{
"href":"https://bitbucket.org/kolaszek/repository-name/commits/c47631f097fd69c294486b5f9b99deb4a05cb285"
},
"self":{
"href":"https://api.bitbucket.org/2.0/repositories/kolaszek/repository-name/commit/c47631f097fd69c294486b5f9b99deb4a05cb285"
}
},
"hash":"c47631f097fd69c294486b5f9b99deb4a05cb285",
"type":"commit"
}
],
"message":"abc\n",
"hash":"450118afff93569d1ce362174e9f1a7f104f9166",
"author":{
"raw":"Tomasz Kolek <tomasz-kolek@go2.pl>"
},
"date":"2016-11-09T12:52:38+00:00",
"type":"commit"
},
"type":"tag",
"name":"a"
},
"old":null
},
{
"links":{
"commits":{
"href":"https://api.bitbucket.org/2.0/repositories/kolaszek/repository-name/commits?include=c47631f097fd69c294486b5f9b99deb4a05cb285"
}
},
"forced":false,
"created":true,
"closed":false,
"truncated":false,
"new":{
"links":{
"commits":{
"href":"https://api.bitbucket.org/2.0/repositories/kolaszek/repository-name/commits/b"
},
"self":{
"href":"https://api.bitbucket.org/2.0/repositories/kolaszek/repository-name/refs/tags/b"
},
"html":{
"href":"https://bitbucket.org/kolaszek/repository-name/commits/tag/b"
}
},
"target":{
"links":{
"html":{
"href":"https://bitbucket.org/kolaszek/repository-name/commits/450118afff93569d1ce362174e9f1a7f104f9166"
},
"self":{
"href":"https://api.bitbucket.org/2.0/repositories/kolaszek/repository-name/commit/450118afff93569d1ce362174e9f1a7f104f9166"
}
},
"parents":[
{
"links":{
"html":{
"href":"https://bitbucket.org/kolaszek/repository-name/commits/c47631f097fd69c294486b5f9b99deb4a05cb285"
},
"self":{
"href":"https://api.bitbucket.org/2.0/repositories/kolaszek/repository-name/commit/c47631f097fd69c294486b5f9b99deb4a05cb285"
}
},
"hash":"c47631f097fd69c294486b5f9b99deb4a05cb285",
"type":"commit"
}
],
"message":"abc\n",
"hash":"450118afff93569d1ce362174e9f1a7f104f9166",
"author":{
"raw":"Tomasz Kolek <tomasz-kolek@go2.pl>"
},
"date":"2016-11-09T12:52:38+00:00",
"type":"commit"
},
"type":"tag",
"name":"b"
},
"old":null
}
]
}
}

View File

@ -0,0 +1,121 @@
{
"actor":{
"links":{
"avatar":{
"href":"https://bitbucket.org/account/kolaszek/avatar/32/"
},
"html":{
"href":"https://bitbucket.org/kolaszek/"
},
"self":{
"href":"https://api.bitbucket.org/2.0/users/kolaszek"
}
},
"website":null,
"display_name":"Tomasz",
"created_on":"2016-06-29T17:51:47.001868+00:00",
"type":"user",
"uuid":"{678ab31f-9f88-4d7a-b343-1bdf9f024917}",
"location":null,
"username":"kolaszek"
},
"repository":{
"links":{
"avatar":{
"href":"https://bitbucket.org/kolaszek/repository-name/avatar/32/"
},
"html":{
"href":"https://bitbucket.org/kolaszek/repository-name"
},
"self":{
"href":"https://api.bitbucket.org/2.0/repositories/kolaszek/repository-name"
}
},
"full_name":"kolaszek/repository-name",
"uuid":"{59005838-3978-410b-84fd-8b4ce1b41089}",
"owner":{
"links":{
"avatar":{
"href":"https://bitbucket.org/account/kolaszek/avatar/32/"
},
"html":{
"href":"https://bitbucket.org/kolaszek/"
},
"self":{
"href":"https://api.bitbucket.org/2.0/users/kolaszek"
}
},
"uuid":"{678ab31f-9f88-4d7a-b343-1bdf9f024917}",
"display_name":"Tomasz",
"type":"user",
"username":"kolaszek"
},
"name":"Repository name",
"is_private":false,
"type":"repository",
"website":"",
"scm":"git"
},
"push":{
"changes":[
{
"links":{
"commits":{
"href":"https://api.bitbucket.org/2.0/repositories/kolaszek/repository-name/commits?include=c47631f097fd69c294486b5f9b99deb4a05cb285"
}
},
"forced":false,
"created":true,
"closed":false,
"truncated":false,
"new":{
"links":{
"commits":{
"href":"https://api.bitbucket.org/2.0/repositories/kolaszek/repository-name/commits/a"
},
"self":{
"href":"https://api.bitbucket.org/2.0/repositories/kolaszek/repository-name/refs/tags/a"
},
"html":{
"href":"https://bitbucket.org/kolaszek/repository-name/commits/tag/a"
}
},
"target":{
"links":{
"html":{
"href":"https://bitbucket.org/kolaszek/repository-name/commits/450118afff93569d1ce362174e9f1a7f104f9166"
},
"self":{
"href":"https://api.bitbucket.org/2.0/repositories/kolaszek/repository-name/commit/450118afff93569d1ce362174e9f1a7f104f9166"
}
},
"parents":[
{
"links":{
"html":{
"href":"https://bitbucket.org/kolaszek/repository-name/commits/c47631f097fd69c294486b5f9b99deb4a05cb285"
},
"self":{
"href":"https://api.bitbucket.org/2.0/repositories/kolaszek/repository-name/commit/c47631f097fd69c294486b5f9b99deb4a05cb285"
}
},
"hash":"c47631f097fd69c294486b5f9b99deb4a05cb285",
"type":"commit"
}
],
"message":"abc\n",
"hash":"450118afff93569d1ce362174e9f1a7f104f9166",
"author":{
"raw":"Tomasz Kolek <tomasz-kolek@go2.pl>"
},
"date":"2016-11-09T12:52:38+00:00",
"type":"commit"
},
"type":"tag",
"name":"a"
},
"old":null
}
]
}
}

View File

@ -0,0 +1,116 @@
{
"repository":{
"type":"repository",
"scm":"git",
"full_name":"kolaszek/repository-name",
"name":"Repository name",
"uuid":"{59005838-3978-410b-84fd-8b4ce1b41089}",
"is_private":false,
"links":{
"self":{
"href":"https://api.bitbucket.org/2.0/repositories/kolaszek/repository-name"
},
"avatar":{
"href":"https://bitbucket.org/kolaszek/repository-name/avatar/32/"
},
"html":{
"href":"https://bitbucket.org/kolaszek/repository-name"
}
},
"owner":{
"display_name":"Tomasz",
"type":"user",
"links":{
"self":{
"href":"https://api.bitbucket.org/2.0/users/kolaszek"
},
"avatar":{
"href":"https://bitbucket.org/account/kolaszek/avatar/32/"
},
"html":{
"href":"https://bitbucket.org/kolaszek/"
}
},
"uuid":"{678ab31f-9f88-4d7a-b343-1bdf9f024917}",
"username":"kolaszek"
},
"website":""
},
"push":{
"changes":[
{
"closed":true,
"old":{
"target":{
"message":"abc\n",
"author":{
"raw":"Tomasz Kolek <tomasz-kolek@go2.pl>"
},
"type":"commit",
"parents":[
{
"type":"commit",
"links":{
"self":{
"href":"https://api.bitbucket.org/2.0/repositories/kolaszek/repository-name/commit/c47631f097fd69c294486b5f9b99deb4a05cb285"
},
"html":{
"href":"https://bitbucket.org/kolaszek/repository-name/commits/c47631f097fd69c294486b5f9b99deb4a05cb285"
}
},
"hash":"c47631f097fd69c294486b5f9b99deb4a05cb285"
}
],
"date":"2016-11-09T12:52:38+00:00",
"hash":"450118afff93569d1ce362174e9f1a7f104f9166",
"links":{
"self":{
"href":"https://api.bitbucket.org/2.0/repositories/kolaszek/repository-name/commit/450118afff93569d1ce362174e9f1a7f104f9166"
},
"html":{
"href":"https://bitbucket.org/kolaszek/repository-name/commits/450118afff93569d1ce362174e9f1a7f104f9166"
}
}
},
"type":"tag",
"links":{
"commits":{
"href":"https://api.bitbucket.org/2.0/repositories/kolaszek/repository-name/commits/a"
},
"self":{
"href":"https://api.bitbucket.org/2.0/repositories/kolaszek/repository-name/refs/tags/a"
},
"html":{
"href":"https://bitbucket.org/kolaszek/repository-name/commits/tag/a"
}
},
"name":"a"
},
"created":false,
"forced":false,
"new":null,
"truncated":false
}
]
},
"actor":{
"type":"user",
"uuid":"{678ab31f-9f88-4d7a-b343-1bdf9f024917}",
"location":null,
"created_on":"2016-06-29T17:51:47.001868+00:00",
"display_name":"Tomasz",
"links":{
"self":{
"href":"https://api.bitbucket.org/2.0/users/kolaszek"
},
"avatar":{
"href":"https://bitbucket.org/account/kolaszek/avatar/32/"
},
"html":{
"href":"https://bitbucket.org/kolaszek/"
}
},
"website":null,
"username":"kolaszek"
}
}

View File

@ -141,6 +141,36 @@ class Bitbucket2HookTests(WebhookTestCase):
} }
self.send_and_test_stream_message('v2_pull_request_comment_action', self.EXPECTED_SUBJECT_PR_EVENTS, expected_message, **kwargs) self.send_and_test_stream_message('v2_pull_request_comment_action', self.EXPECTED_SUBJECT_PR_EVENTS, expected_message, **kwargs)
def test_bitbucket2_on_push_one_tag_event(self):
# type: () -> None
expected_message = u"kolaszek pushed [a](https://bitbucket.org/kolaszek/repository-name/commits/tag/a) tag"
kwargs = {
"HTTP_X_EVENT_KEY": 'pullrequest:push'
}
self.send_and_test_stream_message('v2_push_one_tag', self.EXPECTED_SUBJECT, expected_message, **kwargs)
def test_bitbucket2_on_push_remove_tag_event(self):
# type: () -> None
expected_message = u"kolaszek removed [a](https://bitbucket.org/kolaszek/repository-name/commits/tag/a) tag"
kwargs = {
"HTTP_X_EVENT_KEY": 'pullrequest:push'
}
self.send_and_test_stream_message('v2_push_remove_tag', self.EXPECTED_SUBJECT, expected_message, **kwargs)
def test_bitbucket2_on_push_more_than_one_tag_event(self):
# type: () -> None
expected_message = u"kolaszek pushed [{name}](https://bitbucket.org/kolaszek/repository-name/commits/tag/{name}) tag"
kwargs = {
"HTTP_X_EVENT_KEY": 'pullrequest:push'
}
self.send_and_test_stream_message('v2_push_more_than_one_tag', **kwargs)
msg = self.get_last_message()
self.do_test_subject(msg, self.EXPECTED_SUBJECT)
self.do_test_message(msg, expected_message.format(name='b'))
msg = self.get_second_to_last_message()
self.do_test_subject(msg, self.EXPECTED_SUBJECT)
self.do_test_message(msg, expected_message.format(name='a'))
class BitbucketHookTests(WebhookTestCase): class BitbucketHookTests(WebhookTestCase):
STREAM_NAME = 'bitbucket' STREAM_NAME = 'bitbucket'
URL_TEMPLATE = "/api/v1/external/bitbucket?payload={payload}&stream={stream}" URL_TEMPLATE = "/api/v1/external/bitbucket?payload={payload}&stream={stream}"

View File

@ -3,6 +3,7 @@ from __future__ import absolute_import
import re import re
from functools import partial from functools import partial
from six import text_type from six import text_type
from six.moves import zip
from typing import Any, Callable from typing import Any, Callable
from django.http import HttpRequest, HttpResponse from django.http import HttpRequest, HttpResponse
from django.utils.translation import ugettext as _ from django.utils.translation import ugettext as _
@ -12,7 +13,8 @@ from zerver.decorator import REQ, has_request_variables, api_key_only_webhook_vi
from zerver.models import Client, UserProfile from zerver.models import Client, UserProfile
from zerver.lib.webhooks.git import get_push_commits_event_message, SUBJECT_WITH_BRANCH_TEMPLATE,\ from zerver.lib.webhooks.git import get_push_commits_event_message, SUBJECT_WITH_BRANCH_TEMPLATE,\
get_force_push_commits_event_message, get_remove_branch_event_message, get_pull_request_event_message,\ get_force_push_commits_event_message, get_remove_branch_event_message, get_pull_request_event_message,\
SUBJECT_WITH_PR_OR_ISSUE_INFO_TEMPLATE, get_issue_event_message, get_commits_comment_action_message SUBJECT_WITH_PR_OR_ISSUE_INFO_TEMPLATE, get_issue_event_message, get_commits_comment_action_message,\
get_push_tag_event_message
BITBUCKET_SUBJECT_TEMPLATE = '{repository_name}' BITBUCKET_SUBJECT_TEMPLATE = '{repository_name}'
@ -45,12 +47,19 @@ def api_bitbucket2_webhook(request, user_profile, client, payload=REQ(argument_t
# type: (HttpRequest, UserProfile, Client, Dict[str, Any], str) -> HttpResponse # type: (HttpRequest, UserProfile, Client, Dict[str, Any], str) -> HttpResponse
try: try:
type = get_type(request, payload) type = get_type(request, payload)
subject = get_subject_based_on_type(payload, type) if type != 'push':
body = get_body_based_on_type(type)(payload) 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)
except KeyError as e: except KeyError as e:
return json_error(_("Missing key {} in JSON").format(str(e))) return json_error(_("Missing key {} in JSON").format(str(e)))
check_send_message(user_profile, client, 'stream', [stream], subject, body)
return json_success() return json_success()
def get_subject_for_branch_specified_events(payload): def get_subject_for_branch_specified_events(payload):
@ -60,14 +69,23 @@ def get_subject_for_branch_specified_events(payload):
branch=get_branch_name_for_push_event(payload) branch=get_branch_name_for_push_event(payload)
) )
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:
subjects_list.append(str(get_subject_for_branch_specified_events(payload)))
return subjects_list
def get_subject(payload): def get_subject(payload):
# type: (Dict[str, Any]) -> str # type: (Dict[str, Any]) -> str
return BITBUCKET_SUBJECT_TEMPLATE.format(repository_name=get_repository_name(payload['repository'])) return BITBUCKET_SUBJECT_TEMPLATE.format(repository_name=get_repository_name(payload['repository']))
def get_subject_based_on_type(payload, type): def get_subject_based_on_type(payload, type):
# type: (Dict[str, Any], str) -> text_type # type: (Dict[str, Any], str) -> text_type
if type == 'push':
return get_subject_for_branch_specified_events(payload)
if type.startswith('pull_request'): if type.startswith('pull_request'):
return SUBJECT_WITH_PR_OR_ISSUE_INFO_TEMPLATE.format( return SUBJECT_WITH_PR_OR_ISSUE_INFO_TEMPLATE.format(
repo=get_repository_name(payload.get('repository')), repo=get_repository_name(payload.get('repository')),
@ -112,17 +130,22 @@ def get_type(request, payload):
def get_body_based_on_type(type): def get_body_based_on_type(type):
# type: (str) -> Any # type: (str) -> Any
return GET_BODY_DEPENDING_ON_TYPE_MAPPER.get(type) return GET_SINGLE_MESSAGE_BODY_DEPENDING_ON_TYPE_MAPPER.get(type)
def get_push_body(payload): def get_push_bodies(payload):
# type: (Dict[str, Any]) -> text_type # type: (Dict[str, Any]) -> List[text_type]
change = payload['push']['changes'][-1] messages_list = []
if change.get('closed'): for change in payload['push']['changes']:
return get_remove_branch_push_body(payload, change) potential_tag = (change['new'] or change['old'] or {}).get('type')
elif change.get('forced'): if potential_tag == 'tag':
return get_force_push_body(payload, change) messages_list.append(get_push_tag_body(payload, change))
else: elif change.get('closed'):
return get_normal_push_body(payload, change) 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
def get_remove_branch_push_body(payload, change): def get_remove_branch_push_body(payload, change):
# type: (Dict[str, Any], Dict[str, Any]) -> text_type # type: (Dict[str, Any], Dict[str, Any]) -> text_type
@ -264,6 +287,24 @@ def get_pull_request_comment_action_body(payload, action):
message=payload['comment']['content']['raw'] message=payload['comment']['content']['raw']
) )
def get_push_tag_body(payload, change):
# type: (Dict[str, Any], Dict[str, Any]) -> text_type
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
)
def get_pull_request_title(pullrequest_payload): def get_pull_request_title(pullrequest_payload):
# type: (Dict[str, Any]) -> str # type: (Dict[str, Any]) -> str
return pullrequest_payload['title'] return pullrequest_payload['title']
@ -300,8 +341,7 @@ def get_branch_name_for_push_event(payload):
else: else:
return change['old']['name'] return change['old']['name']
GET_BODY_DEPENDING_ON_TYPE_MAPPER = { GET_SINGLE_MESSAGE_BODY_DEPENDING_ON_TYPE_MAPPER = {
'push': get_push_body,
'fork': get_fork_body, 'fork': get_fork_body,
'commit_comment': get_commit_comment_body, 'commit_comment': get_commit_comment_body,
'change_commit_status': get_commit_status_changed_body, 'change_commit_status': get_commit_status_changed_body,