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.

devops-integration.ts 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 { UseQueryOptions, useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
  21. import { useLocation } from 'react-router-dom';
  22. import {
  23. deleteProjectAlmBinding,
  24. getProjectAlmBinding,
  25. setProjectAzureBinding,
  26. setProjectBitbucketBinding,
  27. setProjectBitbucketCloudBinding,
  28. setProjectGithubBinding,
  29. setProjectGitlabBinding,
  30. } from '../api/alm-settings';
  31. import { HttpStatus } from '../helpers/request';
  32. import { AlmKeys, ProjectAlmBindingParams, ProjectAlmBindingResponse } from '../types/alm-settings';
  33. function useProjectKeyFromLocation() {
  34. const location = useLocation();
  35. // Permissions Templates page uses id in query for template id, so to avoid conflict and unwanted fetching we don't search for project there
  36. if (location.pathname.includes('permission_templates')) {
  37. return null;
  38. }
  39. const search = new URLSearchParams(location.search);
  40. const id = search.get('id');
  41. return id as string;
  42. }
  43. export function useProjectBindingQuery<T = ProjectAlmBindingResponse>(
  44. project?: string,
  45. options?: Omit<
  46. UseQueryOptions<
  47. ProjectAlmBindingResponse | null,
  48. Error,
  49. T,
  50. ['devops_integration', string, 'binding']
  51. >,
  52. 'queryKey' | 'queryFn'
  53. >,
  54. ) {
  55. const keyFromUrl = useProjectKeyFromLocation();
  56. const projectKey = project ?? keyFromUrl;
  57. return useQuery({
  58. queryKey: ['devops_integration', projectKey ?? '_blank_', 'binding'],
  59. queryFn: ({ queryKey: [_, key] }) =>
  60. getProjectAlmBinding(key).catch((e: Response) => {
  61. if (e.status === HttpStatus.NotFound) {
  62. return null;
  63. }
  64. return e;
  65. }),
  66. staleTime: 60_000,
  67. enabled: projectKey !== null,
  68. ...options,
  69. });
  70. }
  71. export function useIsGitHubProjectQuery(project?: string) {
  72. return useProjectBindingQuery<boolean>(project, {
  73. select: (data) => data?.alm === AlmKeys.GitHub,
  74. });
  75. }
  76. export function useDeleteProjectAlmBindingMutation(project?: string) {
  77. const keyFromUrl = useProjectKeyFromLocation();
  78. const client = useQueryClient();
  79. return useMutation({
  80. mutationFn: () => {
  81. const key = project ?? keyFromUrl;
  82. if (key === null) {
  83. throw new Error('No project key');
  84. }
  85. return deleteProjectAlmBinding(key);
  86. },
  87. onSuccess: () => {
  88. client.invalidateQueries({
  89. queryKey: ['devops_integration', project ?? keyFromUrl, 'binding'],
  90. });
  91. },
  92. });
  93. }
  94. const getSetProjectBindingFn = (data: SetBindingParams) => {
  95. const { alm, almSetting, project, monorepo, slug, repository, summaryCommentEnabled } = data;
  96. switch (alm) {
  97. case AlmKeys.Azure: {
  98. return setProjectAzureBinding({
  99. almSetting,
  100. project,
  101. projectName: slug,
  102. repositoryName: repository,
  103. monorepo,
  104. });
  105. }
  106. case AlmKeys.BitbucketServer: {
  107. return setProjectBitbucketBinding({
  108. almSetting,
  109. project,
  110. repository,
  111. slug,
  112. monorepo,
  113. });
  114. }
  115. case AlmKeys.BitbucketCloud: {
  116. return setProjectBitbucketCloudBinding({
  117. almSetting,
  118. project,
  119. repository,
  120. monorepo,
  121. });
  122. }
  123. case AlmKeys.GitHub: {
  124. return setProjectGithubBinding({
  125. almSetting,
  126. project,
  127. repository,
  128. summaryCommentEnabled,
  129. monorepo,
  130. });
  131. }
  132. case AlmKeys.GitLab: {
  133. return setProjectGitlabBinding({
  134. almSetting,
  135. project,
  136. repository,
  137. monorepo,
  138. });
  139. }
  140. default:
  141. return Promise.reject();
  142. }
  143. };
  144. type SetBindingParams = ProjectAlmBindingParams & {
  145. repository: string;
  146. } & (
  147. | { alm: AlmKeys.Azure | AlmKeys.BitbucketServer; slug: string; summaryCommentEnabled?: never }
  148. | { alm: AlmKeys.GitHub; summaryCommentEnabled: boolean; slug?: never }
  149. | {
  150. alm: Exclude<AlmKeys, AlmKeys.Azure | AlmKeys.GitHub | AlmKeys.BitbucketServer>;
  151. slug?: never;
  152. summaryCommentEnabled?: never;
  153. }
  154. );
  155. export function useSetProjectBindingMutation() {
  156. const client = useQueryClient();
  157. return useMutation({
  158. mutationFn: (data: SetBindingParams) => getSetProjectBindingFn(data),
  159. onSuccess: (_, variables) => {
  160. client.invalidateQueries({ queryKey: ['devops_integration', variables.project, 'binding'] });
  161. },
  162. });
  163. }