4646import sonar .audit .severities as sev
4747import sonar .audit .types as typ
4848from sonar .audit .problem import Problem
49+ from sonar import webhooks
4950
5051WRONG_CONFIG_MSG = "Audit config property %s has wrong value %s, skipping audit"
5152
@@ -442,13 +443,10 @@ def __urlstring(self, api: str, params: types.ApiParams, data: str = None) -> st
442443 url += f" - BODY: { data } "
443444 return url
444445
445- def webhooks (self ) -> dict [str , object ]:
446+ def webhooks (self ) -> dict [str , webhooks . WebHook ]:
446447 """
447448 :return: the list of global webhooks
448- :rtype: dict{<webhook_name>: <webhook_data>, ...}
449449 """
450- from sonar import webhooks
451-
452450 return webhooks .get_list (self )
453451
454452 def export (self , export_settings : types .ConfigSettings , full : bool = False ) -> types .ObjectJsonRepr :
@@ -491,16 +489,12 @@ def set_webhooks(self, webhooks_data: types.ObjectJsonRepr) -> bool:
491489 """Sets global webhooks with a list of webhooks represented as JSON
492490
493491 :param webhooks_data: the webhooks JSON representation
494- :return: Whether the operation succeeded or not
492+ :return: The number of webhooks configured
495493 """
494+ log .debug ("%s setting webhooks %s" , str (self ), str (webhooks_data ))
496495 if webhooks_data is None :
497496 return False
498- current_wh = self .webhooks ()
499- # FIXME: Handle several webhooks with same name
500- current_wh_names = [wh .name for wh in current_wh .values ()]
501- wh_map = {wh .name : k for k , wh in current_wh .items ()}
502- log .debug ("Current webhooks = %s" , str (current_wh_names ))
503- _ = [current_wh [wh_map [wh_name ]].update (name = wh_name , ** wh ) for wh_name , wh in webhooks_data .items () if wh_name in current_wh_names ]
497+ webhooks .import_config (self , webhooks_data )
504498 return True
505499
506500 def import_config (self , config_data : types .ObjectJsonRepr ) -> int :
@@ -515,9 +509,15 @@ def import_config(self, config_data: types.ObjectJsonRepr) -> int:
515509 count = 0
516510 config_data = config_data .get ("globalSettings" , {})
517511 flat_settings = util .flatten (config_data )
518- count += sum (1 if self .set_webhooks (v ) else 0 for k , v in config_data .get ("webhooks" , None ) or {})
519512 count += sum (1 if self .set_setting (k , v ) else 0 for k , v in flat_settings .items ())
520513
514+ try :
515+ wh_data = config_data ["generalSettings" ]["webhooks" ]
516+ self .set_webhooks (wh_data )
517+ count += len (wh_data )
518+ except KeyError :
519+ pass
520+
521521 if settings .NEW_CODE_PERIOD in config_data .get ("generalSettings" , {}):
522522 (nc_type , nc_val ) = settings .decode (settings .NEW_CODE_PERIOD , config_data ["generalSettings" ][settings .NEW_CODE_PERIOD ])
523523 try :
0 commit comments