sanitizePath.tsx 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /**
  2. * Remove slugs from the path - we do not want them displayed in the Issues Stream (having them in issue details is ok)
  3. */
  4. const SHORTENED_TYPE = {
  5. organizations: 'org',
  6. customers: 'customer',
  7. projects: 'project',
  8. teams: 'team',
  9. };
  10. export function sanitizePath(path: string) {
  11. return path.replace(
  12. /(?<start>.*?)\/(?<type>organizations|customers|projects|teams)\/(?<primarySlug>[^/]+)\/(?<contentType>[^/]+\/)?(?<tertiarySlug>[^/]+\/)?(?<end>.*)/,
  13. function (...args) {
  14. const matches = args[args.length - 1];
  15. const {start, type, contentType, tertiarySlug, end} = matches;
  16. // `customers` is org-like
  17. const isOrg = ['organizations', 'customers'].includes(type);
  18. const isProject = type === 'projects';
  19. const isRuleConditions = isProject && contentType === 'rule-conditions/';
  20. // `end` should always match and at least return empty string,
  21. // `tertiarySlug` can be undefined
  22. let suffix = `${tertiarySlug ?? ''}${end}`;
  23. if (isOrg && contentType === 'events/' && typeof tertiarySlug === 'string') {
  24. // https://github.com/getsentry/sentry/blob/8d4482f01aa2122c6f6670ab84f9263e6f021467/src/sentry/api/urls.py#L1004
  25. // r"^(?P<organization_slug>[^\/]+)/events/(?P<project_slug>[^\/]+):(?P<event_id>(?:\d+|[A-Fa-f0-9-]{32,36}))/$",
  26. suffix = tertiarySlug.replace(/[^:]+(.*)/, '{projectSlug}$1');
  27. } else if (isOrg && contentType === 'members/') {
  28. // https://github.com/getsentry/sentry/blob/8d4482f01aa2122c6f6670ab84f9263e6f021467/src/sentry/api/urls.py#L1235
  29. // r"^(?P<organization_slug>[^\/]+)/members/(?P<member_id>[^\/]+)/teams/(?P<team_slug>[^\/]+)/$",
  30. suffix = `${tertiarySlug}${end.replace(
  31. /teams\/([^/]+)\/$/,
  32. 'teams/{teamSlug}/'
  33. )}`;
  34. } else if (isProject && tertiarySlug === 'teams/') {
  35. // https://github.com/getsentry/sentry/blob/8d4482f01aa2122c6f6670ab84f9263e6f021467/src/sentry/api/urls.py#L1894
  36. // r"^(?P<organization_slug>[^\/]+)/(?P<project_slug>[^\/]+)/teams/(?P<team_slug>[^\/]+)/$",
  37. suffix = `${tertiarySlug}{teamSlug}/`;
  38. } else if (isRuleConditions) {
  39. // https://github.com/getsentry/sentry/blob/8d4482f01aa2122c6f6670ab84f9263e6f021467/src/sentry/api/urls.py#L1595
  40. // r"^(?P<organization_slug>[^\/]+)/rule-conditions/$",
  41. suffix = '';
  42. }
  43. const contentTypeOrSecondarySlug = isOrg
  44. ? contentType ?? ''
  45. : isRuleConditions
  46. ? 'rule-conditions/'
  47. : `{${SHORTENED_TYPE[type]}Slug}/`;
  48. return `${start}/${type}/{orgSlug}/${contentTypeOrSecondarySlug}${suffix}`;
  49. }
  50. );
  51. }