diff --git a/zerver/lib/webhooks/git.py b/zerver/lib/webhooks/git.py index 6153fca89e..b8945a07fb 100644 --- a/zerver/lib/webhooks/git.py +++ b/zerver/lib/webhooks/git.py @@ -14,8 +14,7 @@ PUSH_PUSHED_TEXT_WITH_URL = u"[pushed]({compare_url})" PUSH_PUSHED_TEXT_WITHOUT_URL = u"pushed" PUSH_COMMITS_MESSAGE_TEMPLATE = u"""{user_name} {pushed_text} to branch {branch_name} -{commits_list} -{commits_more_than_limit} +{commits_data} """ FORCE_PUSH_COMMITS_MESSAGE_TEMPLATE = u"{user_name} [force pushed]({url}) to branch {branch_name}. Head is now {head}" @@ -28,20 +27,6 @@ PULL_REQUEST_CONTENT_MESSAGE_TEMPLATE = u"\n~~~ quote\n{message}\n~~~" def get_push_commits_event_message(user_name, compare_url, branch_name, commits_data): # type: (text_type, Optional[text_type], text_type, List[Dict[str, Any]]) -> text_type - commits_list_message = u'' - for commit in commits_data[:COMMITS_LIMIT]: - commits_list_message += COMMIT_ROW_TEMPLATE.format( - commit_short_sha=commit.get('sha')[:7], - commit_url=commit.get('url'), - commit_msg=commit.get('message').partition('\n')[0] - ) - - if len(commits_data) > COMMITS_LIMIT: - commits_more_than_limit_message = COMMITS_MORE_THAN_LIMIT_TEMPLATE.format( - commits_number=len(commits_data) - COMMITS_LIMIT) - else: - commits_more_than_limit_message = '' - if compare_url: pushed_text_message = PUSH_PUSHED_TEXT_WITH_URL.format(compare_url=compare_url) else: @@ -51,8 +36,7 @@ def get_push_commits_event_message(user_name, compare_url, branch_name, commits_ user_name=user_name, pushed_text=pushed_text_message, branch_name=branch_name, - commits_list=commits_list_message.rstrip(), - commits_more_than_limit=commits_more_than_limit_message + commits_data=get_commits_content(commits_data), ).rstrip() def get_force_push_commits_event_message(user_name, url, branch_name, head): @@ -94,3 +78,18 @@ def get_pull_request_event_message( if pr_message: main_message += '\n' + PULL_REQUEST_CONTENT_MESSAGE_TEMPLATE.format(message=pr_message) return main_message.rstrip() + +def get_commits_content(commits_data): + # type: (List[Dict[str, Any]]) -> text_type + commits_content = u'' + for commit in commits_data[:COMMITS_LIMIT]: + commits_content += COMMIT_ROW_TEMPLATE.format( + commit_short_sha=commit.get('sha')[:7], + commit_url=commit.get('url'), + commit_msg=commit.get('message').partition('\n')[0] + ) + + if len(commits_data) > COMMITS_LIMIT: + commits_content += COMMITS_MORE_THAN_LIMIT_TEMPLATE.format( + commits_number=len(commits_data) - COMMITS_LIMIT) + return commits_content.rstrip()