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.

settings.ts 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 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 { omitBy } from 'lodash';
  21. import throwGlobalError from '../app/utils/throwGlobalError';
  22. import { isCategoryDefinition } from '../apps/settings/utils';
  23. import { getJSON, post, postJSON, RequestData } from '../helpers/request';
  24. import { BranchParameters } from '../types/branch-like';
  25. import { SettingCategoryDefinition, SettingDefinition, SettingValue } from '../types/settings';
  26. export function getDefinitions(component?: string): Promise<SettingCategoryDefinition[]> {
  27. return getJSON('/api/settings/list_definitions', { component }).then(
  28. r => r.definitions,
  29. throwGlobalError
  30. );
  31. }
  32. export function getValues(
  33. data: { keys: string; component?: string } & BranchParameters
  34. ): Promise<SettingValue[]> {
  35. return getJSON('/api/settings/values', data).then(r => r.settings);
  36. }
  37. export function setSettingValue(
  38. definition: SettingDefinition,
  39. value: any,
  40. component?: string
  41. ): Promise<void> {
  42. const { key } = definition;
  43. const data: RequestData = { key, component };
  44. if (isCategoryDefinition(definition) && definition.multiValues) {
  45. data.values = value;
  46. } else if (definition.type === 'PROPERTY_SET') {
  47. data.fieldValues = value
  48. .map((fields: any) => omitBy(fields, value => value == null))
  49. .map(JSON.stringify);
  50. } else {
  51. data.value = value;
  52. }
  53. return post('/api/settings/set', data);
  54. }
  55. export function setSimpleSettingValue(
  56. data: { component?: string; value: string; key: string } & BranchParameters
  57. ): Promise<void | Response> {
  58. return post('/api/settings/set', data).catch(throwGlobalError);
  59. }
  60. export function resetSettingValue(
  61. data: { keys: string; component?: string } & BranchParameters
  62. ): Promise<void> {
  63. return post('/api/settings/reset', data);
  64. }
  65. export function sendTestEmail(to: string, subject: string, message: string): Promise<void> {
  66. return post('/api/emails/send', { to, subject, message });
  67. }
  68. export function checkSecretKey(): Promise<{ secretKeyAvailable: boolean }> {
  69. return getJSON('/api/settings/check_secret_key').catch(throwGlobalError);
  70. }
  71. export function generateSecretKey(): Promise<{ secretKey: string }> {
  72. return postJSON('/api/settings/generate_secret_key').catch(throwGlobalError);
  73. }
  74. export function encryptValue(value: string): Promise<{ encryptedValue: string }> {
  75. return postJSON('/api/settings/encrypt', { value }).catch(throwGlobalError);
  76. }