configureIntegration.tsx 9.6 KB

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