From: Wouter Admiraal Date: Thu, 30 Jan 2020 13:21:59 +0000 (+0100) Subject: SONAR-13001 Add new API helper functions X-Git-Tag: 8.2.0.32929~71 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=33df74d6d5d5266b6ee9347a94b010aa6f9129e4;p=sonarqube.git SONAR-13001 Add new API helper functions --- diff --git a/server/sonar-web/src/main/js/api/alm-integrations.ts b/server/sonar-web/src/main/js/api/alm-integrations.ts new file mode 100644 index 00000000000..9e8430aa05f --- /dev/null +++ b/server/sonar-web/src/main/js/api/alm-integrations.ts @@ -0,0 +1,70 @@ +/* + * SonarQube + * Copyright (C) 2009-2020 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 { get, getJSON, post, postJSON } from 'sonar-ui-common/helpers/request'; +import throwGlobalError from '../app/utils/throwGlobalError'; +import { BitbucketProject, BitbucketRepository } from '../types/alm-integration'; +import { ProjectBase } from './components'; + +export function setAlmPersonalAccessToken(almSetting: string, pat: string): Promise { + return post('/api/alm_integrations/set_pat', { almSetting, pat }).catch(throwGlobalError); +} + +export function checkPersonalAccessTokenIsValid(almSetting: string): Promise { + return get('/api/alm_integrations/check_pat', { almSetting }) + .then(() => true) + .catch(response => { + if (response.status === 400) { + return false; + } else { + return throwGlobalError(response); + } + }); +} + +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); +} diff --git a/server/sonar-web/src/main/js/helpers/mocks/alm-integrations.ts b/server/sonar-web/src/main/js/helpers/mocks/alm-integrations.ts new file mode 100644 index 00000000000..6e04e4807cd --- /dev/null +++ b/server/sonar-web/src/main/js/helpers/mocks/alm-integrations.ts @@ -0,0 +1,41 @@ +/* + * SonarQube + * Copyright (C) 2009-2020 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 { BitbucketProject, BitbucketRepository } from '../../types/alm-integration'; + +export function mockBitbucketProject(overrides: Partial = {}): BitbucketProject { + return { + id: 1, + key: 'project', + name: 'Project', + ...overrides + }; +} + +export function mockBitbucketRepository( + overrides: Partial = {} +): BitbucketRepository { + return { + id: 1, + slug: 'project__repo', + name: 'Repo', + projectKey: 'project', + ...overrides + }; +} diff --git a/server/sonar-web/src/main/js/types/alm-integration.ts b/server/sonar-web/src/main/js/types/alm-integration.ts new file mode 100644 index 00000000000..e746ef153b1 --- /dev/null +++ b/server/sonar-web/src/main/js/types/alm-integration.ts @@ -0,0 +1,37 @@ +/* + * SonarQube + * Copyright (C) 2009-2020 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. + */ +export interface BitbucketProject { + id: number; + key: string; + name: string; +} + +export interface BitbucketRepository { + id: number; + name: string; + projectKey: string; + sqProjectKey?: string; + slug: string; +} + +export type BitbucketProjectRepositories = T.Dict<{ + allShown: boolean; + repositories: BitbucketRepository[]; +}>;