Add handling multiple push info in one request to Bitbucket2 integration.

This commit is contained in:
Tomasz Kolek 2016-11-11 13:32:41 +01:00 committed by Tim Abbott
parent 085cdb7028
commit f05b71b9ca
3 changed files with 266 additions and 5 deletions

View File

@ -0,0 +1,244 @@
{
"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":[
{
"truncated":false,
"closed":false,
"new":{
"links":{
"self":{
"href":"https://api.bitbucket.org/2.0/repositories/kolaszek/repository-name/refs/branches/master"
},
"html":{
"href":"https://bitbucket.org/kolaszek/repository-name/branch/master"
},
"commits":{
"href":"https://api.bitbucket.org/2.0/repositories/kolaszek/repository-name/commits/master"
}
},
"type":"branch",
"repository":{
"uuid":"{59005838-3978-410b-84fd-8b4ce1b41089}",
"links":{
"self":{
"href":"https://api.bitbucket.org/2.0/repositories/kolaszek/repository-name"
},
"html":{
"href":"https://bitbucket.org/kolaszek/repository-name"
},
"avatar":{
"href":"https://bitbucket.org/kolaszek/repository-name/avatar/32/"
}
},
"type":"repository",
"full_name":"kolaszek/repository-name",
"name":"Repository name"
},
"name":"master",
"target":{
"type":"commit",
"hash":"84b96adc644a30fd6465b3d196369d880762afed",
"message":"first commit\n",
"author":{
"user":{
"links":{
"self":{
"href":"https://api.bitbucket.org/2.0/users/kolaszek"
},
"html":{
"href":"https://bitbucket.org/kolaszek/"
},
"avatar":{
"href":"https://bitbucket.org/account/kolaszek/avatar/32/"
}
},
"type":"user",
"username":"kolaszek",
"display_name":"Tomasz",
"uuid":"{678ab31f-9f88-4d7a-b343-1bdf9f024917}"
},
"raw":"Tomasz Kolek <tomasz-kolek@o2.pl>"
},
"links":{
"self":{
"href":"https://api.bitbucket.org/2.0/repositories/kolaszek/repository-name/commit/84b96adc644a30fd6465b3d196369d880762afed"
},
"html":{
"href":"https://bitbucket.org/kolaszek/repository-name/commits/84b96adc644a30fd6465b3d196369d880762afed"
}
},
"date":"2016-06-30T15:32:24+00:00",
"parents":[
]
}
},
"old":null,
"created":true,
"links":{
"html":{
"href":"https://bitbucket.org/kolaszek/repository-name/branch/master"
},
"commits":{
"href":"https://api.bitbucket.org/2.0/repositories/kolaszek/repository-name/commits?include=84b96adc644a30fd6465b3d196369d880762afed"
}
},
"commits":[
{
"type":"commit",
"hash":"84b96adc644a30fd6465b3d196369d880762afed",
"message":"first commit\n",
"author":{
"user":{
"links":{
"self":{
"href":"https://api.bitbucket.org/2.0/users/kolaszek"
},
"html":{
"href":"https://bitbucket.org/kolaszek/"
},
"avatar":{
"href":"https://bitbucket.org/account/kolaszek/avatar/32/"
}
},
"type":"user",
"username":"kolaszek",
"display_name":"Tomasz",
"uuid":"{678ab31f-9f88-4d7a-b343-1bdf9f024917}"
},
"raw":"Tomasz Kolek <tomasz-kolek@o2.pl>"
},
"links":{
"self":{
"href":"https://api.bitbucket.org/2.0/repositories/kolaszek/repository-name/commit/84b96adc644a30fd6465b3d196369d880762afed"
},
"html":{
"href":"https://bitbucket.org/kolaszek/repository-name/commits/84b96adc644a30fd6465b3d196369d880762afed"
}
},
"date":"2016-06-30T15:32:24+00:00",
"parents":[
]
}
],
"forced":false
},
{
"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
}
]
},
"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

@ -169,6 +169,19 @@ class Bitbucket2HookTests(WebhookTestCase):
self.do_test_subject(msg, self.EXPECTED_SUBJECT)
self.do_test_message(msg, expected_message.format(name='a'))
def test_bitbucket2_on_more_than_one_push_event(self):
# type: () -> None
kwargs = {
"HTTP_X_EVENT_KEY": 'pullrequest:push'
}
self.send_and_test_stream_message('v2_more_than_one_push_event', **kwargs)
msg = self.get_second_to_last_message()
self.do_test_message(msg, 'kolaszek [pushed](https://bitbucket.org/kolaszek/repository-name/branch/master) to branch master\n\n* [84b96ad](https://bitbucket.org/kolaszek/repository-name/commits/84b96adc644a30fd6465b3d196369d880762afed): first commit')
self.do_test_subject(msg, self.EXPECTED_SUBJECT_BRANCH_EVENTS)
msg = self.get_last_message()
self.do_test_message(msg, 'kolaszek pushed [a](https://bitbucket.org/kolaszek/repository-name/commits/tag/a) tag')
self.do_test_subject(msg, self.EXPECTED_SUBJECT)
class BitbucketHookTests(WebhookTestCase):
STREAM_NAME = 'bitbucket'
URL_TEMPLATE = "/api/v1/external/bitbucket?payload={payload}&stream={stream}"

View File

@ -4,7 +4,7 @@ import re
from functools import partial
from six import text_type
from six.moves import zip
from typing import Any, Callable
from typing import Any, Callable, Optional
from django.http import HttpRequest, HttpResponse
from django.utils.translation import ugettext as _
from zerver.lib.actions import check_send_message
@ -62,11 +62,11 @@ def api_bitbucket2_webhook(request, user_profile, client, payload=REQ(argument_t
return json_success()
def get_subject_for_branch_specified_events(payload):
# type: (Dict[str, Any]) -> text_type
def get_subject_for_branch_specified_events(payload, branch_name=None):
# type: (Dict[str, Any], Optional[text_type]) -> text_type
return SUBJECT_WITH_BRANCH_TEMPLATE.format(
repo=get_repository_name(payload['repository']),
branch=get_branch_name_for_push_event(payload)
branch=get_branch_name_for_push_event(payload) if branch_name is None else branch_name
)
def get_push_subjects(payload):
@ -77,7 +77,11 @@ def get_push_subjects(payload):
if potential_tag == 'tag':
subjects_list.append(str(get_subject(payload)))
else:
subjects_list.append(str(get_subject_for_branch_specified_events(payload)))
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)))
return subjects_list
def get_subject(payload):