configureIntegration.tsx 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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 List from 'sentry/components/list';
  7. import ListItem from 'sentry/components/list/listItem';
  8. import NavTabs from 'sentry/components/navTabs';
  9. import {IconAdd, IconArrow} from 'sentry/icons';
  10. import {t} from 'sentry/locale';
  11. import {IntegrationProvider, IntegrationWithConfig, Organization} from 'sentry/types';
  12. import {trackIntegrationAnalytics} from 'sentry/utils/integrationUtil';
  13. import {singleLineRenderer} from 'sentry/utils/marked';
  14. import withOrganization from 'sentry/utils/withOrganization';
  15. import AsyncView from 'sentry/views/asyncView';
  16. import AddIntegration from 'sentry/views/organizationIntegrations/addIntegration';
  17. import IntegrationAlertRules from 'sentry/views/organizationIntegrations/integrationAlertRules';
  18. import IntegrationCodeMappings from 'sentry/views/organizationIntegrations/integrationCodeMappings';
  19. import IntegrationExternalTeamMappings from 'sentry/views/organizationIntegrations/integrationExternalTeamMappings';
  20. import IntegrationExternalUserMappings from 'sentry/views/organizationIntegrations/integrationExternalUserMappings';
  21. import IntegrationItem from 'sentry/views/organizationIntegrations/integrationItem';
  22. import IntegrationMainSettings from 'sentry/views/organizationIntegrations/integrationMainSettings';
  23. import IntegrationRepos from 'sentry/views/organizationIntegrations/integrationRepos';
  24. import IntegrationServerlessFunctions from 'sentry/views/organizationIntegrations/integrationServerlessFunctions';
  25. import Form from 'sentry/views/settings/components/forms/form';
  26. import JsonForm from 'sentry/views/settings/components/forms/jsonForm';
  27. import BreadcrumbTitle from 'sentry/views/settings/components/settingsBreadcrumb/breadcrumbTitle';
  28. import SettingsPageHeader from 'sentry/views/settings/components/settingsPageHeader';
  29. type RouteParams = {
  30. orgId: string;
  31. providerKey: string;
  32. integrationId: 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="small"
  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. <BreadcrumbTitle routes={this.props.routes} title={integration.provider.name} />
  146. {integration.configOrganization.length > 0 && (
  147. <Form
  148. hideFooter
  149. saveOnBlur
  150. allowUndo
  151. apiMethod="POST"
  152. initialData={integration.configData || {}}
  153. apiEndpoint={`/organizations/${orgId}/integrations/${integration.id}/`}
  154. >
  155. <JsonForm
  156. fields={integration.configOrganization}
  157. title={
  158. integration.provider.aspects.configure_integration?.title ||
  159. t('Organization Integration Settings')
  160. }
  161. />
  162. </Form>
  163. )}
  164. {instructions && instructions.length > 0 && (
  165. <Alert type="info">
  166. {instructions?.length === 1 ? (
  167. <span
  168. dangerouslySetInnerHTML={{__html: singleLineRenderer(instructions[0])}}
  169. />
  170. ) : (
  171. <List symbol={<IconArrow size="xs" direction="right" />}>
  172. {instructions?.map((instruction, i) => (
  173. <ListItem key={i}>
  174. <span
  175. dangerouslySetInnerHTML={{__html: singleLineRenderer(instruction)}}
  176. />
  177. </ListItem>
  178. )) ?? []}
  179. </List>
  180. )}
  181. </Alert>
  182. )}
  183. {provider.features.includes('alert-rule') && <IntegrationAlertRules />}
  184. {provider.features.includes('commits') && (
  185. <IntegrationRepos {...this.props} integration={integration} />
  186. )}
  187. {provider.features.includes('serverless') && (
  188. <IntegrationServerlessFunctions integration={integration} />
  189. )}
  190. </Fragment>
  191. );
  192. }
  193. renderBody() {
  194. const {integration} = this.state;
  195. const provider = this.state.config.providers.find(
  196. p => p.key === integration.provider.key
  197. );
  198. if (!provider) {
  199. return null;
  200. }
  201. const title = <IntegrationItem integration={integration} />;
  202. const header = (
  203. <SettingsPageHeader noTitleStyles title={title} action={this.getAction(provider)} />
  204. );
  205. return (
  206. <Fragment>
  207. {header}
  208. {this.renderMainContent(provider)}
  209. </Fragment>
  210. );
  211. }
  212. // renders everything below header
  213. renderMainContent(provider: IntegrationProvider) {
  214. // if no code mappings, render the single tab
  215. if (!this.hasStacktraceLinking(provider)) {
  216. return this.renderMainTab(provider);
  217. }
  218. // otherwise render the tab view
  219. const tabs = [
  220. ['repos', t('Repositories')],
  221. ['codeMappings', t('Code Mappings')],
  222. ...(this.hasCodeOwners() ? [['userMappings', t('User Mappings')]] : []),
  223. ...(this.hasCodeOwners() ? [['teamMappings', t('Team Mappings')]] : []),
  224. ] as [id: Tab, label: string][];
  225. if (this.isCustomIntegration()) {
  226. tabs.unshift(['settings', t('Settings')]);
  227. }
  228. return (
  229. <Fragment>
  230. <NavTabs underlined>
  231. {tabs.map(tabTuple => (
  232. <li
  233. key={tabTuple[0]}
  234. className={this.tab === tabTuple[0] ? 'active' : ''}
  235. onClick={() => this.onTabChange(tabTuple[0])}
  236. >
  237. <CapitalizedLink>{tabTuple[1]}</CapitalizedLink>
  238. </li>
  239. ))}
  240. </NavTabs>
  241. {this.renderTabContent(this.tab, provider)}
  242. </Fragment>
  243. );
  244. }
  245. renderTabContent(tab: Tab, provider: IntegrationProvider) {
  246. const {integration} = this.state;
  247. const {organization} = this.props;
  248. switch (tab) {
  249. case 'codeMappings':
  250. return <IntegrationCodeMappings integration={integration} />;
  251. case 'repos':
  252. return this.renderMainTab(provider);
  253. case 'userMappings':
  254. return <IntegrationExternalUserMappings integration={integration} />;
  255. case 'teamMappings':
  256. return <IntegrationExternalTeamMappings integration={integration} />;
  257. case 'settings':
  258. return (
  259. <IntegrationMainSettings
  260. onUpdate={this.onUpdateIntegration}
  261. organization={organization}
  262. integration={integration}
  263. />
  264. );
  265. default:
  266. return this.renderMainTab(provider);
  267. }
  268. }
  269. }
  270. export default withOrganization(ConfigureIntegration);
  271. const CapitalizedLink = styled('a')`
  272. text-transform: capitalize;
  273. `;