configureIntegration.tsx 12 KB

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