diff --git a/zerver/fixtures/bitbucket2/bitbucket2_remove_branch.json b/zerver/fixtures/bitbucket2/bitbucket2_remove_branch.json new file mode 100644 index 0000000000..75d77acaa5 --- /dev/null +++ b/zerver/fixtures/bitbucket2/bitbucket2_remove_branch.json @@ -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 " + }, + "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}" + } +} diff --git a/zerver/tests/webhooks/test_bitbucket.py b/zerver/tests/webhooks/test_bitbucket.py index 211de1b061..b7f039821c 100644 --- a/zerver/tests/webhooks/test_bitbucket.py +++ b/zerver/tests/webhooks/test_bitbucket.py @@ -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" 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): # type: () -> None expected_message = u"User Tomasz(login: kolaszek) forked the repository into [kolaszek/repository-name2](https://bitbucket.org/kolaszek/repository-name2)." diff --git a/zerver/views/webhooks/bitbucket2.py b/zerver/views/webhooks/bitbucket2.py index 626662c57c..80ed96ab6a 100644 --- a/zerver/views/webhooks/bitbucket2.py +++ b/zerver/views/webhooks/bitbucket2.py @@ -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.models import Client, UserProfile 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}' @@ -106,11 +106,20 @@ def get_body_based_on_type(type): def get_push_body(payload): # type: (Dict[str, Any]) -> text_type 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) else: 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): # type: (Dict[str, Any], Dict[str, Any]) -> text_type return get_force_push_commits_event_message( @@ -227,7 +236,11 @@ def get_user_username(payload): def get_branch_name_for_push_event(payload): # 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 = { 'push': get_push_body,