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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 throwGlobalError from '../app/utils/throwGlobalError';
  21. import { getJSON, post, postJSON } from '../helpers/request';
  22. import { GetRulesAppResponse, SearchRulesResponse } from '../types/coding-rules';
  23. export function getRulesApp(): Promise<GetRulesAppResponse> {
  24. return getJSON('/api/rules/app').catch(throwGlobalError);
  25. }
  26. export function searchRules(data: {
  27. activation?: boolean | string;
  28. active_severities?: string;
  29. asc?: boolean | string;
  30. available_since?: string;
  31. cwe?: string;
  32. f?: string;
  33. facets?: string;
  34. include_external?: boolean | string;
  35. inheritance?: string;
  36. is_template?: boolean | string;
  37. languages?: string;
  38. owaspTop10?: string;
  39. p?: number;
  40. ps?: number;
  41. q?: string;
  42. qprofile?: string;
  43. repositories?: string;
  44. rule_key?: string;
  45. s?: string;
  46. sansTop25?: string;
  47. severities?: string;
  48. sonarsourceSecurity?: string;
  49. statuses?: string;
  50. tags?: string;
  51. template_key?: string;
  52. types?: string;
  53. }): Promise<SearchRulesResponse> {
  54. return getJSON('/api/rules/search', data).catch(throwGlobalError);
  55. }
  56. export function takeFacet(response: SearchRulesResponse, property: string) {
  57. const facet = response.facets?.find(f => f.property === property);
  58. return facet ? facet.values : [];
  59. }
  60. export function getRuleDetails(parameters: {
  61. actives?: boolean;
  62. key: string;
  63. }): Promise<{ actives?: T.RuleActivation[]; rule: T.RuleDetails }> {
  64. return getJSON('/api/rules/show', parameters).catch(throwGlobalError);
  65. }
  66. export function getRuleTags(parameters: { ps?: number; q: string }): Promise<string[]> {
  67. return getJSON('/api/rules/tags', parameters).then(r => r.tags, throwGlobalError);
  68. }
  69. export function createRule(data: {
  70. custom_key: string;
  71. markdown_description: string;
  72. name: string;
  73. params?: string;
  74. prevent_reactivation?: boolean;
  75. severity?: string;
  76. status?: string;
  77. template_key: string;
  78. type?: string;
  79. }): Promise<T.RuleDetails> {
  80. return postJSON('/api/rules/create', data).then(
  81. r => r.rule,
  82. response => {
  83. // do not show global error if the status code is 409
  84. // this case should be handled inside a component
  85. if (response && response.status === 409) {
  86. return Promise.reject(response);
  87. } else {
  88. return throwGlobalError(response);
  89. }
  90. }
  91. );
  92. }
  93. export function deleteRule(parameters: { key: string }) {
  94. return post('/api/rules/delete', parameters).catch(throwGlobalError);
  95. }
  96. export function updateRule(data: {
  97. key: string;
  98. markdown_description?: string;
  99. markdown_note?: string;
  100. name?: string;
  101. params?: string;
  102. remediation_fn_base_effort?: string;
  103. remediation_fn_type?: string;
  104. remediation_fy_gap_multiplier?: string;
  105. severity?: string;
  106. status?: string;
  107. tags?: string;
  108. }): Promise<T.RuleDetails> {
  109. return postJSON('/api/rules/update', data).then(r => r.rule, throwGlobalError);
  110. }