integrations: Add webhook code, API endpoint, and tests for mention.

This commit is contained in:
Vamshi Balanaga 2016-12-03 20:50:29 -05:00 committed by Tim Abbott
parent 4634c3d656
commit f89e732d9e
4 changed files with 61 additions and 0 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

View File

@ -120,6 +120,7 @@ WEBHOOK_INTEGRATIONS = [
WebhookIntegration('ifttt', function='zerver.views.webhooks.ifttt.api_iftt_app_webhook', display_name='IFTTT'),
WebhookIntegration('jira', secondary_line_text='(hosted or v5.2+)', display_name='JIRA'),
WebhookIntegration('librato'),
WebhookIntegration('mention', display_name='Mention'),
WebhookIntegration('newrelic', display_name='New Relic'),
WebhookIntegration('pagerduty'),
WebhookIntegration('pingdom'),

View File

@ -0,0 +1,26 @@
# -*- coding: utf-8 -*-
from typing import Text
from zerver.lib.test_classes import WebhookTestCase
class MentionHookTests(WebhookTestCase):
STREAM_NAME = 'test'
URL_TEMPLATE = "/api/v1/external/mention?api_key={api_key}&stream={stream}"
FIXTURE_DIR_NAME = 'mention'
def test_mention_webfeed(self):
# type: () -> None
expected_topic = u"news"
expected_message = (u"**[Historical Sexual Abuse (Football): 29 Nov 2016: House of Commons debates - TheyWorkForYou]"
u"(https://www.theyworkforyou.com/debates/?id=2016-11-29b.1398.7&p=24887)**:\n"
u"\u2026 Culture, Media and Sport\nNothing is more important than keeping children safe."
u" Child sex abuse is an exceptionally vile crime, and all of Government take it very seriously indeed,"
u" as I know this House does.\nChildren up and down the country are \u2026"
)
# use fixture named mention_webfeeds
self.send_and_test_stream_message('webfeeds', expected_topic, expected_message,
content_type="application/x-www-form-urlencoded")
def get_body(self, fixture_name):
# type: (Text) -> Text
return self.fixture_data("mention", fixture_name, file_type="json")

View File

@ -0,0 +1,34 @@
# Webhooks for external integrations.
from __future__ import absolute_import
from django.utils.translation import ugettext as _
from zerver.lib.actions import check_send_message
from zerver.lib.response import json_success, json_error
from zerver.decorator import REQ, has_request_variables, api_key_only_webhook_view
from zerver.lib.validator import check_dict, check_string
from zerver.models import Client, UserProfile
from django.http import HttpRequest, HttpResponse
from typing import Dict, Any, Iterable, Optional, Text
@api_key_only_webhook_view('Mention')
@has_request_variables
def api_mention_webhook(request, user_profile, client,
payload=REQ(argument_type='body'),
stream=REQ(default='mention'),
topic=REQ(default='news')):
# type: (HttpRequest, UserProfile, Client, Dict[str, Iterable[Dict[str, Any]]], Text, Optional[Text]) -> HttpResponse
try:
title = payload["title"]
source_url = payload["url"]
description = payload["description"]
except KeyError as e:
return json_error(_("Missing key {} in JSON").format(str(e)))
# construct the body of the message
body = '**[%s](%s)**:\n%s' % (title, source_url, description)
# send the message
check_send_message(user_profile, client, 'stream', [stream], topic, body)
return json_success()