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.

components.ts 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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 { getJSON, post, postJSON, RequestData } from '../helpers/request';
  22. import { BranchParameters } from '../types/branch-like';
  23. import { ComponentQualifier, TreeComponent, TreeComponentWithPath } from '../types/component';
  24. export interface BaseSearchProjectsParameters {
  25. analyzedBefore?: string;
  26. onProvisionedOnly?: boolean;
  27. projects?: string;
  28. q?: string;
  29. qualifiers?: string;
  30. visibility?: T.Visibility;
  31. }
  32. export interface ProjectBase {
  33. key: string;
  34. name: string;
  35. qualifier: string;
  36. visibility: T.Visibility;
  37. }
  38. export interface Project extends ProjectBase {
  39. id: string;
  40. lastAnalysisDate?: string;
  41. }
  42. export interface SearchProjectsParameters extends BaseSearchProjectsParameters {
  43. p?: number;
  44. ps?: number;
  45. }
  46. export function getComponents(
  47. parameters: SearchProjectsParameters
  48. ): Promise<{
  49. components: Project[];
  50. paging: T.Paging;
  51. }> {
  52. return getJSON('/api/projects/search', parameters);
  53. }
  54. export function bulkDeleteProjects(
  55. parameters: BaseSearchProjectsParameters
  56. ): Promise<void | Response> {
  57. return post('/api/projects/bulk_delete', parameters).catch(throwGlobalError);
  58. }
  59. export function deleteProject(project: string): Promise<void | Response> {
  60. return post('/api/projects/delete', { project }).catch(throwGlobalError);
  61. }
  62. export function deletePortfolio(portfolio: string): Promise<void | Response> {
  63. return post('/api/views/delete', { key: portfolio }).catch(throwGlobalError);
  64. }
  65. export function createProject(data: {
  66. name: string;
  67. project: string;
  68. visibility?: T.Visibility;
  69. }): Promise<{ project: ProjectBase }> {
  70. return postJSON('/api/projects/create', data).catch(throwGlobalError);
  71. }
  72. export function searchProjectTags(data?: { ps?: number; q?: string }): Promise<any> {
  73. return getJSON('/api/project_tags/search', data).catch(throwGlobalError);
  74. }
  75. export function setApplicationTags(data: { application: string; tags: string }): Promise<void> {
  76. return post('/api/applications/set_tags', data);
  77. }
  78. export function setProjectTags(data: { project: string; tags: string }): Promise<void> {
  79. return post('/api/project_tags/set', data);
  80. }
  81. export function getComponentTree(
  82. strategy: string,
  83. component: string,
  84. metrics: string[] = [],
  85. additional: RequestData = {}
  86. ): Promise<{
  87. baseComponent: T.ComponentMeasure;
  88. components: T.ComponentMeasure[];
  89. metrics: T.Metric[];
  90. paging: T.Paging;
  91. }> {
  92. const url = '/api/measures/component_tree';
  93. const data = { ...additional, component, metricKeys: metrics.join(','), strategy };
  94. return getJSON(url, data).catch(throwGlobalError);
  95. }
  96. export function getChildren(
  97. component: string,
  98. metrics: string[] = [],
  99. additional: RequestData = {}
  100. ) {
  101. return getComponentTree('children', component, metrics, additional);
  102. }
  103. export function getComponentLeaves(
  104. component: string,
  105. metrics: string[] = [],
  106. additional: RequestData = {}
  107. ) {
  108. return getComponentTree('leaves', component, metrics, additional);
  109. }
  110. export function getComponent(
  111. data: { component: string; metricKeys: string } & BranchParameters
  112. ): Promise<{ component: T.ComponentMeasure }> {
  113. return getJSON('/api/measures/component', data);
  114. }
  115. type GetTreeParams = {
  116. asc?: boolean;
  117. component: string;
  118. p?: number;
  119. ps?: number;
  120. q?: string;
  121. s?: string;
  122. strategy?: 'all' | 'leaves' | 'children';
  123. } & BranchParameters;
  124. export function getTree<T = TreeComponent>(
  125. data: GetTreeParams & { qualifiers?: string }
  126. ): Promise<{ baseComponent: TreeComponent; components: T[]; paging: T.Paging }> {
  127. return getJSON('/api/components/tree', data).catch(throwGlobalError);
  128. }
  129. export function getFiles(data: GetTreeParams) {
  130. return getTree<TreeComponentWithPath>({ ...data, qualifiers: 'FIL' });
  131. }
  132. export function getDirectories(data: GetTreeParams) {
  133. return getTree<TreeComponentWithPath>({ ...data, qualifiers: 'DIR' });
  134. }
  135. export function getComponentData(data: { component: string } & BranchParameters): Promise<any> {
  136. return getJSON('/api/components/show', data);
  137. }
  138. export function doesComponentExists(
  139. data: { component: string } & BranchParameters
  140. ): Promise<boolean> {
  141. return getComponentData(data).then(
  142. ({ component }) => component !== undefined,
  143. () => false
  144. );
  145. }
  146. export function getComponentShow(data: { component: string } & BranchParameters): Promise<any> {
  147. return getComponentData(data).catch(throwGlobalError);
  148. }
  149. export function getParents(component: string): Promise<any> {
  150. return getComponentShow({ component }).then(r => r.ancestors);
  151. }
  152. export function getBreadcrumbs(data: { component: string } & BranchParameters): Promise<any> {
  153. return getComponentShow(data).then(r => {
  154. const reversedAncestors = [...r.ancestors].reverse();
  155. return [...reversedAncestors, r.component];
  156. });
  157. }
  158. export function getMyProjects(data: {
  159. p?: number;
  160. ps?: number;
  161. }): Promise<{ paging: T.Paging; projects: T.MyProject[] }> {
  162. return getJSON('/api/projects/search_my_projects', data);
  163. }
  164. export interface Component {
  165. id: string;
  166. key: string;
  167. name: string;
  168. isFavorite?: boolean;
  169. analysisDate?: string;
  170. qualifier: ComponentQualifier;
  171. tags: string[];
  172. visibility: T.Visibility;
  173. leakPeriodDate?: string;
  174. needIssueSync?: boolean;
  175. }
  176. export interface Facet {
  177. property: string;
  178. values: Array<{ val: string; count: number }>;
  179. }
  180. export function searchProjects(
  181. data: RequestData
  182. ): Promise<{
  183. components: Component[];
  184. facets: Facet[];
  185. paging: T.Paging;
  186. }> {
  187. const url = '/api/components/search_projects';
  188. return getJSON(url, data);
  189. }
  190. export function searchComponents(data?: {
  191. q?: string;
  192. qualifiers?: string;
  193. ps?: number;
  194. }): Promise<any> {
  195. return getJSON('/api/components/search', data);
  196. }
  197. export function changeKey(data: { from: string; to: string }) {
  198. return post('/api/projects/update_key', data).catch(throwGlobalError);
  199. }
  200. export interface SuggestionsResponse {
  201. projects: Array<{ key: string; name: string }>;
  202. results: Array<{
  203. items: Array<{
  204. isFavorite: boolean;
  205. isRecentlyBrowsed: boolean;
  206. key: string;
  207. match: string;
  208. name: string;
  209. project: string;
  210. }>;
  211. more: number;
  212. q: string;
  213. }>;
  214. warning?: string;
  215. }
  216. export function getSuggestions(
  217. query?: string,
  218. recentlyBrowsed?: string[],
  219. more?: string
  220. ): Promise<SuggestionsResponse> {
  221. const data: RequestData = {};
  222. if (query) {
  223. data.s = query;
  224. }
  225. if (recentlyBrowsed) {
  226. data.recentlyBrowsed = recentlyBrowsed.join();
  227. }
  228. if (more) {
  229. data.more = more;
  230. }
  231. return getJSON('/api/components/suggestions', data).catch(throwGlobalError);
  232. }
  233. export function getComponentForSourceViewer(
  234. data: { component: string } & BranchParameters
  235. ): Promise<T.SourceViewerFile> {
  236. return getJSON('/api/components/app', data);
  237. }
  238. export function getSources(
  239. data: { key: string; from?: number; to?: number } & BranchParameters
  240. ): Promise<T.SourceLine[]> {
  241. return getJSON('/api/sources/lines', data).then(r => r.sources);
  242. }
  243. export function getDuplications(
  244. data: { key: string } & BranchParameters
  245. ): Promise<{ duplications: T.Duplication[]; files: T.Dict<T.DuplicatedFile> }> {
  246. return getJSON('/api/duplications/show', data).catch(throwGlobalError);
  247. }
  248. export function getTests(
  249. data: { sourceFileKey: string; sourceFileLineNumber: number | string } & BranchParameters
  250. ): Promise<any> {
  251. return getJSON('/api/tests/list', data).then(r => r.tests);
  252. }