integrationDetailedView.tsx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. import {Fragment} from 'react';
  2. import styled from '@emotion/styled';
  3. import {addErrorMessage} from 'sentry/actionCreators/indicator';
  4. import type {RequestOptions} from 'sentry/api';
  5. import {Alert} from 'sentry/components/alert';
  6. import type DeprecatedAsyncComponent from 'sentry/components/deprecatedAsyncComponent';
  7. import Form from 'sentry/components/forms/form';
  8. import JsonForm from 'sentry/components/forms/jsonForm';
  9. import type {Data, JsonFormObject} from 'sentry/components/forms/types';
  10. import HookOrDefault from 'sentry/components/hookOrDefault';
  11. import Panel from 'sentry/components/panels/panel';
  12. import PanelItem from 'sentry/components/panels/panelItem';
  13. import {t} from 'sentry/locale';
  14. import {space} from 'sentry/styles/space';
  15. import type {Integration, IntegrationProvider, ObjectStatus} from 'sentry/types';
  16. import {getAlertText, getIntegrationStatus} from 'sentry/utils/integrationUtil';
  17. import normalizeUrl from 'sentry/utils/url/normalizeUrl';
  18. import withOrganization from 'sentry/utils/withOrganization';
  19. import BreadcrumbTitle from 'sentry/views/settings/components/settingsBreadcrumb/breadcrumbTitle';
  20. import IntegrationButton from 'sentry/views/settings/organizationIntegrations/integrationButton';
  21. import {IntegrationContext} from 'sentry/views/settings/organizationIntegrations/integrationContext';
  22. import type {Tab} from './abstractIntegrationDetailedView';
  23. import AbstractIntegrationDetailedView from './abstractIntegrationDetailedView';
  24. import InstalledIntegration from './installedIntegration';
  25. // Show the features tab if the org has features for the integration
  26. const integrationFeatures = ['github', 'slack'];
  27. const FirstPartyIntegrationAlert = HookOrDefault({
  28. hookName: 'component:first-party-integration-alert',
  29. defaultComponent: () => null,
  30. });
  31. const FirstPartyIntegrationAdditionalCTA = HookOrDefault({
  32. hookName: 'component:first-party-integration-additional-cta',
  33. defaultComponent: () => null,
  34. });
  35. type State = {
  36. configurations: Integration[];
  37. information: {providers: IntegrationProvider[]};
  38. };
  39. class IntegrationDetailedView extends AbstractIntegrationDetailedView<
  40. AbstractIntegrationDetailedView['props'],
  41. State & AbstractIntegrationDetailedView['state']
  42. > {
  43. tabs: Tab[] = ['overview', 'configurations', 'features'];
  44. getEndpoints(): ReturnType<DeprecatedAsyncComponent['getEndpoints']> {
  45. const {organization} = this.props;
  46. const {integrationSlug} = this.props.params;
  47. return [
  48. [
  49. 'information',
  50. `/organizations/${organization.slug}/config/integrations/?provider_key=${integrationSlug}`,
  51. ],
  52. [
  53. 'configurations',
  54. `/organizations/${organization.slug}/integrations/?provider_key=${integrationSlug}&includeConfig=0`,
  55. ],
  56. ];
  57. }
  58. get integrationType() {
  59. return 'first_party' as const;
  60. }
  61. get provider() {
  62. return this.state.information.providers[0];
  63. }
  64. get description() {
  65. return this.metadata.description;
  66. }
  67. get author() {
  68. return this.metadata.author;
  69. }
  70. get alerts() {
  71. const provider = this.provider;
  72. const metadata = this.metadata;
  73. // The server response for integration installations includes old icon CSS classes
  74. // We map those to the currently in use values to their react equivalents
  75. // and fallback to IconFlag just in case.
  76. const alerts = (metadata.aspects.alerts || []).map(item => ({
  77. ...item,
  78. showIcon: true,
  79. }));
  80. if (!provider.canAdd && metadata.aspects.externalInstall) {
  81. alerts.push({
  82. type: 'warning',
  83. showIcon: true,
  84. text: metadata.aspects.externalInstall.noticeText,
  85. });
  86. }
  87. return alerts;
  88. }
  89. get resourceLinks() {
  90. const metadata = this.metadata;
  91. return [
  92. {url: metadata.source_url, title: 'View Source'},
  93. {url: metadata.issue_url, title: 'Report Issue'},
  94. ];
  95. }
  96. get metadata() {
  97. return this.provider.metadata;
  98. }
  99. get isEnabled() {
  100. return this.state.configurations.length > 0;
  101. }
  102. get installationStatus() {
  103. // TODO: add transations
  104. const {configurations} = this.state;
  105. const statusList = configurations.map(getIntegrationStatus);
  106. // if we have conflicting statuses, we have a priority order
  107. if (statusList.includes('active')) {
  108. return 'Installed';
  109. }
  110. if (statusList.includes('disabled')) {
  111. return 'Disabled';
  112. }
  113. if (statusList.includes('pending_deletion')) {
  114. return 'Pending Deletion';
  115. }
  116. return 'Not Installed';
  117. }
  118. get integrationName() {
  119. return this.provider.name;
  120. }
  121. get featureData() {
  122. return this.metadata.features;
  123. }
  124. renderTabs() {
  125. // TODO: Convert to styled component
  126. const tabs = integrationFeatures.includes(this.provider.key)
  127. ? this.tabs
  128. : this.tabs.filter(tab => tab !== 'features');
  129. return (
  130. <ul className="nav nav-tabs border-bottom" style={{paddingTop: '30px'}}>
  131. {tabs.map(tabName => (
  132. <li
  133. key={tabName}
  134. className={this.state.tab === tabName ? 'active' : ''}
  135. onClick={() => this.onTabChange(tabName)}
  136. >
  137. <CapitalizedLink>{this.getTabDisplay(tabName)}</CapitalizedLink>
  138. </li>
  139. ))}
  140. </ul>
  141. );
  142. }
  143. onInstall = (integration: Integration) => {
  144. // send the user to the configure integration view for that integration
  145. const {organization} = this.props;
  146. this.props.router.push(
  147. normalizeUrl(
  148. `/settings/${organization.slug}/integrations/${integration.provider.key}/${integration.id}/`
  149. )
  150. );
  151. };
  152. onRemove = (integration: Integration) => {
  153. const {organization} = this.props;
  154. const origIntegrations = [...this.state.configurations];
  155. const integrations = this.state.configurations.map(i =>
  156. i.id === integration.id
  157. ? {...i, organizationIntegrationStatus: 'pending_deletion' as ObjectStatus}
  158. : i
  159. );
  160. this.setState({configurations: integrations});
  161. const options: RequestOptions = {
  162. method: 'DELETE',
  163. error: () => {
  164. this.setState({configurations: origIntegrations});
  165. addErrorMessage(t('Failed to remove Integration'));
  166. },
  167. };
  168. this.api.request(
  169. `/organizations/${organization.slug}/integrations/${integration.id}/`,
  170. options
  171. );
  172. };
  173. onDisable = (integration: Integration) => {
  174. let url: string;
  175. if (!integration.domainName) {
  176. return;
  177. }
  178. const [domainName, orgName] = integration.domainName.split('/');
  179. if (integration.accountType === 'User') {
  180. url = `https://${domainName}/settings/installations/`;
  181. } else {
  182. url = `https://${domainName}/organizations/${orgName}/settings/installations/`;
  183. }
  184. window.open(url, '_blank');
  185. };
  186. handleExternalInstall = () => {
  187. this.trackIntegrationAnalytics('integrations.installation_start');
  188. };
  189. renderAlert() {
  190. return (
  191. <FirstPartyIntegrationAlert
  192. integrations={this.state.configurations ?? []}
  193. hideCTA
  194. />
  195. );
  196. }
  197. renderAdditionalCTA() {
  198. return (
  199. <FirstPartyIntegrationAdditionalCTA
  200. integrations={this.state.configurations ?? []}
  201. />
  202. );
  203. }
  204. renderTopButton(disabledFromFeatures: boolean, userHasAccess: boolean) {
  205. const provider = this.provider;
  206. const buttonProps = {
  207. size: 'sm',
  208. priority: 'primary',
  209. 'data-test-id': 'install-button',
  210. disabled: disabledFromFeatures,
  211. };
  212. return (
  213. <IntegrationContext.Provider
  214. value={{
  215. provider: provider,
  216. type: this.integrationType,
  217. installStatus: this.installationStatus,
  218. analyticsParams: {
  219. view: 'integrations_directory_integration_detail',
  220. already_installed: this.installationStatus !== 'Not Installed',
  221. },
  222. }}
  223. >
  224. <StyledIntegrationButton
  225. userHasAccess={userHasAccess}
  226. onAddIntegration={this.onInstall}
  227. onExternalClick={this.handleExternalInstall}
  228. buttonProps={buttonProps}
  229. />
  230. </IntegrationContext.Provider>
  231. );
  232. }
  233. renderConfigurations() {
  234. const {configurations} = this.state;
  235. const {organization} = this.props;
  236. const provider = this.provider;
  237. if (!configurations.length) {
  238. return this.renderEmptyConfigurations();
  239. }
  240. const alertText = getAlertText(configurations);
  241. return (
  242. <Fragment>
  243. {alertText && (
  244. <Alert type="warning" showIcon>
  245. {alertText}
  246. </Alert>
  247. )}
  248. <Panel>
  249. {configurations.map(integration => (
  250. <PanelItem key={integration.id}>
  251. <InstalledIntegration
  252. organization={organization}
  253. provider={provider}
  254. integration={integration}
  255. onRemove={this.onRemove}
  256. onDisable={this.onDisable}
  257. data-test-id={integration.id}
  258. trackIntegrationAnalytics={this.trackIntegrationAnalytics}
  259. requiresUpgrade={!!alertText}
  260. />
  261. </PanelItem>
  262. ))}
  263. </Panel>
  264. </Fragment>
  265. );
  266. }
  267. getSlackFeatures(): [JsonFormObject[], Data] {
  268. const {configurations} = this.state;
  269. const {organization} = this.props;
  270. const hasIntegration = configurations ? configurations.length > 0 : false;
  271. const forms: JsonFormObject[] = [
  272. {
  273. fields: [
  274. {
  275. name: 'issueAlertsThreadFlag',
  276. type: 'boolean',
  277. label: t('Enable Slack threads on Issue Alerts'),
  278. help: t(
  279. 'Allow Slack integration to post replies in threads for an Issue Alert notification.'
  280. ),
  281. disabled: !hasIntegration,
  282. disabledReason: t(
  283. 'You must have a Slack integration to enable this feature.'
  284. ),
  285. },
  286. {
  287. name: 'metricAlertsThreadFlag',
  288. type: 'boolean',
  289. label: t('Enable Slack threads on Metric Alerts'),
  290. help: t(
  291. 'Allow Slack integration to post replies in threads for an Metric Alert notification.'
  292. ),
  293. disabled: !hasIntegration,
  294. disabledReason: t(
  295. 'You must have a Slack integration to enable this feature.'
  296. ),
  297. },
  298. ],
  299. },
  300. ];
  301. const initialData = {
  302. issueAlertsThreadFlag: organization.issueAlertsThreadFlag,
  303. metricAlertsThreadFlag: organization.metricAlertsThreadFlag,
  304. };
  305. return [forms, initialData];
  306. }
  307. getGithubFeatures(): [JsonFormObject[], Data] {
  308. const {configurations} = this.state;
  309. const {organization} = this.props;
  310. const hasIntegration = configurations ? configurations.length > 0 : false;
  311. const forms: JsonFormObject[] = [
  312. {
  313. fields: [
  314. {
  315. name: 'githubPRBot',
  316. type: 'boolean',
  317. label: t('Enable Comments on Suspect Pull Requests'),
  318. help: t(
  319. 'Allow Sentry to comment on recent pull requests suspected of causing issues.'
  320. ),
  321. disabled: !hasIntegration,
  322. disabledReason: t(
  323. 'You must have a GitHub integration to enable this feature.'
  324. ),
  325. },
  326. {
  327. name: 'githubOpenPRBot',
  328. type: 'boolean',
  329. label: t('Enable Comments on Open Pull Requests'),
  330. help: t(
  331. 'Allow Sentry to comment on open pull requests to show recent error issues for the code being changed.'
  332. ),
  333. disabled: !hasIntegration,
  334. disabledReason: t(
  335. 'You must have a GitHub integration to enable this feature.'
  336. ),
  337. },
  338. {
  339. name: 'githubNudgeInvite',
  340. type: 'boolean',
  341. label: t('Enable Missing Member Detection'),
  342. help: t(
  343. 'Allow Sentry to detect users committing to your GitHub repositories that are not part of your Sentry organization..'
  344. ),
  345. disabled: !hasIntegration,
  346. disabledReason: t(
  347. 'You must have a GitHub integration to enable this feature.'
  348. ),
  349. },
  350. ],
  351. },
  352. ];
  353. const initialData = {
  354. githubPRBot: organization.githubPRBot,
  355. githubOpenPRBot: organization.githubOpenPRBot,
  356. githubNudgeInvite: organization.githubNudgeInvite,
  357. };
  358. return [forms, initialData];
  359. }
  360. renderFeatures() {
  361. const {organization} = this.props;
  362. const endpoint = `/organizations/${organization.slug}/`;
  363. const hasOrgWrite = organization.access.includes('org:write');
  364. let forms: JsonFormObject[], initialData: Data;
  365. switch (this.provider.key) {
  366. case 'github': {
  367. [forms, initialData] = this.getGithubFeatures();
  368. break;
  369. }
  370. case 'slack': {
  371. [forms, initialData] = this.getSlackFeatures();
  372. break;
  373. }
  374. default:
  375. return null;
  376. }
  377. return (
  378. <Form
  379. apiMethod="PUT"
  380. apiEndpoint={endpoint}
  381. saveOnBlur
  382. allowUndo
  383. initialData={initialData}
  384. onSubmitError={() => addErrorMessage('Unable to save change')}
  385. >
  386. <JsonForm
  387. disabled={!hasOrgWrite}
  388. features={organization.features}
  389. forms={forms}
  390. />
  391. </Form>
  392. );
  393. }
  394. renderBody() {
  395. return (
  396. <Fragment>
  397. <BreadcrumbTitle routes={this.props.routes} title={this.integrationName} />
  398. {this.renderAlert()}
  399. {this.renderTopSection()}
  400. {this.renderTabs()}
  401. {this.state.tab === 'overview'
  402. ? this.renderInformationCard()
  403. : this.state.tab === 'configurations'
  404. ? this.renderConfigurations()
  405. : this.renderFeatures()}
  406. </Fragment>
  407. );
  408. }
  409. }
  410. export default withOrganization(IntegrationDetailedView);
  411. const CapitalizedLink = styled('a')`
  412. text-transform: capitalize;
  413. `;
  414. const StyledIntegrationButton = styled(IntegrationButton)`
  415. margin-bottom: ${space(1)};
  416. `;