Skip to content

Commit 4536f0c

Browse files
authored
Merge pull request #1280 from okorach:fix-1275
Fixes
2 parents d3cbbc0 + e7fdcc3 commit 4536f0c

File tree

5 files changed

+24
-8
lines changed

5 files changed

+24
-8
lines changed

sonar-project.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ sonar.python.pylint.reportPaths=build/pylint-report.out
1313
# sonar.python.bandit.reportPaths=build/bandit-report.json
1414

1515
sonar.exclusions=api-doc/**/*, build/**/*, test/**/*
16-
sonar.coverage.exclusions=test/**/*, shellcheck2sonar.py, cli/cust_measures.py, sonar/custom_measures.py, cli/support.py
16+
sonar.coverage.exclusions=test/**/*, shellcheck2sonar.py, cli/cust_measures.py, sonar/custom_measures.py, cli/support.py, cli/sonar-projects-*.py
1717

1818
sonar.tests=test

sonar/devops.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ def create(cls, endpoint: platform.Platform, key: str, plt_type: str, url_or_wor
110110
params.update({"clientSecret": _TO_BE_SET, "clientId": _TO_BE_SET, "workspace": url_or_workspace})
111111
endpoint.post(_CREATE_API_BBCLOUD, params=params)
112112
except HTTPError as e:
113-
if e.response.status_code == HTTPStatus.BAD_REQUEST and endpoint.edition() == "developer":
113+
if e.response.status_code == HTTPStatus.BAD_REQUEST and endpoint.edition() in ("community", "developer"):
114114
log.warning("Can't set DevOps platform '%s', don't you have more that 1 of that type?", key)
115115
raise exceptions.UnsupportedOperation(f"Can't set DevOps platform '{key}', don't you have more that 1 of that type?")
116116
raise
@@ -267,7 +267,11 @@ def import_config(endpoint: platform.Platform, config_data: types.ObjectJsonRepr
267267
o = DevopsPlatform.read(endpoint, name)
268268
except exceptions.ObjectNotFound:
269269
info = data["workspace"] if data["type"] == "bitbucketcloud" else data["url"]
270-
o = DevopsPlatform.create(key=name, endpoint=endpoint, plt_type=data["type"], url_or_workspace=info)
270+
try:
271+
o = DevopsPlatform.create(key=name, endpoint=endpoint, plt_type=data["type"], url_or_workspace=info)
272+
except exceptions.UnsupportedOperation as e:
273+
log.error(str(e))
274+
continue
271275
o.update(**data)
272276

273277

sonar/portfolios.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545

4646
from sonar.portfolio_reference import PortfolioReference
4747

48-
_OBJECTS = {}
4948
_CLASS_LOCK = Lock()
5049

5150
_CREATE_API = "views/create"
@@ -90,6 +89,8 @@ class Portfolio(aggregations.Aggregation):
9089
SEARCH_KEY_FIELD = "key"
9190
SEARCH_RETURN_FIELD = "components"
9291

92+
_OBJECTS = {}
93+
9394
def __init__(self, endpoint: pf.Platform, name: str, key: str = None) -> None:
9495
"""Constructor, don't use - use class methods instead"""
9596
if not key:
@@ -107,7 +108,7 @@ def __init__(self, endpoint: pf.Platform, name: str, key: str = None) -> None:
107108
self.is_sub_portfolio = None
108109
self.parent = None #: Ref to parent portfolio object, if any
109110
self._root_portfolio = None #: Ref to root portfolio, if any
110-
_OBJECTS[self.uuid()] = self
111+
Portfolio._OBJECTS[self.uuid()] = self
111112
log.debug("Created portfolio object name '%s'", name)
112113

113114
@classmethod
@@ -116,8 +117,8 @@ def get_object(cls, endpoint: pf.Platform, key: str) -> Portfolio:
116117
check_supported(endpoint)
117118
log.debug("Getting portfolio object key '%s'", key)
118119
uid = sq.uuid(key, endpoint.url)
119-
if uid in _OBJECTS:
120-
return _OBJECTS[uid]
120+
if uid in Portfolio._OBJECTS:
121+
return Portfolio._OBJECTS[uid]
121122
data = search_by_key(endpoint, key)
122123
if data is None:
123124
raise exceptions.ObjectNotFound(key, f"Portfolio key '{key}' not found")
@@ -311,7 +312,7 @@ def get_components(self) -> types.ApiPayload:
311312

312313
def delete(self) -> bool:
313314
"""Deletes a portfolio, returns whether the operation succeeded"""
314-
return sq.delete_object(self, "views/delete", {"key": self.key}, _OBJECTS)
315+
return sq.delete_object(self, "views/delete", {"key": self.key}, Portfolio._OBJECTS)
315316

316317
def _audit_empty(self, audit_settings: types.ConfigSettings) -> list[problem.Problem]:
317318
"""Audits if a portfolio is empty (no projects)"""
@@ -623,6 +624,7 @@ def get_list(endpoint: pf.Platform, key_list: types.KeyList = None, use_cache: b
623624
if key_list is None or len(key_list) == 0 or not use_cache:
624625
log.info("Listing portfolios")
625626
object_list = search(endpoint=endpoint)
627+
log.info("List = %s", ", ".join(list(object_list.keys())))
626628
return object_list
627629
object_list = {}
628630
for key in util.csv_to_list(key_list):

sonar/sqobject.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,15 @@ def get_search_api(cls, endpoint: object) -> Optional[str]:
5656
api = cls.SEARCH_API
5757
return api
5858

59+
@classmethod
60+
def empty_cache(cls) -> None:
61+
"""Empties the cache of objects of the given class"""
62+
log.info("Emptying cache of %s", str(cls))
63+
try:
64+
cls._OBJECTS = {}
65+
except AttributeError:
66+
pass
67+
5968
def uuid(self) -> str:
6069
"""Returns object unique ID in its class"""
6170
return uuid(self.key, self.endpoint.url)

test/test_config.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ def test_config_import_portfolios() -> None:
148148
# delete all portfolios in test
149149
logging.set_debug_level("DEBUG")
150150
logging.info("Deleting all portfolios")
151+
portfolios.Portfolio.empty_cache()
151152
for p in portfolios.get_list(util.TEST_SQ).values():
152153
if p.is_toplevel():
153154
p.delete()

0 commit comments

Comments
 (0)