Normalize bitbucket pull requests event message.

Add detailed info (description, source and target branch, assignee) to message.
Change subject to 'repo_name / PR #id title'.
Modify some test fixtures for better coverage.
This commit is contained in:
Tomasz Kolek 2016-10-11 18:55:39 +02:00 committed by Tim Abbott
parent 421c51cc14
commit ce9ac30fac
4 changed files with 74 additions and 34 deletions

View File

@ -34,7 +34,7 @@
"raw":"Comment1" "raw":"Comment1"
}, },
"pullrequest":{ "pullrequest":{
"title":"a", "title":"new commit",
"links":{ "links":{
"html":{ "html":{
"href":"https://bitbucket.org/kolaszek/repository-name/pull-requests/3" "href":"https://bitbucket.org/kolaszek/repository-name/pull-requests/3"
@ -44,7 +44,7 @@
} }
}, },
"type":"pullrequest", "type":"pullrequest",
"id":3 "id":1
}, },
"id":20576503 "id":20576503
}, },
@ -103,7 +103,7 @@
"username":"kolaszek" "username":"kolaszek"
}, },
"pullrequest":{ "pullrequest":{
"title":"a", "title":"new commit",
"task_count":0, "task_count":0,
"created_on":"2016-07-04T20:47:02.153006+00:00", "created_on":"2016-07-04T20:47:02.153006+00:00",
"reason":"", "reason":"",
@ -111,7 +111,7 @@
"updated_on":"2016-07-04T20:47:38.613320+00:00", "updated_on":"2016-07-04T20:47:38.613320+00:00",
"state":"OPEN", "state":"OPEN",
"description":"", "description":"",
"id":3, "id":1,
"reviewers":[ "reviewers":[
], ],

View File

@ -62,7 +62,22 @@
} }
}, },
"reviewers":[ "reviewers":[
{
"links":{
"self":{
"href":"https://api.bitbucket.org/2.0/users/tkolek"
},
"html":{
"href":"https://bitbucket.org/tkolek/"
},
"avatar":{
"href":"https://bitbucket.org/account/tkolek/avatar/32/"
} },
"type":"user",
"username":"tkolek",
"uuid":"{bf830c24-bde2-41f1-a790-2c9372a2e789}",
"display_name":"Tomasz Kolek"
}
], ],
"reason":"", "reason":"",
"author":{ "author":{
@ -98,7 +113,7 @@
], ],
"created_on":"2016-07-04T19:15:30.118942+00:00", "created_on":"2016-07-04T19:15:30.118942+00:00",
"description":"", "description":"description",
"comment_count":0, "comment_count":0,
"id":1, "id":1,
"updated_on":"2016-07-04T19:15:30.141038+00:00", "updated_on":"2016-07-04T19:15:30.141038+00:00",

View File

@ -9,6 +9,7 @@ class Bitbucket2HookTests(WebhookTestCase):
URL_TEMPLATE = "/api/v1/external/bitbucket2?stream={stream}&api_key={api_key}" URL_TEMPLATE = "/api/v1/external/bitbucket2?stream={stream}&api_key={api_key}"
FIXTURE_DIR_NAME = 'bitbucket' FIXTURE_DIR_NAME = 'bitbucket'
EXPECTED_SUBJECT = u"Repository name" EXPECTED_SUBJECT = u"Repository name"
EXPECTED_SUBJECT_PR_EVENTS = u"Repository name / PR #1 new commit"
EXPECTED_SUBJECT_BRANCH_EVENTS = u"Repository name / master" EXPECTED_SUBJECT_BRANCH_EVENTS = u"Repository name / master"
def test_bitbucket2_on_push_event(self): def test_bitbucket2_on_push_event(self):
@ -69,75 +70,75 @@ class Bitbucket2HookTests(WebhookTestCase):
def test_bitbucket2_on_pull_request_created_event(self): def test_bitbucket2_on_pull_request_created_event(self):
# type: () -> None # type: () -> None
expected_message = u"User Tomasz(login: kolaszek) created [\"new commit\" pull request](https://bitbucket.org/kolaszek/repository-name/pull-requests/1)" expected_message = u"kolaszek created [PR](https://bitbucket.org/kolaszek/repository-name/pull-requests/1)(assigned to tkolek)\nfrom `new-branch` to `master`\n\n~~~ quote\ndescription\n~~~"
kwargs = { kwargs = {
"HTTP_X_EVENT_KEY": 'pullrequest:created' "HTTP_X_EVENT_KEY": 'pullrequest:created'
} }
self.send_and_test_stream_message('v2_pull_request_created_or_updated', self.EXPECTED_SUBJECT, expected_message, **kwargs) self.send_and_test_stream_message('v2_pull_request_created_or_updated', self.EXPECTED_SUBJECT_PR_EVENTS, expected_message, **kwargs)
def test_bitbucket2_on_pull_request_updated_event(self): def test_bitbucket2_on_pull_request_updated_event(self):
# type: () -> None # type: () -> None
expected_message = u"User Tomasz(login: kolaszek) updated [\"new commit\" pull request](https://bitbucket.org/kolaszek/repository-name/pull-requests/1)" expected_message = u"kolaszek updated [PR](https://bitbucket.org/kolaszek/repository-name/pull-requests/1)(assigned to tkolek)\nfrom `new-branch` to `master`\n\n~~~ quote\ndescription\n~~~"
kwargs = { kwargs = {
"HTTP_X_EVENT_KEY": 'pullrequest:updated' "HTTP_X_EVENT_KEY": 'pullrequest:updated'
} }
self.send_and_test_stream_message('v2_pull_request_created_or_updated', self.EXPECTED_SUBJECT, expected_message, **kwargs) self.send_and_test_stream_message('v2_pull_request_created_or_updated', self.EXPECTED_SUBJECT_PR_EVENTS, expected_message, **kwargs)
def test_bitbucket2_on_pull_request_approved_event(self): def test_bitbucket2_on_pull_request_approved_event(self):
# type: () -> None # type: () -> None
expected_message = u"User Tomasz(login: kolaszek) approved [\"new commit\" pull request](https://bitbucket.org/kolaszek/repository-name/pull-requests/1)" expected_message = u"kolaszek approved [PR](https://bitbucket.org/kolaszek/repository-name/pull-requests/1)"
kwargs = { kwargs = {
"HTTP_X_EVENT_KEY": 'pullrequest:approved' "HTTP_X_EVENT_KEY": 'pullrequest:approved'
} }
self.send_and_test_stream_message('v2_pull_request_approved_or_unapproved', self.EXPECTED_SUBJECT, expected_message, **kwargs) self.send_and_test_stream_message('v2_pull_request_approved_or_unapproved', self.EXPECTED_SUBJECT_PR_EVENTS, expected_message, **kwargs)
def test_bitbucket2_on_pull_request_unapproved_event(self): def test_bitbucket2_on_pull_request_unapproved_event(self):
# type: () -> None # type: () -> None
expected_message = u"User Tomasz(login: kolaszek) unapproved [\"new commit\" pull request](https://bitbucket.org/kolaszek/repository-name/pull-requests/1)" expected_message = u"kolaszek unapproved [PR](https://bitbucket.org/kolaszek/repository-name/pull-requests/1)"
kwargs = { kwargs = {
"HTTP_X_EVENT_KEY": 'pullrequest:unapproved' "HTTP_X_EVENT_KEY": 'pullrequest:unapproved'
} }
self.send_and_test_stream_message('v2_pull_request_approved_or_unapproved', self.EXPECTED_SUBJECT, expected_message, **kwargs) self.send_and_test_stream_message('v2_pull_request_approved_or_unapproved', self.EXPECTED_SUBJECT_PR_EVENTS, expected_message, **kwargs)
def test_bitbucket2_on_pull_request_declined_event(self): def test_bitbucket2_on_pull_request_declined_event(self):
# type: () -> None # type: () -> None
expected_message = u"User Tomasz(login: kolaszek) rejected [\"new commit\" pull request](https://bitbucket.org/kolaszek/repository-name/pull-requests/1)" expected_message = u"kolaszek rejected [PR](https://bitbucket.org/kolaszek/repository-name/pull-requests/1)"
kwargs = { kwargs = {
"HTTP_X_EVENT_KEY": 'pullrequest:rejected' "HTTP_X_EVENT_KEY": 'pullrequest:rejected'
} }
self.send_and_test_stream_message('v2_pull_request_merged_or_rejected', self.EXPECTED_SUBJECT, expected_message, **kwargs) self.send_and_test_stream_message('v2_pull_request_merged_or_rejected', self.EXPECTED_SUBJECT_PR_EVENTS, expected_message, **kwargs)
def test_bitbucket2_on_pull_request_merged_event(self): def test_bitbucket2_on_pull_request_merged_event(self):
# type: () -> None # type: () -> None
expected_message = u"User Tomasz(login: kolaszek) merged [\"new commit\" pull request](https://bitbucket.org/kolaszek/repository-name/pull-requests/1)" expected_message = u"kolaszek merged [PR](https://bitbucket.org/kolaszek/repository-name/pull-requests/1)"
kwargs = { kwargs = {
"HTTP_X_EVENT_KEY": 'pullrequest:merged' "HTTP_X_EVENT_KEY": 'pullrequest:merged'
} }
self.send_and_test_stream_message('v2_pull_request_merged_or_rejected', self.EXPECTED_SUBJECT, expected_message, **kwargs) self.send_and_test_stream_message('v2_pull_request_merged_or_rejected', self.EXPECTED_SUBJECT_PR_EVENTS, expected_message, **kwargs)
def test_bitbucket2_on_pull_request_comment_created_event(self): def test_bitbucket2_on_pull_request_comment_created_event(self):
# type: () -> None # type: () -> None
expected_message = u"User Tomasz(login: kolaszek) created [comment](https://bitbucket.org/kolaszek/repository-name/pull-requests/3/_/diff#comment-20576503 in [\"a\" pull request](https://bitbucket.org/kolaszek/repository-name/pull-requests/3)" expected_message = u"User Tomasz(login: kolaszek) created [comment](https://bitbucket.org/kolaszek/repository-name/pull-requests/3/_/diff#comment-20576503 in [\"new commit\" pull request](https://bitbucket.org/kolaszek/repository-name/pull-requests/3)"
kwargs = { kwargs = {
"HTTP_X_EVENT_KEY": 'pullrequest:comment_created' "HTTP_X_EVENT_KEY": 'pullrequest:comment_created'
} }
self.send_and_test_stream_message('v2_pull_request_comment_action', self.EXPECTED_SUBJECT, 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_pull_request_comment_updated_event(self): def test_bitbucket2_on_pull_request_comment_updated_event(self):
# type: () -> None # type: () -> None
expected_message = u"User Tomasz(login: kolaszek) updated [comment](https://bitbucket.org/kolaszek/repository-name/pull-requests/3/_/diff#comment-20576503 in [\"a\" pull request](https://bitbucket.org/kolaszek/repository-name/pull-requests/3)" expected_message = u"User Tomasz(login: kolaszek) updated [comment](https://bitbucket.org/kolaszek/repository-name/pull-requests/3/_/diff#comment-20576503 in [\"new commit\" pull request](https://bitbucket.org/kolaszek/repository-name/pull-requests/3)"
kwargs = { kwargs = {
"HTTP_X_EVENT_KEY": 'pullrequest:comment_updated' "HTTP_X_EVENT_KEY": 'pullrequest:comment_updated'
} }
self.send_and_test_stream_message('v2_pull_request_comment_action', self.EXPECTED_SUBJECT, 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_pull_request_comment_deleted_event(self): def test_bitbucket2_on_pull_request_comment_deleted_event(self):
# type: () -> None # type: () -> None
expected_message = u"User Tomasz(login: kolaszek) deleted [comment](https://bitbucket.org/kolaszek/repository-name/pull-requests/3/_/diff#comment-20576503 in [\"a\" pull request](https://bitbucket.org/kolaszek/repository-name/pull-requests/3)" expected_message = u"User Tomasz(login: kolaszek) deleted [comment](https://bitbucket.org/kolaszek/repository-name/pull-requests/3/_/diff#comment-20576503 in [\"new commit\" pull request](https://bitbucket.org/kolaszek/repository-name/pull-requests/3)"
kwargs = { kwargs = {
"HTTP_X_EVENT_KEY": 'pullrequest:comment_deleted' "HTTP_X_EVENT_KEY": 'pullrequest:comment_deleted'
} }
self.send_and_test_stream_message('v2_pull_request_comment_action', self.EXPECTED_SUBJECT, expected_message, **kwargs) self.send_and_test_stream_message('v2_pull_request_comment_action', self.EXPECTED_SUBJECT_PR_EVENTS, expected_message, **kwargs)
class BitbucketHookTests(WebhookTestCase): class BitbucketHookTests(WebhookTestCase):
STREAM_NAME = 'bitbucket' STREAM_NAME = 'bitbucket'

View File

@ -11,7 +11,8 @@ 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_remove_branch_event_message get_force_push_commits_event_message, get_remove_branch_event_message, get_pull_request_event_message,\
SUBJECT_WITH_PR_INFO_TEMPLATE
BITBUCKET_SUBJECT_TEMPLATE = '{repository_name}' BITBUCKET_SUBJECT_TEMPLATE = '{repository_name}'
@ -71,6 +72,13 @@ 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': if type == 'push':
return get_subject_for_branch_specified_events(payload) return get_subject_for_branch_specified_events(payload)
if type.startswith('pull_request'):
return SUBJECT_WITH_PR_INFO_TEMPLATE.format(
repo=get_repository_name(payload.get('repository')),
type='PR',
id=payload['pullrequest']['id'],
title=payload['pullrequest']['title']
)
return get_subject(payload) return get_subject(payload)
def get_type(request, payload): def get_type(request, payload):
@ -186,13 +194,29 @@ def get_issue_action_body(payload, action):
) )
def get_pull_request_action_body(payload, action): def get_pull_request_action_body(payload, action):
# type: (Dict[str, Any], str) -> str # type: (Dict[str, Any], str) -> text_type
return BITBUCKET_PULL_REQUEST_ACTION_BODY.format( pull_request = payload['pullrequest']
display_name=get_user_display_name(payload), return get_pull_request_event_message(
username=get_user_username(payload), get_user_username(payload),
action=action, action,
title=get_pull_request_title(payload['pullrequest']), get_pull_request_url(pull_request),
pull_request_url=get_pull_request_url(payload['pullrequest']) )
def get_pull_request_created_or_updated_body(payload, action):
# type: (Dict[str, Any], str) -> text_type
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),
target_branch=pull_request['source']['branch']['name'],
base_branch=pull_request['destination']['branch']['name'],
pr_message=pull_request['description'],
assignee=assignee
) )
def get_pull_request_comment_action_body(payload, action): def get_pull_request_comment_action_body(payload, action):
@ -250,8 +274,8 @@ GET_BODY_DEPENDING_ON_TYPE_MAPPER = {
'issue_updated': partial(get_issue_action_body, action='updated'), 'issue_updated': partial(get_issue_action_body, action='updated'),
'issue_created': partial(get_issue_action_body, action='created'), 'issue_created': partial(get_issue_action_body, action='created'),
'issue_commented': partial(get_issue_action_body, action='commented'), 'issue_commented': partial(get_issue_action_body, action='commented'),
'pull_request_created': partial(get_pull_request_action_body, action='created'), 'pull_request_created': partial(get_pull_request_created_or_updated_body, action='created'),
'pull_request_updated': partial(get_pull_request_action_body, action='updated'), 'pull_request_updated': partial(get_pull_request_created_or_updated_body, action='updated'),
'pull_request_approved': partial(get_pull_request_action_body, action='approved'), 'pull_request_approved': partial(get_pull_request_action_body, action='approved'),
'pull_request_unapproved': partial(get_pull_request_action_body, action='unapproved'), 'pull_request_unapproved': partial(get_pull_request_action_body, action='unapproved'),
'pull_request_merged': partial(get_pull_request_action_body, action='merged'), 'pull_request_merged': partial(get_pull_request_action_body, action='merged'),