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.

urls.ts 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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 { pick } from 'lodash';
  21. import { getBaseUrl, Location } from 'sonar-ui-common/helpers/urls';
  22. import { getProfilePath } from '../apps/quality-profiles/utils';
  23. import { BranchLike, BranchParameters } from '../types/branch-like';
  24. import { ComponentQualifier, isApplication, isPortfolioLike } from '../types/component';
  25. import { GraphType } from '../types/project-activity';
  26. import { SecurityStandard } from '../types/security';
  27. import { getBranchLikeQuery, isBranch, isMainBranch, isPullRequest } from './branch-like';
  28. type Query = Location['query'];
  29. export function getComponentOverviewUrl(
  30. componentKey: string,
  31. componentQualifier: ComponentQualifier | string,
  32. branchParameters?: BranchParameters
  33. ) {
  34. return isPortfolioLike(componentQualifier)
  35. ? getPortfolioUrl(componentKey)
  36. : getProjectQueryUrl(componentKey, branchParameters);
  37. }
  38. export function getComponentAdminUrl(
  39. componentKey: string,
  40. componentQualifier: ComponentQualifier | string
  41. ) {
  42. if (isPortfolioLike(componentQualifier)) {
  43. return getPortfolioAdminUrl(componentKey);
  44. } else if (isApplication(componentQualifier)) {
  45. return getApplicationAdminUrl(componentKey);
  46. } else {
  47. return getProjectUrl(componentKey);
  48. }
  49. }
  50. export function getProjectUrl(project: string, branch?: string): Location {
  51. return { pathname: '/dashboard', query: { id: project, branch } };
  52. }
  53. export function getProjectQueryUrl(project: string, branchParameters?: BranchParameters): Location {
  54. return { pathname: '/dashboard', query: { id: project, ...branchParameters } };
  55. }
  56. export function getPortfolioUrl(key: string): Location {
  57. return { pathname: '/portfolio', query: { id: key } };
  58. }
  59. export function getPortfolioAdminUrl(key: string) {
  60. return {
  61. pathname: '/project/admin/extension/governance/console',
  62. query: { id: key, qualifier: ComponentQualifier.Portfolio }
  63. };
  64. }
  65. export function getApplicationAdminUrl(key: string) {
  66. return { pathname: '/application/console', query: { id: key } };
  67. }
  68. export function getComponentBackgroundTaskUrl(componentKey: string, status?: string): Location {
  69. return { pathname: '/project/background_tasks', query: { id: componentKey, status } };
  70. }
  71. export function getBranchLikeUrl(project: string, branchLike?: BranchLike): Location {
  72. if (isPullRequest(branchLike)) {
  73. return getPullRequestUrl(project, branchLike.key);
  74. } else if (isBranch(branchLike) && !isMainBranch(branchLike)) {
  75. return getBranchUrl(project, branchLike.name);
  76. } else {
  77. return getProjectUrl(project);
  78. }
  79. }
  80. export function getBranchUrl(project: string, branch: string): Location {
  81. return { pathname: '/dashboard', query: { branch, id: project } };
  82. }
  83. export function getPullRequestUrl(project: string, pullRequest: string): Location {
  84. return { pathname: '/dashboard', query: { id: project, pullRequest } };
  85. }
  86. /**
  87. * Generate URL for a global issues page
  88. */
  89. export function getIssuesUrl(query: Query): Location {
  90. const pathname = '/issues';
  91. return { pathname, query };
  92. }
  93. /**
  94. * Generate URL for a component's issues page
  95. */
  96. export function getComponentIssuesUrl(componentKey: string, query?: Query): Location {
  97. return { pathname: '/project/issues', query: { ...(query || {}), id: componentKey } };
  98. }
  99. /**
  100. * Generate URL for a component's security hotspot page
  101. */
  102. export function getComponentSecurityHotspotsUrl(componentKey: string, query: Query = {}): Location {
  103. const { branch, pullRequest, sinceLeakPeriod, hotspots, assignedToMe } = query;
  104. return {
  105. pathname: '/security_hotspots',
  106. query: {
  107. id: componentKey,
  108. branch,
  109. pullRequest,
  110. sinceLeakPeriod,
  111. hotspots,
  112. assignedToMe,
  113. ...pick(query, [
  114. SecurityStandard.SONARSOURCE,
  115. SecurityStandard.OWASP_TOP10,
  116. SecurityStandard.SANS_TOP25
  117. ])
  118. }
  119. };
  120. }
  121. /**
  122. * Generate URL for a component's drilldown page
  123. */
  124. export function getComponentDrilldownUrl(options: {
  125. componentKey: string;
  126. metric: string;
  127. branchLike?: BranchLike;
  128. selectionKey?: string;
  129. treemapView?: boolean;
  130. listView?: boolean;
  131. }): Location {
  132. const { componentKey, metric, branchLike, selectionKey, treemapView, listView } = options;
  133. const query: Query = { id: componentKey, metric, ...getBranchLikeQuery(branchLike) };
  134. if (treemapView) {
  135. query.view = 'treemap';
  136. }
  137. if (listView) {
  138. query.view = 'list';
  139. }
  140. if (selectionKey) {
  141. query.selected = selectionKey;
  142. }
  143. return { pathname: '/component_measures', query };
  144. }
  145. export function getComponentDrilldownUrlWithSelection(
  146. componentKey: string,
  147. selectionKey: string,
  148. metric: string,
  149. branchLike?: BranchLike
  150. ): Location {
  151. return getComponentDrilldownUrl({ componentKey, selectionKey, metric, branchLike });
  152. }
  153. export function getMeasureTreemapUrl(componentKey: string, metric: string) {
  154. return getComponentDrilldownUrl({ componentKey, metric, treemapView: true });
  155. }
  156. export function getActivityUrl(component: string, branchLike?: BranchLike, graph?: GraphType) {
  157. return {
  158. pathname: '/project/activity',
  159. query: { id: component, graph, ...getBranchLikeQuery(branchLike) }
  160. };
  161. }
  162. /**
  163. * Generate URL for a component's measure history
  164. */
  165. export function getMeasureHistoryUrl(component: string, metric: string, branchLike?: BranchLike) {
  166. return {
  167. pathname: '/project/activity',
  168. query: {
  169. id: component,
  170. graph: 'custom',
  171. custom_metrics: metric,
  172. ...getBranchLikeQuery(branchLike)
  173. }
  174. };
  175. }
  176. /**
  177. * Generate URL for a component's permissions page
  178. */
  179. export function getComponentPermissionsUrl(componentKey: string): Location {
  180. return { pathname: '/project_roles', query: { id: componentKey } };
  181. }
  182. /**
  183. * Generate URL for a quality profile
  184. */
  185. export function getQualityProfileUrl(name: string, language: string): Location {
  186. return getProfilePath(name, language);
  187. }
  188. export function getQualityGateUrl(key: string): Location {
  189. return {
  190. pathname: '/quality_gates/show/' + encodeURIComponent(key)
  191. };
  192. }
  193. export function getQualityGatesUrl(): Location {
  194. return {
  195. pathname: '/quality_gates'
  196. };
  197. }
  198. /**
  199. * Generate URL for the rules page
  200. */
  201. export function getRulesUrl(query: Query): Location {
  202. return { pathname: '/coding_rules', query };
  203. }
  204. /**
  205. * Generate URL for the rules page filtering only active deprecated rules
  206. */
  207. export function getDeprecatedActiveRulesUrl(query: Query = {}): Location {
  208. const baseQuery = { activation: 'true', statuses: 'DEPRECATED' };
  209. return getRulesUrl({ ...query, ...baseQuery });
  210. }
  211. export function getRuleUrl(rule: string) {
  212. return getRulesUrl({ open: rule, rule_key: rule });
  213. }
  214. export function getFormattingHelpUrl(): string {
  215. return getBaseUrl() + '/formatting/help';
  216. }
  217. export function getCodeUrl(
  218. project: string,
  219. branchLike?: BranchLike,
  220. selected?: string,
  221. line?: number
  222. ) {
  223. return {
  224. pathname: '/code',
  225. query: { id: project, ...getBranchLikeQuery(branchLike), selected, line }
  226. };
  227. }
  228. export function getHomePageUrl(homepage: T.HomePage) {
  229. switch (homepage.type) {
  230. case 'APPLICATION':
  231. return homepage.branch
  232. ? getProjectUrl(homepage.component, homepage.branch)
  233. : getProjectUrl(homepage.component);
  234. case 'PROJECT':
  235. return homepage.branch
  236. ? getBranchUrl(homepage.component, homepage.branch)
  237. : getProjectUrl(homepage.component);
  238. case 'PORTFOLIO':
  239. return getPortfolioUrl(homepage.component);
  240. case 'PORTFOLIOS':
  241. return '/portfolios';
  242. case 'MY_PROJECTS':
  243. return '/projects';
  244. case 'ISSUES':
  245. case 'MY_ISSUES':
  246. return { pathname: '/issues', query: { resolved: 'false' } };
  247. }
  248. // should never happen, but just in case...
  249. return '/projects';
  250. }