Skip to content

Commit b840557

Browse files
committed
Use utility paging calculation
1 parent a006e88 commit b840557

File tree

6 files changed

+9
-13
lines changed

6 files changed

+9
-13
lines changed

sonar/aggregations.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
import sonar.platform as pf
3232

3333
import sonar.components as comp
34-
34+
from sonar import utilities
3535
from sonar.audit.rules import get_rule
3636
from sonar.audit.problem import Problem
3737

@@ -110,5 +110,4 @@ def count(api: str, endpoint: pf.Platform, params: types.ApiParams = None) -> in
110110
if params is None:
111111
params = {}
112112
params["ps"] = 1
113-
data = json.loads(endpoint.get(api, params=params).text)
114-
return data["paging"]["total"]
113+
return utilities.nbr_total_elements(json.loads(endpoint.get(api, params=params).text))

sonar/applications.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -443,8 +443,7 @@ def count(endpoint: pf.Platform) -> int:
443443
:rtype: int
444444
"""
445445
check_supported(endpoint)
446-
data = json.loads(endpoint.get(APIS["search"], params={"ps": 1, "filter": "qualifier = APP"}).text)
447-
return data["paging"]["total"]
446+
return util.nbr_total_elements(json.loads(endpoint.get(APIS["search"], params={"ps": 1, "filter": "qualifier = APP"}).text))
448447

449448

450449
def check_supported(endpoint: pf.Platform) -> None:

sonar/components.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
2424
"""
2525
from __future__ import annotations
26+
import math
2627
import json
2728

2829
from datetime import datetime
@@ -104,9 +105,9 @@ def get_subcomponents(self, strategy: str = "children", with_issues: bool = Fals
104105
"metricKeys": "bugs,vulnerabilities,code_smells,security_hotspots",
105106
}
106107
data = json.loads(self.get("measures/component_tree", params=parms).text)
107-
nb_comp = data["paging"]["total"]
108+
nb_comp = utilities.nbr_total_elements(data)
108109
log.debug("Found %d subcomponents to %s", nb_comp, str(self))
109-
nb_pages = (nb_comp + 500 - 1) // 500
110+
nb_pages = math.ceil(nb_comp / 500)
110111
comp_list = {}
111112
parms["ps"] = 500
112113
for page in range(nb_pages):

sonar/measures.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,7 @@ def count_history(self, params: ApiParams = None) -> int:
8383
if params is None:
8484
params = {}
8585
params.update({"component": self.concerned_object.key, "metrics": self.metric, "ps": 1})
86-
data = json.loads(self.get(Measure.API_HISTORY, params=params).text)
87-
return data["paging"]["total"]
86+
return util.nbr_total_elements(json.loads(self.get(Measure.API_HISTORY, params=params).text))
8887

8988
def search_history(self, params: ApiParams = None) -> dict[str, any]:
9089
"""Searches the history of the measure

sonar/projects.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1321,8 +1321,7 @@ def count(endpoint: pf.Platform, params: types.ApiParams = None) -> int:
13211321
"""
13221322
new_params = {} if params is None else params.copy()
13231323
new_params.update({"ps": 1, "p": 1})
1324-
data = json.loads(endpoint.get(Project.SEARCH_API, params=params).text)
1325-
return data["paging"]["total"]
1324+
util.nbr_total_elements(json.loads(endpoint.get(Project.SEARCH_API, params=params).text))
13261325

13271326

13281327
def search(endpoint: pf.Platform, params: types.ApiParams = None) -> dict[str, Project]:

sonar/qualityprofiles.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -433,8 +433,7 @@ def projects(self) -> types.KeyList:
433433
self._projects += [p["key"] for p in data["results"]]
434434
page += 1
435435
if self.endpoint.version() >= (10, 0, 0):
436-
nb_pages = (data["paging"]["total"] + 500 - 1) // 500
437-
more = nb_pages >= page
436+
more = util.nbr_pages(data) >= page
438437
else:
439438
more = data["more"]
440439

0 commit comments

Comments
 (0)