webhooks: Migrate 14 webhooks to use check_send_webhook_message.

These are the straightforward ones.

Note that there is a line in zerver.lib.test_classes.build_webhook_url
that lost test coverage. That's because most of our tests test using
stream messages so the webhook URLs being tested always have a query
parameter. So the line that accounts for there being no query
parameters never gets called, which is fine, but we should still
keep it.
This commit is contained in:
Eeshan Garg
2018-03-13 20:13:02 -02:30
committed by Tim Abbott
parent af56df7723
commit 93678e89cd
22 changed files with 96 additions and 119 deletions

View File

@@ -669,7 +669,7 @@ class WebhookTestCase(ZulipTestCase):
has_arguments = kwargs or args has_arguments = kwargs or args
if has_arguments and url.find('?') == -1: if has_arguments and url.find('?') == -1:
url = "{}?".format(url) url = "{}?".format(url) # nocoverage
else: else:
url = "{}&".format(url) url = "{}&".format(url)

View File

@@ -5,7 +5,7 @@ from django.http import HttpRequest, HttpResponse
from django.utils.translation import ugettext as _ from django.utils.translation import ugettext as _
from zerver.decorator import api_key_only_webhook_view from zerver.decorator import api_key_only_webhook_view
from zerver.lib.actions import check_send_stream_message from zerver.lib.webhooks.common import check_send_webhook_message
from zerver.lib.request import REQ, has_request_variables from zerver.lib.request import REQ, has_request_variables
from zerver.lib.response import json_error, json_success from zerver.lib.response import json_error, json_success
from zerver.models import UserProfile from zerver.models import UserProfile
@@ -16,12 +16,10 @@ AIRBRAKE_MESSAGE_TEMPLATE = '[{error_class}]({error_url}): "{error_message}" occ
@api_key_only_webhook_view('Airbrake') @api_key_only_webhook_view('Airbrake')
@has_request_variables @has_request_variables
def api_airbrake_webhook(request: HttpRequest, user_profile: UserProfile, def api_airbrake_webhook(request: HttpRequest, user_profile: UserProfile,
payload: Dict[str, Any] = REQ(argument_type='body'), payload: Dict[str, Any]=REQ(argument_type='body')) -> HttpResponse:
stream: Text = REQ(default='airbrake')) -> HttpResponse:
subject = get_subject(payload) subject = get_subject(payload)
body = get_body(payload) body = get_body(payload)
check_send_stream_message(user_profile, request.client, check_send_webhook_message(request, user_profile, subject, body)
stream, subject, body)
return json_success() return json_success()
def get_subject(payload: Dict[str, Any]) -> str: def get_subject(payload: Dict[str, Any]) -> str:

View File

@@ -6,24 +6,21 @@ from django.http import HttpRequest, HttpResponse
from django.utils.translation import ugettext as _ from django.utils.translation import ugettext as _
from zerver.decorator import api_key_only_webhook_view from zerver.decorator import api_key_only_webhook_view
from zerver.lib.actions import check_send_stream_message
from zerver.lib.request import REQ, has_request_variables from zerver.lib.request import REQ, has_request_variables
from zerver.lib.response import json_error, json_success from zerver.lib.response import json_error, json_success
from zerver.lib.webhooks.common import check_send_webhook_message
from zerver.models import UserProfile from zerver.models import UserProfile
@api_key_only_webhook_view("AppFollow") @api_key_only_webhook_view("AppFollow")
@has_request_variables @has_request_variables
def api_appfollow_webhook(request: HttpRequest, user_profile: UserProfile, def api_appfollow_webhook(request: HttpRequest, user_profile: UserProfile,
stream: Text=REQ(default="appfollow"),
topic: Optional[Text]=REQ(default=None),
payload: Dict[str, Any]=REQ(argument_type="body")) -> HttpResponse: payload: Dict[str, Any]=REQ(argument_type="body")) -> HttpResponse:
message = payload["text"] message = payload["text"]
app_name = re.search('\A(.+)', message).group(0) app_name = re.search('\A(.+)', message).group(0)
if topic is None: topic = app_name
topic = app_name
check_send_stream_message(sender=user_profile, client=request.client, stream_name=stream, check_send_webhook_message(request, user_profile, topic,
topic=topic, body=convert_markdown(message)) body=convert_markdown(message))
return json_success() return json_success()
def convert_markdown(text: Text) -> Text: def convert_markdown(text: Text) -> Text:

View File

@@ -5,9 +5,9 @@ from typing import Any, Dict, Text
from django.http import HttpRequest, HttpResponse from django.http import HttpRequest, HttpResponse
from zerver.decorator import api_key_only_webhook_view from zerver.decorator import api_key_only_webhook_view
from zerver.lib.actions import check_send_stream_message
from zerver.lib.request import REQ, has_request_variables from zerver.lib.request import REQ, has_request_variables
from zerver.lib.response import json_error, json_success from zerver.lib.response import json_error, json_success
from zerver.lib.webhooks.common import check_send_webhook_message
from zerver.models import UserProfile from zerver.models import UserProfile
from .support_event import SUPPORT_EVENTS from .support_event import SUPPORT_EVENTS
@@ -25,8 +25,7 @@ TODO_TEMPLATE = "{user_name} {verb} the todo task [{title}]({url})"
@api_key_only_webhook_view('Basecamp') @api_key_only_webhook_view('Basecamp')
@has_request_variables @has_request_variables
def api_basecamp_webhook(request: HttpRequest, user_profile: UserProfile, def api_basecamp_webhook(request: HttpRequest, user_profile: UserProfile,
payload: Dict[str, Any]=REQ(argument_type='body'), payload: Dict[str, Any]=REQ(argument_type='body')) -> HttpResponse:
stream: Text=REQ(default='basecamp')) -> HttpResponse:
event = get_event_type(payload) event = get_event_type(payload)
if event not in SUPPORT_EVENTS: if event not in SUPPORT_EVENTS:
@@ -52,8 +51,7 @@ def api_basecamp_webhook(request: HttpRequest, user_profile: UserProfile,
logging.warning("Basecamp handling of {} event is not implemented".format(event)) logging.warning("Basecamp handling of {} event is not implemented".format(event))
return json_success() return json_success()
check_send_stream_message(user_profile, request.client, check_send_webhook_message(request, user_profile, subject, body)
stream, subject, body)
return json_success() return json_success()
def get_project_name(payload: Dict[str, Any]) -> Text: def get_project_name(payload: Dict[str, Any]) -> Text:

View File

@@ -8,7 +8,7 @@ from zerver.lib.webhooks.git import COMMITS_LIMIT
class BeanstalkHookTests(WebhookTestCase): class BeanstalkHookTests(WebhookTestCase):
STREAM_NAME = 'commits' STREAM_NAME = 'commits'
URL_TEMPLATE = u"/api/v1/external/beanstalk" URL_TEMPLATE = u"/api/v1/external/beanstalk?stream={stream}"
def test_git_single(self) -> None: def test_git_single(self) -> None:
expected_subject = "work-test / master" expected_subject = "work-test / master"
@@ -88,39 +88,39 @@ class BeanstalkHookTests(WebhookTestCase):
self.api_stream_message(self.TEST_USER_EMAIL, 'git_morethanlimitcommits', expected_subject, expected_message, self.api_stream_message(self.TEST_USER_EMAIL, 'git_morethanlimitcommits', expected_subject, expected_message,
content_type=None) content_type=None)
@patch('zerver.webhooks.beanstalk.view.check_send_stream_message') @patch('zerver.webhooks.beanstalk.view.check_send_webhook_message')
def test_git_single_filtered_by_branches_ignore(self, check_send_stream_message_mock: MagicMock) -> None: def test_git_single_filtered_by_branches_ignore(self, check_send_webhook_message_mock: MagicMock) -> None:
self.url = self.build_webhook_url(branches='changes,development') self.url = self.build_webhook_url(branches='changes,development')
payload = self.get_body('git_singlecommit') payload = self.get_body('git_singlecommit')
result = self.api_post(self.TEST_USER_EMAIL, self.url, payload) result = self.api_post(self.TEST_USER_EMAIL, self.url, payload)
self.assertFalse(check_send_stream_message_mock.called) self.assertFalse(check_send_webhook_message_mock.called)
self.assert_json_success(result) self.assert_json_success(result)
@patch('zerver.webhooks.beanstalk.view.check_send_stream_message') @patch('zerver.webhooks.beanstalk.view.check_send_webhook_message')
def test_git_multiple_committers_filtered_by_branches_ignore( def test_git_multiple_committers_filtered_by_branches_ignore(
self, check_send_stream_message_mock: MagicMock) -> None: self, check_send_webhook_message_mock: MagicMock) -> None:
self.url = self.build_webhook_url(branches='changes,development') self.url = self.build_webhook_url(branches='changes,development')
payload = self.get_body('git_multiple_committers') payload = self.get_body('git_multiple_committers')
result = self.api_post(self.TEST_USER_EMAIL, self.url, payload) result = self.api_post(self.TEST_USER_EMAIL, self.url, payload)
self.assertFalse(check_send_stream_message_mock.called) self.assertFalse(check_send_webhook_message_mock.called)
self.assert_json_success(result) self.assert_json_success(result)
@patch('zerver.webhooks.beanstalk.view.check_send_stream_message') @patch('zerver.webhooks.beanstalk.view.check_send_webhook_message')
def test_git_multiple_filtered_by_branches_ignore( def test_git_multiple_filtered_by_branches_ignore(
self, check_send_stream_message_mock: MagicMock) -> None: self, check_send_webhook_message_mock: MagicMock) -> None:
self.url = self.build_webhook_url(branches='changes,development') self.url = self.build_webhook_url(branches='changes,development')
payload = self.get_body('git_multiple') payload = self.get_body('git_multiple')
result = self.api_post(self.TEST_USER_EMAIL, self.url, payload) result = self.api_post(self.TEST_USER_EMAIL, self.url, payload)
self.assertFalse(check_send_stream_message_mock.called) self.assertFalse(check_send_webhook_message_mock.called)
self.assert_json_success(result) self.assert_json_success(result)
@patch('zerver.webhooks.beanstalk.view.check_send_stream_message') @patch('zerver.webhooks.beanstalk.view.check_send_webhook_message')
def test_git_more_than_limit_filtered_by_branches_ignore( def test_git_more_than_limit_filtered_by_branches_ignore(
self, check_send_stream_message_mock: MagicMock) -> None: self, check_send_webhook_message_mock: MagicMock) -> None:
self.url = self.build_webhook_url(branches='changes,development') self.url = self.build_webhook_url(branches='changes,development')
payload = self.get_body('git_morethanlimitcommits') payload = self.get_body('git_morethanlimitcommits')
result = self.api_post(self.TEST_USER_EMAIL, self.url, payload) result = self.api_post(self.TEST_USER_EMAIL, self.url, payload)
self.assertFalse(check_send_stream_message_mock.called) self.assertFalse(check_send_webhook_message_mock.called)
self.assert_json_success(result) self.assert_json_success(result)
def test_svn_addremove(self) -> None: def test_svn_addremove(self) -> None:

View File

@@ -7,10 +7,10 @@ from typing import Any, Callable, Dict, Optional, Text, TypeVar
from django.http import HttpRequest, HttpResponse from django.http import HttpRequest, HttpResponse
from zerver.decorator import authenticated_rest_api_view from zerver.decorator import authenticated_rest_api_view
from zerver.lib.actions import check_send_stream_message
from zerver.lib.types import ViewFuncT from zerver.lib.types import ViewFuncT
from zerver.lib.request import REQ, has_request_variables from zerver.lib.request import REQ, has_request_variables
from zerver.lib.response import json_success from zerver.lib.response import json_success
from zerver.lib.webhooks.common import check_send_webhook_message
from zerver.lib.validator import check_dict from zerver.lib.validator import check_dict
from zerver.models import UserProfile, get_client from zerver.models import UserProfile, get_client
from zerver.webhooks.github.view import build_message_from_gitlog from zerver.webhooks.github.view import build_message_from_gitlog
@@ -67,6 +67,5 @@ def api_beanstalk_webhook(request: HttpRequest, user_profile: UserProfile,
subject = "svn r%s" % (revision,) subject = "svn r%s" % (revision,)
content = "%s pushed [revision %s](%s):\n\n> %s" % (author, revision, url, short_commit_msg) content = "%s pushed [revision %s](%s):\n\n> %s" % (author, revision, url, short_commit_msg)
check_send_stream_message(user_profile, get_client("ZulipBeanstalkWebhook"), check_send_webhook_message(request, user_profile, subject, content)
"commits", subject, content)
return json_success() return json_success()

View File

@@ -53,25 +53,25 @@ class BitbucketHookTests(WebhookTestCase):
self.api_stream_message(self.TEST_USER_EMAIL, fixture_name, self.EXPECTED_SUBJECT, self.api_stream_message(self.TEST_USER_EMAIL, fixture_name, self.EXPECTED_SUBJECT,
expected_message) expected_message)
@patch('zerver.webhooks.bitbucket.view.check_send_stream_message') @patch('zerver.webhooks.bitbucket.view.check_send_webhook_message')
def test_bitbucket_on_push_event_filtered_by_branches_ignore(self, check_send_stream_message_mock: MagicMock) -> None: def test_bitbucket_on_push_event_filtered_by_branches_ignore(self, check_send_webhook_message_mock: MagicMock) -> None:
fixture_name = 'push' fixture_name = 'push'
payload = self.get_body(fixture_name) payload = self.get_body(fixture_name)
self.url = self.build_webhook_url(payload=payload, self.url = self.build_webhook_url(payload=payload,
branches='changes,development') branches='changes,development')
result = self.api_post(self.TEST_USER_EMAIL, self.url, payload, content_type="application/json,") result = self.api_post(self.TEST_USER_EMAIL, self.url, payload, content_type="application/json,")
self.assertFalse(check_send_stream_message_mock.called) self.assertFalse(check_send_webhook_message_mock.called)
self.assert_json_success(result) self.assert_json_success(result)
@patch('zerver.webhooks.bitbucket.view.check_send_stream_message') @patch('zerver.webhooks.bitbucket.view.check_send_webhook_message')
def test_bitbucket_push_commits_above_limit_filtered_by_branches_ignore( def test_bitbucket_push_commits_above_limit_filtered_by_branches_ignore(
self, check_send_stream_message_mock: MagicMock) -> None: self, check_send_webhook_message_mock: MagicMock) -> None:
fixture_name = 'push_commits_above_limit' fixture_name = 'push_commits_above_limit'
payload = self.get_body(fixture_name) payload = self.get_body(fixture_name)
self.url = self.build_webhook_url(payload=payload, self.url = self.build_webhook_url(payload=payload,
branches='changes,development') branches='changes,development')
result = self.api_post(self.TEST_USER_EMAIL, self.url, payload, content_type="application/json,") result = self.api_post(self.TEST_USER_EMAIL, self.url, payload, content_type="application/json,")
self.assertFalse(check_send_stream_message_mock.called) self.assertFalse(check_send_webhook_message_mock.called)
self.assert_json_success(result) self.assert_json_success(result)
def get_body(self, fixture_name: Text) -> Union[Text, Dict[str, Text]]: def get_body(self, fixture_name: Text) -> Union[Text, Dict[str, Text]]:

View File

@@ -3,10 +3,10 @@ from typing import Any, Mapping, Optional, Text
from django.http import HttpRequest, HttpResponse from django.http import HttpRequest, HttpResponse
from zerver.decorator import authenticated_rest_api_view from zerver.decorator import authenticated_rest_api_view
from zerver.lib.actions import check_send_stream_message
from zerver.lib.request import REQ, has_request_variables from zerver.lib.request import REQ, has_request_variables
from zerver.lib.response import json_success from zerver.lib.response import json_success
from zerver.lib.validator import check_dict from zerver.lib.validator import check_dict
from zerver.lib.webhooks.common import check_send_webhook_message
from zerver.lib.webhooks.git import SUBJECT_WITH_BRANCH_TEMPLATE, \ from zerver.lib.webhooks.git import SUBJECT_WITH_BRANCH_TEMPLATE, \
get_push_commits_event_message get_push_commits_event_message
from zerver.models import UserProfile, get_client from zerver.models import UserProfile, get_client
@@ -15,7 +15,6 @@ from zerver.models import UserProfile, get_client
@has_request_variables @has_request_variables
def api_bitbucket_webhook(request: HttpRequest, user_profile: UserProfile, def api_bitbucket_webhook(request: HttpRequest, user_profile: UserProfile,
payload: Mapping[Text, Any]=REQ(validator=check_dict([])), payload: Mapping[Text, Any]=REQ(validator=check_dict([])),
stream: Text=REQ(default='commits'),
branches: Optional[Text]=REQ(default=None)) -> HttpResponse: branches: Optional[Text]=REQ(default=None)) -> HttpResponse:
repository = payload['repository'] repository = payload['repository']
@@ -46,6 +45,5 @@ def api_bitbucket_webhook(request: HttpRequest, user_profile: UserProfile,
content = get_push_commits_event_message(payload['user'], None, branch, commits) content = get_push_commits_event_message(payload['user'], None, branch, commits)
subject = SUBJECT_WITH_BRANCH_TEMPLATE.format(repo=repository['name'], branch=branch) subject = SUBJECT_WITH_BRANCH_TEMPLATE.format(repo=repository['name'], branch=branch)
check_send_stream_message(user_profile, get_client("ZulipBitBucketWebhook"), check_send_webhook_message(request, user_profile, subject, content)
stream, subject, content)
return json_success() return json_success()

View File

@@ -224,55 +224,55 @@ class Bitbucket2HookTests(WebhookTestCase):
self.EXPECTED_SUBJECT, self.EXPECTED_SUBJECT,
expected_message, **kwargs) expected_message, **kwargs)
@patch('zerver.webhooks.bitbucket2.view.check_send_stream_message') @patch('zerver.webhooks.bitbucket2.view.check_send_webhook_message')
def test_bitbucket2_on_push_event_filtered_by_branches_ignore( def test_bitbucket2_on_push_event_filtered_by_branches_ignore(
self, check_send_stream_message_mock: MagicMock) -> None: self, check_send_webhook_message_mock: MagicMock) -> None:
self.url = self.build_webhook_url(branches='changes,devlopment') self.url = self.build_webhook_url(branches='changes,devlopment')
payload = self.get_body('push') payload = self.get_body('push')
result = self.client_post(self.url, payload, content_type="application/json") result = self.client_post(self.url, payload, content_type="application/json")
self.assertFalse(check_send_stream_message_mock.called) self.assertFalse(check_send_webhook_message_mock.called)
self.assert_json_success(result) self.assert_json_success(result)
@patch('zerver.webhooks.bitbucket2.view.check_send_stream_message') @patch('zerver.webhooks.bitbucket2.view.check_send_webhook_message')
def test_bitbucket2_on_push_commits_above_limit_filtered_by_branches_ignore( def test_bitbucket2_on_push_commits_above_limit_filtered_by_branches_ignore(
self, check_send_stream_message_mock: MagicMock) -> None: self, check_send_webhook_message_mock: MagicMock) -> None:
self.url = self.build_webhook_url(branches='changes,devlopment') self.url = self.build_webhook_url(branches='changes,devlopment')
payload = self.get_body('push_commits_above_limit') payload = self.get_body('push_commits_above_limit')
result = self.client_post(self.url, payload, content_type="application/json") result = self.client_post(self.url, payload, content_type="application/json")
self.assertFalse(check_send_stream_message_mock.called) self.assertFalse(check_send_webhook_message_mock.called)
self.assert_json_success(result) self.assert_json_success(result)
@patch('zerver.webhooks.bitbucket2.view.check_send_stream_message') @patch('zerver.webhooks.bitbucket2.view.check_send_webhook_message')
def test_bitbucket2_on_force_push_event_filtered_by_branches_ignore( def test_bitbucket2_on_force_push_event_filtered_by_branches_ignore(
self, check_send_stream_message_mock: MagicMock) -> None: self, check_send_webhook_message_mock: MagicMock) -> None:
self.url = self.build_webhook_url(branches='changes,devlopment') self.url = self.build_webhook_url(branches='changes,devlopment')
payload = self.get_body('force_push') payload = self.get_body('force_push')
result = self.client_post(self.url, payload, content_type="application/json") result = self.client_post(self.url, payload, content_type="application/json")
self.assertFalse(check_send_stream_message_mock.called) self.assertFalse(check_send_webhook_message_mock.called)
self.assert_json_success(result) self.assert_json_success(result)
@patch('zerver.webhooks.bitbucket2.view.check_send_stream_message') @patch('zerver.webhooks.bitbucket2.view.check_send_webhook_message')
def test_bitbucket2_on_push_multiple_committers_filtered_by_branches_ignore( def test_bitbucket2_on_push_multiple_committers_filtered_by_branches_ignore(
self, check_send_stream_message_mock: MagicMock) -> None: self, check_send_webhook_message_mock: MagicMock) -> None:
self.url = self.build_webhook_url(branches='changes,devlopment') self.url = self.build_webhook_url(branches='changes,devlopment')
payload = self.get_body('push_multiple_committers') payload = self.get_body('push_multiple_committers')
result = self.client_post(self.url, payload, content_type="application/json") result = self.client_post(self.url, payload, content_type="application/json")
self.assertFalse(check_send_stream_message_mock.called) self.assertFalse(check_send_webhook_message_mock.called)
self.assert_json_success(result) self.assert_json_success(result)
@patch('zerver.webhooks.bitbucket2.view.check_send_stream_message') @patch('zerver.webhooks.bitbucket2.view.check_send_webhook_message')
def test_bitbucket2_on_push_multiple_committers_with_others_filtered_by_branches_ignore( def test_bitbucket2_on_push_multiple_committers_with_others_filtered_by_branches_ignore(
self, check_send_stream_message_mock: MagicMock) -> None: self, check_send_webhook_message_mock: MagicMock) -> None:
self.url = self.build_webhook_url(branches='changes,devlopment') self.url = self.build_webhook_url(branches='changes,devlopment')
payload = self.get_body('push_multiple_committers_with_others') payload = self.get_body('push_multiple_committers_with_others')
result = self.client_post(self.url, payload, content_type="application/json") result = self.client_post(self.url, payload, content_type="application/json")
self.assertFalse(check_send_stream_message_mock.called) self.assertFalse(check_send_webhook_message_mock.called)
self.assert_json_success(result) self.assert_json_success(result)
@patch('zerver.webhooks.bitbucket2.view.check_send_stream_message') @patch('zerver.webhooks.bitbucket2.view.check_send_webhook_message')
def test_bitbucket2_on_push_without_changes_ignore( def test_bitbucket2_on_push_without_changes_ignore(
self, check_send_stream_message_mock: MagicMock) -> None: self, check_send_webhook_message_mock: MagicMock) -> None:
payload = self.get_body('push_without_changes') payload = self.get_body('push_without_changes')
result = self.client_post(self.url, payload, content_type="application/json") result = self.client_post(self.url, payload, content_type="application/json")
self.assertFalse(check_send_stream_message_mock.called) self.assertFalse(check_send_webhook_message_mock.called)
self.assert_json_success(result) self.assert_json_success(result)

View File

@@ -7,9 +7,9 @@ from django.http import HttpRequest, HttpResponse
from django.utils.translation import ugettext as _ from django.utils.translation import ugettext as _
from zerver.decorator import api_key_only_webhook_view from zerver.decorator import api_key_only_webhook_view
from zerver.lib.actions import check_send_stream_message
from zerver.lib.request import REQ, has_request_variables from zerver.lib.request import REQ, has_request_variables
from zerver.lib.response import json_error, json_success from zerver.lib.response import json_error, json_success
from zerver.lib.webhooks.common import check_send_webhook_message
from zerver.lib.webhooks.git import SUBJECT_WITH_BRANCH_TEMPLATE, \ from zerver.lib.webhooks.git import SUBJECT_WITH_BRANCH_TEMPLATE, \
SUBJECT_WITH_PR_OR_ISSUE_INFO_TEMPLATE, \ SUBJECT_WITH_PR_OR_ISSUE_INFO_TEMPLATE, \
get_commits_comment_action_message, get_force_push_commits_event_message, \ get_commits_comment_action_message, get_force_push_commits_event_message, \
@@ -46,14 +46,12 @@ class UnknownTriggerType(Exception):
@has_request_variables @has_request_variables
def api_bitbucket2_webhook(request: HttpRequest, user_profile: UserProfile, def api_bitbucket2_webhook(request: HttpRequest, user_profile: UserProfile,
payload: Dict[str, Any]=REQ(argument_type='body'), payload: Dict[str, Any]=REQ(argument_type='body'),
stream: str=REQ(default='bitbucket'),
branches: Optional[Text]=REQ(default=None)) -> HttpResponse: branches: Optional[Text]=REQ(default=None)) -> HttpResponse:
type = get_type(request, payload) type = get_type(request, payload)
if type != 'push': if type != 'push':
subject = get_subject_based_on_type(payload, type) subject = get_subject_based_on_type(payload, type)
body = get_body_based_on_type(type)(payload) body = get_body_based_on_type(type)(payload)
check_send_stream_message(user_profile, request.client, check_send_webhook_message(request, user_profile, subject, body)
stream, subject, body)
else: else:
# ignore push events with no changes # ignore push events with no changes
if not payload['push']['changes']: if not payload['push']['changes']:
@@ -65,8 +63,8 @@ def api_bitbucket2_webhook(request: HttpRequest, user_profile: UserProfile,
subjects = get_push_subjects(payload) subjects = get_push_subjects(payload)
bodies_list = get_push_bodies(payload) bodies_list = get_push_bodies(payload)
for body, subject in zip(bodies_list, subjects): for body, subject in zip(bodies_list, subjects):
check_send_stream_message(user_profile, request.client, check_send_webhook_message(request, user_profile, subject, body)
stream, subject, body)
return json_success() return json_success()
def get_subject_for_branch_specified_events(payload: Dict[str, Any], def get_subject_for_branch_specified_events(payload: Dict[str, Any],

View File

@@ -6,9 +6,9 @@ import ujson
from django.http import HttpRequest, HttpResponse from django.http import HttpRequest, HttpResponse
from zerver.decorator import api_key_only_webhook_view from zerver.decorator import api_key_only_webhook_view
from zerver.lib.actions import check_send_stream_message
from zerver.lib.request import REQ, has_request_variables from zerver.lib.request import REQ, has_request_variables
from zerver.lib.response import json_error, json_success from zerver.lib.response import json_error, json_success
from zerver.lib.webhooks.common import check_send_webhook_message
from zerver.models import UserProfile from zerver.models import UserProfile
CIRCLECI_SUBJECT_TEMPLATE = u'{repository_name}' CIRCLECI_SUBJECT_TEMPLATE = u'{repository_name}'
@@ -19,13 +19,12 @@ FAILED_STATUS = 'failed'
@api_key_only_webhook_view('CircleCI') @api_key_only_webhook_view('CircleCI')
@has_request_variables @has_request_variables
def api_circleci_webhook(request: HttpRequest, user_profile: UserProfile, def api_circleci_webhook(request: HttpRequest, user_profile: UserProfile,
payload: Dict[str, Any]=REQ(argument_type='body'), payload: Dict[str, Any]=REQ(argument_type='body')) -> HttpResponse:
stream: Text=REQ(default='circleci')) -> HttpResponse:
payload = payload['payload'] payload = payload['payload']
subject = get_subject(payload) subject = get_subject(payload)
body = get_body(payload) body = get_body(payload)
check_send_stream_message(user_profile, request.client, stream, subject, body) check_send_webhook_message(request, user_profile, subject, body)
return json_success() return json_success()
def get_subject(payload: Dict[str, Any]) -> Text: def get_subject(payload: Dict[str, Any]) -> Text:

View File

@@ -7,9 +7,9 @@ from django.http import HttpRequest, HttpResponse
from django.utils.translation import ugettext as _ from django.utils.translation import ugettext as _
from zerver.decorator import api_key_only_webhook_view from zerver.decorator import api_key_only_webhook_view
from zerver.lib.actions import check_send_stream_message
from zerver.lib.request import REQ, has_request_variables from zerver.lib.request import REQ, has_request_variables
from zerver.lib.response import json_error, json_success from zerver.lib.response import json_error, json_success
from zerver.lib.webhooks.common import check_send_webhook_message
from zerver.models import UserProfile from zerver.models import UserProfile
CODESHIP_SUBJECT_TEMPLATE = '{project_name}' CODESHIP_SUBJECT_TEMPLATE = '{project_name}'
@@ -26,13 +26,12 @@ CODESHIP_STATUS_MAPPER = {
@api_key_only_webhook_view('Codeship') @api_key_only_webhook_view('Codeship')
@has_request_variables @has_request_variables
def api_codeship_webhook(request: HttpRequest, user_profile: UserProfile, def api_codeship_webhook(request: HttpRequest, user_profile: UserProfile,
payload: Dict[str, Any]=REQ(argument_type='body'), payload: Dict[str, Any]=REQ(argument_type='body')) -> HttpResponse:
stream: str=REQ(default='codeship')) -> HttpResponse:
payload = payload['build'] payload = payload['build']
subject = get_subject_for_http_request(payload) subject = get_subject_for_http_request(payload)
body = get_body_for_http_request(payload) body = get_body_for_http_request(payload)
check_send_stream_message(user_profile, request.client, stream, subject, body) check_send_webhook_message(request, user_profile, subject, body)
return json_success() return json_success()

View File

@@ -5,9 +5,9 @@ from django.http import HttpRequest, HttpResponse
from django.utils.translation import ugettext as _ from django.utils.translation import ugettext as _
from zerver.decorator import api_key_only_webhook_view from zerver.decorator import api_key_only_webhook_view
from zerver.lib.actions import check_send_stream_message
from zerver.lib.request import REQ, has_request_variables from zerver.lib.request import REQ, has_request_variables
from zerver.lib.response import json_error, json_success from zerver.lib.response import json_error, json_success
from zerver.lib.webhooks.common import check_send_webhook_message
from zerver.models import UserProfile from zerver.models import UserProfile
CRASHLYTICS_SUBJECT_TEMPLATE = '{display_id}: {title}' CRASHLYTICS_SUBJECT_TEMPLATE = '{display_id}: {title}'
@@ -22,8 +22,7 @@ VERIFICATION_EVENT = 'verification'
@api_key_only_webhook_view('Crashlytics') @api_key_only_webhook_view('Crashlytics')
@has_request_variables @has_request_variables
def api_crashlytics_webhook(request: HttpRequest, user_profile: UserProfile, def api_crashlytics_webhook(request: HttpRequest, user_profile: UserProfile,
payload: Dict[str, Any]=REQ(argument_type='body'), payload: Dict[str, Any]=REQ(argument_type='body')) -> HttpResponse:
stream: Text=REQ(default='crashlytics')) -> HttpResponse:
event = payload['event'] event = payload['event']
if event == VERIFICATION_EVENT: if event == VERIFICATION_EVENT:
subject = CRASHLYTICS_SETUP_SUBJECT_TEMPLATE subject = CRASHLYTICS_SETUP_SUBJECT_TEMPLATE
@@ -39,6 +38,5 @@ def api_crashlytics_webhook(request: HttpRequest, user_profile: UserProfile,
url=issue_body['url'] url=issue_body['url']
) )
check_send_stream_message(user_profile, request.client, stream, check_send_webhook_message(request, user_profile, subject, body)
subject, body)
return json_success() return json_success()

View File

@@ -4,9 +4,9 @@ from django.http import HttpRequest, HttpResponse
from django.utils.translation import ugettext as _ from django.utils.translation import ugettext as _
from zerver.decorator import api_key_only_webhook_view from zerver.decorator import api_key_only_webhook_view
from zerver.lib.actions import check_send_stream_message
from zerver.lib.request import REQ, has_request_variables from zerver.lib.request import REQ, has_request_variables
from zerver.lib.response import json_error, json_success from zerver.lib.response import json_error, json_success
from zerver.lib.webhooks.common import check_send_webhook_message
from zerver.models import UserProfile from zerver.models import UserProfile
def body_template(score: int) -> str: def body_template(score: int) -> str:
@@ -18,9 +18,7 @@ def body_template(score: int) -> str:
@api_key_only_webhook_view("Delighted") @api_key_only_webhook_view("Delighted")
@has_request_variables @has_request_variables
def api_delighted_webhook(request: HttpRequest, user_profile: UserProfile, def api_delighted_webhook(request: HttpRequest, user_profile: UserProfile,
payload: Dict[str, Dict[str, Any]]=REQ(argument_type='body'), payload: Dict[str, Dict[str, Any]]=REQ(argument_type='body')) -> HttpResponse:
stream: str=REQ(default='delighted'),
topic: str=REQ(default='Survey Response')) -> HttpResponse:
person = payload['event_data']['person'] person = payload['event_data']['person']
selected_payload = {'email': person['email']} selected_payload = {'email': person['email']}
selected_payload['score'] = payload['event_data']['score'] selected_payload['score'] = payload['event_data']['score']
@@ -28,7 +26,7 @@ def api_delighted_webhook(request: HttpRequest, user_profile: UserProfile,
BODY_TEMPLATE = body_template(selected_payload['score']) BODY_TEMPLATE = body_template(selected_payload['score'])
body = BODY_TEMPLATE.format(**selected_payload) body = BODY_TEMPLATE.format(**selected_payload)
topic = 'Survey Response'
check_send_stream_message(user_profile, request.client, stream, check_send_webhook_message(request, user_profile, topic, body)
topic, body)
return json_success() return json_success()

View File

@@ -15,7 +15,7 @@ from zerver.lib.test_classes import WebhookTestCase
class DeskDotComHookTests(WebhookTestCase): class DeskDotComHookTests(WebhookTestCase):
STREAM_NAME = 'deskdotcom' STREAM_NAME = 'deskdotcom'
URL_TEMPLATE = "/api/v1/external/deskdotcom" URL_TEMPLATE = "/api/v1/external/deskdotcom?stream={stream}"
FIXTURE_DIR_NAME = 'deskdotcom' FIXTURE_DIR_NAME = 'deskdotcom'
def test_static_text_message(self) -> None: def test_static_text_message(self) -> None:

View File

@@ -4,9 +4,9 @@ from typing import Text
from django.http import HttpRequest, HttpResponse from django.http import HttpRequest, HttpResponse
from zerver.decorator import authenticated_rest_api_view from zerver.decorator import authenticated_rest_api_view
from zerver.lib.actions import check_send_stream_message
from zerver.lib.request import REQ, has_request_variables from zerver.lib.request import REQ, has_request_variables
from zerver.lib.response import json_success from zerver.lib.response import json_success
from zerver.lib.webhooks.common import check_send_webhook_message
from zerver.models import UserProfile, get_client from zerver.models import UserProfile, get_client
# Desk.com's integrations all make the user supply a template, where it fills # Desk.com's integrations all make the user supply a template, where it fills
@@ -16,9 +16,8 @@ from zerver.models import UserProfile, get_client
# from the "data" param and post it, which this does. # from the "data" param and post it, which this does.
@authenticated_rest_api_view(is_webhook=True) @authenticated_rest_api_view(is_webhook=True)
@has_request_variables @has_request_variables
def api_deskdotcom_webhook(request: HttpRequest, user_profile: UserProfile, data: Text=REQ(), def api_deskdotcom_webhook(request: HttpRequest, user_profile: UserProfile,
topic: Text=REQ(default="Desk.com notification"), data: Text=REQ()) -> HttpResponse:
stream: Text=REQ(default="desk.com")) -> HttpResponse: topic = "Desk.com notification"
check_send_stream_message(user_profile, get_client("ZulipDeskWebhook"), check_send_webhook_message(request, user_profile, topic, data)
stream, topic, data)
return json_success() return json_success()

View File

@@ -5,7 +5,7 @@ from zerver.lib.test_classes import WebhookTestCase
class DropboxHookTests(WebhookTestCase): class DropboxHookTests(WebhookTestCase):
STREAM_NAME = 'test' STREAM_NAME = 'test'
URL_TEMPLATE = "/api/v1/external/dropbox?&api_key={api_key}" URL_TEMPLATE = "/api/v1/external/dropbox?&api_key={api_key}&stream={stream}"
FIXTURE_DIR_NAME = 'dropbox' FIXTURE_DIR_NAME = 'dropbox'
def test_file_updated(self) -> None: def test_file_updated(self) -> None:

View File

@@ -1,18 +1,17 @@
from typing import Text from typing import Text
from django.http import HttpRequest, HttpResponse from django.http import HttpRequest, HttpResponse
from zerver.lib.actions import check_send_stream_message
from zerver.lib.response import json_success from zerver.lib.response import json_success
from zerver.lib.webhooks.common import check_send_webhook_message
from zerver.decorator import REQ, has_request_variables, api_key_only_webhook_view from zerver.decorator import REQ, has_request_variables, api_key_only_webhook_view
from zerver.models import UserProfile from zerver.models import UserProfile
@api_key_only_webhook_view('Dropbox') @api_key_only_webhook_view('Dropbox')
@has_request_variables @has_request_variables
def api_dropbox_webhook(request: HttpRequest, user_profile: UserProfile, def api_dropbox_webhook(request: HttpRequest, user_profile: UserProfile) -> HttpResponse:
stream: Text=REQ(default='test'),
topic: Text=REQ(default='Dropbox')) -> HttpResponse:
if request.method == 'GET': if request.method == 'GET':
return HttpResponse(request.GET['challenge']) return HttpResponse(request.GET['challenge'])
elif request.method == 'POST': elif request.method == 'POST':
check_send_stream_message(user_profile, request.client, topic = 'Dropbox'
stream, topic, "File has been updated on Dropbox!") check_send_webhook_message(request, user_profile, topic,
"File has been updated on Dropbox!")
return json_success() return json_success()

View File

@@ -4,7 +4,7 @@ from zerver.lib.test_classes import WebhookTestCase
class FlockHookTests(WebhookTestCase): class FlockHookTests(WebhookTestCase):
STREAM_NAME = 'test' STREAM_NAME = 'test'
URL_TEMPLATE = u"/api/v1/external/flock?api_key={api_key}" URL_TEMPLATE = u"/api/v1/external/flock?api_key={api_key}&stream={stream}"
def test_flock_message(self) -> None: def test_flock_message(self) -> None:
expected_subject = u"Flock notifications" expected_subject = u"Flock notifications"

View File

@@ -1,6 +1,6 @@
# Webhooks for external integrations. # Webhooks for external integrations.
from zerver.lib.actions import check_send_stream_message
from zerver.lib.response import json_success from zerver.lib.response import json_success
from zerver.lib.webhooks.common import check_send_webhook_message
from zerver.decorator import REQ, has_request_variables, api_key_only_webhook_view from zerver.decorator import REQ, has_request_variables, api_key_only_webhook_view
from zerver.models import UserProfile from zerver.models import UserProfile
from django.http import HttpRequest, HttpResponse from django.http import HttpRequest, HttpResponse
@@ -11,16 +11,16 @@ CHECK_IS_REPLY = "in reply to"
@api_key_only_webhook_view('Flock') @api_key_only_webhook_view('Flock')
@has_request_variables @has_request_variables
def api_flock_webhook(request: HttpRequest, user_profile: UserProfile, def api_flock_webhook(request: HttpRequest, user_profile: UserProfile,
payload: Dict[str, Any]=REQ(argument_type='body'), payload: Dict[str, Any]=REQ(argument_type='body')) -> HttpResponse:
stream: str=REQ(default='test'),
topic: str=REQ(default='Flock notifications')) -> HttpResponse:
if len(payload["text"]) != 0: if len(payload["text"]) != 0:
message_body = payload["text"] message_body = payload["text"]
else: else:
message_body = payload["notification"] message_body = payload["notification"]
topic = 'Flock notifications'
body = u"{}".format(message_body) body = u"{}".format(message_body)
check_send_stream_message(user_profile, request.client, stream, topic, body) check_send_webhook_message(request, user_profile, topic, body)
return json_success() return json_success()

View File

@@ -418,20 +418,20 @@ class GitlabHookTests(WebhookTestCase):
HTTP_X_GITLAB_EVENT="Test Hook" HTTP_X_GITLAB_EVENT="Test Hook"
) )
@patch('zerver.webhooks.gitlab.view.check_send_stream_message') @patch('zerver.lib.webhooks.common.check_send_webhook_message')
def test_push_event_message_filtered_by_branches_ignore( def test_push_event_message_filtered_by_branches_ignore(
self, check_send_stream_message_mock: MagicMock) -> None: self, check_send_webhook_message_mock: MagicMock) -> None:
self.url = self.build_webhook_url(branches='master,development') self.url = self.build_webhook_url(branches='master,development')
payload = self.get_body('push') payload = self.get_body('push')
result = self.client_post(self.url, payload, HTTP_X_GITLAB_EVENT='Push Hook', content_type="application/json") result = self.client_post(self.url, payload, HTTP_X_GITLAB_EVENT='Push Hook', content_type="application/json")
self.assertFalse(check_send_stream_message_mock.called) self.assertFalse(check_send_webhook_message_mock.called)
self.assert_json_success(result) self.assert_json_success(result)
@patch('zerver.webhooks.gitlab.view.check_send_stream_message') @patch('zerver.lib.webhooks.common.check_send_webhook_message')
def test_push_commits_more_than_limit_message_filtered_by_branches_ignore( def test_push_commits_more_than_limit_message_filtered_by_branches_ignore(
self, check_send_stream_message_mock: MagicMock) -> None: self, check_send_webhook_message_mock: MagicMock) -> None:
self.url = self.build_webhook_url(branches='master,development') self.url = self.build_webhook_url(branches='master,development')
payload = self.get_body('push_commits_more_than_limit') payload = self.get_body('push_commits_more_than_limit')
result = self.client_post(self.url, payload, HTTP_X_GITLAB_EVENT='Push Hook', content_type="application/json") result = self.client_post(self.url, payload, HTTP_X_GITLAB_EVENT='Push Hook', content_type="application/json")
self.assertFalse(check_send_stream_message_mock.called) self.assertFalse(check_send_webhook_message_mock.called)
self.assert_json_success(result) self.assert_json_success(result)

View File

@@ -5,9 +5,9 @@ import re
from django.http import HttpRequest, HttpResponse from django.http import HttpRequest, HttpResponse
from zerver.decorator import api_key_only_webhook_view from zerver.decorator import api_key_only_webhook_view
from zerver.lib.actions import check_send_stream_message
from zerver.lib.request import REQ, has_request_variables from zerver.lib.request import REQ, has_request_variables
from zerver.lib.response import json_success from zerver.lib.response import json_success
from zerver.lib.webhooks.common import check_send_webhook_message
from zerver.lib.webhooks.git import EMPTY_SHA, \ from zerver.lib.webhooks.git import EMPTY_SHA, \
SUBJECT_WITH_PR_OR_ISSUE_INFO_TEMPLATE, \ SUBJECT_WITH_PR_OR_ISSUE_INFO_TEMPLATE, \
get_commits_comment_action_message, get_issue_event_message, \ get_commits_comment_action_message, get_issue_event_message, \
@@ -269,16 +269,13 @@ EVENT_FUNCTION_MAPPER = {
@api_key_only_webhook_view("Gitlab") @api_key_only_webhook_view("Gitlab")
@has_request_variables @has_request_variables
def api_gitlab_webhook(request: HttpRequest, user_profile: UserProfile, def api_gitlab_webhook(request: HttpRequest, user_profile: UserProfile,
stream: Text=REQ(default='gitlab'),
topic: Text=REQ(default=None),
payload: Dict[str, Any]=REQ(argument_type='body'), payload: Dict[str, Any]=REQ(argument_type='body'),
branches: Optional[Text]=REQ(default=None)) -> HttpResponse: branches: Optional[Text]=REQ(default=None)) -> HttpResponse:
event = get_event(request, payload, branches) event = get_event(request, payload, branches)
if event is not None: if event is not None:
body = get_body_based_on_event(event)(payload) body = get_body_based_on_event(event)(payload)
if topic is None: topic = get_subject_based_on_event(event, payload)
topic = get_subject_based_on_event(event, payload) check_send_webhook_message(request, user_profile, topic, body)
check_send_stream_message(user_profile, request.client, stream, topic, body)
return json_success() return json_success()
def get_body_based_on_event(event: str) -> Any: def get_body_based_on_event(event: str) -> Any: