From ce9ac30fac539fbb923a513a6702b7cbd40b1a44 Mon Sep 17 00:00:00 2001 From: Tomasz Kolek Date: Tue, 11 Oct 2016 18:55:39 +0200 Subject: [PATCH] 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. --- ...bucket_v2_pull_request_comment_action.json | 8 ++-- ...et_v2_pull_request_created_or_updated.json | 19 +++++++- zerver/tests/webhooks/test_bitbucket.py | 37 ++++++++-------- zerver/views/webhooks/bitbucket2.py | 44 ++++++++++++++----- 4 files changed, 74 insertions(+), 34 deletions(-) diff --git a/zerver/fixtures/bitbucket/bitbucket_v2_pull_request_comment_action.json b/zerver/fixtures/bitbucket/bitbucket_v2_pull_request_comment_action.json index 2280bf3b7f..691f286e8e 100644 --- a/zerver/fixtures/bitbucket/bitbucket_v2_pull_request_comment_action.json +++ b/zerver/fixtures/bitbucket/bitbucket_v2_pull_request_comment_action.json @@ -34,7 +34,7 @@ "raw":"Comment1" }, "pullrequest":{ - "title":"a", + "title":"new commit", "links":{ "html":{ "href":"https://bitbucket.org/kolaszek/repository-name/pull-requests/3" @@ -44,7 +44,7 @@ } }, "type":"pullrequest", - "id":3 + "id":1 }, "id":20576503 }, @@ -103,7 +103,7 @@ "username":"kolaszek" }, "pullrequest":{ - "title":"a", + "title":"new commit", "task_count":0, "created_on":"2016-07-04T20:47:02.153006+00:00", "reason":"", @@ -111,7 +111,7 @@ "updated_on":"2016-07-04T20:47:38.613320+00:00", "state":"OPEN", "description":"", - "id":3, + "id":1, "reviewers":[ ], diff --git a/zerver/fixtures/bitbucket/bitbucket_v2_pull_request_created_or_updated.json b/zerver/fixtures/bitbucket/bitbucket_v2_pull_request_created_or_updated.json index da984cd649..9b41d3a8dc 100644 --- a/zerver/fixtures/bitbucket/bitbucket_v2_pull_request_created_or_updated.json +++ b/zerver/fixtures/bitbucket/bitbucket_v2_pull_request_created_or_updated.json @@ -62,7 +62,22 @@ } }, "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":"", "author":{ @@ -98,7 +113,7 @@ ], "created_on":"2016-07-04T19:15:30.118942+00:00", - "description":"", + "description":"description", "comment_count":0, "id":1, "updated_on":"2016-07-04T19:15:30.141038+00:00", diff --git a/zerver/tests/webhooks/test_bitbucket.py b/zerver/tests/webhooks/test_bitbucket.py index 6afc78a9e0..13ddf5df50 100644 --- a/zerver/tests/webhooks/test_bitbucket.py +++ b/zerver/tests/webhooks/test_bitbucket.py @@ -9,6 +9,7 @@ class Bitbucket2HookTests(WebhookTestCase): URL_TEMPLATE = "/api/v1/external/bitbucket2?stream={stream}&api_key={api_key}" FIXTURE_DIR_NAME = 'bitbucket' EXPECTED_SUBJECT = u"Repository name" + EXPECTED_SUBJECT_PR_EVENTS = u"Repository name / PR #1 new commit" EXPECTED_SUBJECT_BRANCH_EVENTS = u"Repository name / master" def test_bitbucket2_on_push_event(self): @@ -69,75 +70,75 @@ class Bitbucket2HookTests(WebhookTestCase): def test_bitbucket2_on_pull_request_created_event(self): # 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 = { "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): # 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 = { "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): # 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 = { "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): # 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 = { "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): # 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 = { "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): # 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 = { "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): # 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 = { "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): # 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 = { "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): # 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 = { "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): STREAM_NAME = 'bitbucket' diff --git a/zerver/views/webhooks/bitbucket2.py b/zerver/views/webhooks/bitbucket2.py index 80ed96ab6a..9fcdb3f387 100644 --- a/zerver/views/webhooks/bitbucket2.py +++ b/zerver/views/webhooks/bitbucket2.py @@ -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.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_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}' @@ -71,6 +72,13 @@ def get_subject_based_on_type(payload, type): # type: (Dict[str, Any], str) -> text_type if type == 'push': 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) def get_type(request, payload): @@ -186,13 +194,29 @@ def get_issue_action_body(payload, action): ) def get_pull_request_action_body(payload, action): - # type: (Dict[str, Any], str) -> str - return BITBUCKET_PULL_REQUEST_ACTION_BODY.format( - display_name=get_user_display_name(payload), - username=get_user_username(payload), - action=action, - title=get_pull_request_title(payload['pullrequest']), - pull_request_url=get_pull_request_url(payload['pullrequest']) + # type: (Dict[str, Any], str) -> text_type + pull_request = payload['pullrequest'] + return get_pull_request_event_message( + get_user_username(payload), + action, + get_pull_request_url(pull_request), + ) + +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): @@ -250,8 +274,8 @@ GET_BODY_DEPENDING_ON_TYPE_MAPPER = { 'issue_updated': partial(get_issue_action_body, action='updated'), 'issue_created': partial(get_issue_action_body, action='created'), 'issue_commented': partial(get_issue_action_body, action='commented'), - 'pull_request_created': partial(get_pull_request_action_body, action='created'), - 'pull_request_updated': partial(get_pull_request_action_body, action='updated'), + 'pull_request_created': partial(get_pull_request_created_or_updated_body, action='created'), + '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_unapproved': partial(get_pull_request_action_body, action='unapproved'), 'pull_request_merged': partial(get_pull_request_action_body, action='merged'),