You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

measures.ts 9.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2024 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. import { keyBy } from 'lodash';
  21. import { isDiffMetric } from '../../../helpers/measures';
  22. import { mockMeasure } from '../../../helpers/testMocks';
  23. import { SoftwareImpactSeverity } from '../../../types/clean-code-taxonomy';
  24. import { IssueDeprecatedStatus, IssueType, RawIssue } from '../../../types/issues';
  25. import { MetricKey, MetricType } from '../../../types/metrics';
  26. import { Measure } from '../../../types/types';
  27. import { ComponentTree } from './components';
  28. import { IssueData } from './issues';
  29. import { listAllComponent, listAllComponentTrees } from './utils';
  30. const MAX_RATING = 5;
  31. export type MeasureRecords = Record<string, Record<string, Measure>>;
  32. export function mockFullMeasureData(tree: ComponentTree, issueList: IssueData[]) {
  33. const measures: MeasureRecords = {};
  34. listAllComponentTrees(tree).forEach((tree) => {
  35. measures[tree.component.key] = keyBy(
  36. Object.values(MetricKey).map((metricKey) => mockComponentMeasure(tree, issueList, metricKey)),
  37. 'metric',
  38. );
  39. });
  40. return measures;
  41. }
  42. function mockComponentMeasure(tree: ComponentTree, issueList: IssueData[], metricKey: MetricKey) {
  43. const componentKeys = listAllComponent(tree).map(({ key }) => key);
  44. switch (metricKey) {
  45. case MetricKey.ncloc:
  46. return mockMeasure({
  47. metric: metricKey,
  48. value: '16000',
  49. });
  50. case MetricKey.ncloc_language_distribution:
  51. return mockMeasure({
  52. metric: metricKey,
  53. value: 'java=10000;javascript=5000;css=1000',
  54. });
  55. case MetricKey.security_issues:
  56. return mockMeasure({
  57. metric: metricKey,
  58. value: JSON.stringify({
  59. total: 1,
  60. [SoftwareImpactSeverity.High]: 0,
  61. [SoftwareImpactSeverity.Medium]: 1,
  62. [SoftwareImpactSeverity.Low]: 0,
  63. }),
  64. });
  65. case MetricKey.new_security_issues:
  66. return mockMeasure({
  67. metric: metricKey,
  68. period: {
  69. index: 1,
  70. value: JSON.stringify({
  71. total: 3,
  72. [SoftwareImpactSeverity.High]: 2,
  73. [SoftwareImpactSeverity.Medium]: 0,
  74. [SoftwareImpactSeverity.Low]: 1,
  75. }),
  76. },
  77. value: undefined,
  78. });
  79. case MetricKey.reliability_issues:
  80. return mockMeasure({
  81. metric: metricKey,
  82. value: JSON.stringify({
  83. total: 3,
  84. [SoftwareImpactSeverity.High]: 0,
  85. [SoftwareImpactSeverity.Medium]: 2,
  86. [SoftwareImpactSeverity.Low]: 1,
  87. }),
  88. });
  89. case MetricKey.new_reliability_issues:
  90. return mockMeasure({
  91. metric: metricKey,
  92. period: {
  93. index: 1,
  94. value: JSON.stringify({
  95. total: 2,
  96. [SoftwareImpactSeverity.High]: 0,
  97. [SoftwareImpactSeverity.Medium]: 1,
  98. [SoftwareImpactSeverity.Low]: 1,
  99. }),
  100. },
  101. value: undefined,
  102. });
  103. case MetricKey.maintainability_issues:
  104. return mockMeasure({
  105. metric: metricKey,
  106. value: JSON.stringify({
  107. total: 2,
  108. [SoftwareImpactSeverity.High]: 0,
  109. [SoftwareImpactSeverity.Medium]: 0,
  110. [SoftwareImpactSeverity.Low]: 1,
  111. }),
  112. });
  113. case MetricKey.new_maintainability_issues:
  114. return mockMeasure({
  115. metric: metricKey,
  116. period: {
  117. index: 1,
  118. value: JSON.stringify({
  119. total: 5,
  120. [SoftwareImpactSeverity.High]: 2,
  121. [SoftwareImpactSeverity.Medium]: 2,
  122. [SoftwareImpactSeverity.Low]: 1,
  123. }),
  124. },
  125. value: undefined,
  126. });
  127. }
  128. const issues = issueList
  129. .map(({ issue }) => issue)
  130. .filter(({ component }) => componentKeys.includes(component))
  131. .filter(({ status }) =>
  132. [
  133. IssueDeprecatedStatus.Open,
  134. IssueDeprecatedStatus.Reopened,
  135. IssueDeprecatedStatus.Confirmed,
  136. ].includes(status as IssueDeprecatedStatus),
  137. );
  138. if (isIssueType(metricKey)) {
  139. switch (metricKey) {
  140. case MetricKey.bugs:
  141. return mockMeasure({
  142. metric: metricKey,
  143. period: undefined,
  144. value: String(issues.filter(({ type }) => type === IssueType.Bug).length),
  145. });
  146. case MetricKey.new_bugs:
  147. return mockMeasure({
  148. metric: metricKey,
  149. period: {
  150. index: 0,
  151. value: String(issues.filter(({ type }) => type === IssueType.Bug).length),
  152. },
  153. value: undefined,
  154. });
  155. case MetricKey.code_smells:
  156. return mockMeasure({
  157. metric: metricKey,
  158. period: undefined,
  159. value: String(issues.filter(({ type }) => type === IssueType.CodeSmell).length),
  160. });
  161. case MetricKey.new_code_smells:
  162. return mockMeasure({
  163. metric: metricKey,
  164. period: {
  165. index: 0,
  166. value: String(issues.filter(({ type }) => type === IssueType.CodeSmell).length),
  167. },
  168. value: undefined,
  169. });
  170. case MetricKey.vulnerabilities:
  171. return mockMeasure({
  172. metric: metricKey,
  173. period: undefined,
  174. value: String(issues.filter(({ type }) => type === IssueType.Vulnerability).length),
  175. });
  176. case MetricKey.new_vulnerabilities:
  177. return mockMeasure({
  178. metric: metricKey,
  179. period: {
  180. index: 0,
  181. value: String(issues.filter(({ type }) => type === IssueType.Vulnerability).length),
  182. },
  183. value: undefined,
  184. });
  185. case MetricKey.open_issues:
  186. return mockMeasure({
  187. metric: metricKey,
  188. value: String(issues.length),
  189. });
  190. }
  191. }
  192. if (isIssueRelatedRating(metricKey)) {
  193. switch (metricKey) {
  194. case MetricKey.reliability_rating:
  195. return mockMeasure({
  196. metric: metricKey,
  197. period: undefined,
  198. ...computeRating(issues, IssueType.Bug),
  199. });
  200. case MetricKey.new_reliability_rating:
  201. return mockMeasure({
  202. metric: metricKey,
  203. period: {
  204. index: 0,
  205. ...computeRating(issues, IssueType.Bug),
  206. },
  207. value: undefined,
  208. });
  209. case MetricKey.sqale_rating:
  210. return mockMeasure({
  211. metric: metricKey,
  212. period: undefined,
  213. ...computeRating(issues, IssueType.CodeSmell),
  214. });
  215. case MetricKey.new_maintainability_rating:
  216. return mockMeasure({
  217. metric: metricKey,
  218. period: {
  219. index: 0,
  220. ...computeRating(issues, IssueType.CodeSmell),
  221. },
  222. value: undefined,
  223. });
  224. case MetricKey.security_rating:
  225. return mockMeasure({
  226. metric: metricKey,
  227. period: undefined,
  228. ...computeRating(issues, IssueType.Vulnerability),
  229. });
  230. case MetricKey.new_security_rating:
  231. return mockMeasure({
  232. metric: metricKey,
  233. period: {
  234. index: 0,
  235. ...computeRating(issues, IssueType.Vulnerability),
  236. },
  237. value: undefined,
  238. });
  239. }
  240. }
  241. // Defaults.
  242. if (isDiffMetric(metricKey)) {
  243. return mockMeasure({
  244. metric: metricKey,
  245. value: undefined,
  246. });
  247. }
  248. return mockMeasure({
  249. metric: metricKey,
  250. period: undefined,
  251. });
  252. }
  253. export function getMetricTypeFromKey(metricKey: string) {
  254. if (/(coverage|duplication)$/.test(metricKey)) {
  255. return MetricType.Percent;
  256. } else if (metricKey.includes('_rating')) {
  257. return MetricType.Rating;
  258. } else if (
  259. [
  260. MetricKey.reliability_issues,
  261. MetricKey.new_reliability_issues,
  262. MetricKey.security_issues,
  263. MetricKey.new_security_issues,
  264. MetricKey.maintainability_issues,
  265. MetricKey.new_maintainability_issues,
  266. ].includes(metricKey as MetricKey)
  267. ) {
  268. return MetricType.Data;
  269. }
  270. return MetricType.Integer;
  271. }
  272. function isIssueType(metricKey: MetricKey) {
  273. return [
  274. MetricKey.bugs,
  275. MetricKey.new_bugs,
  276. MetricKey.code_smells,
  277. MetricKey.new_code_smells,
  278. MetricKey.vulnerabilities,
  279. MetricKey.new_vulnerabilities,
  280. MetricKey.open_issues,
  281. ].includes(metricKey);
  282. }
  283. function isIssueRelatedRating(metricKey: MetricKey) {
  284. return [
  285. MetricKey.reliability_rating,
  286. MetricKey.new_reliability_rating,
  287. MetricKey.sqale_rating,
  288. MetricKey.new_maintainability_rating,
  289. MetricKey.security_rating,
  290. MetricKey.new_security_rating,
  291. ].includes(metricKey);
  292. }
  293. /**
  294. * Ratings are not only based on the number of issues, but also their severity, and sometimes their
  295. * ratio to the LOC. But using the number will suffice as an approximation in our tests.
  296. */
  297. function computeRating(issues: RawIssue[], type: IssueType) {
  298. const value = Math.max(Math.min(issues.filter((i) => i.type === type).length, MAX_RATING), 1);
  299. return {
  300. value: `${value}.0`,
  301. bestValue: value === 1,
  302. };
  303. }