From d8101ca1397320b7a80c94e1c3dd9f3d384af17b Mon Sep 17 00:00:00 2001 From: Eeshan Garg Date: Sat, 22 Sep 2018 18:08:52 -0230 Subject: [PATCH] webhooks/bitbucket2: Improve test coverage. The lack of coverage was due to: * An unused function that was never used anywhere. * get_commit_status_changed_body was using a regex where it didn't really need to use one. And there was an if statement that assumed that the payload might NOT contain the URL to the commit. However, I checked the payload and there shouldn't be any instances where a commit event is generated but there is no URL to the commit. * get_push_tag_body had an `else` condition that really can't happen in any payload. I verified this by checking the BitBucket webhook docs. --- tools/test-backend | 1 - zerver/webhooks/bitbucket2/view.py | 21 ++++++++------------- 2 files changed, 8 insertions(+), 14 deletions(-) diff --git a/tools/test-backend b/tools/test-backend index 7c8c6af774..012b3d3d96 100755 --- a/tools/test-backend +++ b/tools/test-backend @@ -100,7 +100,6 @@ not_yet_fully_covered = { 'zerver/data_import/gitter.py', 'zerver/data_import/import_util.py', # Webhook integrations with incomplete coverage - 'zerver/webhooks/bitbucket2/view.py', 'zerver/webhooks/freshdesk/view.py', 'zerver/webhooks/github/view.py', 'zerver/webhooks/github_legacy/view.py', diff --git a/zerver/webhooks/bitbucket2/view.py b/zerver/webhooks/bitbucket2/view.py index 2005ddfcd3..800d42255f 100644 --- a/zerver/webhooks/bitbucket2/view.py +++ b/zerver/webhooks/bitbucket2/view.py @@ -226,13 +226,13 @@ def get_commit_comment_body(payload: Dict[str, Any]) -> str: ) def get_commit_status_changed_body(payload: Dict[str, Any]) -> str: - commit_id = re.match('.*/commit/(?P[A-Za-z0-9]*$)', - payload['commit_status']['links']['commit']['href']) - if commit_id: - commit_info = "{}/{}".format(get_repository_url(payload['repository']), - commit_id.group('commit_id')) - else: - commit_info = 'commit' + commit_api_url = payload['commit_status']['links']['commit']['href'] + commit_id = commit_api_url.split('/')[-1] + + commit_info = "{}/{}".format( + get_repository_url(payload['repository']), + commit_id + ) return BITBUCKET_COMMIT_STATUS_CHANGED_BODY.format( key=payload['commit_status']['key'], @@ -331,9 +331,7 @@ def get_push_tag_body(payload: Dict[str, Any], change: Dict[str, Any]) -> str: elif change.get('closed'): tag = change['old'] action = 'removed' - else: - tag = change['new'] - action = None + return get_push_tag_event_message( get_user_username(payload), tag.get('name'), @@ -366,9 +364,6 @@ def get_repo_updated_body(payload: Dict[str, Any]) -> str: return body -def get_pull_request_title(pullrequest_payload: Dict[str, Any]) -> str: - return pullrequest_payload['title'] - def get_pull_request_url(pullrequest_payload: Dict[str, Any]) -> str: return pullrequest_payload['links']['html']['href']