github: Add list and fixtures for ignored events to webhook.

This commit is contained in:
Tomasz Kolek
2017-02-08 21:30:25 +01:00
committed by Tim Abbott
parent 349835e619
commit e1d73ea36f
6 changed files with 1765 additions and 3 deletions

View File

@@ -1,4 +1,5 @@
import ujson
from mock import patch, MagicMock
from typing import Dict, Optional, Text
from zerver.models import Message
@@ -178,3 +179,35 @@ class GithubWebhookTest(WebhookTestCase):
expected_message = u"baxterthehacker unassigned [PR](https://github.com/baxterthehacker/public-repo/pull/1)"
self.send_and_test_stream_message('unassigned_pull_request', self.EXPECTED_SUBJECT_PR_EVENTS, expected_message,
HTTP_X_GITHUB_EVENT='pull_request')
@patch('zerver.webhooks.github_webhook.view.check_send_message')
def test_pull_request_labeled_ignore(self, check_send_message_mock):
# type: (MagicMock) -> None
payload = self.get_body('labeled_pull_request')
result = self.client_post(self.url, payload, HTTP_X_GITHUB_EVENT='pull_request', content_type="application/json")
self.assertFalse(check_send_message_mock.called)
self.assert_json_success(result)
@patch('zerver.webhooks.github_webhook.view.check_send_message')
def test_pull_request_unlabeled_ignore(self, check_send_message_mock):
# type: (MagicMock) -> None
payload = self.get_body('unlabeled_pull_request')
result = self.client_post(self.url, payload, HTTP_X_GITHUB_EVENT='pull_request', content_type="application/json")
self.assertFalse(check_send_message_mock.called)
self.assert_json_success(result)
@patch('zerver.webhooks.github_webhook.view.check_send_message')
def test_pull_request_request_review_ignore(self, check_send_message_mock):
# type: (MagicMock) -> None
payload = self.get_body('request_review_pull_request')
result = self.client_post(self.url, payload, HTTP_X_GITHUB_EVENT='pull_request', content_type="application/json")
self.assertFalse(check_send_message_mock.called)
self.assert_json_success(result)
@patch('zerver.webhooks.github_webhook.view.check_send_message')
def test_pull_request_request_review_remove_ignore(self, check_send_message_mock):
# type: (MagicMock) -> None
payload = self.get_body('request_review_removed_pull_request')
result = self.client_post(self.url, payload, HTTP_X_GITHUB_EVENT='pull_request', content_type="application/json")
self.assertFalse(check_send_message_mock.called)
self.assert_json_success(result)

View File

@@ -1,10 +1,12 @@
from __future__ import absolute_import
import re
import logging
from functools import partial
from typing import Any, Callable, Text, Dict
from typing import Any, Callable, Text, Dict, Optional
from django.http import HttpRequest, HttpResponse
from zerver.lib.actions import check_send_message
from zerver.lib.response import json_success
from zerver.lib.request import JsonableError
from zerver.models import Client, UserProfile
from zerver.decorator import api_key_only_webhook_view, REQ, has_request_variables
@@ -384,14 +386,14 @@ def api_github_webhook(
payload=REQ(argument_type='body'), stream=REQ(default='github')):
# type: (HttpRequest, UserProfile, Client, Dict[str, Any], Text) -> HttpResponse
event = get_event(request, payload)
if event != 'ping':
if event != 'ping' and event is not None:
subject = get_subject_based_on_type(payload, event)
body = get_body_function_based_on_type(event)(payload)
check_send_message(user_profile, client, 'stream', [stream], subject, body)
return json_success()
def get_event(request, payload):
# type: (HttpRequest, Dict[str, Any]) -> str
# type: (HttpRequest, Dict[str, Any]) -> Optional[str]
event = request.META['HTTP_X_GITHUB_EVENT']
if event == 'pull_request':
action = payload['action']
@@ -401,6 +403,9 @@ def get_event(request, payload):
return 'assigned_or_unassigned_pull_request'
if action == 'closed':
return 'closed_pull_request'
if action in ('labeled', 'unlabeled', 'review_requested', 'review_request_removed'):
logging.warn('Event pull_request with {} action is unsupported'.format(action))
return None
raise UnknownEventType(u'Event pull_request with {} action is unsupported'.format(action))
if event == 'push':
if is_commit_push_event(payload):