diff --git a/zerver/lib/webhooks/git.py b/zerver/lib/webhooks/git.py index ccb35ad7d6..6153fca89e 100644 --- a/zerver/lib/webhooks/git.py +++ b/zerver/lib/webhooks/git.py @@ -6,11 +6,12 @@ SUBJECT_WITH_PR_INFO_TEMPLATE = u'{repo} / {type} #{id} {title}' EMPTY_SHA = '0000000000000000000000000000000000000000' -PUSH_COMMITS_LIMIT = 10 +COMMITS_LIMIT = 10 +COMMIT_ROW_TEMPLATE = u'* [{commit_short_sha}]({commit_url}): {commit_msg}\n' +COMMITS_MORE_THAN_LIMIT_TEMPLATE = u"[and {commits_number} more commit(s)]" + PUSH_PUSHED_TEXT_WITH_URL = u"[pushed]({compare_url})" PUSH_PUSHED_TEXT_WITHOUT_URL = u"pushed" -PUSH_COMMIT_ROW_TEMPLATE = u'* [{commit_short_sha}]({commit_url}): {commit_msg}\n' -PUSH_COMMITS_MORE_THAN_LIMIT_TEMPLATE = u"[and {commits_number} more commit(s)]" PUSH_COMMITS_MESSAGE_TEMPLATE = u"""{user_name} {pushed_text} to branch {branch_name} {commits_list} @@ -28,16 +29,16 @@ 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[:PUSH_COMMITS_LIMIT]: - commits_list_message += PUSH_COMMIT_ROW_TEMPLATE.format( + 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) > PUSH_COMMITS_LIMIT: - commits_more_than_limit_message = PUSH_COMMITS_MORE_THAN_LIMIT_TEMPLATE.format( - commits_number=len(commits_data) - PUSH_COMMITS_LIMIT) + 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 = '' diff --git a/zerver/tests/webhooks/test_beanstalk.py b/zerver/tests/webhooks/test_beanstalk.py index 4a04aa8e2c..232f474611 100644 --- a/zerver/tests/webhooks/test_beanstalk.py +++ b/zerver/tests/webhooks/test_beanstalk.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- from six import text_type -from zerver.lib.webhooks.git import PUSH_COMMITS_LIMIT +from zerver.lib.webhooks.git import COMMITS_LIMIT from zerver.lib.test_helpers import WebhookTestCase class BeanstalkHookTests(WebhookTestCase): @@ -35,7 +35,7 @@ class BeanstalkHookTests(WebhookTestCase): expected_subject = "work-test / master" expected_message = """Leo Franchi [pushed](http://lfranchi-svn.beanstalkapp.com/work-test) to branch master -{}[and {} more commit(s)]""".format((commits_info * PUSH_COMMITS_LIMIT), 50 - PUSH_COMMITS_LIMIT) +{}[and {} more commit(s)]""".format((commits_info * COMMITS_LIMIT), 50 - COMMITS_LIMIT) self.send_and_test_stream_message('git_morethanlimitcommits', expected_subject, expected_message, content_type=None, **self.api_auth(self.TEST_USER_EMAIL)) diff --git a/zerver/tests/webhooks/test_bitbucket.py b/zerver/tests/webhooks/test_bitbucket.py index 793c10ba2e..6afc78a9e0 100644 --- a/zerver/tests/webhooks/test_bitbucket.py +++ b/zerver/tests/webhooks/test_bitbucket.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- from six import text_type from typing import Union -from zerver.lib.webhooks.git import PUSH_COMMITS_LIMIT +from zerver.lib.webhooks.git import COMMITS_LIMIT from zerver.lib.test_helpers import WebhookTestCase class Bitbucket2HookTests(WebhookTestCase): @@ -19,7 +19,7 @@ class Bitbucket2HookTests(WebhookTestCase): def test_bitbucket2_on_push_commits_above_limit_event(self): # type: () -> None - number_of_hidden_commits = 50 - PUSH_COMMITS_LIMIT + number_of_hidden_commits = 50 - COMMITS_LIMIT commit_info = '* [84b96ad](https://bitbucket.org/kolaszek/repository-name/commits/84b96adc644a30fd6465b3d196369d880762afed): first commit\n' expected_message = u"kolaszek [pushed](https://bitbucket.org/kolaszek/repository-name/branch/master) to branch master\n\n{}[and {} more commit(s)]".format( (commit_info * 10), diff --git a/zerver/tests/webhooks/test_github.py b/zerver/tests/webhooks/test_github.py index 875f633b1c..cc1e4fa633 100644 --- a/zerver/tests/webhooks/test_github.py +++ b/zerver/tests/webhooks/test_github.py @@ -3,7 +3,7 @@ from six import text_type from typing import Dict, Optional from zerver.models import Message -from zerver.lib.webhooks.git import PUSH_COMMITS_LIMIT +from zerver.lib.webhooks.git import COMMITS_LIMIT from zerver.lib.test_helpers import WebhookTestCase class GithubV1HookTests(WebhookTestCase): @@ -80,8 +80,8 @@ class GithubV1HookTests(WebhookTestCase): # type: () -> None commit_info = "* [48c329a](https://github.com/zbenjamin/zulip-test/commit/48c329a0b68a9a379ff195ee3f1c1f4ab0b2a89e): Add baz\n" expected_subject = "zbenjamin [pushed](https://github.com/zbenjamin/zulip-test/compare/4f9adc4777d5...b95449196980) to branch master\n\n{}[and {} more commit(s)]".format( - commit_info * PUSH_COMMITS_LIMIT, - 50 - PUSH_COMMITS_LIMIT, + commit_info * COMMITS_LIMIT, + 50 - COMMITS_LIMIT, ) self.basic_test('push_commits_more_than_limit', 'commits', 'zulip-test / master', expected_subject) @@ -216,8 +216,8 @@ class GithubV2HookTests(WebhookTestCase): # type: () -> None commit_info = "* [48c329a](https://github.com/zbenjamin/zulip-test/commit/48c329a0b68a9a379ff195ee3f1c1f4ab0b2a89e): Add baz\n" expected_subject = "zbenjamin [pushed](https://github.com/zbenjamin/zulip-test/compare/4f9adc4777d5...b95449196980) to branch master\n\n{}[and {} more commit(s)]".format( - commit_info * PUSH_COMMITS_LIMIT, - 50 - PUSH_COMMITS_LIMIT, + commit_info * COMMITS_LIMIT, + 50 - COMMITS_LIMIT, ) self.basic_test('push_commits_more_than_limit', 'commits', 'zulip-test / master', expected_subject) diff --git a/zerver/tests/webhooks/test_gitlab.py b/zerver/tests/webhooks/test_gitlab.py index b700f6fa86..7f725e0a53 100644 --- a/zerver/tests/webhooks/test_gitlab.py +++ b/zerver/tests/webhooks/test_gitlab.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -from zerver.lib.webhooks.git import PUSH_COMMITS_LIMIT +from zerver.lib.webhooks.git import COMMITS_LIMIT from zerver.lib.test_helpers import WebhookTestCase class GitlabHookTests(WebhookTestCase): @@ -18,8 +18,8 @@ class GitlabHookTests(WebhookTestCase): expected_subject = u"my-awesome-project / tomek" commits_info = u'* [66abd2d](https://gitlab.com/tomaszkolek0/my-awesome-project/commit/66abd2da28809ffa128ed0447965cf11d7f863a7): b\n' expected_message = u"Tomasz Kolek [pushed](https://gitlab.com/tomaszkolek0/my-awesome-project/compare/5fcdd5551fc3085df79bece2c32b1400802ac407...eb6ae1e591e0819dc5bf187c6bfe18ec065a80e9) to branch tomek\n\n{}[and {} more commit(s)]".format( - commits_info * PUSH_COMMITS_LIMIT, - 50 - PUSH_COMMITS_LIMIT, + commits_info * COMMITS_LIMIT, + 50 - COMMITS_LIMIT, ) self.send_and_test_stream_message('push_commits_more_than_limit', expected_subject, expected_message, HTTP_X_GITLAB_EVENT="Push Hook")