configureIntegration.tsx 13 KB

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