configureIntegration.tsx 13 KB

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