Normalize bitbucket commit, PR and issues comment event's subject and content.

This commit is contained in:
Tomasz Kolek 2016-10-21 20:08:26 +02:00 committed by Tim Abbott
parent ef6a1328ce
commit cbd05eb0b2
2 changed files with 40 additions and 24 deletions

View File

@ -46,7 +46,7 @@ class Bitbucket2HookTests(WebhookTestCase):
def test_bitbucket2_on_commit_comment_created_event(self): def test_bitbucket2_on_commit_comment_created_event(self):
# type: () -> None # type: () -> None
expected_message = u"User Tomasz(login: kolaszek) added [comment](https://bitbucket.org/kolaszek/repository-name/commits/32c4ea19aa3af10acd08e419e2c354941a365d74#comment-3354963) to commit." expected_message = u"kolaszek [commented](https://bitbucket.org/kolaszek/repository-name/commits/32c4ea19aa3af10acd08e419e2c354941a365d74#comment-3354963) [Commit](https://bitbucket.org/kolaszek/repository-name/commits/32c4ea19aa3af10acd08e419e2c354941a365d74)\n\n~~~ quote\nNice fix!\n~~~"
self.send_and_test_stream_message('v2_commit_comment_created', self.EXPECTED_SUBJECT, expected_message) self.send_and_test_stream_message('v2_commit_comment_created', self.EXPECTED_SUBJECT, expected_message)
def test_bitbucket2_on_commit_status_changed_event(self): def test_bitbucket2_on_commit_status_changed_event(self):
@ -66,7 +66,7 @@ class Bitbucket2HookTests(WebhookTestCase):
def test_bitbucket2_on_issue_commented_event(self): def test_bitbucket2_on_issue_commented_event(self):
# type: () -> None # type: () -> None
expected_message = u"kolaszek commented [Issue](https://bitbucket.org/kolaszek/repository-name/issues/2/bug)" expected_message = u"kolaszek [commented](https://bitbucket.org/kolaszek/repository-name/issues/2#comment-28973596) [Issue](https://bitbucket.org/kolaszek/repository-name/issues/2/bug)"
self.send_and_test_stream_message('v2_issue_commented', self.EXPECTED_SUBJECT_ISSUE_EVENTS, expected_message) self.send_and_test_stream_message('v2_issue_commented', self.EXPECTED_SUBJECT_ISSUE_EVENTS, expected_message)
def test_bitbucket2_on_pull_request_created_event(self): def test_bitbucket2_on_pull_request_created_event(self):
@ -119,7 +119,7 @@ class Bitbucket2HookTests(WebhookTestCase):
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 [\"new commit\" pull request](https://bitbucket.org/kolaszek/repository-name/pull-requests/3)" expected_message = u"kolaszek [commented](https://bitbucket.org/kolaszek/repository-name/pull-requests/3/_/diff#comment-20576503) [PR](https://bitbucket.org/kolaszek/repository-name/pull-requests/3)\n\n~~~ quote\nComment1\n~~~"
kwargs = { kwargs = {
"HTTP_X_EVENT_KEY": 'pullrequest:comment_created' "HTTP_X_EVENT_KEY": 'pullrequest:comment_created'
} }
@ -127,7 +127,7 @@ class Bitbucket2HookTests(WebhookTestCase):
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 [\"new commit\" pull request](https://bitbucket.org/kolaszek/repository-name/pull-requests/3)" expected_message = u"kolaszek updated [comment](https://bitbucket.org/kolaszek/repository-name/pull-requests/3/_/diff#comment-20576503) [PR](https://bitbucket.org/kolaszek/repository-name/pull-requests/3)\n\n~~~ quote\nComment1\n~~~"
kwargs = { kwargs = {
"HTTP_X_EVENT_KEY": 'pullrequest:comment_updated' "HTTP_X_EVENT_KEY": 'pullrequest:comment_updated'
} }
@ -135,7 +135,7 @@ class Bitbucket2HookTests(WebhookTestCase):
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 [\"new commit\" pull request](https://bitbucket.org/kolaszek/repository-name/pull-requests/3)" expected_message = u"kolaszek deleted [comment](https://bitbucket.org/kolaszek/repository-name/pull-requests/3/_/diff#comment-20576503) [PR](https://bitbucket.org/kolaszek/repository-name/pull-requests/3)\n\n~~~ quote\nComment1\n~~~"
kwargs = { kwargs = {
"HTTP_X_EVENT_KEY": 'pullrequest:comment_deleted' "HTTP_X_EVENT_KEY": 'pullrequest:comment_deleted'
} }

View File

@ -21,8 +21,7 @@ USER_PART = 'User {display_name}(login: {username})'
BITBUCKET_FORK_BODY = USER_PART + ' forked the repository into [{fork_name}]({fork_url}).' BITBUCKET_FORK_BODY = USER_PART + ' forked the repository into [{fork_name}]({fork_url}).'
BITBUCKET_COMMIT_COMMENT_BODY = USER_PART + ' added [comment]({url_to_comment}) to commit.' BITBUCKET_COMMIT_COMMENT_BODY = USER_PART + ' added [comment]({url_to_comment}) to commit.'
BITBUCKET_COMMIT_STATUS_CHANGED_BODY = '[System {key}]({system_url}) changed status of {commit_info} to {status}.' BITBUCKET_COMMIT_STATUS_CHANGED_BODY = '[System {key}]({system_url}) changed status of {commit_info} to {status}.'
BITBUCKET_PULL_REQUEST_COMMENT_ACTION_BODY = USER_PART + ' {action} [comment]({comment_url} ' + \
'in ["{title}" pull request]({pull_request_url})'
PULL_REQUEST_SUPPORTED_ACTIONS = [ PULL_REQUEST_SUPPORTED_ACTIONS = [
'approved', 'approved',
@ -167,11 +166,15 @@ def get_fork_body(payload):
) )
def get_commit_comment_body(payload): def get_commit_comment_body(payload):
# type: (Dict[str, Any]) -> str # type: (Dict[str, Any]) -> text_type
return BITBUCKET_COMMIT_COMMENT_BODY.format( comment = payload.get('comment')
display_name=get_user_display_name(payload), action = u'[commented]({})'.format(comment['links']['html']['href'])
username=get_user_username(payload), return get_pull_request_event_message(
url_to_comment=payload['comment']['links']['html']['href'] get_user_username(payload),
action,
comment['commit']['links']['html']['href'],
message=comment['content']['raw'],
type='Commit'
) )
def get_commit_status_changed_body(payload): def get_commit_status_changed_body(payload):
@ -189,6 +192,11 @@ def get_commit_status_changed_body(payload):
status=payload['commit_status']['state'] status=payload['commit_status']['state']
) )
def get_issue_commented_body(payload):
# type: (Dict[str, Any]) -> text_type
action = '[commented]({})'.format(payload['comment']['links']['html']['href'])
return get_issue_action_body(payload, action)
def get_issue_action_body(payload, action): def get_issue_action_body(payload, action):
# type: (Dict[str, Any], str) -> text_type # type: (Dict[str, Any], str) -> text_type
issue = payload['issue'] issue = payload['issue']
@ -233,15 +241,23 @@ def get_pull_request_created_or_updated_body(payload, action):
assignee=assignee assignee=assignee
) )
def get_pull_request_comment_created_action_body(payload):
# type: (Dict[str, Any]) -> text_type
action = '[commented]({})'.format(payload['comment']['links']['html']['href'])
return get_pull_request_comment_action_body(payload, action)
def get_pull_request_deleted_or_updated_comment_action_body(payload, action):
# type: (Dict[str, Any], text_type) -> text_type
action = "{} [comment]({})".format(action, payload['comment']['links']['html']['href'])
return get_pull_request_comment_action_body(payload, action)
def get_pull_request_comment_action_body(payload, action): def get_pull_request_comment_action_body(payload, action):
# type: (Dict[str, Any], str) -> str # type: (Dict[str, Any], str) -> text_type
return BITBUCKET_PULL_REQUEST_COMMENT_ACTION_BODY.format( return get_pull_request_event_message(
display_name=get_user_display_name(payload), get_user_username(payload),
username=get_user_username(payload), action,
action=action, payload['pullrequest']['links']['html']['href'],
comment_url=payload['comment']['links']['html']['href'], message=payload['comment']['content']['raw']
title=get_pull_request_title(payload['pullrequest']),
pull_request_url=get_pull_request_url(payload['pullrequest'])
) )
def get_pull_request_title(pullrequest_payload): def get_pull_request_title(pullrequest_payload):
@ -287,14 +303,14 @@ GET_BODY_DEPENDING_ON_TYPE_MAPPER = {
'change_commit_status': get_commit_status_changed_body, 'change_commit_status': get_commit_status_changed_body,
'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': get_issue_commented_body,
'pull_request_created': partial(get_pull_request_created_or_updated_body, action='created'), '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_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'),
'pull_request_rejected': partial(get_pull_request_action_body, action='rejected'), 'pull_request_rejected': partial(get_pull_request_action_body, action='rejected'),
'pull_request_comment_created': partial(get_pull_request_comment_action_body, action='created'), 'pull_request_comment_created': get_pull_request_comment_created_action_body,
'pull_request_comment_updated': partial(get_pull_request_comment_action_body, action='updated'), 'pull_request_comment_updated': partial(get_pull_request_deleted_or_updated_comment_action_body, action='updated'),
'pull_request_comment_deleted': partial(get_pull_request_comment_action_body, action='deleted') 'pull_request_comment_deleted': partial(get_pull_request_deleted_or_updated_comment_action_body, action='deleted')
} }