aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-web/src/main/js/api/alm-integrations.ts
blob: 2a023a6b103c291597d744e2cd4945801ab619d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*
 * SonarQube
 * Copyright (C) 2009-2022 SonarSource SA
 * mailto:info AT sonarsource DOT com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
import { throwGlobalError } from '../helpers/error';
import { get, getJSON, parseError, post, postJSON } from '../helpers/request';
import {
  AzureProject,
  AzureRepository,
  BitbucketCloudRepository,
  BitbucketProject,
  BitbucketRepository,
  GithubOrganization,
  GithubRepository,
  GitlabProject,
} from '../types/alm-integration';
import { Paging } from '../types/types';
import { ProjectBase } from './components';

export function setAlmPersonalAccessToken(
  almSetting: string,
  pat: string,
  username?: string
): Promise<void> {
  return post('/api/alm_integrations/set_pat', { almSetting, pat, username }).catch(
    throwGlobalError
  );
}

export function checkPersonalAccessTokenIsValid(
  almSetting: string
): Promise<{ status: boolean; error?: string }> {
  return get('/api/alm_integrations/check_pat', { almSetting })
    .then(() => ({ status: true }))
    .catch(async (response: Response) => {
      if (response.status === 400) {
        const error = await parseError(response);
        return { status: false, error };
      }
      return throwGlobalError(response);
    });
}

export function getAzureProjects(almSetting: string): Promise<{ projects: AzureProject[] }> {
  return getJSON('/api/alm_integrations/list_azure_projects', { almSetting }).catch(
    throwGlobalError
  );
}

export function getAzureRepositories(
  almSetting: string,
  projectName: string
): Promise<{ repositories: AzureRepository[] }> {
  return getJSON('/api/alm_integrations/search_azure_repos', { almSetting, projectName }).catch(
    throwGlobalError
  );
}

export function searchAzureRepositories(
  almSetting: string,
  searchQuery: string
): Promise<{ repositories: AzureRepository[] }> {
  return getJSON('/api/alm_integrations/search_azure_repos', { almSetting, searchQuery }).catch(
    throwGlobalError
  );
}

export function importAzureRepository(
  almSetting: string,
  projectName: string,
  repositoryName: string
): Promise<{ project: ProjectBase }> {
  return postJSON('/api/alm_integrations/import_azure_project', {
    almSetting,
    projectName,
    repositoryName,
  }).catch(throwGlobalError);
}

export function getBitbucketServerProjects(
  almSetting: string
): Promise<{ projects: BitbucketProject[] }> {
  return getJSON('/api/alm_integrations/list_bitbucketserver_projects', { almSetting });
}

export function getBitbucketServerRepositories(
  almSetting: string,
  projectName: string
): Promise<{
  isLastPage: boolean;
  repositories: BitbucketRepository[];
}> {
  return getJSON('/api/alm_integrations/search_bitbucketserver_repos', {
    almSetting,
    projectName,
  });
}

export function importBitbucketServerProject(
  almSetting: string,
  projectKey: string,
  repositorySlug: string
): Promise<{ project: ProjectBase }> {
  return postJSON('/api/alm_integrations/import_bitbucketserver_project', {
    almSetting,
    projectKey,
    repositorySlug,
  }).catch(throwGlobalError);
}

export function searchForBitbucketServerRepositories(
  almSetting: string,
  repositoryName: string
): Promise<{
  isLastPage: boolean;
  repositories: BitbucketRepository[];
}> {
  return getJSON('/api/alm_integrations/search_bitbucketserver_repos', {
    almSetting,
    repositoryName,
  });
}

export function searchForBitbucketCloudRepositories(
  almSetting: string,
  repositoryName: string,
  pageSize: number,
  page?: number
): Promise<{
  isLastPage: boolean;
  repositories: BitbucketCloudRepository[];
}> {
  return getJSON('/api/alm_integrations/search_bitbucketcloud_repos', {
    almSetting,
    repositoryName,
    p: page,
    ps: pageSize,
  });
}

export function getGithubClientId(almSetting: string): Promise<{ clientId?: string }> {
  return getJSON('/api/alm_integrations/get_github_client_id', { almSetting });
}

export function importBitbucketCloudRepository(
  almSetting: string,
  repositorySlug: string
): Promise<{ project: ProjectBase }> {
  return postJSON('/api/alm_integrations/import_bitbucketcloud_repo', {
    almSetting,
    repositorySlug,
  }).catch(throwGlobalError);
}

export function importGithubRepository(
  almSetting: string,
  organization: string,
  repositoryKey: string
): Promise<{ project: ProjectBase }> {
  return postJSON('/api/alm_integrations/import_github_project', {
    almSetting,
    organization,
    repositoryKey,
  }).catch(throwGlobalError);
}

export function getGithubOrganizations(
  almSetting: string,
  token: string
): Promise<{ organizations: GithubOrganization[] }> {
  return getJSON('/api/alm_integrations/list_github_organizations', {
    almSetting,
    token,
  }).catch((response?: Response) => {
    if (response && response.status !== 400) {
      throwGlobalError(response);
    }
  });
}

export function getGithubRepositories(data: {
  almSetting: string;
  organization: string;
  pageSize: number;
  page?: number;
  query?: string;
}): Promise<{ repositories: GithubRepository[]; paging: Paging }> {
  const { almSetting, organization, pageSize, page = 1, query } = data;
  return getJSON('/api/alm_integrations/list_github_repositories', {
    almSetting,
    organization,
    p: page,
    ps: pageSize,
    q: query || undefined,
  }).catch(throwGlobalError);
}

export function getGitlabProjects(data: {
  almSetting: string;
  page?: number;
  pageSize?: number;
  query?: string;
}): Promise<{ projects: GitlabProject[]; projectsPaging: Paging }> {
  const { almSetting, pageSize, page, query } = data;
  return getJSON('/api/alm_integrations/search_gitlab_repos', {
    almSetting,
    projectName: query || undefined,
    p: page,
    ps: pageSize,
  })
    .then(({ repositories, paging }) => ({ projects: repositories, projectsPaging: paging }))
    .catch(throwGlobalError);
}

export function importGitlabProject(data: {
  almSetting: string;
  gitlabProjectId: string;
}): Promise<{ project: ProjectBase }> {
  const { almSetting, gitlabProjectId } = data;
  return postJSON('/api/alm_integrations/import_gitlab_project', {
    almSetting,
    gitlabProjectId,
  }).catch(throwGlobalError);
}