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 6.8KB

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