/* * SonarQube * Copyright (C) 2009-2019 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 * as React from 'react'; import { translate } from 'sonar-ui-common/helpers/l10n'; import { SubmitButton, ResetButtonLink } from 'sonar-ui-common/components/controls/buttons'; import Modal from 'sonar-ui-common/components/controls/Modal'; import { Alert } from 'sonar-ui-common/components/ui/Alert'; import Select from 'sonar-ui-common/components/controls/Select'; import SeverityHelper from '../../../components/shared/SeverityHelper'; import { activateRule, Profile } from '../../../api/quality-profiles'; import { SEVERITIES } from '../../../helpers/constants'; import { sortProfiles } from '../../quality-profiles/utils'; interface Props { activation?: T.RuleActivation; modalHeader: string; onClose: () => void; onDone: (severity: string) => Promise; organization: string | undefined; profiles: Profile[]; rule: T.Rule | T.RuleDetails; } interface State { params: T.Dict; profile: string; severity: string; submitting: boolean; } export default class ActivationFormModal extends React.PureComponent { mounted = false; constructor(props: Props) { super(props); const profilesWithDepth = this.getQualityProfilesWithDepth(props); this.state = { params: this.getParams(props), profile: profilesWithDepth.length > 0 ? profilesWithDepth[0].key : '', severity: props.activation ? props.activation.severity : props.rule.severity, submitting: false }; } componentDidMount() { this.mounted = true; } componentWillUnmount() { this.mounted = false; } getParams = ({ activation, rule } = this.props) => { const params: T.Dict = {}; if (rule && rule.params) { for (const param of rule.params) { params[param.key] = param.defaultValue || ''; } if (activation && activation.params) { for (const param of activation.params) { params[param.key] = param.value; } } } return params; }; // Choose QP which a user can administrate, which are the same language and which are not built-in getQualityProfilesWithDepth = ({ profiles } = this.props) => { return sortProfiles( profiles.filter( profile => !profile.isBuiltIn && profile.actions && profile.actions.edit && profile.language === this.props.rule.lang ) ).map(profile => ({ ...profile, // Decrease depth by 1, so the top level starts at 0 depth: profile.depth - 1 })); }; handleFormSubmit = (event: React.SyntheticEvent) => { event.preventDefault(); this.setState({ submitting: true }); const data = { key: this.state.profile, organization: this.props.organization, params: this.state.params, rule: this.props.rule.key, severity: this.state.severity }; activateRule(data) .then(() => this.props.onDone(data.severity)) .then( () => { if (this.mounted) { this.setState({ submitting: false }); this.props.onClose(); } }, () => { if (this.mounted) { this.setState({ submitting: false }); } } ); }; handleParameterChange = (event: React.SyntheticEvent) => { const { name, value } = event.currentTarget; this.setState((state: State) => ({ params: { ...state.params, [name]: value } })); }; handleProfileChange = ({ value }: { value: string }) => { this.setState({ profile: value }); }; handleSeverityChange = ({ value }: { value: string }) => { this.setState({ severity: value }); }; renderSeverityOption = ({ value }: { value: string }) => { return ; }; render() { const { activation, rule } = this.props; const { profile, severity, submitting } = this.state; const { params = [] } = rule; const profilesWithDepth = this.getQualityProfilesWithDepth(); const isCustomRule = !!(rule as T.RuleDetails).templateKey; const activeInAllProfiles = profilesWithDepth.length <= 0; const isUpdateMode = !!activation; return (

{this.props.modalHeader}

{!isUpdateMode && activeInAllProfiles && ( {translate('coding_rules.active_in_all_profiles')} )}
({ label: translate('severity', severity), value: severity }))} searchable={false} value={severity} valueRenderer={this.renderSeverityOption} />
{isCustomRule ? (

{translate('coding_rules.custom_rule.activation_notice')}

) : ( params.map(param => (
{param.type === 'TEXT' ? (