/* * SonarQube * Copyright (C) 2009-2017 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 React from 'react'; import { sortBy, uniqBy } from 'lodash'; import ConditionsAlert from './ConditionsAlert'; import AddConditionForm from './AddConditionForm'; import Condition from './Condition'; import { translate, getLocalizedMetricName } from '../../../helpers/l10n'; function getKey(condition, index) { return condition.id ? condition.id : `new-${index}`; } export default class Conditions extends React.PureComponent { state = { error: null }; componentWillUpdate(nextProps) { if (nextProps.qualityGate !== this.props.qualityGate) { this.setState({ error: null }); } } handleError(error) { try { error.response.json().then(r => { const message = r.errors.map(e => e.msg).join('. '); this.setState({ error: message }); }); } catch (ex) { this.setState({ error: translate('default_error_message') }); } } handleResetError() { this.setState({ error: null }); } render() { const { qualityGate, conditions, metrics, edit, onAddCondition, onSaveCondition, onDeleteCondition } = this.props; const existingConditions = conditions.filter(condition => metrics[condition.metric]); const sortedConditions = sortBy( existingConditions, condition => metrics[condition.metric] && metrics[condition.metric].name ); const duplicates = []; const savedConditions = existingConditions.filter(condition => condition.id != null); savedConditions.forEach(condition => { const sameCount = savedConditions.filter( sample => sample.metric === condition.metric && sample.period === condition.period ).length; if (sameCount > 1) { duplicates.push(condition); } }); const uniqDuplicates = uniqBy(duplicates, d => d.metric).map(condition => ({ ...condition, metric: metrics[condition.metric] })); return (

{translate('quality_gates.conditions')}

{this.state.error &&
{this.state.error}
} {uniqDuplicates.length > 0 && (

{translate('quality_gates.duplicated_conditions')}

    {uniqDuplicates.map(d => (
  • {getLocalizedMetricName(d.metric)}
  • ))}
)} {sortedConditions.length ? ( {edit && {sortedConditions.map((condition, index) => ( ))}
{translate('quality_gates.conditions.metric')} {translate('quality_gates.conditions.leak')} {translate('quality_gates.conditions.operator')} {translate('quality_gates.conditions.warning')} {translate('quality_gates.conditions.error')}}
) : (
{translate('quality_gates.no_conditions')}
)} {edit && }
); } }