diff options
Diffstat (limited to 'server/sonar-server')
5 files changed, 68 insertions, 46 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/component/ws/ComponentAppAction.java b/server/sonar-server/src/main/java/org/sonar/server/component/ws/ComponentAppAction.java index e73451be671..f853ce26652 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/component/ws/ComponentAppAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/component/ws/ComponentAppAction.java @@ -211,11 +211,13 @@ public class ComponentAppAction implements RequestHandler { private void appendMeasures(JsonWriter json, Map<String, MeasureDto> measuresByMetricKey, Multiset<String> severitiesAggregation, Integer periodIndex) { json.name("measures").beginObject(); - json.prop("fNcloc", formatMeasure(measuresByMetricKey.get(CoreMetrics.NCLOC_KEY), periodIndex)); - json.prop("fCoverage", formatMeasure(measuresByMetricKey.get(CoreMetrics.COVERAGE_KEY), periodIndex)); - json.prop("fDuplicationDensity", formatMeasure(measuresByMetricKey.get(CoreMetrics.DUPLICATED_LINES_DENSITY_KEY), periodIndex)); - json.prop("fDebt", formatMeasure(measuresByMetricKey.get(CoreMetrics.TECHNICAL_DEBT_KEY), periodIndex)); - json.prop("fTests", formatMeasure(measuresByMetricKey.get(CoreMetrics.TESTS_KEY), periodIndex)); + json.prop("fNcloc", formatMeasureOrVariation(measuresByMetricKey.get(CoreMetrics.NCLOC_KEY), periodIndex)); + json.prop("fCoverage", formatMeasureOrVariation(measuresByMetricKey.get(CoreMetrics.COVERAGE_KEY), periodIndex)); + json.prop("fDuplicationDensity", formatMeasureOrVariation(measuresByMetricKey.get(CoreMetrics.DUPLICATED_LINES_DENSITY_KEY), periodIndex)); + json.prop("fDebt", formatMeasureOrVariation(measuresByMetricKey.get(CoreMetrics.TECHNICAL_DEBT_KEY), periodIndex)); + json.prop("fSqaleRating", formatMeasureOrVariation(measuresByMetricKey.get(CoreMetrics.SQALE_RATING_KEY), periodIndex)); + json.prop("fSqaleDebtRatio", formatMeasureOrVariation(measuresByMetricKey.get(CoreMetrics.SQALE_DEBT_RATIO_KEY), periodIndex)); + json.prop("fTests", formatMeasureOrVariation(measuresByMetricKey.get(CoreMetrics.TESTS_KEY), periodIndex)); json.prop("fIssues", i18n.formatInteger(UserSession.get().locale(), severitiesAggregation.size())); for (String severity : severitiesAggregation.elementSet()) { @@ -268,7 +270,7 @@ public class ComponentAppAction implements RequestHandler { json.beginObject() .prop("key", manualRule.key().toString()) .prop("name", manualRule.name()) - .endObject(); + .endObject(); } json.endArray(); } @@ -334,7 +336,7 @@ public class ComponentAppAction implements RequestHandler { for (MeasureDto measureDto : dbClient.measureDao().findByComponentKeyAndMetricKeys(fileKey, newArrayList(CoreMetrics.NCLOC_KEY, CoreMetrics.COVERAGE_KEY, CoreMetrics.DUPLICATED_LINES_KEY, CoreMetrics.DUPLICATED_LINES_DENSITY_KEY, CoreMetrics.TECHNICAL_DEBT_KEY, CoreMetrics.TESTS_KEY, - CoreMetrics.SCM_AUTHORS_BY_LINE_KEY), + CoreMetrics.SCM_AUTHORS_BY_LINE_KEY, CoreMetrics.SQALE_RATING_KEY, CoreMetrics.SQALE_DEBT_RATIO_KEY), session)) { measuresByMetricKey.put(measureDto.getKey().metricKey(), measureDto); } @@ -368,51 +370,58 @@ public class ComponentAppAction implements RequestHandler { } @CheckForNull - private String formatMeasure(@Nullable MeasureDto measure, @Nullable Integer periodIndex) { - if (measure != null) { - Metric metric = CoreMetrics.getMetric(measure.getKey().metricKey()); - if (periodIndex == null) { - Double value = measure.getValue(); - if (value != null) { - return formatValue(value, metric.getType()); - } - } else { - Double variation = measure.getVariation(periodIndex); - if (variation != null) { - return formatVariation(variation, metric.getType()); - } - } + private String formatMeasureOrVariation(@Nullable MeasureDto measure, @Nullable Integer periodIndex) { + if (periodIndex == null) { + return formatMeasure(measure); + } else { + return formatVariation(measure, periodIndex); } - return null; } @CheckForNull - private String formatValue(Double value, Metric.ValueType metricType) { - if (metricType.equals(Metric.ValueType.FLOAT)) { - return i18n.formatDouble(UserSession.get().locale(), value); - } - if (metricType.equals(Metric.ValueType.INT)) { - return i18n.formatInteger(UserSession.get().locale(), value.intValue()); - } - if (metricType.equals(Metric.ValueType.PERCENT)) { - return i18n.formatDouble(UserSession.get().locale(), value) + "%"; - } - if (metricType.equals(Metric.ValueType.WORK_DUR)) { - return durations.format(UserSession.get().locale(), durations.create(value.longValue()), Durations.DurationFormat.SHORT); + private String formatMeasure(@Nullable MeasureDto measure) { + if (measure != null) { + Metric metric = CoreMetrics.getMetric(measure.getKey().metricKey()); + Metric.ValueType metricType = metric.getType(); + Double value = measure.getValue(); + String data = measure.getData(); + + if (metricType.equals(Metric.ValueType.FLOAT) && value != null) { + return i18n.formatDouble(UserSession.get().locale(), value); + } + if (metricType.equals(Metric.ValueType.INT) && value != null) { + return i18n.formatInteger(UserSession.get().locale(), value.intValue()); + } + if (metricType.equals(Metric.ValueType.PERCENT) && value != null) { + return i18n.formatDouble(UserSession.get().locale(), value) + "%"; + } + if (metricType.equals(Metric.ValueType.WORK_DUR) && value != null) { + return durations.format(UserSession.get().locale(), durations.create(value.longValue()), Durations.DurationFormat.SHORT); + } + if ((metricType.equals(Metric.ValueType.STRING) || metricType.equals(Metric.ValueType.RATING)) && data != null) { + return data; + } } return null; } @CheckForNull - private String formatVariation(Double value, Metric.ValueType metricType) { - if (metricType.equals(Metric.ValueType.FLOAT) || metricType.equals(Metric.ValueType.PERCENT)) { - return i18n.formatDouble(UserSession.get().locale(), value); - } - if (metricType.equals(Metric.ValueType.INT)) { - return i18n.formatInteger(UserSession.get().locale(), value.intValue()); - } - if (metricType.equals(Metric.ValueType.WORK_DUR)) { - return durations.format(UserSession.get().locale(), durations.create(value.longValue()), Durations.DurationFormat.SHORT); + private String formatVariation(@Nullable MeasureDto measure, Integer periodIndex) { + if (measure != null) { + Double variation = measure.getVariation(periodIndex); + if (variation != null) { + Metric metric = CoreMetrics.getMetric(measure.getKey().metricKey()); + Metric.ValueType metricType = metric.getType(); + if (metricType.equals(Metric.ValueType.FLOAT) || metricType.equals(Metric.ValueType.PERCENT)) { + return i18n.formatDouble(UserSession.get().locale(), variation); + } + if (metricType.equals(Metric.ValueType.INT)) { + return i18n.formatInteger(UserSession.get().locale(), variation.intValue()); + } + if (metricType.equals(Metric.ValueType.WORK_DUR)) { + return durations.format(UserSession.get().locale(), durations.create(variation.longValue()), Durations.DurationFormat.SHORT); + } + } } return null; } diff --git a/server/sonar-server/src/main/resources/org/sonar/server/component/ws/components-example-app.json b/server/sonar-server/src/main/resources/org/sonar/server/component/ws/components-example-app.json index a04bad19e9d..8070e3d3df2 100644 --- a/server/sonar-server/src/main/resources/org/sonar/server/component/ws/components-example-app.json +++ b/server/sonar-server/src/main/resources/org/sonar/server/component/ws/components-example-app.json @@ -47,7 +47,10 @@ "fNcloc": "12", "fDebt": "4h", "fIssues": "4", - "fInfoIssues": "4" + "fInfoIssues": "4", + "fDuplicationDensity": "1.2", + "fSqaleRating" : "C", + "fSqaleDebtRatio" : "35.0%" }, "extensions": [ ["metricsTab", "Metrics"] diff --git a/server/sonar-server/src/test/java/org/sonar/server/component/ws/ComponentAppActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/component/ws/ComponentAppActionTest.java index 9a0f822eef3..25ee502d97a 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/component/ws/ComponentAppActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/component/ws/ComponentAppActionTest.java @@ -206,6 +206,8 @@ public class ComponentAppActionTest { addMeasure(CoreMetrics.NCLOC_KEY, 200); addMeasure(CoreMetrics.COVERAGE_KEY, 95.4); addMeasure(CoreMetrics.DUPLICATED_LINES_DENSITY_KEY, 7.4); + addMeasure(CoreMetrics.SQALE_RATING_KEY, "C"); + addMeasure(CoreMetrics.SQALE_DEBT_RATIO_KEY, 35d); measures.add(MeasureDto.createFor(MeasureKey.of(COMPONENT_KEY, CoreMetrics.TECHNICAL_DEBT_KEY)).setValue(182.0)); when(durations.format(any(Locale.class), any(Duration.class), eq(Durations.DurationFormat.SHORT))).thenReturn("3h 2min"); @@ -227,6 +229,7 @@ public class ComponentAppActionTest { addVariationMeasure(CoreMetrics.NCLOC_KEY, 2, 1); addVariationMeasure(CoreMetrics.COVERAGE_KEY, 5d, 1); addVariationMeasure(CoreMetrics.DUPLICATED_LINES_DENSITY_KEY, 1.2, 1); + addVariationMeasure(CoreMetrics.SQALE_DEBT_RATIO_KEY, 5d, 1); measures.add(MeasureDto.createFor(MeasureKey.of(COMPONENT_KEY, CoreMetrics.TECHNICAL_DEBT_KEY)).setVariation(1, 10.0)); when(durations.format(any(Locale.class), any(Duration.class), eq(Durations.DurationFormat.SHORT))).thenReturn("10min"); @@ -463,6 +466,10 @@ public class ComponentAppActionTest { when(i18n.formatDouble(any(Locale.class), eq(value))).thenReturn(Double.toString(value)); } + private void addMeasure(String metricKey, String value) { + measures.add(MeasureDto.createFor(MeasureKey.of(COMPONENT_KEY, metricKey)).setTextValue(value)); + } + private void addVariationMeasure(String metricKey, Integer value, Integer periodIndex) { measures.add(MeasureDto.createFor(MeasureKey.of(COMPONENT_KEY, metricKey)).setVariation(periodIndex, value.doubleValue())); when(i18n.formatInteger(any(Locale.class), eq(value))).thenReturn(Integer.toString(value)); diff --git a/server/sonar-server/src/test/resources/org/sonar/server/component/ws/ComponentAppActionTest/app_with_measures.json b/server/sonar-server/src/test/resources/org/sonar/server/component/ws/ComponentAppActionTest/app_with_measures.json index 8f26dd9621c..e9d2d29c52f 100644 --- a/server/sonar-server/src/test/resources/org/sonar/server/component/ws/ComponentAppActionTest/app_with_measures.json +++ b/server/sonar-server/src/test/resources/org/sonar/server/component/ws/ComponentAppActionTest/app_with_measures.json @@ -19,7 +19,9 @@ "fNcloc": "200", "fCoverage": "95.4%", "fDuplicationDensity": "7.4%", - "fDebt": "3h 2min" + "fDebt": "3h 2min", + "fSqaleRating" : "C", + "fSqaleDebtRatio" : "35.0%" }, "tabs": [ "coverage" diff --git a/server/sonar-server/src/test/resources/org/sonar/server/component/ws/ComponentAppActionTest/app_with_measures_when_period_is_set.json b/server/sonar-server/src/test/resources/org/sonar/server/component/ws/ComponentAppActionTest/app_with_measures_when_period_is_set.json index cce534b0462..41501a758ab 100644 --- a/server/sonar-server/src/test/resources/org/sonar/server/component/ws/ComponentAppActionTest/app_with_measures_when_period_is_set.json +++ b/server/sonar-server/src/test/resources/org/sonar/server/component/ws/ComponentAppActionTest/app_with_measures_when_period_is_set.json @@ -21,7 +21,8 @@ "fNcloc": "2", "fCoverage": "5.0", "fDuplicationDensity": "1.2", - "fDebt": "10min" + "fDebt": "10min", + "fSqaleDebtRatio" : "5.0" }, "tabs": [ "coverage" |