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.

SoftwareImpactMeasureCard.tsx 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 { LinkHighlight, LinkStandalone } from '@sonarsource/echoes-react';
  22. import classNames from 'classnames';
  23. import { Badge, LightGreyCard, LightGreyCardTitle, TextBold, TextSubdued } from 'design-system';
  24. import * as React from 'react';
  25. import { FormattedMessage, useIntl } from 'react-intl';
  26. import Tooltip from '../../../components/controls/Tooltip';
  27. import { DEFAULT_ISSUES_QUERY } from '../../../components/shared/utils';
  28. import {
  29. SOFTWARE_QUALITIES_METRIC_KEYS_MAP,
  30. getIssueTypeBySoftwareQuality,
  31. } from '../../../helpers/issues';
  32. import { formatMeasure } from '../../../helpers/measures';
  33. import { isDefined } from '../../../helpers/types';
  34. import { getComponentIssuesUrl } from '../../../helpers/urls';
  35. import { Branch } from '../../../types/branch-like';
  36. import {
  37. SoftwareImpactMeasureData,
  38. SoftwareImpactSeverity,
  39. SoftwareQuality,
  40. } from '../../../types/clean-code-taxonomy';
  41. import { MetricKey, MetricType } from '../../../types/metrics';
  42. import { QualityGateStatusConditionEnhanced } from '../../../types/quality-gates';
  43. import { Component, MeasureEnhanced } from '../../../types/types';
  44. import { OverviewDisabledLinkTooltip } from '../components/OverviewDisabledLinkTooltip';
  45. import { Status, softwareQualityToMeasure } from '../utils';
  46. import SoftwareImpactMeasureBreakdownCard from './SoftwareImpactMeasureBreakdownCard';
  47. import SoftwareImpactMeasureRating from './SoftwareImpactMeasureRating';
  48. export interface SoftwareImpactBreakdownCardProps {
  49. component: Component;
  50. conditions: QualityGateStatusConditionEnhanced[];
  51. softwareQuality: SoftwareQuality;
  52. ratingMetricKey: MetricKey;
  53. measures: MeasureEnhanced[];
  54. branch?: Branch;
  55. }
  56. export function SoftwareImpactMeasureCard(props: Readonly<SoftwareImpactBreakdownCardProps>) {
  57. const { component, conditions, softwareQuality, ratingMetricKey, measures, branch } = props;
  58. const intl = useIntl();
  59. // Find measure for this software quality
  60. const metricKey = softwareQualityToMeasure(softwareQuality);
  61. const measureRaw = measures.find((m) => m.metric.key === metricKey);
  62. const measure = JSON.parse(measureRaw?.value ?? 'null') as SoftwareImpactMeasureData;
  63. const alternativeMeasure = measures.find(
  64. (m) => m.metric.key === SOFTWARE_QUALITIES_METRIC_KEYS_MAP[softwareQuality].deprecatedMetric,
  65. );
  66. // Find rating measure
  67. const ratingMeasure = measures.find((m) => m.metric.key === ratingMetricKey);
  68. const count = formatMeasure(measure?.total ?? alternativeMeasure?.value, MetricType.ShortInteger);
  69. const totalLinkHref = getComponentIssuesUrl(component.key, {
  70. ...DEFAULT_ISSUES_QUERY,
  71. ...(isDefined(measure)
  72. ? { impactSoftwareQualities: softwareQuality }
  73. : { types: getIssueTypeBySoftwareQuality(softwareQuality) }),
  74. branch: branch?.name,
  75. });
  76. // We highlight the highest severity breakdown card with non-zero count
  77. const highlightedSeverity =
  78. measure &&
  79. [SoftwareImpactSeverity.High, SoftwareImpactSeverity.Medium, SoftwareImpactSeverity.Low].find(
  80. (severity) => measure[severity] > 0,
  81. );
  82. const countTooltipOverlay = component.needIssueSync ? (
  83. <OverviewDisabledLinkTooltip />
  84. ) : (
  85. intl.formatMessage({ id: 'overview.measures.software_impact.count_tooltip' })
  86. );
  87. const failed = conditions.some((c) => c.level === Status.ERROR && c.metric === ratingMetricKey);
  88. return (
  89. <LightGreyCard
  90. data-testid={`overview__software-impact-card-${softwareQuality}`}
  91. className="sw-w-1/3 sw-overflow-hidden sw-rounded-2 sw-p-4 sw-flex-col"
  92. >
  93. <LightGreyCardTitle>
  94. <TextBold name={intl.formatMessage({ id: `software_quality.${softwareQuality}` })} />
  95. {failed && (
  96. <Badge className="sw-h-fit" variant="deleted">
  97. <FormattedMessage id="overview.measures.failed_badge" />
  98. </Badge>
  99. )}
  100. </LightGreyCardTitle>
  101. <div className="sw-flex sw-flex-col sw-gap-3">
  102. <div className="sw-flex sw-mt-4">
  103. <div
  104. className={classNames('sw-flex sw-gap-1 sw-items-center', {
  105. 'sw-opacity-60': component.needIssueSync,
  106. })}
  107. >
  108. {count ? (
  109. <Tooltip overlay={countTooltipOverlay}>
  110. <LinkStandalone
  111. data-testid={`overview__software-impact-${softwareQuality}`}
  112. aria-label={intl.formatMessage(
  113. {
  114. id: `overview.measures.software_impact.see_list_of_x_open_issues`,
  115. },
  116. {
  117. count,
  118. softwareQuality: intl.formatMessage({
  119. id: `software_quality.${softwareQuality}`,
  120. }),
  121. },
  122. )}
  123. className="sw-text-lg sw-font-semibold"
  124. highlight={LinkHighlight.CurrentColor}
  125. to={totalLinkHref}
  126. >
  127. {count}
  128. </LinkStandalone>
  129. </Tooltip>
  130. ) : (
  131. <StyledDash className="sw-font-bold" name="-" />
  132. )}
  133. <TextSubdued className="sw-self-end sw-body-sm sw-pb-1">
  134. {intl.formatMessage({ id: 'overview.measures.software_impact.total_open_issues' })}
  135. </TextSubdued>
  136. </div>
  137. <div className="sw-flex-grow sw-flex sw-justify-end">
  138. <SoftwareImpactMeasureRating
  139. softwareQuality={softwareQuality}
  140. value={ratingMeasure?.value}
  141. />
  142. </div>
  143. </div>
  144. {measure && (
  145. <div className="sw-flex sw-gap-2">
  146. {[
  147. SoftwareImpactSeverity.High,
  148. SoftwareImpactSeverity.Medium,
  149. SoftwareImpactSeverity.Low,
  150. ].map((severity) => (
  151. <SoftwareImpactMeasureBreakdownCard
  152. branch={branch}
  153. key={severity}
  154. component={component}
  155. softwareQuality={softwareQuality}
  156. value={measure?.[severity]?.toString()}
  157. severity={severity}
  158. active={highlightedSeverity === severity}
  159. />
  160. ))}
  161. </div>
  162. )}
  163. </div>
  164. </LightGreyCard>
  165. );
  166. }
  167. const StyledDash = styled(TextBold)`
  168. font-size: 36px;
  169. `;
  170. export default SoftwareImpactMeasureCard;