reviewboard.notifications.webhooks¶
- class FakeHTTPRequest(user, local_site_name=None)[source]¶
Bases:
HttpRequest
A fake HttpRequest implementation.
The WebAPI serialization methods use HttpRequest.build_absolute_uri to generate all the links, but none of the various signals that generate webhook events have the request plumbed through. Since we don’t actually need a valid request, this impersonates it enough to get valid results from build_absolute_uri.
- __init__(user, local_site_name=None)[source]¶
Initialize a FakeHTTPRequest.
- Parameters:
user (
django.contrib.auth.models.User
) – The user who initiated the request.local_site_name (
unicode
, optional) – The local site name (if the request was carried out against a local site).
- class CustomPayloadParser(*args, **kwargs)[source]¶
Bases:
Parser
A custom template parser that blocks certain tags.
This extends Django’s Parser class for template parsing, and removes some built-in tags, in order to prevent mailicious use.
- invalid_block_tag(token, command, parse_until=None)[source]¶
Raise an error when an invalid block tag is found.
Normally Django produces a suitable error, but in modern versions of Django, the error is _too_ helpful, reminding the user to register or load the tag. This isn’t useful for WebHooks, so we override to use the older, simpler message used in Django 1.6.
- render_custom_content(body, context_data={})[source]¶
Render custom content for the payload using Django templating.
This will take the custom payload content template provided by the user and render it using a stripped down version of Django’s templating system.
In order to keep the payload safe, we use a limited Context along with a custom Parser that blocks certain template tags. This gives us tags like
{% for %}
and{% if %}
, but blacklists tags like{% load %}
and{% include %}
.- Parameters:
- Returns:
The rendered template.
- Return type:
- Raises:
django.template.TemplateSyntaxError – There was a syntax error in the template.
- normalize_webhook_payload(payload, request, use_string_keys=False)[source]¶
Normalize a payload for a WebHook, returning a safe, primitive version.
This will take a payload containing various data types and model references and turn it into a payload built out of specific, whitelisted types (strings, bools, ints, dicts, lists, and datetimes). This payload is safe to include in custom templates without worrying about access to dangerous functions, and is easy to serialize.
- Parameters:
payload (
dict
) – The payload to normalize.request (
django.http.HttpRequest
) – The HTTP request from the client.use_string_keys (
bool
, optional) – Whether to normalize all keys to strings.
- Returns:
The normalized payload.
- Return type:
- Raises:
TypeError – An unsupported data type was found in the payload. This is an issue with the caller.
- dispatch_webhook_event(request, webhook_targets, event, payload)[source]¶
Dispatch the given event and payload to the given WebHook targets.
- Parameters:
request (
django.http.HttpRequest
) – The HTTP request from the client.webhook_targets (
list
of :class:`` :class:`` :class:`` :class:`` :class:`` :class:`` :class:`` :class:`` :class:`` :class:`` :class:`` :class:`` :class:`` :class:`` :class:`` :class:`` :class:`` :class:`` :class:`` :class:`` :class:``reviewboard.notifications.models.WebHookTarget
) – The list of WebHook targets containing endpoint URLs to dispatch to.event (
unicode
) – The name of the event being dispatched.payload (
dict
) – The payload data to encode for the WebHook payload.
- Raises:
ValueError – There was an error with the payload format. Details are in the log and the exception message.