Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

ConditionReviewAndUpdateModal.tsx 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 { ButtonPrimary, Link, Modal, SubHeading, Title } from 'design-system';
  21. import { sortBy } from 'lodash';
  22. import * as React from 'react';
  23. import { FormattedMessage } from 'react-intl';
  24. import { createCondition, updateCondition } from '../../../api/quality-gates';
  25. import { useDocUrl } from '../../../helpers/docs';
  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. isOptimizing?: boolean;
  43. }
  44. export default function CaycReviewUpdateConditionsModal(props: Readonly<Props>) {
  45. const {
  46. conditions,
  47. qualityGate,
  48. metrics,
  49. onSaveCondition,
  50. onAddCondition,
  51. lockEditing,
  52. onClose,
  53. isOptimizing,
  54. } = props;
  55. const { weakConditions, missingConditions } = getWeakMissingAndNonCaycConditions(conditions);
  56. const sortedWeakConditions = sortBy(
  57. weakConditions,
  58. (condition) => metrics[condition.metric]?.name,
  59. );
  60. const sortedMissingConditions = sortBy(
  61. missingConditions,
  62. (condition) => metrics[condition.metric]?.name,
  63. );
  64. const getDocUrl = useDocUrl();
  65. const updateCaycQualityGate = React.useCallback(() => {
  66. const promiseArr: Promise<Condition | undefined | void>[] = [];
  67. const { weakConditions, missingConditions } = getWeakMissingAndNonCaycConditions(conditions);
  68. weakConditions.forEach((condition) => {
  69. promiseArr.push(
  70. updateCondition({
  71. ...getCorrectCaycCondition(condition),
  72. id: condition.id,
  73. })
  74. .then((resultCondition) => {
  75. const currentCondition = conditions.find((con) => con.metric === condition.metric);
  76. if (currentCondition) {
  77. onSaveCondition({ ...resultCondition, isCaycCondition: true }, currentCondition);
  78. }
  79. })
  80. .catch(() => undefined),
  81. );
  82. });
  83. missingConditions.forEach((condition) => {
  84. promiseArr.push(
  85. createCondition({
  86. ...getCorrectCaycCondition(condition),
  87. gateName: qualityGate.name,
  88. })
  89. .then((resultCondition) => {
  90. onAddCondition({ ...resultCondition, isCaycCondition: true });
  91. })
  92. .catch(() => undefined),
  93. );
  94. });
  95. return Promise.all(promiseArr).then(() => {
  96. lockEditing();
  97. });
  98. }, [conditions, qualityGate, lockEditing, onAddCondition, onSaveCondition]);
  99. const body = (
  100. <div className="sw-mb-10">
  101. <SubHeading as="p" className="sw-body-sm">
  102. <FormattedMessage
  103. id={
  104. isOptimizing
  105. ? 'quality_gates.cayc.review_optimize_modal.description1'
  106. : 'quality_gates.cayc.review_update_modal.description1'
  107. }
  108. values={{
  109. cayc_link: (
  110. <Link to={getDocUrl('/user-guide/clean-as-you-code/')}>
  111. {translate('quality_gates.cayc')}
  112. </Link>
  113. ),
  114. }}
  115. />
  116. </SubHeading>
  117. {sortedMissingConditions.length > 0 && (
  118. <>
  119. <Title as="h4" className="sw-mb-2 sw-mt-4 sw-body-sm-highlight">
  120. {translateWithParameters(
  121. 'quality_gates.cayc.review_update_modal.add_condition.header',
  122. sortedMissingConditions.length,
  123. )}
  124. </Title>
  125. <ConditionsTable
  126. {...props}
  127. conditions={sortedMissingConditions}
  128. showEdit={false}
  129. isCaycModal
  130. />
  131. </>
  132. )}
  133. {sortedWeakConditions.length > 0 && (
  134. <>
  135. <Title as="h4" className="sw-mb-2 sw-mt-4 sw-body-sm-highlight">
  136. {translateWithParameters(
  137. 'quality_gates.cayc.review_update_modal.modify_condition.header',
  138. sortedWeakConditions.length,
  139. )}
  140. </Title>
  141. <ConditionsTable
  142. {...props}
  143. conditions={sortedWeakConditions}
  144. showEdit={false}
  145. isCaycModal
  146. />
  147. </>
  148. )}
  149. <Title as="h4" className="sw-mb-2 sw-mt-4 sw-body-sm-highlight">
  150. {translate('quality_gates.cayc.review_update_modal.description2')}
  151. </Title>
  152. </div>
  153. );
  154. return (
  155. <Modal
  156. isLarge
  157. headerTitle={translateWithParameters(
  158. isOptimizing
  159. ? 'quality_gates.cayc.review_optimize_modal.header'
  160. : 'quality_gates.cayc.review_update_modal.header',
  161. qualityGate.name,
  162. )}
  163. onClose={onClose}
  164. body={body}
  165. primaryButton={
  166. <ButtonPrimary
  167. autoFocus
  168. id="fix-quality-gate"
  169. type="submit"
  170. onClick={updateCaycQualityGate}
  171. >
  172. {translate(
  173. isOptimizing
  174. ? 'quality_gates.cayc.review_optimize_modal.confirm_text'
  175. : 'quality_gates.cayc.review_update_modal.confirm_text',
  176. )}
  177. </ButtonPrimary>
  178. }
  179. secondaryButtonLabel={translate('close')}
  180. />
  181. );
  182. }