From e68b957f3df67d23786b86baa79ddbd96c7dc15b Mon Sep 17 00:00:00 2001 From: Eeshan Garg Date: Thu, 6 Apr 2017 18:56:29 -0230 Subject: [PATCH] webhooks/gogs: Filter specific Gogs branches. --- zerver/lib/test_classes.py | 15 ++++++-- zerver/webhooks/gogs/doc.html | 7 ++++ zerver/webhooks/gogs/tests.py | 64 ++++++++++++++++++++++++++++++++++- zerver/webhooks/gogs/view.py | 10 ++++-- 4 files changed, 89 insertions(+), 7 deletions(-) diff --git a/zerver/lib/test_classes.py b/zerver/lib/test_classes.py index fd93473a62..3245fe70fb 100644 --- a/zerver/lib/test_classes.py +++ b/zerver/lib/test_classes.py @@ -536,10 +536,19 @@ class WebhookTestCase(ZulipTestCase): return msg - def build_webhook_url(self): - # type: () -> Text + def build_webhook_url(self, **kwargs): + # type: (**Any) -> Text api_key = self.get_api_key(self.TEST_USER_EMAIL) - return self.URL_TEMPLATE.format(stream=self.STREAM_NAME, api_key=api_key) + url = self.URL_TEMPLATE.format(stream=self.STREAM_NAME, api_key=api_key) + if kwargs and url.find('?') == -1: + url = "{}?".format(url) + else: + url = "{}&".format(url) + + for key, value in kwargs.items(): + url = "{}{}={}&".format(url, key, value) + + return url[:-1] if kwargs else url def get_body(self, fixture_name): # type: (Text) -> Union[Text, Dict[str, Text]] diff --git a/zerver/webhooks/gogs/doc.html b/zerver/webhooks/gogs/doc.html index ff221cf861..61e10c5937 100644 --- a/zerver/webhooks/gogs/doc.html +++ b/zerver/webhooks/gogs/doc.html @@ -30,6 +30,13 @@

where api_key is the API key of your Zulip bot.

+

+ You can also limit the notifications you receive to specific + branches by specifying them in a comma-separated list at the end + of the URL, like so: +

+

{{ external_api_uri_subdomain }}/v1/external/gogs?api_key=abcdefgh&stream=gogs&branches=master,development

+

Then, set Content type to application/json.

diff --git a/zerver/webhooks/gogs/tests.py b/zerver/webhooks/gogs/tests.py index e0af6d5303..c3abfc7149 100644 --- a/zerver/webhooks/gogs/tests.py +++ b/zerver/webhooks/gogs/tests.py @@ -1,5 +1,7 @@ # -*- coding: utf-8 -*- -from typing import Text +from mock import patch, MagicMock +from typing import Text, Optional + from zerver.lib.webhooks.git import COMMITS_LIMIT from zerver.lib.test_classes import WebhookTestCase @@ -24,6 +26,23 @@ class GogsHookTests(WebhookTestCase): expected_message = u"""john [pushed](http://localhost:3000/john/try-git/compare/479e6b772b7fba19412457483f50b201286d0103...d8fce16c72a2ff56a5afc8a08645a6ce45491794) 2 commits to branch master. Commits by Benjamin(1) and John(1)\n\n{}* Webhook Test ([d8fce16](http://localhost:3000/john/try-git/commit/d8fce16c72a2ff56a5afc8a08645a6ce45491794))""".format(commit_info) self.send_and_test_stream_message('push_commits_multiple_committers', expected_subject, expected_message, HTTP_X_GOGS_EVENT='push') + def test_push_multiple_committers_filtered_by_branches(self): + # type: () -> None + self.url = self.build_webhook_url(branches='master,development') + commit_info = u'* Webhook Test ([d8fce16](http://localhost:3000/john/try-git/commit/d8fce16c72a2ff56a5afc8a08645a6ce45491794))\n' + expected_subject = u"try-git / master" + expected_message = u"""john [pushed](http://localhost:3000/john/try-git/compare/479e6b772b7fba19412457483f50b201286d0103...d8fce16c72a2ff56a5afc8a08645a6ce45491794) 2 commits to branch master. Commits by Benjamin(1) and John(1)\n\n{}* Webhook Test ([d8fce16](http://localhost:3000/john/try-git/commit/d8fce16c72a2ff56a5afc8a08645a6ce45491794))""".format(commit_info) + self.send_and_test_stream_message('push_commits_multiple_committers', expected_subject, expected_message, HTTP_X_GOGS_EVENT='push') + + def test_push_filtered_by_branches(self): + # type: () -> None + self.url = self.build_webhook_url(branches='master,development') + expected_subject = u"try-git / master" + expected_message = u"""john [pushed](http://localhost:3000/john/try-git/compare/479e6b772b7fba19412457483f50b201286d0103...d8fce16c72a2ff56a5afc8a08645a6ce45491794) 1 commit to branch master. Commits by John(1) + +* Webhook Test ([d8fce16](http://localhost:3000/john/try-git/commit/d8fce16c72a2ff56a5afc8a08645a6ce45491794))""" + self.send_and_test_stream_message('push', expected_subject, expected_message, HTTP_X_GOGS_EVENT='push') + def test_push_commits_more_than_limits(self): # type: () -> None expected_subject = u"try-git / master" @@ -34,6 +53,17 @@ class GogsHookTests(WebhookTestCase): ) self.send_and_test_stream_message('push_commits_more_than_limits', expected_subject, expected_message, HTTP_X_GOGS_EVENT='push') + def test_push_commits_more_than_limits_filtered_by_branches(self): + # type: () -> None + self.url = self.build_webhook_url(branches='master,development') + expected_subject = u"try-git / master" + commits_info = "* Webhook Test ([d8fce16](http://localhost:3000/john/try-git/commit/d8fce16c72a2ff56a5afc8a08645a6ce45491794))\n" + expected_message = u"john [pushed](http://localhost:3000/john/try-git/compare/479e6b772b7fba19412457483f50b201286d0103...d8fce16c72a2ff56a5afc8a08645a6ce45491794) 30 commits to branch master. Commits by John(30)\n\n{}[and {} more commit(s)]".format( + commits_info * COMMITS_LIMIT, + 30 - COMMITS_LIMIT + ) + self.send_and_test_stream_message('push_commits_more_than_limits', expected_subject, expected_message, HTTP_X_GOGS_EVENT='push') + def test_new_branch(self): # type: () -> None expected_subject = u"try-git / my_feature" @@ -60,3 +90,35 @@ from `feature` to `master`""" expected_message = u"""john merged [PR #2](http://localhost:3000/john/try-git/pulls/2) from `feature` to `master`""" self.send_and_test_stream_message('pull_request_merged', expected_subject, expected_message, HTTP_X_GOGS_EVENT='pull_request') + + @patch('zerver.webhooks.gogs.view.check_send_message') + def test_push_filtered_by_branches_ignore(self, check_send_message_mock): + # type: (MagicMock) -> None + self.url = self.build_webhook_url(branches='changes,development') + payload = self.get_body('push') + result = self.client_post(self.url, payload, HTTP_X_GOGS_EVENT='push', + content_type="application/json") + self.assertFalse(check_send_message_mock.called) + self.assert_json_success(result) + + @patch('zerver.webhooks.gogs.view.check_send_message') + def test_push_commits_more_than_limits_filtered_by_branches_ignore( + self, check_send_message_mock): + # type: (MagicMock) -> None + self.url = self.build_webhook_url(branches='changes,development') + payload = self.get_body('push_commits_more_than_limits') + result = self.client_post(self.url, payload, HTTP_X_GOGS_EVENT='push', + content_type="application/json") + self.assertFalse(check_send_message_mock.called) + self.assert_json_success(result) + + @patch('zerver.webhooks.gogs.view.check_send_message') + def test_push_multiple_committers_filtered_by_branches_ignore( + self, check_send_message_mock): + # type: (MagicMock) -> None + self.url = self.build_webhook_url(branches='changes,development') + payload = self.get_body('push_commits_multiple_committers') + result = self.client_post(self.url, payload, HTTP_X_GOGS_EVENT='push', + content_type="application/json") + self.assertFalse(check_send_message_mock.called) + self.assert_json_success(result) diff --git a/zerver/webhooks/gogs/view.py b/zerver/webhooks/gogs/view.py index 6b78f9d993..aa6217a2aa 100644 --- a/zerver/webhooks/gogs/view.py +++ b/zerver/webhooks/gogs/view.py @@ -64,18 +64,22 @@ def format_pull_request_event(payload): @has_request_variables def api_gogs_webhook(request, user_profile, client, payload=REQ(argument_type='body'), - stream=REQ(default='commits')): - # type: (HttpRequest, UserProfile, Client, Dict[str, Any], Text) -> HttpResponse + stream=REQ(default='commits'), + branches=REQ(default=None)): + # type: (HttpRequest, UserProfile, Client, Dict[str, Any], Text, Optional[Text]) -> HttpResponse repo = payload['repository']['name'] event = request.META['HTTP_X_GOGS_EVENT'] try: if event == 'push': + branch = payload['ref'].replace('refs/heads/', '') + if branches is not None and branches.find(branch) == -1: + return json_success() body = format_push_event(payload) topic = SUBJECT_WITH_BRANCH_TEMPLATE.format( repo=repo, - branch=payload['ref'].replace('refs/heads/', '') + branch=branch ) elif event == 'create': body = format_new_branch_event(payload)