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.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2024 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 { isCategoryDefinition } from '../apps/settings/utils';
  22. import { throwGlobalError } from '../helpers/error';
  23. import { getJSON, post, postJSON, RequestData } from '../helpers/request';
  24. import { BranchParameters } from '../types/branch-like';
  25. import {
  26. ExtendedSettingDefinition,
  27. SettingDefinition,
  28. SettingValue,
  29. SettingValueResponse,
  30. } from '../types/settings';
  31. export function getDefinitions(component?: string): Promise<ExtendedSettingDefinition[]> {
  32. return getJSON('/api/settings/list_definitions', { component }).then(
  33. (r) => r.definitions,
  34. throwGlobalError
  35. );
  36. }
  37. export function getValue(
  38. data: { key: string; component?: string } & BranchParameters
  39. ): Promise<SettingValue> {
  40. return getValues({ keys: [data.key], component: data.component }).then(([result]) => result);
  41. }
  42. export function getValues(
  43. data: { keys: string[]; component?: string } & BranchParameters
  44. ): Promise<SettingValue[]> {
  45. return getJSON('/api/settings/values', {
  46. keys: data.keys.join(','),
  47. component: data.component,
  48. }).then((r: SettingValueResponse) => [
  49. ...r.settings,
  50. ...r.setSecuredSettings.map((key) => ({ key })),
  51. ]);
  52. }
  53. export function getAllValues(
  54. data: { component?: string } & BranchParameters = {}
  55. ): Promise<SettingValue[]> {
  56. return getJSON('/api/settings/values', data).then((r: SettingValueResponse) => [
  57. ...r.settings,
  58. ...r.setSecuredSettings.map((key) => ({ key })),
  59. ]);
  60. }
  61. export function setSettingValue(
  62. definition: SettingDefinition,
  63. value: any,
  64. component?: string
  65. ): Promise<void> {
  66. const { key } = definition;
  67. const data: RequestData = { key, component };
  68. if (isCategoryDefinition(definition) && definition.multiValues) {
  69. data.values = value;
  70. } else if (definition.type === 'PROPERTY_SET') {
  71. data.fieldValues = value
  72. .map((fields: any) => omitBy(fields, (value) => value == null))
  73. .map(JSON.stringify);
  74. } else {
  75. data.value = value;
  76. }
  77. return post('/api/settings/set', data);
  78. }
  79. export function setSimpleSettingValue(
  80. data: { component?: string; value: string; key: string } & BranchParameters
  81. ): Promise<void | Response> {
  82. return post('/api/settings/set', data).catch(throwGlobalError);
  83. }
  84. export function resetSettingValue(
  85. data: { keys: string; component?: string } & BranchParameters
  86. ): Promise<void> {
  87. return post('/api/settings/reset', data);
  88. }
  89. export function sendTestEmail(to: string, subject: string, message: string): Promise<void> {
  90. return post('/api/emails/send', { to, subject, message });
  91. }
  92. export function checkSecretKey(): Promise<{ secretKeyAvailable: boolean }> {
  93. return getJSON('/api/settings/check_secret_key').catch(throwGlobalError);
  94. }
  95. export function generateSecretKey(): Promise<{ secretKey: string }> {
  96. return getJSON('/api/settings/generate_secret_key').catch(throwGlobalError);
  97. }
  98. export function encryptValue(value: string): Promise<{ encryptedValue: string }> {
  99. return postJSON('/api/settings/encrypt', { value }).catch(throwGlobalError);
  100. }
  101. export function getLoginMessage(): Promise<{ message: string }> {
  102. return getJSON('/api/settings/login_message').catch(throwGlobalError);
  103. }