@@ -336,8 +336,27 @@ class NotificationTransport(SerializerModel):
336
336
mode = models .TextField (choices = TransportMode .choices , default = TransportMode .LOCAL )
337
337
338
338
webhook_url = models .TextField (blank = True , validators = [DomainlessURLValidator ()])
339
- webhook_mapping = models .ForeignKey (
340
- "NotificationWebhookMapping" , on_delete = models .SET_DEFAULT , null = True , default = None
339
+ webhook_mapping_body = models .ForeignKey (
340
+ "NotificationWebhookMapping" ,
341
+ on_delete = models .SET_DEFAULT ,
342
+ null = True ,
343
+ default = None ,
344
+ related_name = "+" ,
345
+ help_text = _ (
346
+ "Customize the body of the request. "
347
+ "Mapping should return data that is JSON-serializable."
348
+ ),
349
+ )
350
+ webhook_mapping_headers = models .ForeignKey (
351
+ "NotificationWebhookMapping" ,
352
+ on_delete = models .SET_DEFAULT ,
353
+ null = True ,
354
+ default = None ,
355
+ related_name = "+" ,
356
+ help_text = _ (
357
+ "Configure additional headers to be sent. "
358
+ "Mapping should return a dictionary of key-value pairs"
359
+ ),
341
360
)
342
361
send_once = models .BooleanField (
343
362
default = False ,
@@ -360,8 +379,8 @@ def send(self, notification: "Notification") -> list[str]:
360
379
361
380
def send_local (self , notification : "Notification" ) -> list [str ]:
362
381
"""Local notification delivery"""
363
- if self .webhook_mapping :
364
- self .webhook_mapping .evaluate (
382
+ if self .webhook_mapping_body :
383
+ self .webhook_mapping_body .evaluate (
365
384
user = notification .user ,
366
385
request = None ,
367
386
notification = notification ,
@@ -380,9 +399,18 @@ def send_webhook(self, notification: "Notification") -> list[str]:
380
399
if notification .event and notification .event .user :
381
400
default_body ["event_user_email" ] = notification .event .user .get ("email" , None )
382
401
default_body ["event_user_username" ] = notification .event .user .get ("username" , None )
383
- if self .webhook_mapping :
402
+ headers = {}
403
+ if self .webhook_mapping_body :
384
404
default_body = sanitize_item (
385
- self .webhook_mapping .evaluate (
405
+ self .webhook_mapping_body .evaluate (
406
+ user = notification .user ,
407
+ request = None ,
408
+ notification = notification ,
409
+ )
410
+ )
411
+ if self .webhook_mapping_headers :
412
+ headers = sanitize_item (
413
+ self .webhook_mapping_headers .evaluate (
386
414
user = notification .user ,
387
415
request = None ,
388
416
notification = notification ,
@@ -392,6 +420,7 @@ def send_webhook(self, notification: "Notification") -> list[str]:
392
420
response = get_http_session ().post (
393
421
self .webhook_url ,
394
422
json = default_body ,
423
+ headers = headers ,
395
424
)
396
425
response .raise_for_status ()
397
426
except RequestException as exc :
0 commit comments