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.

quality-profiles.ts 9.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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 { map } from 'lodash';
  21. import throwGlobalError from '../app/utils/throwGlobalError';
  22. import { Exporter, ProfileChangelogEvent } from '../apps/quality-profiles/types';
  23. import { csvEscape } from '../helpers/csv';
  24. import { getJSON, post, postJSON, RequestData } from '../helpers/request';
  25. export interface ProfileActions {
  26. associateProjects?: boolean;
  27. copy?: boolean;
  28. delete?: boolean;
  29. edit?: boolean;
  30. setAsDefault?: boolean;
  31. }
  32. export interface Actions {
  33. create?: boolean;
  34. }
  35. export interface Profile {
  36. actions?: ProfileActions;
  37. key: string;
  38. name: string;
  39. language: string;
  40. languageName: string;
  41. isInherited?: boolean;
  42. parentKey?: string;
  43. parentName?: string;
  44. isDefault?: boolean;
  45. activeRuleCount: number;
  46. activeDeprecatedRuleCount: number;
  47. rulesUpdatedAt?: string;
  48. lastUsed?: string;
  49. userUpdatedAt?: string;
  50. isBuiltIn?: boolean;
  51. projectCount?: number;
  52. }
  53. export interface SearchQualityProfilesParameters {
  54. defaults?: boolean;
  55. language?: string;
  56. project?: string;
  57. qualityProfile?: string;
  58. }
  59. export interface SearchQualityProfilesResponse {
  60. actions?: Actions;
  61. profiles: Profile[];
  62. }
  63. export function searchQualityProfiles(
  64. parameters?: SearchQualityProfilesParameters
  65. ): Promise<SearchQualityProfilesResponse> {
  66. return getJSON('/api/qualityprofiles/search', parameters).catch(throwGlobalError);
  67. }
  68. export function getQualityProfile({
  69. compareToSonarWay,
  70. profile: { key }
  71. }: {
  72. compareToSonarWay?: boolean;
  73. profile: Profile;
  74. }): Promise<any> {
  75. return getJSON('/api/qualityprofiles/show', { compareToSonarWay, key });
  76. }
  77. export function createQualityProfile(data: RequestData): Promise<any> {
  78. return postJSON('/api/qualityprofiles/create', data).catch(throwGlobalError);
  79. }
  80. export function restoreQualityProfile(data: RequestData): Promise<any> {
  81. return postJSON('/api/qualityprofiles/restore', data).catch(throwGlobalError);
  82. }
  83. export interface ProfileProject {
  84. key: string;
  85. name: string;
  86. selected: boolean;
  87. }
  88. export function getProfileProjects(
  89. data: RequestData
  90. ): Promise<{ more: boolean; paging: T.Paging; results: ProfileProject[] }> {
  91. return getJSON('/api/qualityprofiles/projects', data).catch(throwGlobalError);
  92. }
  93. export function getProfileInheritance({
  94. language,
  95. name: qualityProfile
  96. }: Profile): Promise<{
  97. ancestors: T.ProfileInheritanceDetails[];
  98. children: T.ProfileInheritanceDetails[];
  99. profile: T.ProfileInheritanceDetails;
  100. }> {
  101. return getJSON('/api/qualityprofiles/inheritance', {
  102. language,
  103. qualityProfile
  104. }).catch(throwGlobalError);
  105. }
  106. export function setDefaultProfile({ language, name: qualityProfile }: Profile) {
  107. return post('/api/qualityprofiles/set_default', {
  108. language,
  109. qualityProfile
  110. });
  111. }
  112. export function renameProfile(key: string, name: string) {
  113. return post('/api/qualityprofiles/rename', { key, name }).catch(throwGlobalError);
  114. }
  115. export function copyProfile(fromKey: string, toName: string): Promise<any> {
  116. return postJSON('/api/qualityprofiles/copy', { fromKey, toName }).catch(throwGlobalError);
  117. }
  118. export function deleteProfile({ language, name: qualityProfile }: Profile) {
  119. return post('/api/qualityprofiles/delete', { language, qualityProfile }).catch(throwGlobalError);
  120. }
  121. export function changeProfileParent(
  122. { language, name: qualityProfile }: Profile,
  123. parentProfile?: Profile
  124. ) {
  125. return post('/api/qualityprofiles/change_parent', {
  126. language,
  127. qualityProfile,
  128. parentQualityProfile: parentProfile ? parentProfile.name : undefined
  129. }).catch(throwGlobalError);
  130. }
  131. export function getQualityProfileBackupUrl({ language, name: qualityProfile }: Profile) {
  132. const queryParams = Object.entries({ language, qualityProfile })
  133. .map(([key, value]) => `${key}=${encodeURIComponent(value)}`)
  134. .join('&');
  135. return `/api/qualityprofiles/backup?${queryParams}`;
  136. }
  137. export function getQualityProfileExporterUrl(
  138. { key: exporterKey }: Exporter,
  139. { language, name: qualityProfile }: Profile
  140. ) {
  141. const queryParams = Object.entries({ exporterKey, language, qualityProfile })
  142. .map(([key, value]) => `${key}=${encodeURIComponent(value)}`)
  143. .join('&');
  144. return `/api/qualityprofiles/export?${queryParams}`;
  145. }
  146. export function getImporters(): Promise<
  147. Array<{ key: string; languages: Array<string>; name: string }>
  148. > {
  149. return getJSON('/api/qualityprofiles/importers').then(r => r.importers, throwGlobalError);
  150. }
  151. export function getExporters(): Promise<any> {
  152. return getJSON('/api/qualityprofiles/exporters').then(r => r.exporters);
  153. }
  154. export function getProfileChangelog(
  155. since: any,
  156. to: any,
  157. { language, name: qualityProfile }: Profile,
  158. page?: number
  159. ): Promise<{
  160. events: ProfileChangelogEvent[];
  161. p: number;
  162. ps: number;
  163. total: number;
  164. }> {
  165. return getJSON('/api/qualityprofiles/changelog', {
  166. since,
  167. to,
  168. language,
  169. qualityProfile,
  170. p: page
  171. });
  172. }
  173. export interface CompareResponse {
  174. left: { name: string };
  175. right: { name: string };
  176. inLeft: Array<{ key: string; name: string; severity: string }>;
  177. inRight: Array<{ key: string; name: string; severity: string }>;
  178. modified: Array<{
  179. key: string;
  180. name: string;
  181. left: { params: T.Dict<string>; severity: string };
  182. right: { params: T.Dict<string>; severity: string };
  183. }>;
  184. }
  185. export function compareProfiles(leftKey: string, rightKey: string): Promise<CompareResponse> {
  186. return getJSON('/api/qualityprofiles/compare', { leftKey, rightKey });
  187. }
  188. export function associateProject({ language, name: qualityProfile }: Profile, project: string) {
  189. return post('/api/qualityprofiles/add_project', {
  190. language,
  191. qualityProfile,
  192. project
  193. }).catch(throwGlobalError);
  194. }
  195. export function dissociateProject({ language, name: qualityProfile }: Profile, project: string) {
  196. return post('/api/qualityprofiles/remove_project', {
  197. language,
  198. qualityProfile,
  199. project
  200. }).catch(throwGlobalError);
  201. }
  202. export interface SearchUsersGroupsParameters {
  203. language: string;
  204. qualityProfile: string;
  205. q?: string;
  206. selected?: 'all' | 'selected' | 'deselected';
  207. }
  208. interface SearchUsersResponse {
  209. users: T.UserSelected[];
  210. paging: T.Paging;
  211. }
  212. export function searchUsers(parameters: SearchUsersGroupsParameters): Promise<SearchUsersResponse> {
  213. return getJSON('/api/qualityprofiles/search_users', parameters).catch(throwGlobalError);
  214. }
  215. export interface SearchGroupsResponse {
  216. groups: Array<{ name: string }>;
  217. paging: T.Paging;
  218. }
  219. export function searchGroups(
  220. parameters: SearchUsersGroupsParameters
  221. ): Promise<SearchGroupsResponse> {
  222. return getJSON('/api/qualityprofiles/search_groups', parameters).catch(throwGlobalError);
  223. }
  224. export interface AddRemoveUserParameters {
  225. language: string;
  226. login: string;
  227. qualityProfile: string;
  228. }
  229. export function addUser(parameters: AddRemoveUserParameters): Promise<void | Response> {
  230. return post('/api/qualityprofiles/add_user', parameters).catch(throwGlobalError);
  231. }
  232. export function removeUser(parameters: AddRemoveUserParameters): Promise<void | Response> {
  233. return post('/api/qualityprofiles/remove_user', parameters).catch(throwGlobalError);
  234. }
  235. export interface AddRemoveGroupParameters {
  236. group: string;
  237. language: string;
  238. qualityProfile: string;
  239. }
  240. export function addGroup(parameters: AddRemoveGroupParameters): Promise<void | Response> {
  241. return post('/api/qualityprofiles/add_group', parameters).catch(throwGlobalError);
  242. }
  243. export function removeGroup(parameters: AddRemoveGroupParameters): Promise<void | Response> {
  244. return post('/api/qualityprofiles/remove_group', parameters).catch(throwGlobalError);
  245. }
  246. export interface BulkActivateParameters {
  247. activation?: boolean;
  248. active_severities?: string;
  249. asc?: boolean;
  250. available_since?: string;
  251. compareToProfile?: string;
  252. inheritance?: string;
  253. is_template?: string;
  254. languages?: string;
  255. q?: string;
  256. qprofile?: string;
  257. repositories?: string;
  258. rule_key?: string;
  259. s?: string;
  260. severities?: string;
  261. statuses?: string;
  262. tags?: string;
  263. targetKey: string;
  264. targetSeverity?: string;
  265. template_key?: string;
  266. types?: string;
  267. }
  268. export function bulkActivateRules(data: BulkActivateParameters) {
  269. return postJSON('/api/qualityprofiles/activate_rules', data);
  270. }
  271. export function bulkDeactivateRules(data: BulkActivateParameters) {
  272. return postJSON('/api/qualityprofiles/deactivate_rules', data);
  273. }
  274. export function activateRule(data: {
  275. key: string;
  276. params?: T.Dict<string>;
  277. reset?: boolean;
  278. rule: string;
  279. severity?: string;
  280. }) {
  281. const params =
  282. data.params && map(data.params, (value, key) => `${key}=${csvEscape(value)}`).join(';');
  283. return post('/api/qualityprofiles/activate_rule', { ...data, params }).catch(throwGlobalError);
  284. }
  285. export function deactivateRule(data: { key: string; rule: string }) {
  286. return post('/api/qualityprofiles/deactivate_rule', data).catch(throwGlobalError);
  287. }