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.

NewCodeMeasuresPanel.tsx 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 styled from '@emotion/styled';
  21. import {
  22. LightGreyCard,
  23. LightLabel,
  24. SnoozeCircleIcon,
  25. TextError,
  26. TextSubdued,
  27. TrendUpCircleIcon,
  28. themeColor,
  29. } from 'design-system';
  30. import React from 'react';
  31. import { useIntl } from 'react-intl';
  32. import { getTabPanelId } from '../../../components/controls/BoxedTabs';
  33. import { getLeakValue } from '../../../components/measure/utils';
  34. import { DEFAULT_ISSUES_QUERY } from '../../../components/shared/utils';
  35. import { getBranchLikeQuery } from '../../../helpers/branch-like';
  36. import { findMeasure, formatMeasure } from '../../../helpers/measures';
  37. import {
  38. getComponentDrilldownUrl,
  39. getComponentIssuesUrl,
  40. getComponentSecurityHotspotsUrl,
  41. } from '../../../helpers/urls';
  42. import { Branch } from '../../../types/branch-like';
  43. import { isApplication } from '../../../types/component';
  44. import { IssueStatus } from '../../../types/issues';
  45. import { MetricKey, MetricType } from '../../../types/metrics';
  46. import { QualityGateStatus } from '../../../types/quality-gates';
  47. import { Component, MeasureEnhanced } from '../../../types/types';
  48. import { IssueMeasuresCardInner } from '../components/IssueMeasuresCardInner';
  49. import MeasuresCardNumber from '../components/MeasuresCardNumber';
  50. import MeasuresCardPercent from '../components/MeasuresCardPercent';
  51. import {
  52. MeasurementType,
  53. MeasuresTabs,
  54. Status,
  55. getConditionRequiredLabel,
  56. getMeasurementMetricKey,
  57. } from '../utils';
  58. interface Props {
  59. branch?: Branch;
  60. component: Component;
  61. measures: MeasureEnhanced[];
  62. qgStatuses?: QualityGateStatus[];
  63. }
  64. export default function NewCodeMeasuresPanel(props: Readonly<Props>) {
  65. const { branch, component, measures, qgStatuses } = props;
  66. const intl = useIntl();
  67. const isApp = isApplication(component.qualifier);
  68. const failedConditions = qgStatuses?.flatMap((qg) => qg.failedConditions) ?? [];
  69. const newIssues = getLeakValue(findMeasure(measures, MetricKey.new_violations));
  70. const newIssuesCondition = failedConditions.find((c) => c.metric === MetricKey.new_violations);
  71. const issuesConditionFailed = newIssuesCondition?.level === Status.ERROR;
  72. const newAcceptedIssues = getLeakValue(findMeasure(measures, MetricKey.new_accepted_issues));
  73. const newSecurityHotspots = getLeakValue(
  74. findMeasure(measures, MetricKey.new_security_hotspots),
  75. ) as string;
  76. let issuesFooter;
  77. if (newIssuesCondition && !isApp) {
  78. issuesFooter = issuesConditionFailed ? (
  79. <TextError
  80. className="sw-font-regular sw-body-xs sw-inline"
  81. text={getConditionRequiredLabel(newIssuesCondition, intl, true)}
  82. />
  83. ) : (
  84. <LightLabel className="sw-body-xs">
  85. {getConditionRequiredLabel(newIssuesCondition, intl)}
  86. </LightLabel>
  87. );
  88. }
  89. return (
  90. <div className="sw-mt-6" id={getTabPanelId(MeasuresTabs.New)}>
  91. <LightGreyCard className="sw-flex sw-rounded-2 sw-gap-4">
  92. <IssueMeasuresCardInner
  93. data-test="overview__measures-new_issues"
  94. linkDisabled={component.needIssueSync}
  95. className="sw-w-1/2"
  96. metric={MetricKey.new_violations}
  97. value={formatMeasure(newIssues, MetricType.ShortInteger)}
  98. header={intl.formatMessage({
  99. id: 'overview.new_issues',
  100. })}
  101. url={getComponentIssuesUrl(component.key, {
  102. ...getBranchLikeQuery(branch),
  103. ...DEFAULT_ISSUES_QUERY,
  104. inNewCodePeriod: 'true',
  105. })}
  106. failed={issuesConditionFailed}
  107. icon={issuesConditionFailed && <TrendUpCircleIcon />}
  108. footer={issuesFooter}
  109. />
  110. <StyledCardSeparator />
  111. <IssueMeasuresCardInner
  112. data-test="overview__measures-accepted_issues"
  113. linkDisabled={component.needIssueSync}
  114. className="sw-w-1/2"
  115. metric={MetricKey.new_accepted_issues}
  116. value={formatMeasure(newAcceptedIssues, MetricType.ShortInteger)}
  117. header={intl.formatMessage({
  118. id: 'overview.accepted_issues',
  119. })}
  120. url={getComponentIssuesUrl(component.key, {
  121. ...getBranchLikeQuery(branch),
  122. issueStatuses: IssueStatus.Accepted,
  123. inNewCodePeriod: 'true',
  124. })}
  125. footer={
  126. <TextSubdued className="sw-body-xs">
  127. {intl.formatMessage({ id: 'overview.accepted_issues.help' })}
  128. </TextSubdued>
  129. }
  130. icon={
  131. <SnoozeCircleIcon
  132. color={
  133. newAcceptedIssues === '0' ? 'overviewCardDefaultIcon' : 'overviewCardWarningIcon'
  134. }
  135. />
  136. }
  137. />
  138. </LightGreyCard>
  139. <div className="sw-grid sw-grid-cols-2 sw-gap-4 sw-mt-4">
  140. <MeasuresCardPercent
  141. branchLike={branch}
  142. componentKey={component.key}
  143. conditions={failedConditions}
  144. measures={measures}
  145. measurementType={MeasurementType.Coverage}
  146. label="overview.quality_gate.coverage"
  147. url={getComponentDrilldownUrl({
  148. componentKey: component.key,
  149. metric: getMeasurementMetricKey(MeasurementType.Coverage, true),
  150. branchLike: branch,
  151. listView: true,
  152. })}
  153. conditionMetric={MetricKey.new_coverage}
  154. linesMetric={MetricKey.new_lines_to_cover}
  155. useDiffMetric
  156. showRequired={!isApp}
  157. />
  158. <MeasuresCardPercent
  159. branchLike={branch}
  160. componentKey={component.key}
  161. conditions={failedConditions}
  162. measures={measures}
  163. measurementType={MeasurementType.Duplication}
  164. label="overview.quality_gate.duplications"
  165. url={getComponentDrilldownUrl({
  166. componentKey: component.key,
  167. metric: getMeasurementMetricKey(MeasurementType.Coverage, true),
  168. branchLike: branch,
  169. listView: true,
  170. })}
  171. conditionMetric={MetricKey.new_duplicated_lines_density}
  172. linesMetric={MetricKey.new_lines}
  173. useDiffMetric
  174. showRequired={!isApp}
  175. />
  176. <MeasuresCardNumber
  177. label={
  178. newSecurityHotspots === '1'
  179. ? 'issue.type.SECURITY_HOTSPOT'
  180. : 'issue.type.SECURITY_HOTSPOT.plural'
  181. }
  182. url={getComponentSecurityHotspotsUrl(component.key, {
  183. ...getBranchLikeQuery(branch),
  184. })}
  185. value={newSecurityHotspots}
  186. conditions={failedConditions}
  187. conditionMetric={MetricKey.new_security_hotspots_reviewed}
  188. showRequired={!isApp}
  189. />
  190. </div>
  191. </div>
  192. );
  193. }
  194. const StyledCardSeparator = styled.div`
  195. width: 1px;
  196. background-color: ${themeColor('projectCardBorder')};
  197. `;