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.

QualityGateCondition.tsx 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 { LinkBox, TextMuted } from 'design-system';
  21. import * as React from 'react';
  22. import { Path } from 'react-router-dom';
  23. import { getBranchLikeQuery } from '~sonar-aligned/helpers/branch-like';
  24. import { formatMeasure } from '~sonar-aligned/helpers/measures';
  25. import {
  26. getComponentIssuesUrl,
  27. getComponentSecurityHotspotsUrl,
  28. } from '~sonar-aligned/helpers/urls';
  29. import { MetricKey, MetricType } from '~sonar-aligned/types/metrics';
  30. import IssueTypeIcon from '../../../components/icon-mappers/IssueTypeIcon';
  31. import MeasureIndicator from '../../../components/measure/MeasureIndicator';
  32. import {
  33. DEFAULT_ISSUES_QUERY,
  34. isIssueMeasure,
  35. propsToIssueParams,
  36. } from '../../../components/shared/utils';
  37. import { translate } from '../../../helpers/l10n';
  38. import { isDiffMetric, localizeMetric } from '../../../helpers/measures';
  39. import { getOperatorLabel } from '../../../helpers/qualityGates';
  40. import { getComponentDrilldownUrl } from '../../../helpers/urls';
  41. import { BranchLike } from '../../../types/branch-like';
  42. import { IssueType } from '../../../types/issues';
  43. import { QualityGateStatusConditionEnhanced } from '../../../types/quality-gates';
  44. import { Component, Dict } from '../../../types/types';
  45. import { RATING_TO_SEVERITIES_MAPPING } from '../utils';
  46. interface Props {
  47. branchLike?: BranchLike;
  48. component: Pick<Component, 'key'>;
  49. condition: QualityGateStatusConditionEnhanced;
  50. }
  51. export default class QualityGateCondition extends React.PureComponent<Props> {
  52. getIssuesUrl = (inNewCodePeriod: boolean, customQuery: Dict<string>) => {
  53. const query: Dict<string | undefined> = {
  54. ...DEFAULT_ISSUES_QUERY,
  55. ...getBranchLikeQuery(this.props.branchLike),
  56. ...customQuery,
  57. };
  58. if (inNewCodePeriod) {
  59. Object.assign(query, { inNewCodePeriod: 'true' });
  60. }
  61. return getComponentIssuesUrl(this.props.component.key, query);
  62. };
  63. getUrlForSecurityHotspot(inNewCodePeriod: boolean) {
  64. return getComponentSecurityHotspotsUrl(
  65. this.props.component.key,
  66. this.props.branchLike,
  67. inNewCodePeriod ? { inNewCodePeriod: 'true' } : {},
  68. );
  69. }
  70. getUrlForCodeSmells(inNewCodePeriod: boolean) {
  71. return this.getIssuesUrl(inNewCodePeriod, { types: 'CODE_SMELL' });
  72. }
  73. getUrlForBugsOrVulnerabilities(type: string, inNewCodePeriod: boolean) {
  74. const { condition } = this.props;
  75. const threshold = condition.level === 'ERROR' ? condition.error : condition.warning;
  76. return this.getIssuesUrl(inNewCodePeriod, {
  77. types: type,
  78. severities: RATING_TO_SEVERITIES_MAPPING[Number(threshold) - 1],
  79. });
  80. }
  81. wrapWithLink(children: React.ReactNode) {
  82. const { branchLike, component, condition } = this.props;
  83. const metricKey = condition.measure.metric.key;
  84. const METRICS_TO_URL_MAPPING: Dict<() => Partial<Path>> = {
  85. [MetricKey.reliability_rating]: () =>
  86. this.getUrlForBugsOrVulnerabilities(IssueType.Bug, false),
  87. [MetricKey.new_reliability_rating]: () =>
  88. this.getUrlForBugsOrVulnerabilities(IssueType.Bug, true),
  89. [MetricKey.security_rating]: () =>
  90. this.getUrlForBugsOrVulnerabilities(IssueType.Vulnerability, false),
  91. [MetricKey.new_security_rating]: () =>
  92. this.getUrlForBugsOrVulnerabilities(IssueType.Vulnerability, true),
  93. [MetricKey.sqale_rating]: () => this.getUrlForCodeSmells(false),
  94. [MetricKey.new_maintainability_rating]: () => this.getUrlForCodeSmells(true),
  95. [MetricKey.security_hotspots_reviewed]: () => this.getUrlForSecurityHotspot(false),
  96. [MetricKey.new_security_hotspots_reviewed]: () => this.getUrlForSecurityHotspot(true),
  97. };
  98. if (METRICS_TO_URL_MAPPING[metricKey]) {
  99. return <LinkBox to={METRICS_TO_URL_MAPPING[metricKey]()}>{children}</LinkBox>;
  100. }
  101. const url = isIssueMeasure(condition.measure.metric.key)
  102. ? getComponentIssuesUrl(component.key, {
  103. ...propsToIssueParams(condition.measure.metric.key, condition.period != null),
  104. ...getBranchLikeQuery(branchLike),
  105. })
  106. : getComponentDrilldownUrl({
  107. componentKey: component.key,
  108. metric: condition.measure.metric.key,
  109. branchLike,
  110. listView: true,
  111. });
  112. return <LinkBox to={url}>{children}</LinkBox>;
  113. }
  114. getPrimaryText = () => {
  115. const { condition } = this.props;
  116. const { measure } = condition;
  117. const { metric } = measure;
  118. const isDiff = isDiffMetric(metric.key);
  119. const subText =
  120. !isDiff && condition.period != null
  121. ? `${localizeMetric(metric.key)} ${translate('quality_gates.conditions.new_code')}`
  122. : localizeMetric(metric.key);
  123. if (metric.type !== MetricType.Rating) {
  124. const actual = (condition.period ? measure.period?.value : measure.value) as string;
  125. const formattedValue = formatMeasure(actual, metric.type, {
  126. decimals: 1,
  127. omitExtraDecimalZeros: metric.type === MetricType.Percent,
  128. });
  129. return `${formattedValue} ${subText}`;
  130. }
  131. return subText;
  132. };
  133. render() {
  134. const { condition } = this.props;
  135. const { measure } = condition;
  136. const { metric } = measure;
  137. const threshold = (condition.level === 'ERROR' ? condition.error : condition.warning) as string;
  138. const actual = (condition.period ? measure.period?.value : measure.value) as string;
  139. const operator = getOperatorLabel(condition.op, metric);
  140. return this.wrapWithLink(
  141. <div className="sw-flex sw-items-center sw-p-2">
  142. <MeasureIndicator
  143. className="sw-flex sw-justify-center sw-w-6 sw-mx-4"
  144. decimals={2}
  145. metricKey={measure.metric.key}
  146. metricType={measure.metric.type}
  147. value={actual}
  148. />
  149. <div className="sw-flex sw-flex-col sw-text-sm">
  150. <div className="sw-flex sw-items-center">
  151. <IssueTypeIcon className="sw-mr-2" type={metric.key} />
  152. <span className="sw-body-sm-highlight sw-text-ellipsis sw-max-w-abs-300">
  153. {this.getPrimaryText()}
  154. </span>
  155. </div>
  156. <TextMuted text={`${operator} ${formatMeasure(threshold, metric.type)}`} />
  157. </div>
  158. </div>,
  159. );
  160. }
  161. }