aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-web/src/main/js/apps/coding-rules/components/RuleDetailsProfiles.tsx
blob: 94b9422d37ce6aa1f78b9bd4ca4c12e1367b39bb (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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/*
 * SonarQube
 * Copyright (C) 2009-2022 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 { filter } from 'lodash';
import * as React from 'react';
import { Link } from 'react-router';
import { activateRule, deactivateRule, Profile } from '../../../api/quality-profiles';
import InstanceMessage from '../../../components/common/InstanceMessage';
import { Button } from '../../../components/controls/buttons';
import ConfirmButton from '../../../components/controls/ConfirmButton';
import Tooltip from '../../../components/controls/Tooltip';
import SeverityHelper from '../../../components/shared/SeverityHelper';
import { translate, translateWithParameters } from '../../../helpers/l10n';
import { getQualityProfileUrl } from '../../../helpers/urls';
import { Dict, RuleActivation, RuleDetails } from '../../../types/types';
import BuiltInQualityProfileBadge from '../../quality-profiles/components/BuiltInQualityProfileBadge';
import ActivationButton from './ActivationButton';
import RuleInheritanceIcon from './RuleInheritanceIcon';

interface Props {
  activations: RuleActivation[] | undefined;
  canWrite: boolean | undefined;
  onActivate: () => Promise<void>;
  onDeactivate: () => Promise<void>;
  referencedProfiles: Dict<Profile>;
  ruleDetails: RuleDetails;
}

export default class RuleDetailsProfiles extends React.PureComponent<Props> {
  handleActivate = () => this.props.onActivate();

  handleDeactivate = (key?: string) => {
    if (key) {
      deactivateRule({
        key,
        rule: this.props.ruleDetails.key
      }).then(this.props.onDeactivate, () => {});
    }
  };

  handleRevert = (key?: string) => {
    if (key) {
      activateRule({
        key,
        rule: this.props.ruleDetails.key,
        reset: true
      }).then(this.props.onActivate, () => {});
    }
  };

  renderInheritedProfile = (activation: RuleActivation, profile: Profile) => {
    if (!profile.parentName) {
      return null;
    }
    const profilePath = getQualityProfileUrl(profile.parentName, profile.language);
    return (
      <div className="coding-rules-detail-quality-profile-inheritance">
        {(activation.inherit === 'OVERRIDES' || activation.inherit === 'INHERITED') && (
          <>
            <RuleInheritanceIcon className="text-middle" inheritance={activation.inherit} />
            <Link className="link-base-color little-spacer-left text-middle" to={profilePath}>
              {profile.parentName}
            </Link>
          </>
        )}
      </div>
    );
  };

  renderSeverity = (activation: RuleActivation, parentActivation?: RuleActivation) => (
    <td className="coding-rules-detail-quality-profile-severity">
      <Tooltip overlay={translate('coding_rules.activation_severity')}>
        <span>
          <SeverityHelper className="display-inline-flex-center" severity={activation.severity} />
        </span>
      </Tooltip>
      {parentActivation !== undefined && activation.severity !== parentActivation.severity && (
        <div className="coding-rules-detail-quality-profile-inheritance">
          {translate('coding_rules.original')} {translate('severity', parentActivation.severity)}
        </div>
      )}
    </td>
  );

  renderParameter = (param: { key: string; value: string }, parentActivation?: RuleActivation) => {
    const originalParam =
      parentActivation && parentActivation.params.find(p => p.key === param.key);
    const originalValue = originalParam && originalParam.value;

    return (
      <div className="coding-rules-detail-quality-profile-parameter" key={param.key}>
        <span className="key">{param.key}</span>
        <span className="sep">: </span>
        <span className="value" title={param.value}>
          {param.value}
        </span>
        {parentActivation && param.value !== originalValue && (
          <div className="coding-rules-detail-quality-profile-inheritance">
            {translate('coding_rules.original')} <span className="value">{originalValue}</span>
          </div>
        )}
      </div>
    );
  };

  renderParameters = (activation: RuleActivation, parentActivation?: RuleActivation) => (
    <td className="coding-rules-detail-quality-profile-parameters">
      {activation.params.map(param => this.renderParameter(param, parentActivation))}
    </td>
  );

  renderActions = (activation: RuleActivation, profile: Profile) => {
    const canEdit = profile.actions && profile.actions.edit && !profile.isBuiltIn;
    const { ruleDetails } = this.props;
    const hasParent = activation.inherit !== 'NONE' && profile.parentKey;
    return (
      <td className="coding-rules-detail-quality-profile-actions">
        {canEdit && (
          <>
            {!ruleDetails.isTemplate && (
              <ActivationButton
                activation={activation}
                buttonText={translate('change_verb')}
                className="coding-rules-detail-quality-profile-change"
                modalHeader={translate('coding_rules.change_details')}
                onDone={this.handleActivate}
                profiles={[profile]}
                rule={ruleDetails}
              />
            )}
            {hasParent ? (
              activation.inherit === 'OVERRIDES' &&
              profile.parentName && (
                <ConfirmButton
                  confirmButtonText={translate('yes')}
                  confirmData={profile.key}
                  modalBody={translateWithParameters(
                    'coding_rules.revert_to_parent_definition.confirm',
                    profile.parentName
                  )}
                  modalHeader={translate('coding_rules.revert_to_parent_definition')}
                  onConfirm={this.handleRevert}>
                  {({ onClick }) => (
                    <Button
                      className="coding-rules-detail-quality-profile-revert button-red spacer-left"
                      onClick={onClick}>
                      {translate('coding_rules.revert_to_parent_definition')}
                    </Button>
                  )}
                </ConfirmButton>
              )
            ) : (
              <ConfirmButton
                confirmButtonText={translate('yes')}
                confirmData={profile.key}
                modalBody={translate('coding_rules.deactivate.confirm')}
                modalHeader={translate('coding_rules.deactivate')}
                onConfirm={this.handleDeactivate}>
                {({ onClick }) => (
                  <Button
                    className="coding-rules-detail-quality-profile-deactivate button-red spacer-left"
                    onClick={onClick}>
                    {translate('coding_rules.deactivate')}
                  </Button>
                )}
              </ConfirmButton>
            )}
          </>
        )}
      </td>
    );
  };

  renderActivation = (activation: RuleActivation) => {
    const { activations = [], ruleDetails } = this.props;
    const profile = this.props.referencedProfiles[activation.qProfile];
    if (!profile) {
      return null;
    }

    const parentActivation = activations.find(x => x.qProfile === profile.parentKey);

    return (
      <tr data-profile={profile.key} key={profile.key}>
        <td className="coding-rules-detail-quality-profile-name">
          <Link to={getQualityProfileUrl(profile.name, profile.language)}>{profile.name}</Link>
          {profile.isBuiltIn && <BuiltInQualityProfileBadge className="spacer-left" />}
          {this.renderInheritedProfile(activation, profile)}
        </td>

        {this.renderSeverity(activation, parentActivation)}
        {!ruleDetails.templateKey && this.renderParameters(activation, parentActivation)}
        {this.renderActions(activation, profile)}
      </tr>
    );
  };

  render() {
    const { activations = [], referencedProfiles, ruleDetails } = this.props;
    const canActivate = Object.values(referencedProfiles).some(profile =>
      Boolean(profile.actions && profile.actions.edit && profile.language === ruleDetails.lang)
    );

    return (
      <div className="js-rule-profiles coding-rule-section">
        <div className="coding-rules-detail-quality-profiles-section">
          <div className="coding-rule-section-separator" />

          <h3 className="coding-rules-detail-title">
            <InstanceMessage message={translate('coding_rules.quality_profiles')} />
          </h3>

          {canActivate && (
            <ActivationButton
              buttonText={translate('coding_rules.activate')}
              className="coding-rules-quality-profile-activate spacer-left"
              modalHeader={translate('coding_rules.activate_in_quality_profile')}
              onDone={this.handleActivate}
              profiles={filter(
                this.props.referencedProfiles,
                profile => !activations.find(activation => activation.qProfile === profile.key)
              )}
              rule={ruleDetails}
            />
          )}

          {activations.length > 0 && (
            <table
              className="coding-rules-detail-quality-profiles width-100"
              id="coding-rules-detail-quality-profiles">
              <tbody>{activations.map(this.renderActivation)}</tbody>
            </table>
          )}
        </div>
      </div>
    );
  }
}