configureIntegration.tsx 9.5 KB

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