configureIntegration.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. import {Fragment} from 'react';
  2. import {RouteComponentProps} from 'react-router';
  3. import styled from '@emotion/styled';
  4. import {addErrorMessage, addSuccessMessage} from 'sentry/actionCreators/indicator';
  5. import {Client} from 'sentry/api';
  6. import Access from 'sentry/components/acl/access';
  7. import Alert from 'sentry/components/alert';
  8. import Button from 'sentry/components/button';
  9. import Confirm from 'sentry/components/confirm';
  10. import Form from 'sentry/components/forms/form';
  11. import JsonForm from 'sentry/components/forms/jsonForm';
  12. import List from 'sentry/components/list';
  13. import ListItem from 'sentry/components/list/listItem';
  14. import NavTabs from 'sentry/components/navTabs';
  15. import {IconAdd, IconArrow} from 'sentry/icons';
  16. import {t} from 'sentry/locale';
  17. import {
  18. IntegrationProvider,
  19. IntegrationWithConfig,
  20. Organization,
  21. PluginWithProjectList,
  22. } from 'sentry/types';
  23. import {trackIntegrationAnalytics} from 'sentry/utils/integrationUtil';
  24. import {singleLineRenderer} from 'sentry/utils/marked';
  25. import withApi from 'sentry/utils/withApi';
  26. import withOrganization from 'sentry/utils/withOrganization';
  27. import AsyncView from 'sentry/views/asyncView';
  28. import AddIntegration from 'sentry/views/organizationIntegrations/addIntegration';
  29. import IntegrationAlertRules from 'sentry/views/organizationIntegrations/integrationAlertRules';
  30. import IntegrationCodeMappings from 'sentry/views/organizationIntegrations/integrationCodeMappings';
  31. import IntegrationExternalTeamMappings from 'sentry/views/organizationIntegrations/integrationExternalTeamMappings';
  32. import IntegrationExternalUserMappings from 'sentry/views/organizationIntegrations/integrationExternalUserMappings';
  33. import IntegrationItem from 'sentry/views/organizationIntegrations/integrationItem';
  34. import IntegrationMainSettings from 'sentry/views/organizationIntegrations/integrationMainSettings';
  35. import IntegrationRepos from 'sentry/views/organizationIntegrations/integrationRepos';
  36. import IntegrationServerlessFunctions from 'sentry/views/organizationIntegrations/integrationServerlessFunctions';
  37. import BreadcrumbTitle from 'sentry/views/settings/components/settingsBreadcrumb/breadcrumbTitle';
  38. import SettingsPageHeader from 'sentry/views/settings/components/settingsPageHeader';
  39. type RouteParams = {
  40. integrationId: string;
  41. orgId: string;
  42. providerKey: string;
  43. };
  44. type Props = RouteComponentProps<RouteParams, {}> & {
  45. api: Client;
  46. organization: Organization;
  47. };
  48. type Tab = 'repos' | 'codeMappings' | 'userMappings' | 'teamMappings' | 'settings';
  49. type State = AsyncView['state'] & {
  50. config: {providers: IntegrationProvider[]};
  51. integration: IntegrationWithConfig;
  52. plugins: PluginWithProjectList[] | null;
  53. tab?: Tab;
  54. };
  55. class ConfigureIntegration extends AsyncView<Props, State> {
  56. getEndpoints(): ReturnType<AsyncView['getEndpoints']> {
  57. const {orgId, integrationId} = this.props.params;
  58. return [
  59. ['config', `/organizations/${orgId}/config/integrations/`],
  60. ['integration', `/organizations/${orgId}/integrations/${integrationId}/`],
  61. ['plugins', `/organizations/${orgId}/plugins/configs/`],
  62. ];
  63. }
  64. componentDidMount() {
  65. const {
  66. location,
  67. router,
  68. organization,
  69. params: {orgId, providerKey},
  70. } = this.props;
  71. // This page should not be accessible by members
  72. if (!organization.access.includes('org:integrations')) {
  73. router.push({
  74. pathname: `/settings/${orgId}/integrations/${providerKey}/`,
  75. });
  76. }
  77. const value =
  78. (['codeMappings', 'userMappings', 'teamMappings'] as const).find(
  79. tab => tab === location.query.tab
  80. ) || 'repos';
  81. // eslint-disable-next-line react/no-did-mount-set-state
  82. this.setState({tab: value});
  83. }
  84. onRequestSuccess({stateKey, data}) {
  85. if (stateKey !== 'integration') {
  86. return;
  87. }
  88. trackIntegrationAnalytics('integrations.details_viewed', {
  89. integration: data.provider.key,
  90. integration_type: 'first_party',
  91. organization: this.props.organization,
  92. });
  93. }
  94. getTitle() {
  95. return this.state.integration
  96. ? this.state.integration.provider.name
  97. : 'Configure Integration';
  98. }
  99. hasStacktraceLinking(provider: IntegrationProvider) {
  100. // CodeOwners will only work if the provider has StackTrace Linking
  101. return (
  102. provider.features.includes('stacktrace-link') &&
  103. this.props.organization.features.includes('integrations-stacktrace-link')
  104. );
  105. }
  106. hasCodeOwners() {
  107. return this.props.organization.features.includes('integrations-codeowners');
  108. }
  109. isCustomIntegration() {
  110. const {integration} = this.state;
  111. const {organization} = this.props;
  112. return (
  113. organization.features.includes('integrations-custom-scm') &&
  114. integration.provider.key === 'custom_scm'
  115. );
  116. }
  117. onTabChange = (value: Tab) => {
  118. this.setState({tab: value});
  119. };
  120. get tab() {
  121. return this.state.tab || 'repos';
  122. }
  123. onUpdateIntegration = () => {
  124. this.setState(this.getDefaultState(), this.fetchData);
  125. };
  126. handleJiraMigration = async () => {
  127. try {
  128. const {
  129. params: {orgId, integrationId},
  130. } = this.props;
  131. await this.api.requestPromise(
  132. `/organizations/${orgId}/integrations/${integrationId}/issues/`,
  133. {
  134. method: 'PUT',
  135. data: {},
  136. }
  137. );
  138. this.setState(
  139. {
  140. plugins: (this.state.plugins || []).filter(({id}) => id === 'jira'),
  141. },
  142. () => addSuccessMessage(t('Migration in progress.'))
  143. );
  144. } catch (error) {
  145. addErrorMessage(t('Something went wrong! Please try again.'));
  146. }
  147. };
  148. getAction = (provider: IntegrationProvider | undefined) => {
  149. const {integration, plugins} = this.state;
  150. const shouldMigrateJiraPlugin =
  151. provider &&
  152. ['jira', 'jira_server'].includes(provider.key) &&
  153. (plugins || []).find(({id}) => id === 'jira');
  154. const action =
  155. provider && provider.key === 'pagerduty' ? (
  156. <AddIntegration
  157. provider={provider}
  158. onInstall={this.onUpdateIntegration}
  159. account={integration.domainName}
  160. organization={this.props.organization}
  161. >
  162. {onClick => (
  163. <Button
  164. priority="primary"
  165. size="sm"
  166. icon={<IconAdd size="xs" isCircled />}
  167. onClick={() => onClick()}
  168. >
  169. {t('Add Services')}
  170. </Button>
  171. )}
  172. </AddIntegration>
  173. ) : shouldMigrateJiraPlugin ? (
  174. <Access access={['org:integrations']}>
  175. {({hasAccess}) => (
  176. <Confirm
  177. disabled={!hasAccess}
  178. header="Migrate Linked Issues from Jira Plugins"
  179. renderMessage={() => (
  180. <Fragment>
  181. <p>
  182. {t(
  183. 'This will automatically associate all the Linked Issues of your Jira Plugins to this integration.'
  184. )}
  185. </p>
  186. <p>
  187. {t(
  188. 'If the Jira Plugins had the option checked to automatically create a Jira ticket for every new Sentry issue checked, you will need to create alert rules to recreate this behavior. Jira Server does not have this feature.'
  189. )}
  190. </p>
  191. <p>
  192. {t(
  193. 'Once the migration is complete, your Jira Plugins will be disabled.'
  194. )}
  195. </p>
  196. </Fragment>
  197. )}
  198. onConfirm={() => {
  199. this.handleJiraMigration();
  200. }}
  201. >
  202. <Button priority="primary" size="md" disabled={!hasAccess}>
  203. {t('Migrate Plugin')}
  204. </Button>
  205. </Confirm>
  206. )}
  207. </Access>
  208. ) : null;
  209. return action;
  210. };
  211. // TODO(Steve): Refactor components into separate tabs and use more generic tab logic
  212. renderMainTab(provider: IntegrationProvider) {
  213. const {orgId} = this.props.params;
  214. const {integration} = this.state;
  215. const instructions =
  216. integration.dynamicDisplayInformation?.configure_integration?.instructions;
  217. return (
  218. <Fragment>
  219. {integration.configOrganization.length > 0 && (
  220. <Form
  221. hideFooter
  222. saveOnBlur
  223. allowUndo
  224. apiMethod="POST"
  225. initialData={integration.configData || {}}
  226. apiEndpoint={`/organizations/${orgId}/integrations/${integration.id}/`}
  227. >
  228. <JsonForm
  229. fields={integration.configOrganization}
  230. title={
  231. integration.provider.aspects.configure_integration?.title ||
  232. t('Organization Integration Settings')
  233. }
  234. />
  235. </Form>
  236. )}
  237. {instructions && instructions.length > 0 && (
  238. <Alert type="info">
  239. {instructions?.length === 1 ? (
  240. <span
  241. dangerouslySetInnerHTML={{__html: singleLineRenderer(instructions[0])}}
  242. />
  243. ) : (
  244. <List symbol={<IconArrow size="xs" direction="right" />}>
  245. {instructions?.map((instruction, i) => (
  246. <ListItem key={i}>
  247. <span
  248. dangerouslySetInnerHTML={{__html: singleLineRenderer(instruction)}}
  249. />
  250. </ListItem>
  251. )) ?? []}
  252. </List>
  253. )}
  254. </Alert>
  255. )}
  256. {provider.features.includes('alert-rule') && <IntegrationAlertRules />}
  257. {provider.features.includes('commits') && (
  258. <IntegrationRepos {...this.props} integration={integration} />
  259. )}
  260. {provider.features.includes('serverless') && (
  261. <IntegrationServerlessFunctions integration={integration} />
  262. )}
  263. </Fragment>
  264. );
  265. }
  266. renderBody() {
  267. const {integration} = this.state;
  268. const provider = this.state.config.providers.find(
  269. p => p.key === integration.provider.key
  270. );
  271. if (!provider) {
  272. return null;
  273. }
  274. const title = <IntegrationItem integration={integration} />;
  275. const header = (
  276. <SettingsPageHeader noTitleStyles title={title} action={this.getAction(provider)} />
  277. );
  278. return (
  279. <Fragment>
  280. {header}
  281. {this.renderMainContent(provider)}
  282. <BreadcrumbTitle
  283. routes={this.props.routes}
  284. title={t('Configure %s', integration.provider.name)}
  285. />
  286. </Fragment>
  287. );
  288. }
  289. // renders everything below header
  290. renderMainContent(provider: IntegrationProvider) {
  291. // if no code mappings, render the single tab
  292. if (!this.hasStacktraceLinking(provider)) {
  293. return this.renderMainTab(provider);
  294. }
  295. // otherwise render the tab view
  296. const tabs = [
  297. ['repos', t('Repositories')],
  298. ['codeMappings', t('Code Mappings')],
  299. ...(this.hasCodeOwners() ? [['userMappings', t('User Mappings')]] : []),
  300. ...(this.hasCodeOwners() ? [['teamMappings', t('Team Mappings')]] : []),
  301. ] as [id: Tab, label: string][];
  302. if (this.isCustomIntegration()) {
  303. tabs.unshift(['settings', t('Settings')]);
  304. }
  305. return (
  306. <Fragment>
  307. <NavTabs underlined>
  308. {tabs.map(tabTuple => (
  309. <li
  310. key={tabTuple[0]}
  311. className={this.tab === tabTuple[0] ? 'active' : ''}
  312. onClick={() => this.onTabChange(tabTuple[0])}
  313. >
  314. <CapitalizedLink>{tabTuple[1]}</CapitalizedLink>
  315. </li>
  316. ))}
  317. </NavTabs>
  318. {this.renderTabContent(this.tab, provider)}
  319. </Fragment>
  320. );
  321. }
  322. renderTabContent(tab: Tab, provider: IntegrationProvider) {
  323. const {integration} = this.state;
  324. const {organization} = this.props;
  325. switch (tab) {
  326. case 'codeMappings':
  327. return <IntegrationCodeMappings integration={integration} />;
  328. case 'repos':
  329. return this.renderMainTab(provider);
  330. case 'userMappings':
  331. return <IntegrationExternalUserMappings integration={integration} />;
  332. case 'teamMappings':
  333. return <IntegrationExternalTeamMappings integration={integration} />;
  334. case 'settings':
  335. return (
  336. <IntegrationMainSettings
  337. onUpdate={this.onUpdateIntegration}
  338. organization={organization}
  339. integration={integration}
  340. />
  341. );
  342. default:
  343. return this.renderMainTab(provider);
  344. }
  345. }
  346. }
  347. export default withOrganization(withApi(ConfigureIntegration));
  348. const CapitalizedLink = styled('a')`
  349. text-transform: capitalize;
  350. `;