You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

utils.ts 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2022 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 { NewCodePeriodSettingType } from '../../types/types';
  21. export function validateDays(days: string) {
  22. const parsed = parseInt(days, 10);
  23. return !(days.length < 1 || isNaN(parsed) || parsed < 1 || String(parsed) !== days);
  24. }
  25. export function getSettingValue({
  26. analysis,
  27. days,
  28. referenceBranch,
  29. type
  30. }: {
  31. analysis?: string;
  32. days?: string;
  33. referenceBranch?: string;
  34. type?: NewCodePeriodSettingType;
  35. }) {
  36. switch (type) {
  37. case 'NUMBER_OF_DAYS':
  38. return days;
  39. case 'REFERENCE_BRANCH':
  40. return referenceBranch;
  41. case 'SPECIFIC_ANALYSIS':
  42. return analysis;
  43. default:
  44. return undefined;
  45. }
  46. }
  47. export function validateSetting(state: {
  48. analysis?: string;
  49. currentSetting?: NewCodePeriodSettingType;
  50. currentSettingValue?: string;
  51. days: string;
  52. overrideGeneralSetting?: boolean;
  53. referenceBranch?: string;
  54. selected?: NewCodePeriodSettingType;
  55. }) {
  56. const {
  57. analysis = '',
  58. currentSetting,
  59. currentSettingValue,
  60. days,
  61. overrideGeneralSetting,
  62. referenceBranch = '',
  63. selected
  64. } = state;
  65. let isChanged;
  66. if (!currentSetting && overrideGeneralSetting !== undefined) {
  67. isChanged = overrideGeneralSetting;
  68. } else {
  69. isChanged =
  70. overrideGeneralSetting === false ||
  71. selected !== currentSetting ||
  72. (selected === 'NUMBER_OF_DAYS' && days !== currentSettingValue) ||
  73. (selected === 'SPECIFIC_ANALYSIS' && analysis !== currentSettingValue) ||
  74. (selected === 'REFERENCE_BRANCH' && referenceBranch !== currentSettingValue);
  75. }
  76. const isValid =
  77. overrideGeneralSetting === false ||
  78. selected === 'PREVIOUS_VERSION' ||
  79. (selected === 'SPECIFIC_ANALYSIS' && analysis.length > 0) ||
  80. (selected === 'NUMBER_OF_DAYS' && validateDays(days)) ||
  81. (selected === 'REFERENCE_BRANCH' && referenceBranch.length > 0);
  82. return { isChanged, isValid };
  83. }