integrationDetailedView.tsx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  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 {organization} = this.props;
  206. const provider = this.provider;
  207. const buttonProps = {
  208. size: 'sm',
  209. priority: 'primary',
  210. 'data-test-id': 'install-button',
  211. disabled: disabledFromFeatures,
  212. };
  213. return (
  214. <IntegrationContext.Provider
  215. value={{
  216. provider: provider,
  217. type: this.integrationType,
  218. installStatus: this.installationStatus,
  219. analyticsParams: {
  220. view: 'integrations_directory_integration_detail',
  221. already_installed: this.installationStatus !== 'Not Installed',
  222. },
  223. }}
  224. >
  225. <StyledIntegrationButton
  226. organization={organization}
  227. userHasAccess={userHasAccess}
  228. onAddIntegration={this.onInstall}
  229. onExternalClick={this.handleExternalInstall}
  230. buttonProps={buttonProps}
  231. />
  232. </IntegrationContext.Provider>
  233. );
  234. }
  235. renderConfigurations() {
  236. const {configurations} = this.state;
  237. const {organization} = this.props;
  238. const provider = this.provider;
  239. if (!configurations.length) {
  240. return this.renderEmptyConfigurations();
  241. }
  242. const alertText = getAlertText(configurations);
  243. return (
  244. <Fragment>
  245. {alertText && (
  246. <Alert type="warning" showIcon>
  247. {alertText}
  248. </Alert>
  249. )}
  250. <Panel>
  251. {configurations.map(integration => (
  252. <PanelItem key={integration.id}>
  253. <InstalledIntegration
  254. organization={organization}
  255. provider={provider}
  256. integration={integration}
  257. onRemove={this.onRemove}
  258. onDisable={this.onDisable}
  259. data-test-id={integration.id}
  260. trackIntegrationAnalytics={this.trackIntegrationAnalytics}
  261. requiresUpgrade={!!alertText}
  262. />
  263. </PanelItem>
  264. ))}
  265. </Panel>
  266. </Fragment>
  267. );
  268. }
  269. getSlackFeatures(): [JsonFormObject[], Data] {
  270. const {configurations} = this.state;
  271. const {organization} = this.props;
  272. const hasIntegration = configurations ? configurations.length > 0 : false;
  273. const forms: JsonFormObject[] = [
  274. {
  275. fields: [
  276. {
  277. name: 'issueAlertsThreadFlag',
  278. type: 'boolean',
  279. label: t('Enable Slack threads on Issue Alerts'),
  280. help: t(
  281. 'Allow Slack integration to post replies in threads for an Issue Alert notification.'
  282. ),
  283. disabled: !hasIntegration,
  284. disabledReason: t(
  285. 'You must have a Slack integration to enable this feature.'
  286. ),
  287. },
  288. {
  289. name: 'metricAlertsThreadFlag',
  290. type: 'boolean',
  291. label: t('Enable Slack threads on Metric Alerts'),
  292. help: t(
  293. 'Allow Slack integration to post replies in threads for an Metric Alert notification.'
  294. ),
  295. disabled: !hasIntegration,
  296. disabledReason: t(
  297. 'You must have a Slack integration to enable this feature.'
  298. ),
  299. },
  300. ],
  301. },
  302. ];
  303. const initialData = {
  304. issueAlertsThreadFlag: organization.issueAlertsThreadFlag,
  305. metricAlertsThreadFlag: organization.metricAlertsThreadFlag,
  306. };
  307. return [forms, initialData];
  308. }
  309. getGithubFeatures(): [JsonFormObject[], Data] {
  310. const {configurations} = this.state;
  311. const {organization} = this.props;
  312. const hasIntegration = configurations ? configurations.length > 0 : false;
  313. const forms: JsonFormObject[] = [
  314. {
  315. fields: [
  316. {
  317. name: 'githubPRBot',
  318. type: 'boolean',
  319. label: t('Enable Comments on Suspect Pull Requests'),
  320. help: t(
  321. 'Allow Sentry to comment on recent pull requests suspected of causing issues.'
  322. ),
  323. disabled: !hasIntegration,
  324. disabledReason: t(
  325. 'You must have a GitHub integration to enable this feature.'
  326. ),
  327. },
  328. {
  329. name: 'githubOpenPRBot',
  330. type: 'boolean',
  331. label: t('Enable Comments on Open Pull Requests'),
  332. help: t(
  333. 'Allow Sentry to comment on open pull requests to show recent error issues for the code being changed.'
  334. ),
  335. disabled: !hasIntegration,
  336. disabledReason: t(
  337. 'You must have a GitHub integration to enable this feature.'
  338. ),
  339. },
  340. {
  341. name: 'githubNudgeInvite',
  342. type: 'boolean',
  343. label: t('Enable Missing Member Detection'),
  344. help: t(
  345. 'Allow Sentry to detect users committing to your GitHub repositories that are not part of your Sentry organization..'
  346. ),
  347. disabled: !hasIntegration,
  348. disabledReason: t(
  349. 'You must have a GitHub integration to enable this feature.'
  350. ),
  351. },
  352. ],
  353. },
  354. ];
  355. const initialData = {
  356. githubPRBot: organization.githubPRBot,
  357. githubOpenPRBot: organization.githubOpenPRBot,
  358. githubNudgeInvite: organization.githubNudgeInvite,
  359. };
  360. return [forms, initialData];
  361. }
  362. renderFeatures() {
  363. const {organization} = this.props;
  364. const endpoint = `/organizations/${organization.slug}/`;
  365. const hasOrgWrite = organization.access.includes('org:write');
  366. let forms: JsonFormObject[], initialData: Data;
  367. switch (this.provider.key) {
  368. case 'github': {
  369. [forms, initialData] = this.getGithubFeatures();
  370. break;
  371. }
  372. case 'slack': {
  373. [forms, initialData] = this.getSlackFeatures();
  374. break;
  375. }
  376. default:
  377. return null;
  378. }
  379. return (
  380. <Form
  381. apiMethod="PUT"
  382. apiEndpoint={endpoint}
  383. saveOnBlur
  384. allowUndo
  385. initialData={initialData}
  386. onSubmitError={() => addErrorMessage('Unable to save change')}
  387. >
  388. <JsonForm
  389. disabled={!hasOrgWrite}
  390. features={organization.features}
  391. forms={forms}
  392. />
  393. </Form>
  394. );
  395. }
  396. renderBody() {
  397. return (
  398. <Fragment>
  399. <BreadcrumbTitle routes={this.props.routes} title={this.integrationName} />
  400. {this.renderAlert()}
  401. {this.renderTopSection()}
  402. {this.renderTabs()}
  403. {this.state.tab === 'overview'
  404. ? this.renderInformationCard()
  405. : this.state.tab === 'configurations'
  406. ? this.renderConfigurations()
  407. : this.renderFeatures()}
  408. </Fragment>
  409. );
  410. }
  411. }
  412. export default withOrganization(IntegrationDetailedView);
  413. const CapitalizedLink = styled('a')`
  414. text-transform: capitalize;
  415. `;
  416. const StyledIntegrationButton = styled(IntegrationButton)`
  417. margin-bottom: ${space(1)};
  418. `;