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
|
/*
* SonarQube
* Copyright (C) 2009-2023 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 {
ButtonSecondary,
ContentCell,
DangerButtonSecondary,
HeadingDark,
Link,
Spinner,
Table,
TableRow,
UnorderedList,
} from 'design-system';
import { sortBy } from 'lodash';
import * as React from 'react';
import { deleteRule, searchRules } from '../../../api/rules';
import ConfirmButton from '../../../components/controls/ConfirmButton';
import IssueSeverityIcon from '../../../components/icon-mappers/IssueSeverityIcon';
import { translate, translateWithParameters } from '../../../helpers/l10n';
import { getRuleUrl } from '../../../helpers/urls';
import { IssueSeverity } from '../../../types/issues';
import { Rule, RuleDetails } from '../../../types/types';
import CustomRuleButton from './CustomRuleButton';
interface Props {
canChange?: boolean;
ruleDetails: RuleDetails;
}
interface State {
loading: boolean;
rules?: Rule[];
}
const COLUMN_COUNT = 3;
const COLUMN_COUNT_WITH_EDIT_PERMISSIONS = 4;
export default class RuleDetailsCustomRules extends React.PureComponent<Props, State> {
mounted = false;
state: State = { loading: false };
componentDidMount() {
this.mounted = true;
this.fetchRules();
}
componentDidUpdate(prevProps: Props) {
if (prevProps.ruleDetails.key !== this.props.ruleDetails.key) {
this.fetchRules();
}
}
componentWillUnmount() {
this.mounted = false;
}
fetchRules = () => {
this.setState({ loading: true });
searchRules({
f: 'name,severity,params',
template_key: this.props.ruleDetails.key,
}).then(
({ rules }) => {
if (this.mounted) {
this.setState({ rules, loading: false });
}
},
() => {
if (this.mounted) {
this.setState({ loading: false });
}
},
);
};
handleRuleCreate = (newRuleDetails: RuleDetails) => {
if (this.mounted) {
this.setState(({ rules = [] }: State) => ({
rules: [...rules, newRuleDetails],
}));
}
};
handleRuleDelete = (ruleKey: string) => {
return deleteRule({ key: ruleKey }).then(() => {
if (this.mounted) {
this.setState(({ rules = [] }) => ({
rules: rules.filter((rule) => rule.key !== ruleKey),
}));
}
});
};
renderRule = (rule: Rule) => (
<TableRow data-rule={rule.key} key={rule.key}>
<ContentCell>
<Link to={getRuleUrl(rule.key)}>{rule.name}</Link>
</ContentCell>
<ContentCell>
<IssueSeverityIcon
className="sw-mr-1"
severity={rule.severity as IssueSeverity}
aria-hidden
/>
{translate('severity', rule.severity)}
</ContentCell>
<ContentCell>
<UnorderedList className="sw-mt-0">
{rule.params
?.filter((param) => param.defaultValue)
.map((param) => (
<li key={param.key}>
<span className="sw-font-semibold">{param.key}</span>
<span>: </span>
<span title={param.defaultValue}>{param.defaultValue}</span>
</li>
))}
</UnorderedList>
</ContentCell>
{this.props.canChange && (
<ContentCell>
<ConfirmButton
confirmButtonText={translate('delete')}
confirmData={rule.key}
isDestructive
modalBody={translateWithParameters('coding_rules.delete.custom.confirm', rule.name)}
modalHeader={translate('coding_rules.delete_rule')}
onConfirm={this.handleRuleDelete}
>
{({ onClick }) => (
<DangerButtonSecondary
className="js-delete-custom-rule"
aria-label={translateWithParameters('coding_rules.delete_rule_x', rule.name)}
onClick={onClick}
>
{translate('delete')}
</DangerButtonSecondary>
)}
</ConfirmButton>
</ContentCell>
)}
</TableRow>
);
render() {
const { loading, rules = [] } = this.state;
return (
<div className="js-rule-custom-rules">
<div>
<HeadingDark as="h2">{translate('coding_rules.custom_rules')}</HeadingDark>
{this.props.canChange && (
<CustomRuleButton onDone={this.handleRuleCreate} templateRule={this.props.ruleDetails}>
{({ onClick }) => (
<ButtonSecondary className="js-create-custom-rule sw-mt-6" onClick={onClick}>
{translate('coding_rules.create')}
</ButtonSecondary>
)}
</CustomRuleButton>
)}
<Spinner className="sw-my-6" loading={loading}>
{rules.length > 0 && (
<Table
className="sw-my-6"
id="coding-rules-detail-custom-rules"
columnCount={
this.props.canChange ? COLUMN_COUNT_WITH_EDIT_PERMISSIONS : COLUMN_COUNT
}
>
{sortBy(rules, (rule) => rule.name).map(this.renderRule)}
</Table>
)}
</Spinner>
</div>
</div>
);
}
}
|