aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-web/src/main/js/apps/overview/branches/QualityGateConditions.tsx
blob: 51d9dd8582037cbb3c0bae9c7b2b783061eb5484 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
 * SonarQube
 * Copyright (C) 2009-2024 SonarSource SA
 * mailto:info AT sonarsource DOT com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
import { CardSeparator, Link } from 'design-system';
import { sortBy } from 'lodash';
import * as React from 'react';
import { MetricKey } from '~sonar-aligned/types/metrics';
import { translate } from '../../../helpers/l10n';
import { BranchLike } from '../../../types/branch-like';
import { QualityGateStatusConditionEnhanced } from '../../../types/quality-gates';
import { Component } from '../../../types/types';
import { CAYC_CONDITION_ORDER_PRIORITIES } from '../../quality-gates/utils';
import QualityGateCondition from './QualityGateCondition';
import QualityGateSimplifiedCondition from './QualityGateSimplifiedCondition';

const LEVEL_ORDER = ['ERROR', 'WARN'];

export interface QualityGateConditionsProps {
  branchLike?: BranchLike;
  collapsible?: boolean;
  component: Pick<Component, 'key'>;
  failedConditions: QualityGateStatusConditionEnhanced[];
  isBuiltInQualityGate?: boolean;
}

const MAX_CONDITIONS = 5;

export function QualityGateConditions(props: Readonly<QualityGateConditionsProps>) {
  const { branchLike, collapsible, component, failedConditions, isBuiltInQualityGate } = props;
  const [collapsed, toggleCollapsed] = React.useState(Boolean(collapsible));

  const handleToggleCollapsed = React.useCallback(() => toggleCollapsed(!collapsed), [collapsed]);

  const isSimplifiedCondition = React.useCallback(
    (condition: QualityGateStatusConditionEnhanced) => {
      const { metric } = condition.measure;
      return metric.key === MetricKey.new_violations && isBuiltInQualityGate;
    },
    [isBuiltInQualityGate],
  );

  const sortedConditions = sortBy(failedConditions, [
    (condition) => CAYC_CONDITION_ORDER_PRIORITIES[condition.metric],
    (condition) => LEVEL_ORDER.indexOf(condition.level),
  ]);

  let renderConditions;
  let renderCollapsed;

  if (collapsed && sortedConditions.length > MAX_CONDITIONS) {
    renderConditions = sortedConditions.slice(0, MAX_CONDITIONS);
    renderCollapsed = true;
  } else {
    renderConditions = sortedConditions;
    renderCollapsed = false;
  }

  return (
    <ul id="overview-quality-gate-conditions-list" className="sw-mb-2">
      {renderConditions.map((condition) => (
        <div key={condition.measure.metric.key}>
          {isSimplifiedCondition(condition) ? (
            <QualityGateSimplifiedCondition
              branchLike={branchLike}
              component={component}
              condition={condition}
            />
          ) : (
            <QualityGateCondition
              branchLike={branchLike}
              component={component}
              condition={condition}
            />
          )}
          <CardSeparator />
        </div>
      ))}
      {renderCollapsed && (
        <li className="sw-flex sw-justify-center sw-my-3">
          <Link onClick={handleToggleCollapsed} to={{}} preventDefault>
            <span className="sw-font-semibold sw-text-sm">{translate('show_more')}</span>
          </Link>
        </li>
      )}
    </ul>
  );
}

export default React.memo(QualityGateConditions);