Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

ConditionReviewAndUpdateModal.tsx 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2023 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. import { sortBy } from 'lodash';
  21. import * as React from 'react';
  22. import { FormattedMessage } from 'react-intl';
  23. import { createCondition, updateCondition } from '../../../api/quality-gates';
  24. import DocLink from '../../../components/common/DocLink';
  25. import ConfirmModal from '../../../components/controls/ConfirmModal';
  26. import { translate, translateWithParameters } from '../../../helpers/l10n';
  27. import { Condition, Dict, Metric, QualityGate } from '../../../types/types';
  28. import { getCorrectCaycCondition, getWeakMissingAndNonCaycConditions } from '../utils';
  29. import ConditionsTable from './ConditionsTable';
  30. interface Props {
  31. canEdit: boolean;
  32. metrics: Dict<Metric>;
  33. updatedConditionId?: string;
  34. conditions: Condition[];
  35. scope: 'new' | 'overall' | 'new-cayc';
  36. onClose: () => void;
  37. onAddCondition: (condition: Condition) => void;
  38. onRemoveCondition: (condition: Condition) => void;
  39. onSaveCondition: (newCondition: Condition, oldCondition: Condition) => void;
  40. lockEditing: () => void;
  41. qualityGate: QualityGate;
  42. }
  43. export default class CaycReviewUpdateConditionsModal extends React.PureComponent<Props> {
  44. updateCaycQualityGate = () => {
  45. const { conditions, qualityGate } = this.props;
  46. const promiseArr: Promise<Condition | undefined | void>[] = [];
  47. const { weakConditions, missingConditions } = getWeakMissingAndNonCaycConditions(conditions);
  48. weakConditions.forEach((condition) => {
  49. promiseArr.push(
  50. updateCondition({
  51. ...getCorrectCaycCondition(condition),
  52. id: condition.id,
  53. })
  54. .then((resultCondition) => {
  55. const currentCondition = conditions.find((con) => con.metric === condition.metric);
  56. if (currentCondition) {
  57. this.props.onSaveCondition(resultCondition, currentCondition);
  58. }
  59. })
  60. .catch(() => undefined)
  61. );
  62. });
  63. missingConditions.forEach((condition) => {
  64. promiseArr.push(
  65. createCondition({
  66. ...getCorrectCaycCondition(condition),
  67. gateName: qualityGate.name,
  68. })
  69. .then((resultCondition) => this.props.onAddCondition(resultCondition))
  70. .catch(() => undefined)
  71. );
  72. });
  73. return Promise.all(promiseArr).then(() => {
  74. this.props.lockEditing();
  75. });
  76. };
  77. render() {
  78. const { conditions, qualityGate, metrics } = this.props;
  79. const { weakConditions, missingConditions } = getWeakMissingAndNonCaycConditions(conditions);
  80. const sortedWeakConditions = sortBy(
  81. weakConditions,
  82. (condition) => metrics[condition.metric] && metrics[condition.metric].name
  83. );
  84. const sortedMissingConditions = sortBy(
  85. missingConditions,
  86. (condition) => metrics[condition.metric] && metrics[condition.metric].name
  87. );
  88. return (
  89. <ConfirmModal
  90. header={translateWithParameters(
  91. 'quality_gates.cayc.review_update_modal.header',
  92. qualityGate.name
  93. )}
  94. confirmButtonText={translate('quality_gates.cayc.review_update_modal.confirm_text')}
  95. onClose={this.props.onClose}
  96. onConfirm={this.updateCaycQualityGate}
  97. size="medium"
  98. >
  99. <div className="quality-gate-section huge-spacer-bottom">
  100. <p>
  101. <FormattedMessage
  102. id="quality_gates.cayc.review_update_modal.description1"
  103. defaultMessage={translate('quality_gates.cayc.review_update_modal.description1')}
  104. values={{
  105. cayc_link: (
  106. <DocLink to="/user-guide/clean-as-you-code/">
  107. {translate('quality_gates.cayc')}
  108. </DocLink>
  109. ),
  110. }}
  111. />
  112. </p>
  113. {sortedMissingConditions.length > 0 && (
  114. <>
  115. <h4 className="big-spacer-top spacer-bottom">
  116. {translateWithParameters(
  117. 'quality_gates.cayc.review_update_modal.add_condition.header',
  118. sortedMissingConditions.length
  119. )}
  120. </h4>
  121. <ConditionsTable
  122. {...this.props}
  123. conditions={sortedMissingConditions}
  124. showEdit={false}
  125. isCaycModal={true}
  126. />
  127. </>
  128. )}
  129. {sortedWeakConditions.length > 0 && (
  130. <>
  131. <h4 className="big-spacer-top spacer-bottom">
  132. {translateWithParameters(
  133. 'quality_gates.cayc.review_update_modal.modify_condition.header',
  134. sortedWeakConditions.length
  135. )}
  136. </h4>
  137. <ConditionsTable
  138. {...this.props}
  139. conditions={sortedWeakConditions}
  140. showEdit={false}
  141. isCaycModal={true}
  142. />
  143. </>
  144. )}
  145. <h4 className="big-spacer-top spacer-bottom">
  146. {translate('quality_gates.cayc.review_update_modal.description2')}
  147. </h4>
  148. </div>
  149. </ConfirmModal>
  150. );
  151. }
  152. }