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.

alm-integrations.ts 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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 { get, getJSON, parseError, post, postJSON } from '../helpers/request';
  22. import {
  23. AzureProject,
  24. AzureRepository,
  25. BitbucketCloudRepository,
  26. BitbucketProject,
  27. BitbucketRepository,
  28. GithubOrganization,
  29. GithubRepository,
  30. GitlabProject
  31. } from '../types/alm-integration';
  32. import { ProjectBase } from './components';
  33. export function setAlmPersonalAccessToken(
  34. almSetting: string,
  35. pat: string,
  36. username?: string
  37. ): Promise<void> {
  38. return post('/api/alm_integrations/set_pat', { almSetting, pat, username }).catch(
  39. throwGlobalError
  40. );
  41. }
  42. export function checkPersonalAccessTokenIsValid(
  43. almSetting: string
  44. ): Promise<{ status: boolean; error?: string }> {
  45. return get('/api/alm_integrations/check_pat', { almSetting })
  46. .then(() => ({ status: true }))
  47. .catch(async (response: Response) => {
  48. if (response.status === 400) {
  49. const error = await parseError(response);
  50. return { status: false, error };
  51. }
  52. return throwGlobalError(response);
  53. });
  54. }
  55. export function getAzureProjects(almSetting: string): Promise<{ projects: AzureProject[] }> {
  56. return getJSON('/api/alm_integrations/list_azure_projects', { almSetting }).catch(
  57. throwGlobalError
  58. );
  59. }
  60. export function getAzureRepositories(
  61. almSetting: string,
  62. projectName: string
  63. ): Promise<{ repositories: AzureRepository[] }> {
  64. return getJSON('/api/alm_integrations/search_azure_repos', { almSetting, projectName }).catch(
  65. throwGlobalError
  66. );
  67. }
  68. export function searchAzureRepositories(
  69. almSetting: string,
  70. searchQuery: string
  71. ): Promise<{ repositories: AzureRepository[] }> {
  72. return getJSON('/api/alm_integrations/search_azure_repos', { almSetting, searchQuery }).catch(
  73. throwGlobalError
  74. );
  75. }
  76. export function importAzureRepository(
  77. almSetting: string,
  78. projectName: string,
  79. repositoryName: string
  80. ): Promise<{ project: ProjectBase }> {
  81. return postJSON('/api/alm_integrations/import_azure_project', {
  82. almSetting,
  83. projectName,
  84. repositoryName
  85. }).catch(throwGlobalError);
  86. }
  87. export function getBitbucketServerProjects(
  88. almSetting: string
  89. ): Promise<{ projects: BitbucketProject[] }> {
  90. return getJSON('/api/alm_integrations/list_bitbucketserver_projects', { almSetting });
  91. }
  92. export function getBitbucketServerRepositories(
  93. almSetting: string,
  94. projectName: string
  95. ): Promise<{
  96. isLastPage: boolean;
  97. repositories: BitbucketRepository[];
  98. }> {
  99. return getJSON('/api/alm_integrations/search_bitbucketserver_repos', {
  100. almSetting,
  101. projectName
  102. });
  103. }
  104. export function importBitbucketServerProject(
  105. almSetting: string,
  106. projectKey: string,
  107. repositorySlug: string
  108. ): Promise<{ project: ProjectBase }> {
  109. return postJSON('/api/alm_integrations/import_bitbucketserver_project', {
  110. almSetting,
  111. projectKey,
  112. repositorySlug
  113. }).catch(throwGlobalError);
  114. }
  115. export function searchForBitbucketServerRepositories(
  116. almSetting: string,
  117. repositoryName: string
  118. ): Promise<{
  119. isLastPage: boolean;
  120. repositories: BitbucketRepository[];
  121. }> {
  122. return getJSON('/api/alm_integrations/search_bitbucketserver_repos', {
  123. almSetting,
  124. repositoryName
  125. });
  126. }
  127. export function searchForBitbucketCloudRepositories(
  128. almSetting: string,
  129. repositoryName: string,
  130. pageSize: number,
  131. page?: number
  132. ): Promise<{
  133. isLastPage: boolean;
  134. repositories: BitbucketCloudRepository[];
  135. }> {
  136. return getJSON('/api/alm_integrations/search_bitbucketcloud_repos', {
  137. almSetting,
  138. repositoryName,
  139. p: page,
  140. ps: pageSize
  141. });
  142. }
  143. export function getGithubClientId(almSetting: string): Promise<{ clientId?: string }> {
  144. return getJSON('/api/alm_integrations/get_github_client_id', { almSetting });
  145. }
  146. export function importBitbucketCloudRepository(
  147. almSetting: string,
  148. repositorySlug: string
  149. ): Promise<{ project: ProjectBase }> {
  150. return postJSON('/api/alm_integrations/import_bitbucketcloud_repo', {
  151. almSetting,
  152. repositorySlug
  153. }).catch(throwGlobalError);
  154. }
  155. export function importGithubRepository(
  156. almSetting: string,
  157. organization: string,
  158. repositoryKey: string
  159. ): Promise<{ project: ProjectBase }> {
  160. return postJSON('/api/alm_integrations/import_github_project', {
  161. almSetting,
  162. organization,
  163. repositoryKey
  164. }).catch(throwGlobalError);
  165. }
  166. export function getGithubOrganizations(
  167. almSetting: string,
  168. token: string
  169. ): Promise<{ organizations: GithubOrganization[] }> {
  170. return getJSON('/api/alm_integrations/list_github_organizations', {
  171. almSetting,
  172. token
  173. }).catch((response?: Response) => {
  174. if (response && response.status !== 400) {
  175. throwGlobalError(response);
  176. }
  177. });
  178. }
  179. export function getGithubRepositories(data: {
  180. almSetting: string;
  181. organization: string;
  182. pageSize: number;
  183. page?: number;
  184. query?: string;
  185. }): Promise<{ repositories: GithubRepository[]; paging: T.Paging }> {
  186. const { almSetting, organization, pageSize, page = 1, query } = data;
  187. return getJSON('/api/alm_integrations/list_github_repositories', {
  188. almSetting,
  189. organization,
  190. p: page,
  191. ps: pageSize,
  192. q: query || undefined
  193. }).catch(throwGlobalError);
  194. }
  195. export function getGitlabProjects(data: {
  196. almSetting: string;
  197. page?: number;
  198. pageSize?: number;
  199. query?: string;
  200. }): Promise<{ projects: GitlabProject[]; projectsPaging: T.Paging }> {
  201. const { almSetting, pageSize, page, query } = data;
  202. return getJSON('/api/alm_integrations/search_gitlab_repos', {
  203. almSetting,
  204. projectName: query || undefined,
  205. p: page,
  206. ps: pageSize
  207. })
  208. .then(({ repositories, paging }) => ({ projects: repositories, projectsPaging: paging }))
  209. .catch(throwGlobalError);
  210. }
  211. export function importGitlabProject(data: {
  212. almSetting: string;
  213. gitlabProjectId: string;
  214. }): Promise<{ project: ProjectBase }> {
  215. const { almSetting, gitlabProjectId } = data;
  216. return postJSON('/api/alm_integrations/import_gitlab_project', {
  217. almSetting,
  218. gitlabProjectId
  219. }).catch(throwGlobalError);
  220. }