/* * SonarQube * Copyright (C) 2009-2018 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 { request, checkStatus, parseJSON, getJSON, post, postJSON, RequestData } from '../helpers/request'; import { Paging } from '../app/types'; import throwGlobalError from '../app/utils/throwGlobalError'; export interface ProfileActions { copy?: boolean; edit?: boolean; setAsDefault?: boolean; } export interface Actions { create?: boolean; } export interface Profile { actions?: ProfileActions; key: string; name: string; language: string; languageName: string; isInherited?: boolean; parentKey?: string; parentName?: string; isDefault?: boolean; activeRuleCount: number; activeDeprecatedRuleCount: number; rulesUpdatedAt?: string; lastUsed?: string; userUpdatedAt?: string; organization: string; isBuiltIn?: boolean; projectCount?: number; } export interface SearchQualityProfilesParameters { defaults?: boolean; language?: string; organization?: string; project?: string; qualityProfile?: string; } export interface SearchQualityProfilesResponse { actions?: Actions; profiles: Profile[]; } export function searchQualityProfiles( parameters: SearchQualityProfilesParameters ): Promise { return getJSON('/api/qualityprofiles/search', parameters); } export function getQualityProfile(data: { compareToSonarWay?: boolean; profile: string; }): Promise { return getJSON('/api/qualityprofiles/show', data); } export function createQualityProfile(data: RequestData): Promise { return request('/api/qualityprofiles/create') .setMethod('post') .setData(data) .submit() .then(checkStatus) .then(parseJSON); } export function restoreQualityProfile(data: RequestData): Promise { return request('/api/qualityprofiles/restore') .setMethod('post') .setData(data) .submit() .then(checkStatus) .then(parseJSON); } export function getProfileProjects(data: RequestData): Promise { return getJSON('/api/qualityprofiles/projects', data); } export function getProfileInheritance(profileKey: string): Promise { return getJSON('/api/qualityprofiles/inheritance', { profileKey }); } export function setDefaultProfile(profileKey: string): Promise { return post('/api/qualityprofiles/set_default', { profileKey }); } export function renameProfile(key: string, name: string): Promise { return post('/api/qualityprofiles/rename', { key, name }); } export function copyProfile(fromKey: string, toName: string): Promise { return postJSON('/api/qualityprofiles/copy', { fromKey, toName }); } export function deleteProfile(profileKey: string): Promise { return post('/api/qualityprofiles/delete', { profileKey }); } export function changeProfileParent(profileKey: string, parentKey: string): Promise { return post('/api/qualityprofiles/change_parent', { profileKey, parentKey }); } export function getImporters(): Promise { return getJSON('/api/qualityprofiles/importers').then(r => r.importers); } export function getExporters(): Promise { return getJSON('/api/qualityprofiles/exporters').then(r => r.exporters); } export function getProfileChangelog(data: RequestData): Promise { return getJSON('/api/qualityprofiles/changelog', data); } export function compareProfiles(leftKey: string, rightKey: string): Promise { return getJSON('/api/qualityprofiles/compare', { leftKey, rightKey }); } export function associateProject(profileKey: string, projectKey: string): Promise { return post('/api/qualityprofiles/add_project', { profileKey, projectKey }); } export function dissociateProject(profileKey: string, projectKey: string): Promise { return post('/api/qualityprofiles/remove_project', { profileKey, projectKey }); } export interface SearchUsersGroupsParameters { language: string; organization?: string; qualityProfile: string; q?: string; selected?: 'all' | 'selected' | 'deselected'; } export interface SearchUsersResponse { users: Array<{ avatar?: string; login: string; name: string; selected?: boolean; }>; paging: Paging; } export function searchUsers(parameters: SearchUsersGroupsParameters): Promise { return getJSON('/api/qualityprofiles/search_users', parameters).catch(throwGlobalError); } export interface SearchGroupsResponse { groups: Array<{ name: string }>; paging: Paging; } export function searchGroups( parameters: SearchUsersGroupsParameters ): Promise { return getJSON('/api/qualityprofiles/search_groups', parameters).catch(throwGlobalError); } export interface AddRemoveUserParameters { language: string; login: string; organization?: string; qualityProfile: string; } export function addUser(parameters: AddRemoveUserParameters): Promise { return post('/api/qualityprofiles/add_user', parameters).catch(throwGlobalError); } export function removeUser(parameters: AddRemoveUserParameters): Promise { return post('/api/qualityprofiles/remove_user', parameters).catch(throwGlobalError); } export interface AddRemoveGroupParameters { group: string; language: string; organization?: string; qualityProfile: string; } export function addGroup(parameters: AddRemoveGroupParameters): Promise { return post('/api/qualityprofiles/add_group', parameters).catch(throwGlobalError); } export function removeGroup(parameters: AddRemoveGroupParameters): Promise { return post('/api/qualityprofiles/remove_group', parameters).catch(throwGlobalError); }