integrationUtil.tsx 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. import capitalize from 'lodash/capitalize';
  2. import * as qs from 'query-string';
  3. import {Result} from 'sentry/components/forms/controls/selectAsyncControl';
  4. import {
  5. IconAsana,
  6. IconBitbucket,
  7. IconCodecov,
  8. IconGeneric,
  9. IconGithub,
  10. IconGitlab,
  11. IconJira,
  12. IconSentry,
  13. IconVsts,
  14. } from 'sentry/icons';
  15. import {t} from 'sentry/locale';
  16. import HookStore from 'sentry/stores/hookStore';
  17. import type {
  18. AppOrProviderOrPlugin,
  19. CodeOwner,
  20. DocIntegration,
  21. ExternalActorMapping,
  22. ExternalActorMappingOrSuggestion,
  23. Integration,
  24. IntegrationFeature,
  25. IntegrationInstallationStatus,
  26. IntegrationType,
  27. PluginWithProjectList,
  28. SentryApp,
  29. SentryAppInstallation,
  30. } from 'sentry/types';
  31. import {Hooks} from 'sentry/types/hooks';
  32. import {trackAnalytics} from 'sentry/utils/analytics';
  33. import {IconSize} from './theme';
  34. /**
  35. * TODO: remove alias once all usages are updated
  36. * @deprecated Use trackAnalytics instead
  37. */
  38. export const trackIntegrationAnalytics = trackAnalytics;
  39. /**
  40. * In sentry.io the features list supports rendering plan details. If the hook
  41. * is not registered for rendering the features list like this simply show the
  42. * features as a normal list.
  43. */
  44. const generateFeaturesList = p => (
  45. <ul>
  46. {p.features.map((f, i) => (
  47. <li key={i}>{f.description}</li>
  48. ))}
  49. </ul>
  50. );
  51. const generateIntegrationFeatures = p =>
  52. p.children({
  53. disabled: false,
  54. disabledReason: null,
  55. ungatedFeatures: p.features,
  56. gatedFeatureGroups: [],
  57. });
  58. const defaultFeatureGateComponents: ReturnType<Hooks['integrations:feature-gates']> = {
  59. IntegrationFeatures: generateIntegrationFeatures,
  60. FeatureList: generateFeaturesList,
  61. };
  62. export const getIntegrationFeatureGate = () => {
  63. const defaultHook = () => defaultFeatureGateComponents;
  64. const featureHook = HookStore.get('integrations:feature-gates')[0] || defaultHook;
  65. return featureHook();
  66. };
  67. export const getSentryAppInstallStatus = (install: SentryAppInstallation | undefined) => {
  68. if (install) {
  69. return capitalize(install.status) as IntegrationInstallationStatus;
  70. }
  71. return 'Not Installed';
  72. };
  73. export const getCategories = (features: IntegrationFeature[]): string[] => {
  74. const transform = features.map(({featureGate}) => {
  75. const feature = featureGate
  76. .replace(/integrations/g, '')
  77. .replace(/-/g, ' ')
  78. .trim();
  79. switch (feature) {
  80. case 'actionable notification':
  81. return 'notification action';
  82. case 'issue basic':
  83. case 'issue link':
  84. case 'issue sync':
  85. case 'project management':
  86. return 'issue tracking';
  87. case 'commits':
  88. return 'source code management';
  89. case 'chat unfurl':
  90. return 'chat';
  91. default:
  92. return feature;
  93. }
  94. });
  95. return [...new Set(transform)];
  96. };
  97. export const getCategoriesForIntegration = (
  98. integration: AppOrProviderOrPlugin
  99. ): string[] => {
  100. if (isSentryApp(integration)) {
  101. return ['internal', 'unpublished'].includes(integration.status)
  102. ? [integration.status]
  103. : getCategories(integration.featureData);
  104. }
  105. if (isPlugin(integration)) {
  106. return getCategories(integration.featureDescriptions);
  107. }
  108. if (isDocIntegration(integration)) {
  109. return getCategories(integration.features ?? []);
  110. }
  111. return getCategories(integration.metadata.features);
  112. };
  113. export function isSentryApp(
  114. integration: AppOrProviderOrPlugin
  115. ): integration is SentryApp {
  116. return !!(integration as SentryApp).uuid;
  117. }
  118. export function isPlugin(
  119. integration: AppOrProviderOrPlugin
  120. ): integration is PluginWithProjectList {
  121. return integration.hasOwnProperty('shortName');
  122. }
  123. export function isDocIntegration(
  124. integration: AppOrProviderOrPlugin
  125. ): integration is DocIntegration {
  126. return integration.hasOwnProperty('isDraft');
  127. }
  128. export function isExternalActorMapping(
  129. mapping: ExternalActorMappingOrSuggestion
  130. ): mapping is ExternalActorMapping {
  131. return mapping.hasOwnProperty('id');
  132. }
  133. export const getIntegrationType = (
  134. integration: AppOrProviderOrPlugin
  135. ): IntegrationType => {
  136. if (isSentryApp(integration)) {
  137. return 'sentry_app';
  138. }
  139. if (isPlugin(integration)) {
  140. return 'plugin';
  141. }
  142. if (isDocIntegration(integration)) {
  143. return 'document';
  144. }
  145. return 'first_party';
  146. };
  147. export const convertIntegrationTypeToSnakeCase = (
  148. type: 'plugin' | 'firstParty' | 'sentryApp' | 'docIntegration'
  149. ) => {
  150. switch (type) {
  151. case 'firstParty':
  152. return 'first_party';
  153. case 'sentryApp':
  154. return 'sentry_app';
  155. case 'docIntegration':
  156. return 'document';
  157. default:
  158. return type;
  159. }
  160. };
  161. export const safeGetQsParam = (param: string) => {
  162. try {
  163. const query = qs.parse(window.location.search) || {};
  164. return query[param];
  165. } catch {
  166. return undefined;
  167. }
  168. };
  169. export const getIntegrationIcon = (
  170. integrationType?: string,
  171. iconSize: IconSize = 'md'
  172. ) => {
  173. switch (integrationType) {
  174. case 'asana':
  175. return <IconAsana size={iconSize} />;
  176. case 'bitbucket':
  177. return <IconBitbucket size={iconSize} />;
  178. case 'gitlab':
  179. return <IconGitlab size={iconSize} />;
  180. case 'github':
  181. case 'github_enterprise':
  182. return <IconGithub size={iconSize} />;
  183. case 'jira':
  184. case 'jira_server':
  185. return <IconJira size={iconSize} />;
  186. case 'vsts':
  187. return <IconVsts size={iconSize} />;
  188. case 'codecov':
  189. return <IconCodecov size={iconSize} />;
  190. default:
  191. return <IconGeneric size={iconSize} />;
  192. }
  193. };
  194. export function getCodeOwnerIcon(
  195. provider: CodeOwner['provider'],
  196. iconSize: IconSize = 'md'
  197. ) {
  198. switch (provider ?? '') {
  199. case 'github':
  200. return <IconGithub size={iconSize} />;
  201. case 'gitlab':
  202. return <IconGitlab size={iconSize} />;
  203. default:
  204. return <IconSentry size={iconSize} />;
  205. }
  206. }
  207. // used for project creation and onboarding
  208. // determines what integration maps to what project platform
  209. export const platformToIntegrationMap = {
  210. 'node-awslambda': 'aws_lambda',
  211. 'python-awslambda': 'aws_lambda',
  212. };
  213. export const isSlackIntegrationUpToDate = (integrations: Integration[]): boolean => {
  214. return integrations.every(
  215. integration =>
  216. integration.provider.key !== 'slack' || integration.scopes?.includes('commands')
  217. );
  218. };
  219. export const getAlertText = (integrations?: Integration[]): string | undefined => {
  220. return isSlackIntegrationUpToDate(integrations || [])
  221. ? undefined
  222. : t(
  223. 'Update to the latest version of our Slack app to get access to personal and team notifications.'
  224. );
  225. };
  226. /**
  227. * Uses the mapping and baseEndpoint to derive the details for the mappings request.
  228. * @param baseEndpoint Must have a trailing slash, since the id is appended for PUT requests!
  229. * @param mapping The mapping or suggestion being sent to the endpoint
  230. * @returns An object containing the request method (apiMethod), and final endpoint (apiEndpoint)
  231. */
  232. export const getExternalActorEndpointDetails = (
  233. baseEndpoint: string,
  234. mapping?: ExternalActorMappingOrSuggestion
  235. ): {apiEndpoint: string; apiMethod: 'POST' | 'PUT'} => {
  236. const isValidMapping = mapping && isExternalActorMapping(mapping);
  237. return {
  238. apiMethod: isValidMapping ? 'PUT' : 'POST',
  239. apiEndpoint: isValidMapping ? `${baseEndpoint}${mapping.id}/` : baseEndpoint,
  240. };
  241. };
  242. export const sentryNameToOption = ({id, name}): Result => ({
  243. value: id,
  244. label: name,
  245. });
  246. export function getIntegrationStatus(integration: Integration) {
  247. // there are multiple status fields for an integration we consider
  248. const statusList = [integration.organizationIntegrationStatus, integration.status];
  249. const firstNotActive = statusList.find(s => s !== 'active');
  250. // Active if everything is active, otherwise the first inactive status
  251. return firstNotActive ?? 'active';
  252. }