Skip to content

Commit 23bc5d3

Browse files
authored
Import webhooks (#1998)
* Fixes #1989 * Quality pass
1 parent ad8331e commit 23bc5d3

File tree

4 files changed

+30
-23
lines changed

4 files changed

+30
-23
lines changed

sonar/platform.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import sonar.audit.severities as sev
4747
import sonar.audit.types as typ
4848
from sonar.audit.problem import Problem
49+
from sonar import webhooks
4950

5051
WRONG_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:

sonar/projects.py

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1226,16 +1226,7 @@ def set_webhooks(self, webhook_data: types.ObjectJsonRepr) -> None:
12261226
:param dict webhook_data: JSON describing the webhooks
12271227
:return: Nothing
12281228
"""
1229-
current_wh = self.webhooks()
1230-
current_wh_names = [wh.name for wh in current_wh.values()]
1231-
wh_map = {wh.name: k for k, wh in current_wh.items()}
1232-
# FIXME: Handle several webhooks with same name
1233-
for wh_name, wh in webhook_data.items():
1234-
if wh_name in current_wh_names:
1235-
current_wh[wh_map[wh_name]].update(name=wh_name, **wh)
1236-
else:
1237-
hook = webhooks.WebHook.create(endpoint=self.endpoint, name=wh_name, url="", project=self.key)
1238-
hook.update(**wh)
1229+
webhooks.import_config(self.endpoint, webhook_data, self.key)
12391230

12401231
def set_settings(self, data: types.ObjectJsonRepr) -> None:
12411232
"""Sets project settings (webhooks, settings, new code period)

sonar/webhooks.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ def create(cls, endpoint: pf.Platform, name: str, url: str, secret: Optional[str
7070
:param str project: Webhook project key, optional
7171
:return: The created WebHook
7272
"""
73+
log.info("Creating webhook name %s, url %s project %s", name, url, str(project))
7374
params = util.remove_nones({"name": name, "url": url, "secret": secret, "project": project})
7475
try:
7576
endpoint.post(WebHook.API[c.CREATE], params=params)
@@ -201,6 +202,22 @@ def export(endpoint: pf.Platform, project_key: str = None, full: bool = False) -
201202
return json_data if len(json_data) > 0 else None
202203

203204

205+
def import_config(endpoint: pf.Platform, data: types.ObjectJsonRepr, project_key: Optional[str] = None) -> None:
206+
"""Imports a set of webhooks defined from a JSON description"""
207+
208+
log.debug("Importing webhooks %s for %s", str(data), str(project_key))
209+
current_wh = get_list(endpoint, project_key=project_key)
210+
existing_webhooks = {wh.name: k for k, wh in current_wh.items()}
211+
212+
# FIXME: Handle several webhooks with same name
213+
for wh_name, wh_data in data.items():
214+
if wh_name in existing_webhooks:
215+
current_wh[existing_webhooks[wh_name]].update(name=wh_name, **wh_data)
216+
else:
217+
hook = WebHook.create(endpoint=endpoint, name=wh_name, url=wh_data.get("url", "https://to.be.defined"), project=project_key)
218+
hook.update(**wh_data)
219+
220+
204221
def audit(endpoint: pf.Platform) -> list[problem.Problem]:
205222
"""Audits web hooks and returns list of found problems"""
206223
log.info("Auditing webhooks")

test/unit/test_webhooks.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,3 @@ def test_create_delete() -> None:
9595
hook.delete()
9696
with pytest.raises(exceptions.ObjectNotFound):
9797
hook.refresh()
98-

0 commit comments

Comments
 (0)