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.1KB

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