mirror of
https://github.com/ansible/awx.git
synced 2026-05-07 17:37:37 -02:30
Use custom webhook bodies as is (instead of as a sub-field in webhook)
This commit is contained in:
@@ -4227,6 +4227,28 @@ class NotificationTemplateSerializer(BaseSerializer):
|
|||||||
error_list.append(_("Field '{}' unavailable".format(exc.message)))
|
error_list.append(_("Field '{}' unavailable".format(exc.message)))
|
||||||
except SecurityError as exc:
|
except SecurityError as exc:
|
||||||
error_list.append(_("Security error due to field '{}'".format(exc.message)))
|
error_list.append(_("Security error due to field '{}'".format(exc.message)))
|
||||||
|
|
||||||
|
# Ensure that if a webhook body was provided, that it can be rendered as a dictionary
|
||||||
|
notification_type = ''
|
||||||
|
if self.instance:
|
||||||
|
notification_type = getattr(self.instance, 'notification_type', '')
|
||||||
|
else:
|
||||||
|
notification_type = self.initial_data.get('notification_type', '')
|
||||||
|
|
||||||
|
if notification_type == 'webhook':
|
||||||
|
for event in messages:
|
||||||
|
if not messages[event]:
|
||||||
|
continue
|
||||||
|
body = messages[event].get('body', {})
|
||||||
|
if body:
|
||||||
|
try:
|
||||||
|
potential_body = json.loads(body)
|
||||||
|
if not isinstance(potential_body, dict):
|
||||||
|
error_list.append(_("Webhook body for '{}' should be a json dictionary. Found type '{}'."
|
||||||
|
.format(event, type(potential_body).__name__)))
|
||||||
|
except json.JSONDecodeError as exc:
|
||||||
|
error_list.append(_("Webhook body for '{}' is not a valid json dictionary ({}).".format(event, exc)))
|
||||||
|
|
||||||
if error_list:
|
if error_list:
|
||||||
raise serializers.ValidationError(error_list)
|
raise serializers.ValidationError(error_list)
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
# Copyright (c) 2016 Ansible, Inc.
|
# Copyright (c) 2016 Ansible, Inc.
|
||||||
# All Rights Reserved.
|
# All Rights Reserved.
|
||||||
|
|
||||||
|
import json
|
||||||
import logging
|
import logging
|
||||||
import requests
|
import requests
|
||||||
|
|
||||||
@@ -37,6 +38,15 @@ class WebhookBackend(AWXBaseEmailBackend):
|
|||||||
super(WebhookBackend, self).__init__(fail_silently=fail_silently)
|
super(WebhookBackend, self).__init__(fail_silently=fail_silently)
|
||||||
|
|
||||||
def format_body(self, body):
|
def format_body(self, body):
|
||||||
|
# If `body` has body field, attempt to use this as the main body,
|
||||||
|
# otherwise, leave it as a sub-field
|
||||||
|
if isinstance(body, dict) and 'body' in body and isinstance(body['body'], str):
|
||||||
|
try:
|
||||||
|
potential_body = json.loads(body['body'])
|
||||||
|
if isinstance(potential_body, dict):
|
||||||
|
body = potential_body
|
||||||
|
except json.JSONDecodeError:
|
||||||
|
pass
|
||||||
return body
|
return body
|
||||||
|
|
||||||
def send_messages(self, messages):
|
def send_messages(self, messages):
|
||||||
|
|||||||
Reference in New Issue
Block a user