reviewboard.notifications.webhooks¶
-
class
FakeHTTPRequest
(user, local_site_name=None)[source]¶ Bases:
django.http.request.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:
django.template.base.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.
-
render_custom_content
(body, context_data={})[source]¶ Renders 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 %}.
-
normalize_webhook_payload
(payload, request)[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.
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 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.