]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-12256 Simplify measure rounding
authorWouter Admiraal <wouter.admiraal@sonarsource.com>
Tue, 14 Apr 2020 15:11:16 +0000 (17:11 +0200)
committersonartech <sonartech@sonarsource.com>
Fri, 17 Apr 2020 20:03:43 +0000 (20:03 +0000)
server/sonar-web/src/main/js/apps/overview/__tests__/utils-test.ts [deleted file]
server/sonar-web/src/main/js/apps/overview/components/MeasurementLabel.tsx
server/sonar-web/src/main/js/apps/overview/components/QualityGateCondition.tsx
server/sonar-web/src/main/js/apps/overview/components/__tests__/__snapshots__/QualityGateCondition-test.tsx.snap
server/sonar-web/src/main/js/apps/overview/utils.ts
server/sonar-web/src/main/js/components/measure/Measure.tsx

diff --git a/server/sonar-web/src/main/js/apps/overview/__tests__/utils-test.ts b/server/sonar-web/src/main/js/apps/overview/__tests__/utils-test.ts
deleted file mode 100644 (file)
index 7e06a7b..0000000
+++ /dev/null
@@ -1,71 +0,0 @@
-/*
- * SonarQube
- * Copyright (C) 2009-2020 SonarSource SA
- * mailto:info AT sonarsource DOT com
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
- */
-import { mockQualityGateStatusCondition } from '../../../helpers/mocks/quality-gates';
-import { mockMeasureEnhanced, mockMetric } from '../../../helpers/testMocks';
-import { MetricKey } from '../../../types/metrics';
-import { getThreshold } from '../utils';
-
-// eslint-disable-next-line no-console
-console.error = jest.fn();
-
-describe('getThreshold', () => {
-  it('return undefined if condition is not found', () => {
-    expect(getThreshold([], '')).toBeUndefined();
-    expect(getThreshold([mockMeasure()], '')).toBeUndefined();
-    expect(
-      getThreshold(
-        [
-          {
-            metric: mockMetric({ key: MetricKey.quality_gate_details }),
-            value: 'badly typed json should fail'
-          }
-        ],
-        ''
-      )
-    ).toBeUndefined();
-    // eslint-disable-next-line no-console
-    expect(console.error).toBeCalled();
-  });
-
-  it('should return the threshold for the right metric', () => {
-    expect(getThreshold([mockMeasure()], MetricKey.new_coverage)).toBe(85);
-    expect(getThreshold([mockMeasure()], MetricKey.new_duplicated_lines_density)).toBe(5);
-  });
-});
-
-function mockMeasure() {
-  return mockMeasureEnhanced({
-    metric: mockMetric({ key: MetricKey.quality_gate_details }),
-    value: JSON.stringify({
-      conditions: [
-        mockQualityGateStatusCondition({
-          metric: MetricKey.new_coverage,
-          level: 'ERROR',
-          error: '85'
-        }),
-        mockQualityGateStatusCondition({
-          metric: MetricKey.new_duplicated_lines_density,
-          level: 'OK',
-          warning: '5'
-        })
-      ]
-    })
-  });
-}
index 3de85a144c9a8d4803ff31a36c35e2dbbf55c35f..a1989eaf346700dd7c8e03e02e1362926e557aa0 100644 (file)
 import * as React from 'react';
 import { FormattedMessage } from 'react-intl';
 import { translate } from 'sonar-ui-common/helpers/l10n';
-import {
-  formatMeasure,
-  getMinDecimalsCountToBeDistinctFromThreshold
-} from 'sonar-ui-common/helpers/measures';
+import { formatMeasure } from 'sonar-ui-common/helpers/measures';
 import { getLeakValue } from '../../../components/measure/utils';
 import DrilldownLink from '../../../components/shared/DrilldownLink';
 import { findMeasure } from '../../../helpers/measures';
@@ -33,7 +30,6 @@ import {
   getMeasurementLabelKeys,
   getMeasurementLinesMetricKey,
   getMeasurementMetricKey,
-  getThreshold,
   MeasurementType
 } from '../utils';
 
@@ -105,12 +101,7 @@ export default class MeasurementLabel extends React.Component<Props> {
         className="overview-measures-value text-light"
         component={component.key}
         metric={metric}>
-        {formatMeasure(value, 'PERCENT', {
-          decimals: getMinDecimalsCountToBeDistinctFromThreshold(
-            parseFloat(value),
-            getThreshold(measures, metric)
-          )
-        })}
+        {formatMeasure(value, 'PERCENT', { decimals: 2, omitExtraDecimalZeros: true })}
       </DrilldownLink>
     );
     const label = this.getLabelText();
index a605545e0d7a21cae6dc79d0c24cc0e79b3d7b8e..9c7e5135c999eecc953b20ebeeeae96c113cd679 100644 (file)
@@ -22,10 +22,7 @@ import * as React from 'react';
 import { Link } from 'react-router';
 import IssueTypeIcon from 'sonar-ui-common/components/icons/IssueTypeIcon';
 import { translate } from 'sonar-ui-common/helpers/l10n';
-import {
-  formatMeasure,
-  getMinDecimalsCountToBeDistinctFromThreshold
-} from 'sonar-ui-common/helpers/measures';
+import { formatMeasure } from 'sonar-ui-common/helpers/measures';
 import Measure from '../../../components/measure/Measure';
 import DrilldownLink from '../../../components/shared/DrilldownLink';
 import { getBranchLikeQuery } from '../../../helpers/branch-like';
@@ -128,22 +125,16 @@ export default class QualityGateCondition extends React.PureComponent<Props> {
       : measure.value) as string;
 
     let operator = translate('quality_gates.operator', condition.op);
-    let decimals: number | undefined = undefined;
 
     if (metric.type === 'RATING') {
       operator = translate('quality_gates.operator', condition.op, 'rating');
-    } else if (metric.type === 'PERCENT') {
-      decimals = getMinDecimalsCountToBeDistinctFromThreshold(
-        parseFloat(actual),
-        parseFloat(threshold)
-      );
     }
 
     return this.wrapWithLink(
       <div className="overview-quality-gate-condition-container display-flex-center">
         <div className="overview-quality-gate-condition-value text-center">
           <Measure
-            decimals={decimals}
+            decimals={2}
             metricKey={measure.metric.key}
             metricType={measure.metric.type}
             value={actual}
index 35573f3846a480a86b32f3988f1d63a54facff83..54f74ec811ab440b1be70fe36ac7b52ad007804a 100644 (file)
@@ -24,6 +24,7 @@ exports[`new_maintainability_rating 1`] = `
       className="overview-quality-gate-condition-value text-center"
     >
       <Measure
+        decimals={2}
         metricKey="new_maintainability_rating"
         metricType="RATING"
         value="3"
@@ -65,6 +66,7 @@ exports[`new_open_issues 1`] = `
       className="overview-quality-gate-condition-value text-center"
     >
       <Measure
+        decimals={2}
         metricKey="new_open_issues"
         metricType="INT"
         value="3"
@@ -117,6 +119,7 @@ exports[`new_reliability_rating 1`] = `
       className="overview-quality-gate-condition-value text-center"
     >
       <Measure
+        decimals={2}
         metricKey="new_reliability_rating"
         metricType="RATING"
         value="3"
@@ -169,6 +172,7 @@ exports[`new_security_rating 1`] = `
       className="overview-quality-gate-condition-value text-center"
     >
       <Measure
+        decimals={2}
         metricKey="new_security_rating"
         metricType="RATING"
         value="3"
@@ -210,6 +214,7 @@ exports[`open_issues 1`] = `
       className="overview-quality-gate-condition-value text-center"
     >
       <Measure
+        decimals={2}
         metricKey="open_issues"
         metricType="INT"
         value="3"
@@ -261,6 +266,7 @@ exports[`reliability_rating 1`] = `
       className="overview-quality-gate-condition-value text-center"
     >
       <Measure
+        decimals={2}
         metricKey="reliability_rating"
         metricType="RATING"
         value="3"
@@ -312,6 +318,7 @@ exports[`security_rating 1`] = `
       className="overview-quality-gate-condition-value text-center"
     >
       <Measure
+        decimals={2}
         metricKey="security_rating"
         metricType="RATING"
         value="3"
@@ -364,6 +371,7 @@ exports[`should work with branch 1`] = `
       className="overview-quality-gate-condition-value text-center"
     >
       <Measure
+        decimals={2}
         metricKey="new_maintainability_rating"
         metricType="RATING"
         value="3"
@@ -414,6 +422,7 @@ exports[`sqale_rating 1`] = `
       className="overview-quality-gate-condition-value text-center"
     >
       <Measure
+        decimals={2}
         metricKey="sqale_rating"
         metricType="RATING"
         value="3"
index 3a24ba48428aa3f6bc16ef273ba71ebe37998aff..39e122d0a0d99b477e38115873e6369b1a46da94 100644 (file)
@@ -25,7 +25,6 @@ import DuplicationsRating from 'sonar-ui-common/components/ui/DuplicationsRating
 import { translate } from 'sonar-ui-common/helpers/l10n';
 import CoverageRating from '../../components/ui/CoverageRating';
 import { MetricKey } from '../../types/metrics';
-import { QualityGateStatusConditionEnhanced } from '../../types/quality-gates';
 
 export const METRICS: string[] = [
   // quality gate
@@ -219,37 +218,3 @@ export function getMeasurementLabelKeys(type: MeasurementType, useDiffMetric: bo
     labelKey: MEASUREMENTS_MAP[type].labelKey
   };
 }
-
-/*
- * Extract a specific metric's threshold from the quality gate details
- */
-export function getThreshold(
-  measures: T.MeasureEnhanced[],
-  metricKey: MetricKey | string
-): number | undefined {
-  const detailsMeasure = measures.find(
-    measure => measure.metric.key === MetricKey.quality_gate_details
-  );
-  if (detailsMeasure && detailsMeasure.value) {
-    const details = safeParse(detailsMeasure.value);
-    const conditions: QualityGateStatusConditionEnhanced[] = details.conditions || [];
-
-    const condition = conditions.find(c => c.metric === metricKey);
-    if (condition) {
-      return parseFloat(
-        (condition.level === 'ERROR' ? condition.error : condition.warning) as string
-      );
-    }
-  }
-  return undefined;
-}
-
-function safeParse(json: string) {
-  try {
-    return JSON.parse(json);
-  } catch (e) {
-    // eslint-disable-next-line no-console
-    console.error(e);
-    return {};
-  }
-}
index f17388b4d7675d49117b8cfe702a0e434d9a6d77..90f78adbebb51b846f6b716783a3fb209a02800e 100644 (file)
@@ -50,7 +50,10 @@ export default function Measure({
   }
 
   if (metricType !== 'RATING') {
-    const formattedValue = formatMeasure(value, metricType, { decimals });
+    const formattedValue = formatMeasure(value, metricType, {
+      decimals,
+      omitExtraDecimalZeros: metricType === 'PERCENT'
+    });
     return <span className={className}>{formattedValue != null ? formattedValue : '–'}</span>;
   }