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.

quality-profiles.ts 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 { UseQueryResult, useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
  21. import {
  22. ActivateRuleParameters,
  23. AddRemoveGroupParameters,
  24. AddRemoveUserParameters,
  25. DeactivateRuleParameters,
  26. Profile,
  27. activateRule,
  28. addGroup,
  29. addUser,
  30. compareProfiles,
  31. deactivateRule,
  32. getProfileInheritance,
  33. } from '../api/quality-profiles';
  34. import { ProfileInheritanceDetails } from '../types/types';
  35. export function useProfileInheritanceQuery(
  36. profile?: Pick<Profile, 'language' | 'name' | 'parentKey'>,
  37. ): UseQueryResult<{
  38. ancestors: ProfileInheritanceDetails[];
  39. children: ProfileInheritanceDetails[];
  40. profile: ProfileInheritanceDetails | null;
  41. }> {
  42. const { language, name, parentKey } = profile ?? {};
  43. return useQuery({
  44. queryKey: ['quality-profiles', 'inheritance', language, name, parentKey],
  45. queryFn: async ({ queryKey: [, , language, name] }) => {
  46. if (!language || !name) {
  47. return { ancestors: [], children: [], profile: null };
  48. }
  49. const response = await getProfileInheritance({ language, name });
  50. response.ancestors.reverse();
  51. return response;
  52. },
  53. });
  54. }
  55. export function useProfilesCompareQuery(leftKey: string, rightKey: string) {
  56. return useQuery({
  57. queryKey: ['quality-profiles', 'compare', leftKey, rightKey],
  58. queryFn: ({ queryKey: [, , leftKey, rightKey] }) => {
  59. if (!leftKey || !rightKey) {
  60. return null;
  61. }
  62. return compareProfiles(leftKey, rightKey);
  63. },
  64. });
  65. }
  66. export function useActivateRuleMutation(onSuccess: (data: ActivateRuleParameters) => unknown) {
  67. const queryClient = useQueryClient();
  68. return useMutation({
  69. mutationFn: activateRule,
  70. onSuccess: (_, data) => {
  71. queryClient.invalidateQueries({ queryKey: ['rules', 'details'] });
  72. onSuccess(data);
  73. },
  74. });
  75. }
  76. export function useDeactivateRuleMutation(onSuccess: (data: DeactivateRuleParameters) => unknown) {
  77. const queryClient = useQueryClient();
  78. return useMutation({
  79. mutationFn: deactivateRule,
  80. onSuccess: (_, data) => {
  81. queryClient.invalidateQueries({ queryKey: ['rules', 'details'] });
  82. onSuccess(data);
  83. },
  84. });
  85. }
  86. export function useAddUserMutation(onSuccess: () => unknown) {
  87. return useMutation({
  88. mutationFn: (data: AddRemoveUserParameters) => addUser(data),
  89. onSuccess,
  90. });
  91. }
  92. export function useAddGroupMutation(onSuccess: () => unknown) {
  93. return useMutation({
  94. mutationFn: (data: AddRemoveGroupParameters) => addGroup(data),
  95. onSuccess,
  96. });
  97. }