Skip to content

Commit ff5cbb6

Browse files
committed
Move common migration data in component instead of projects/branches
1 parent 6db6f04 commit ff5cbb6

File tree

3 files changed

+31
-52
lines changed

3 files changed

+31
-52
lines changed

sonar/branches.py

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -220,9 +220,6 @@ def export(self, export_settings: types.ConfigSettings) -> types.ObjectJsonRepr:
220220
:return: The branch new code period definition
221221
:rtype: str
222222
"""
223-
from sonar.issues import count as issue_count
224-
from sonar.hotspots import count as hotspot_count
225-
226223
log.debug("Exporting %s", str(self))
227224
data = {settings.NEW_CODE_PERIOD: self.new_code()}
228225
if self.is_main():
@@ -234,29 +231,7 @@ def export(self, export_settings: types.ConfigSettings) -> types.ObjectJsonRepr:
234231
if export_settings.get("FULL_EXPORT", True):
235232
data.update({"name": self.name, "project": self.concerned_object.key})
236233
if export_settings.get("MODE", "") == "MIGRATION":
237-
data["lastAnalysis"] = util.date_to_string(self.last_analysis())
238-
lang_distrib = self.get_measure("ncloc_language_distribution")
239-
loc_distrib = {}
240-
if lang_distrib:
241-
loc_distrib = {m.split("=")[0]: int(m.split("=")[1]) for m in lang_distrib.split(";")}
242-
loc_distrib["total"] = self.loc()
243-
data["ncloc"] = loc_distrib
244-
tpissues = self.count_third_party_issues()
245-
inst_issues = self.count_instantiated_rules_issues()
246-
params = self.search_params()
247-
data["issues"] = {
248-
"thirdParty": tpissues if len(tpissues) > 0 else 0,
249-
"instantiatedRules": inst_issues if len(inst_issues) > 0 else 0,
250-
"falsePositives": issue_count(self.endpoint, issueStatuses=["FALSE_POSITIVE"], **params),
251-
}
252-
status = "accepted" if self.endpoint.version() >= (10, 2, 0) else "wontFix"
253-
data["issues"][status] = issue_count(self.endpoint, issueStatuses=[status.upper()], **params)
254-
data["hotspots"] = {
255-
"acknowledged": hotspot_count(self.endpoint, resolution=["ACKNOWLEDGED"], **params),
256-
"safe": hotspot_count(self.endpoint, resolution=["SAFE"], **params),
257-
"fixed": hotspot_count(self.endpoint, resolution=["FIXED"], **params),
258-
}
259-
log.debug("%s has these notable issues %s", str(self), str(data["issues"]))
234+
data.update(self.migration_export())
260235
data = util.remove_nones(data)
261236
return None if len(data) == 0 else data
262237

sonar/components.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,35 @@ def get_hotspots(self, filters: types.ApiParams = None) -> dict[str, object]:
166166
params.update(filters)
167167
return search(endpoint=self.endpoint, filters=params)
168168

169+
def migration_export(self) -> dict[str, any]:
170+
from sonar.issues import count as issue_count
171+
from sonar.hotspots import count as hotspot_count
172+
173+
json_data = {"lastAnalysis": utilities.date_to_string(self.last_analysis())}
174+
lang_distrib = self.get_measure("ncloc_language_distribution")
175+
loc_distrib = {}
176+
if lang_distrib:
177+
loc_distrib = {m.split("=")[0]: int(m.split("=")[1]) for m in lang_distrib.split(";")}
178+
loc_distrib["total"] = self.loc()
179+
json_data["ncloc"] = loc_distrib
180+
tpissues = self.count_third_party_issues()
181+
inst_issues = self.count_instantiated_rules_issues()
182+
params = self.search_params()
183+
json_data["issues"] = {
184+
"thirdParty": tpissues if len(tpissues) > 0 else 0,
185+
"instantiatedRules": inst_issues if len(inst_issues) > 0 else 0,
186+
"falsePositives": issue_count(self.endpoint, issueStatuses=["FALSE_POSITIVE"], **params),
187+
}
188+
status = "accepted" if self.endpoint.version() >= (10, 2, 0) else "wontFix"
189+
json_data["issues"][status] = issue_count(self.endpoint, issueStatuses=[status.upper()], **params)
190+
json_data["hotspots"] = {
191+
"acknowledged": hotspot_count(self.endpoint, resolution=["ACKNOWLEDGED"], **params),
192+
"safe": hotspot_count(self.endpoint, resolution=["SAFE"], **params),
193+
"fixed": hotspot_count(self.endpoint, resolution=["FIXED"], **params),
194+
}
195+
log.debug("%s has these notable issues %s", str(self), str(json_data["issues"]))
196+
return json_data
197+
169198
def get_measures(self, metrics_list: types.KeyList) -> dict[str, any]:
170199
"""Retrieves a project list of measures
171200

sonar/projects.py

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -953,9 +953,6 @@ def export(self, export_settings: types.ConfigSettings, settings_list: dict[str,
953953
:return: All project configuration settings
954954
:rtype: dict
955955
"""
956-
from sonar.issues import count as issue_count
957-
from sonar.hotspots import count as hotspot_count
958-
959956
log.info("Exporting %s", str(self))
960957
try:
961958
json_data = self._json.copy()
@@ -980,15 +977,9 @@ def export(self, export_settings: types.ConfigSettings, settings_list: dict[str,
980977
json_data = util.filter_export(json_data, _IMPORTABLE_PROPERTIES, export_settings.get("FULL_EXPORT", False))
981978

982979
if export_settings.get("MODE", "") == "MIGRATION":
983-
json_data["lastAnalysis"] = util.date_to_string(self.last_analysis())
980+
json_data.update(self.migration_export())
984981
json_data["detectedCi"] = self.ci()
985982
json_data["revision"] = self.revision()
986-
lang_distrib = self.get_measure("ncloc_language_distribution")
987-
loc_distrib = {}
988-
if lang_distrib:
989-
loc_distrib = {m.split("=")[0]: int(m.split("=")[1]) for m in lang_distrib.split(";")}
990-
loc_distrib["total"] = self.loc()
991-
json_data["ncloc"] = loc_distrib
992983
last_task = self.last_task()
993984
json_data["backgroundTasks"] = {}
994985
if last_task:
@@ -997,22 +988,6 @@ def export(self, export_settings: types.ConfigSettings, settings_list: dict[str,
997988
"lastTaskWarnings": last_task.warnings(),
998989
"taskHistory": [t._json for t in self.task_history()],
999990
}
1000-
tpissues = self.count_third_party_issues()
1001-
inst_issues = self.count_instantiated_rules_issues()
1002-
params = self.search_params()
1003-
json_data["issues"] = {
1004-
"thirdParty": tpissues if len(tpissues) > 0 else 0,
1005-
"instantiatedRules": inst_issues if len(inst_issues) > 0 else 0,
1006-
"falsePositives": issue_count(self.endpoint, issueStatuses=["FALSE_POSITIVE"], **params),
1007-
}
1008-
status = "accepted" if self.endpoint.version() >= (10, 2, 0) else "wontFix"
1009-
json_data["issues"][status] = issue_count(self.endpoint, issueStatuses=[status.upper()], **params)
1010-
json_data["hotspots"] = {
1011-
"acknowledged": hotspot_count(self.endpoint, resolution=["ACKNOWLEDGED"], **params),
1012-
"safe": hotspot_count(self.endpoint, resolution=["SAFE"], **params),
1013-
"fixed": hotspot_count(self.endpoint, resolution=["FIXED"], **params),
1014-
}
1015-
log.debug("%s has these notable issues %s", str(self), str(json_data["issues"]))
1016991

1017992
settings_dict = settings.get_bulk(endpoint=self.endpoint, component=self, settings_list=settings_list, include_not_set=False)
1018993
# json_data.update({s.to_json() for s in settings_dict.values() if include_inherited or not s.inherited})

0 commit comments

Comments
 (0)