Add bitbucket2 remove branch message handler.

This commit is contained in:
Tomasz Kolek 2016-10-06 16:14:51 +02:00 committed by Tim Abbott
parent b031d6b029
commit 56208d7ea1
3 changed files with 168 additions and 3 deletions

View File

@ -0,0 +1,147 @@
{
"repository":{
"is_private":true,
"type":"repository",
"full_name":"kolaszek/repository-name",
"name":"Repository name",
"website":null,
"scm":"git",
"owner":{
"display_name":"Tomasz",
"links":{
"avatar":{
"href":"https://bitbucket.org/account/kolaszek/avatar/32/"
},
"self":{
"href":"https://api.bitbucket.org/2.0/users/kolaszek"
},
"html":{
"href":"https://bitbucket.org/kolaszek/"
}
},
"username":"kolaszek",
"type":"user",
"uuid":"{678ab31f-9f88-4d7a-b343-1bdf9f024917}"
},
"uuid":"{ede265ff-3649-4068-a7da-81ac581b6f47}",
"links":{
"avatar":{
"href":"https://bitbucket.org/kolaszek/repository-name/avatar/32/"
},
"self":{
"href":"https://api.bitbucket.org/2.0/repositories/kolaszek/repository-name"
},
"html":{
"href":"https://bitbucket.org/kolaszek/repository-name"
}
}
},
"push":{
"changes":[
{
"old":{
"repository":{
"type":"repository",
"name":"Repository name",
"full_name":"kolaszek/repository-name",
"links":{
"avatar":{
"href":"https://bitbucket.org/kolaszek/repository-name/avatar/32/"
},
"self":{
"href":"https://api.bitbucket.org/2.0/repositories/kolaszek/repository-name"
},
"html":{
"href":"https://bitbucket.org/kolaszek/repository-name"
}
},
"uuid":"{ede265ff-3649-4068-a7da-81ac581b6f47}"
},
"type":"branch",
"links":{
"self":{
"href":"https://api.bitbucket.org/2.0/repositories/kolaszek/repository-name/refs/branches/master"
},
"commits":{
"href":"https://api.bitbucket.org/2.0/repositories/kolaszek/repository-name/commits/master"
},
"html":{
"href":"https://bitbucket.org/kolaszek/repository-name/branch/master"
}
},
"target":{
"author":{
"user":{
"display_name":"Tomasz",
"links":{
"avatar":{
"href":"https://bitbucket.org/account/kolaszek/avatar/32/"
},
"self":{
"href":"https://api.bitbucket.org/2.0/users/kolaszek"
},
"html":{
"href":"https://bitbucket.org/kolaszek/"
}
},
"username":"kolaszek",
"type":"user",
"uuid":"{678ab31f-9f88-4d7a-b343-1bdf9f024917}"
},
"raw":"Tomasz Kolek <tomasz-kolek@o2.pl>"
},
"type":"commit",
"links":{
"self":{
"href":"https://api.bitbucket.org/2.0/repositories/kolaszek/repository-name/commit/25f93d22b719e2d678a7ad5ee0ef0d1fcdf39c12"
},
"html":{
"href":"https://bitbucket.org/kolaszek/repository-name/commits/25f93d22b719e2d678a7ad5ee0ef0d1fcdf39c12"
}
},
"hash":"25f93d22b719e2d678a7ad5ee0ef0d1fcdf39c12",
"parents":[
{
"type":"commit",
"links":{
"self":{
"href":"https://api.bitbucket.org/2.0/repositories/kolaszek/repository-name/commit/f438e1c1b456b7d9ff278991aaca24123e7a591c"
},
"html":{
"href":"https://bitbucket.org/kolaszek/repository-name/commits/f438e1c1b456b7d9ff278991aaca24123e7a591c"
}
},
"hash":"f438e1c1b456b7d9ff278991aaca24123e7a591c"
}
],
"date":"2016-10-05T19:07:51+00:00",
"message":"c\n"
},
"name":"master"
},
"closed":true,
"new":null,
"created":false,
"forced":false,
"truncated":false
}
]
},
"actor":{
"display_name":"Tomasz",
"links":{
"avatar":{
"href":"https://bitbucket.org/account/kolaszek/avatar/32/"
},
"self":{
"href":"https://api.bitbucket.org/2.0/users/kolaszek"
},
"html":{
"href":"https://bitbucket.org/kolaszek/"
}
},
"username":"kolaszek",
"type":"user",
"uuid":"{678ab31f-9f88-4d7a-b343-1bdf9f024917}"
}
}

View File

@ -18,6 +18,11 @@ class Bitbucket2HookTests(WebhookTestCase):
expected_message = u"kolaszek [force pushed](https://bitbucket.org/kolaszek/repository-name/branch/master) to branch master. Head is now 25f93d22b719e2d678a7ad5ee0ef0d1fcdf39c12" expected_message = u"kolaszek [force pushed](https://bitbucket.org/kolaszek/repository-name/branch/master) to branch master. Head is now 25f93d22b719e2d678a7ad5ee0ef0d1fcdf39c12"
self.send_and_test_stream_message('force_push', self.EXPECTED_SUBJECT_BRANCH_EVENTS, expected_message) self.send_and_test_stream_message('force_push', self.EXPECTED_SUBJECT_BRANCH_EVENTS, expected_message)
def test_bitbucket2_on_remove_branch_event(self):
# type: () -> None
expected_message = u"kolaszek deleted branch master"
self.send_and_test_stream_message('remove_branch', self.EXPECTED_SUBJECT_BRANCH_EVENTS, expected_message)
def test_bitbucket2_on_fork_event(self): def test_bitbucket2_on_fork_event(self):
# type: () -> None # type: () -> None
expected_message = u"User Tomasz(login: kolaszek) forked the repository into [kolaszek/repository-name2](https://bitbucket.org/kolaszek/repository-name2)." expected_message = u"User Tomasz(login: kolaszek) forked the repository into [kolaszek/repository-name2](https://bitbucket.org/kolaszek/repository-name2)."

View File

@ -11,7 +11,7 @@ from zerver.lib.response import json_success, json_error
from zerver.decorator import REQ, has_request_variables, api_key_only_webhook_view from zerver.decorator import REQ, has_request_variables, api_key_only_webhook_view
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_force_push_commits_event_message, get_remove_branch_event_message
BITBUCKET_SUBJECT_TEMPLATE = '{repository_name}' BITBUCKET_SUBJECT_TEMPLATE = '{repository_name}'
@ -106,11 +106,20 @@ def get_body_based_on_type(type):
def get_push_body(payload): def get_push_body(payload):
# type: (Dict[str, Any]) -> text_type # type: (Dict[str, Any]) -> text_type
change = payload['push']['changes'][-1] change = payload['push']['changes'][-1]
if change.get('forced'): if change.get('closed'):
return get_remove_branch_push_body(payload, change)
elif change.get('forced'):
return get_force_push_body(payload, change) return get_force_push_body(payload, change)
else: else:
return get_normal_push_body(payload, change) return get_normal_push_body(payload, change)
def get_remove_branch_push_body(payload, change):
# type: (Dict[str, Any], Dict[str, Any]) -> text_type
return get_remove_branch_event_message(
get_user_username(payload),
change['old']['name'],
)
def get_force_push_body(payload, change): def get_force_push_body(payload, change):
# type: (Dict[str, Any], Dict[str, Any]) -> text_type # type: (Dict[str, Any], Dict[str, Any]) -> text_type
return get_force_push_commits_event_message( return get_force_push_commits_event_message(
@ -227,7 +236,11 @@ def get_user_username(payload):
def get_branch_name_for_push_event(payload): def get_branch_name_for_push_event(payload):
# type: (Dict[str, Any]) -> str # type: (Dict[str, Any]) -> str
return payload['push']['changes'][-1]['new']['name'] change = payload['push']['changes'][-1]
if change.get('new'):
return change['new']['name']
else:
return change['old']['name']
GET_BODY_DEPENDING_ON_TYPE_MAPPER = { GET_BODY_DEPENDING_ON_TYPE_MAPPER = {
'push': get_push_body, 'push': get_push_body,