Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Performance-for-ai-code-fix #1547

Merged
merged 5 commits into from
Dec 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions sonar/platform.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,20 @@ def __run_request(self, request: callable, api: str, params: types.ApiParams = N
util.handle_error(e, "")
return r

def get_paginated(self, api: str, return_field: str, params: types.ApiParams = None) -> types.ObjectJsonRepr:
"""Returns all pages of a paginated API"""
new_params = {} if params is None else params.copy()
new_params["ps"] = 500
new_params["p"] = 1
data = json.loads(self.get(api, params=new_params).text)
nb_pages = util.nbr_pages(data, api_version=1)
if nb_pages == 1:
return data
for page in range(2, nb_pages + 1):
new_params["p"] = page
data[return_field].update(json.loads(self.get(api, params=new_params).text)[return_field])
return data

def global_permissions(self) -> dict[str, any]:
"""Returns the SonarQube platform global permissions

Expand Down Expand Up @@ -938,11 +952,3 @@ def audit(endpoint: Platform, audit_settings: types.ConfigSettings, **kwargs) ->
if "write_q" in kwargs:
kwargs["write_q"].put(pbs)
return pbs


def log_and_exit(exception: Exception) -> None:
"""If HTTP response is not OK, display an error log and exit"""
err_code, msg = util.http_error_and_code(exception)
if err_code is None:
return
util.exit_fatal(msg, err_code)
21 changes: 14 additions & 7 deletions sonar/projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
"visibility",
"qualityGate",
"webhooks",
"aiCodeFix",
)

_UNNEEDED_CONTEXT_DATA = (
Expand Down Expand Up @@ -631,19 +632,16 @@ def revision(self) -> str:
return self._revision

def ai_code_fix(self) -> Optional[str]:
"""Returns whether this porject is enabled for AI Code Fix (if only enabled per project)"""
log.debug("Getting project AI Code Fix suggestion flag")
"""Returns whether this project is enabled for AI Code Fix (if only enabled per project)"""
log.debug("Getting project AI Code Fix suggestion flag for %s", str(self))
global_setting = settings.Setting.read(key=settings.AI_CODE_FIX, endpoint=self.endpoint)
log.debug("Global Setting = %s JSON = %s", str(global_setting.value), util.json_dump(self.sq_json))
if not global_setting or global_setting.value != "ENABLED_FOR_SOME_PROJECTS":
return None
if "isAiCodeFixEnabled" not in self.sq_json:
r = self.get("components/search_projects")
data = json.loads(r.text)
data = self.endpoint.get_paginated(api="components/search_projects", params={"filter": "qualifier=TRK"}, return_field="components")
p_data = next((p for p in data["components"] if p["key"] == self.key), None)
if p_data:
self.sq_json.update(p_data)
log.debug("RETURNING = %s", str(self.sq_json.get("isAiCodeFixEnabled", None)))
return self.sq_json.get("isAiCodeFixEnabled", None)

def __audit_scanner(self, audit_settings: types.ConfigSettings) -> list[Problem]:
Expand Down Expand Up @@ -1484,7 +1482,16 @@ def get_list(endpoint: pf.Platform, key_list: types.KeyList = None, use_cache: b
with _CLASS_LOCK:
if key_list is None or len(key_list) == 0 or not use_cache:
log.info("Listing projects")
return dict(sorted(search(endpoint=endpoint).items()))
p_list = dict(sorted(search(endpoint=endpoint).items()))
global_setting = settings.Setting.read(key=settings.AI_CODE_FIX, endpoint=endpoint)
if not global_setting or global_setting.value != "ENABLED_FOR_SOME_PROJECTS":
return p_list
for d in endpoint.get_paginated(api="components/search_projects", params={"filter": "qualifier=TRK"}, return_field="components")[
"components"
]:
if d["key"] in p_list:
p_list[d["key"]].sq_json.update(d)
return p_list
return {key: Project.get_object(endpoint, key) for key in sorted(key_list)}


Expand Down
Loading