1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
|
/*
* SonarQube
* Copyright (C) 2009-2021 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 * as classNames from 'classnames';
import * as difference from 'date-fns/difference_in_milliseconds';
import * as React from 'react';
import DateTimeFormatter from '../../../../components/intl/DateTimeFormatter';
import Measure from '../../../../components/measure/Measure';
import CoverageRating from '../../../../components/ui/CoverageRating';
import DuplicationsRating from '../../../../components/ui/DuplicationsRating';
import Rating from '../../../../components/ui/Rating';
import { translate, translateWithParameters } from '../../../../helpers/l10n';
import { isDefined } from '../../../../helpers/types';
import { ComponentQualifier } from '../../../../types/component';
import { MetricKey } from '../../../../types/metrics';
import { formatDuration } from '../../utils';
import ProjectCardMeasure from './ProjectCardMeasure';
export interface ProjectCardMeasuresProps {
isNewCode: boolean;
measures: T.Dict<string | undefined>;
componentQualifier: ComponentQualifier;
newCodeStartingDate?: string;
}
function renderCoverage(props: ProjectCardMeasuresProps) {
const { measures, isNewCode } = props;
const coverageMetric = isNewCode ? MetricKey.new_coverage : MetricKey.coverage;
return (
<ProjectCardMeasure metricKey={coverageMetric} label={translate('metric.coverage.name')}>
<div className="display-flex-center">
<Measure
className="big"
metricKey={coverageMetric}
metricType="PERCENT"
value={measures[coverageMetric]}
/>
{measures[coverageMetric] && (
<span className="spacer-left project-card-measure-secondary-info">
<CoverageRating value={measures[coverageMetric]} />
</span>
)}
</div>
</ProjectCardMeasure>
);
}
function renderDuplication(props: ProjectCardMeasuresProps) {
const { measures, isNewCode } = props;
const duplicationMetric = isNewCode
? MetricKey.new_duplicated_lines_density
: MetricKey.duplicated_lines_density;
return (
<ProjectCardMeasure
metricKey={duplicationMetric}
label={translate('metric.duplicated_lines_density.short_name')}>
<div className="display-flex-center">
<Measure
className="big"
metricKey={duplicationMetric}
metricType="PERCENT"
value={measures[duplicationMetric]}
/>
{measures[duplicationMetric] != null && (
<span className="spacer-left project-card-measure-secondary-info">
<DuplicationsRating value={Number(measures[duplicationMetric])} />
</span>
)}
</div>
</ProjectCardMeasure>
);
}
function renderRatings(props: ProjectCardMeasuresProps) {
const { isNewCode, measures } = props;
const measureList = [
{
iconLabel: translate('metric.bugs.name'),
noShrink: true,
metricKey: isNewCode ? MetricKey.new_bugs : MetricKey.bugs,
metricRatingKey: isNewCode ? MetricKey.new_reliability_rating : MetricKey.reliability_rating,
metricType: 'SHORT_INT'
},
{
iconLabel: translate('metric.vulnerabilities.name'),
metricKey: isNewCode ? MetricKey.new_vulnerabilities : MetricKey.vulnerabilities,
metricRatingKey: isNewCode ? MetricKey.new_security_rating : MetricKey.security_rating,
metricType: 'SHORT_INT'
},
{
iconKey: 'security_hotspots',
iconLabel: translate('projects.security_hotspots_reviewed'),
metricKey: isNewCode
? MetricKey.new_security_hotspots_reviewed
: MetricKey.security_hotspots_reviewed,
metricRatingKey: isNewCode
? MetricKey.new_security_review_rating
: MetricKey.security_review_rating,
metricType: 'PERCENT'
},
{
iconLabel: translate('metric.code_smells.name'),
metricKey: isNewCode ? MetricKey.new_code_smells : MetricKey.code_smells,
metricRatingKey: isNewCode ? MetricKey.new_maintainability_rating : MetricKey.sqale_rating,
metricType: 'SHORT_INT'
}
];
return measureList.map(measure => {
const { iconKey, iconLabel, metricKey, metricRatingKey, metricType, noShrink } = measure;
return (
<ProjectCardMeasure
className={classNames({ 'flex-0': noShrink })}
key={metricKey}
metricKey={metricKey}
iconKey={iconKey}
label={iconLabel}>
<Measure
className="spacer-right big project-card-measure-secondary-info"
metricKey={metricKey}
metricType={metricType}
value={measures[metricKey]}
/>
<span className="big">
<Rating value={measures[metricRatingKey]} />
</span>
</ProjectCardMeasure>
);
});
}
export default function ProjectCardMeasures(props: ProjectCardMeasuresProps) {
const { isNewCode, measures, componentQualifier, newCodeStartingDate } = props;
const { ncloc } = measures;
if (!isNewCode && !ncloc) {
return (
<div className="note big-spacer-top">
{componentQualifier === ComponentQualifier.Application
? translate('portfolio.app.empty')
: translate('overview.project.main_branch_empty')}
</div>
);
}
const newCodeTimespan = newCodeStartingDate ? difference(Date.now(), newCodeStartingDate) : 0;
const measureList = [
...renderRatings(props),
renderCoverage(props),
renderDuplication(props)
].filter(isDefined);
return (
<>
{isNewCode && newCodeTimespan !== undefined && newCodeStartingDate && (
<DateTimeFormatter date={newCodeStartingDate}>
{formattedNewCodeStartingDate => (
<p className="spacer-top spacer-bottom" title={formattedNewCodeStartingDate}>
{translateWithParameters(
'projects.new_code_period_x',
formatDuration(newCodeTimespan)
)}
</p>
)}
</DateTimeFormatter>
)}
<div className="display-flex-row display-flex-space-between">
{measureList.map((measure, i) => (
// eslint-disable-next-line react/no-array-index-key
<React.Fragment key={i}>
{i > 0 && <span className="bordered-left little-spacer" />}
{measure}
</React.Fragment>
))}
</div>
</>
);
}
|