configureIntegration.tsx 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. import {Fragment} from 'react';
  2. import {RouteComponentProps} from 'react-router';
  3. import styled from '@emotion/styled';
  4. import Alert from 'sentry/components/alert';
  5. import Button from 'sentry/components/button';
  6. import Form from 'sentry/components/forms/form';
  7. import JsonForm from 'sentry/components/forms/jsonForm';
  8. import List from 'sentry/components/list';
  9. import ListItem from 'sentry/components/list/listItem';
  10. import NavTabs from 'sentry/components/navTabs';
  11. import {IconAdd, IconArrow} from 'sentry/icons';
  12. import {t} from 'sentry/locale';
  13. import {IntegrationProvider, IntegrationWithConfig, Organization} from 'sentry/types';
  14. import {trackIntegrationAnalytics} from 'sentry/utils/integrationUtil';
  15. import {singleLineRenderer} from 'sentry/utils/marked';
  16. import withOrganization from 'sentry/utils/withOrganization';
  17. import AsyncView from 'sentry/views/asyncView';
  18. import AddIntegration from 'sentry/views/organizationIntegrations/addIntegration';
  19. import IntegrationAlertRules from 'sentry/views/organizationIntegrations/integrationAlertRules';
  20. import IntegrationCodeMappings from 'sentry/views/organizationIntegrations/integrationCodeMappings';
  21. import IntegrationExternalTeamMappings from 'sentry/views/organizationIntegrations/integrationExternalTeamMappings';
  22. import IntegrationExternalUserMappings from 'sentry/views/organizationIntegrations/integrationExternalUserMappings';
  23. import IntegrationItem from 'sentry/views/organizationIntegrations/integrationItem';
  24. import IntegrationMainSettings from 'sentry/views/organizationIntegrations/integrationMainSettings';
  25. import IntegrationRepos from 'sentry/views/organizationIntegrations/integrationRepos';
  26. import IntegrationServerlessFunctions from 'sentry/views/organizationIntegrations/integrationServerlessFunctions';
  27. import BreadcrumbTitle from 'sentry/views/settings/components/settingsBreadcrumb/breadcrumbTitle';
  28. import SettingsPageHeader from 'sentry/views/settings/components/settingsPageHeader';
  29. type RouteParams = {
  30. integrationId: string;
  31. orgId: string;
  32. providerKey: string;
  33. };
  34. type Props = RouteComponentProps<RouteParams, {}> & {
  35. organization: Organization;
  36. };
  37. type Tab = 'repos' | 'codeMappings' | 'userMappings' | 'teamMappings' | 'settings';
  38. type State = AsyncView['state'] & {
  39. config: {providers: IntegrationProvider[]};
  40. integration: IntegrationWithConfig;
  41. tab?: Tab;
  42. };
  43. class ConfigureIntegration extends AsyncView<Props, State> {
  44. getEndpoints(): ReturnType<AsyncView['getEndpoints']> {
  45. const {orgId, integrationId} = this.props.params;
  46. return [
  47. ['config', `/organizations/${orgId}/config/integrations/`],
  48. ['integration', `/organizations/${orgId}/integrations/${integrationId}/`],
  49. ];
  50. }
  51. componentDidMount() {
  52. const {
  53. location,
  54. router,
  55. organization,
  56. params: {orgId, providerKey},
  57. } = this.props;
  58. // This page should not be accessible by members
  59. if (!organization.access.includes('org:integrations')) {
  60. router.push({
  61. pathname: `/settings/${orgId}/integrations/${providerKey}/`,
  62. });
  63. }
  64. const value =
  65. (['codeMappings', 'userMappings', 'teamMappings'] as const).find(
  66. tab => tab === location.query.tab
  67. ) || 'repos';
  68. // eslint-disable-next-line react/no-did-mount-set-state
  69. this.setState({tab: value});
  70. }
  71. onRequestSuccess({stateKey, data}) {
  72. if (stateKey !== 'integration') {
  73. return;
  74. }
  75. trackIntegrationAnalytics('integrations.details_viewed', {
  76. integration: data.provider.key,
  77. integration_type: 'first_party',
  78. organization: this.props.organization,
  79. });
  80. }
  81. getTitle() {
  82. return this.state.integration
  83. ? this.state.integration.provider.name
  84. : 'Configure Integration';
  85. }
  86. hasStacktraceLinking(provider: IntegrationProvider) {
  87. // CodeOwners will only work if the provider has StackTrace Linking
  88. return (
  89. provider.features.includes('stacktrace-link') &&
  90. this.props.organization.features.includes('integrations-stacktrace-link')
  91. );
  92. }
  93. hasCodeOwners() {
  94. return this.props.organization.features.includes('integrations-codeowners');
  95. }
  96. isCustomIntegration() {
  97. const {integration} = this.state;
  98. const {organization} = this.props;
  99. return (
  100. organization.features.includes('integrations-custom-scm') &&
  101. integration.provider.key === 'custom_scm'
  102. );
  103. }
  104. onTabChange = (value: Tab) => {
  105. this.setState({tab: value});
  106. };
  107. get tab() {
  108. return this.state.tab || 'repos';
  109. }
  110. onUpdateIntegration = () => {
  111. this.setState(this.getDefaultState(), this.fetchData);
  112. };
  113. getAction = (provider: IntegrationProvider | undefined) => {
  114. const {integration} = this.state;
  115. const action =
  116. provider && provider.key === 'pagerduty' ? (
  117. <AddIntegration
  118. provider={provider}
  119. onInstall={this.onUpdateIntegration}
  120. account={integration.domainName}
  121. organization={this.props.organization}
  122. >
  123. {onClick => (
  124. <Button
  125. priority="primary"
  126. size="sm"
  127. icon={<IconAdd size="xs" isCircled />}
  128. onClick={() => onClick()}
  129. >
  130. {t('Add Services')}
  131. </Button>
  132. )}
  133. </AddIntegration>
  134. ) : null;
  135. return action;
  136. };
  137. // TODO(Steve): Refactor components into separate tabs and use more generic tab logic
  138. renderMainTab(provider: IntegrationProvider) {
  139. const {orgId} = this.props.params;
  140. const {integration} = this.state;
  141. const instructions =
  142. integration.dynamicDisplayInformation?.configure_integration?.instructions;
  143. return (
  144. <Fragment>
  145. {integration.configOrganization.length > 0 && (
  146. <Form
  147. hideFooter
  148. saveOnBlur
  149. allowUndo
  150. apiMethod="POST"
  151. initialData={integration.configData || {}}
  152. apiEndpoint={`/organizations/${orgId}/integrations/${integration.id}/`}
  153. >
  154. <JsonForm
  155. fields={integration.configOrganization}
  156. title={
  157. integration.provider.aspects.configure_integration?.title ||
  158. t('Organization Integration Settings')
  159. }
  160. />
  161. </Form>
  162. )}
  163. {instructions && instructions.length > 0 && (
  164. <Alert type="info">
  165. {instructions?.length === 1 ? (
  166. <span
  167. dangerouslySetInnerHTML={{__html: singleLineRenderer(instructions[0])}}
  168. />
  169. ) : (
  170. <List symbol={<IconArrow size="xs" direction="right" />}>
  171. {instructions?.map((instruction, i) => (
  172. <ListItem key={i}>
  173. <span
  174. dangerouslySetInnerHTML={{__html: singleLineRenderer(instruction)}}
  175. />
  176. </ListItem>
  177. )) ?? []}
  178. </List>
  179. )}
  180. </Alert>
  181. )}
  182. {provider.features.includes('alert-rule') && <IntegrationAlertRules />}
  183. {provider.features.includes('commits') && (
  184. <IntegrationRepos {...this.props} integration={integration} />
  185. )}
  186. {provider.features.includes('serverless') && (
  187. <IntegrationServerlessFunctions integration={integration} />
  188. )}
  189. </Fragment>
  190. );
  191. }
  192. renderBody() {
  193. const {integration} = this.state;
  194. const provider = this.state.config.providers.find(
  195. p => p.key === integration.provider.key
  196. );
  197. if (!provider) {
  198. return null;
  199. }
  200. const title = <IntegrationItem integration={integration} />;
  201. const header = (
  202. <SettingsPageHeader noTitleStyles title={title} action={this.getAction(provider)} />
  203. );
  204. return (
  205. <Fragment>
  206. {header}
  207. {this.renderMainContent(provider)}
  208. <BreadcrumbTitle
  209. routes={this.props.routes}
  210. title={t('Configure %s', integration.provider.name)}
  211. />
  212. </Fragment>
  213. );
  214. }
  215. // renders everything below header
  216. renderMainContent(provider: IntegrationProvider) {
  217. // if no code mappings, render the single tab
  218. if (!this.hasStacktraceLinking(provider)) {
  219. return this.renderMainTab(provider);
  220. }
  221. // otherwise render the tab view
  222. const tabs = [
  223. ['repos', t('Repositories')],
  224. ['codeMappings', t('Code Mappings')],
  225. ...(this.hasCodeOwners() ? [['userMappings', t('User Mappings')]] : []),
  226. ...(this.hasCodeOwners() ? [['teamMappings', t('Team Mappings')]] : []),
  227. ] as [id: Tab, label: string][];
  228. if (this.isCustomIntegration()) {
  229. tabs.unshift(['settings', t('Settings')]);
  230. }
  231. return (
  232. <Fragment>
  233. <NavTabs underlined>
  234. {tabs.map(tabTuple => (
  235. <li
  236. key={tabTuple[0]}
  237. className={this.tab === tabTuple[0] ? 'active' : ''}
  238. onClick={() => this.onTabChange(tabTuple[0])}
  239. >
  240. <CapitalizedLink>{tabTuple[1]}</CapitalizedLink>
  241. </li>
  242. ))}
  243. </NavTabs>
  244. {this.renderTabContent(this.tab, provider)}
  245. </Fragment>
  246. );
  247. }
  248. renderTabContent(tab: Tab, provider: IntegrationProvider) {
  249. const {integration} = this.state;
  250. const {organization} = this.props;
  251. switch (tab) {
  252. case 'codeMappings':
  253. return <IntegrationCodeMappings integration={integration} />;
  254. case 'repos':
  255. return this.renderMainTab(provider);
  256. case 'userMappings':
  257. return <IntegrationExternalUserMappings integration={integration} />;
  258. case 'teamMappings':
  259. return <IntegrationExternalTeamMappings integration={integration} />;
  260. case 'settings':
  261. return (
  262. <IntegrationMainSettings
  263. onUpdate={this.onUpdateIntegration}
  264. organization={organization}
  265. integration={integration}
  266. />
  267. );
  268. default:
  269. return this.renderMainTab(provider);
  270. }
  271. }
  272. }
  273. export default withOrganization(ConfigureIntegration);
  274. const CapitalizedLink = styled('a')`
  275. text-transform: capitalize;
  276. `;