integrationDetailedView.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. import {Fragment} from 'react';
  2. import styled from '@emotion/styled';
  3. import {addErrorMessage} from 'sentry/actionCreators/indicator';
  4. import {RequestOptions} from 'sentry/api';
  5. import {Alert} from 'sentry/components/alert';
  6. import {Button} from 'sentry/components/button';
  7. import DeprecatedAsyncComponent from 'sentry/components/deprecatedAsyncComponent';
  8. import Form from 'sentry/components/forms/form';
  9. import JsonForm from 'sentry/components/forms/jsonForm';
  10. import {JsonFormObject} from 'sentry/components/forms/types';
  11. import HookOrDefault from 'sentry/components/hookOrDefault';
  12. import Panel from 'sentry/components/panels/panel';
  13. import PanelItem from 'sentry/components/panels/panelItem';
  14. import {IconOpen} from 'sentry/icons';
  15. import {t} from 'sentry/locale';
  16. import {space} from 'sentry/styles/space';
  17. import {Integration, IntegrationProvider, ObjectStatus} from 'sentry/types';
  18. import {getAlertText, getIntegrationStatus} from 'sentry/utils/integrationUtil';
  19. import {normalizeUrl} from 'sentry/utils/withDomainRequired';
  20. import withOrganization from 'sentry/utils/withOrganization';
  21. import BreadcrumbTitle from 'sentry/views/settings/components/settingsBreadcrumb/breadcrumbTitle';
  22. import AbstractIntegrationDetailedView, {Tab} from './abstractIntegrationDetailedView';
  23. import {AddIntegrationButton} from './addIntegrationButton';
  24. import InstalledIntegration from './installedIntegration';
  25. // Show the features tab if the org has features for the integration
  26. const integrationFeatures = {github: ['pr-comment-bot']};
  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 {organization} = this.props;
  127. // TODO(cathy): remove feature check
  128. const tabs =
  129. this.provider.key in integrationFeatures &&
  130. organization.features.filter(value =>
  131. integrationFeatures[this.provider.key].includes(value)
  132. )
  133. ? this.tabs
  134. : this.tabs.filter(tab => tab !== 'features');
  135. return (
  136. <ul className="nav nav-tabs border-bottom" style={{paddingTop: '30px'}}>
  137. {tabs.map(tabName => (
  138. <li
  139. key={tabName}
  140. className={this.state.tab === tabName ? 'active' : ''}
  141. onClick={() => this.onTabChange(tabName)}
  142. >
  143. <CapitalizedLink>{this.getTabDisplay(tabName)}</CapitalizedLink>
  144. </li>
  145. ))}
  146. </ul>
  147. );
  148. }
  149. onInstall = (integration: Integration) => {
  150. // send the user to the configure integration view for that integration
  151. const {organization} = this.props;
  152. this.props.router.push(
  153. normalizeUrl(
  154. `/settings/${organization.slug}/integrations/${integration.provider.key}/${integration.id}/`
  155. )
  156. );
  157. };
  158. onRemove = (integration: Integration) => {
  159. const {organization} = this.props;
  160. const origIntegrations = [...this.state.configurations];
  161. const integrations = this.state.configurations.map(i =>
  162. i.id === integration.id
  163. ? {...i, organizationIntegrationStatus: 'pending_deletion' as ObjectStatus}
  164. : i
  165. );
  166. this.setState({configurations: integrations});
  167. const options: RequestOptions = {
  168. method: 'DELETE',
  169. error: () => {
  170. this.setState({configurations: origIntegrations});
  171. addErrorMessage(t('Failed to remove Integration'));
  172. },
  173. };
  174. this.api.request(
  175. `/organizations/${organization.slug}/integrations/${integration.id}/`,
  176. options
  177. );
  178. };
  179. onDisable = (integration: Integration) => {
  180. let url: string;
  181. const [domainName, orgName] = integration.domainName.split('/');
  182. if (integration.accountType === 'User') {
  183. url = `https://${domainName}/settings/installations/`;
  184. } else {
  185. url = `https://${domainName}/organizations/${orgName}/settings/installations/`;
  186. }
  187. window.open(url, '_blank');
  188. };
  189. handleExternalInstall = () => {
  190. this.trackIntegrationAnalytics('integrations.installation_start');
  191. };
  192. renderAlert() {
  193. return (
  194. <FirstPartyIntegrationAlert
  195. integrations={this.state.configurations ?? []}
  196. hideCTA
  197. />
  198. );
  199. }
  200. renderAdditionalCTA() {
  201. return (
  202. <FirstPartyIntegrationAdditionalCTA
  203. integrations={this.state.configurations ?? []}
  204. />
  205. );
  206. }
  207. renderTopButton(disabledFromFeatures: boolean, userHasAccess: boolean) {
  208. const {organization} = this.props;
  209. const provider = this.provider;
  210. const {metadata} = provider;
  211. const size = 'sm' as const;
  212. const priority = 'primary' as const;
  213. const buttonProps = {
  214. style: {marginBottom: space(1)},
  215. size,
  216. priority,
  217. 'data-test-id': 'install-button',
  218. disabled: disabledFromFeatures,
  219. organization,
  220. };
  221. if (!userHasAccess) {
  222. return this.renderRequestIntegrationButton();
  223. }
  224. if (provider.canAdd) {
  225. return (
  226. <AddIntegrationButton
  227. provider={provider}
  228. onAddIntegration={this.onInstall}
  229. analyticsParams={{
  230. view: 'integrations_directory_integration_detail',
  231. already_installed: this.installationStatus !== 'Not Installed',
  232. }}
  233. {...buttonProps}
  234. />
  235. );
  236. }
  237. if (metadata.aspects.externalInstall) {
  238. return (
  239. <Button
  240. icon={<IconOpen />}
  241. href={metadata.aspects.externalInstall.url}
  242. onClick={this.handleExternalInstall}
  243. external
  244. {...buttonProps}
  245. >
  246. {metadata.aspects.externalInstall.buttonText}
  247. </Button>
  248. );
  249. }
  250. // This should never happen but we can't return undefined without some refactoring.
  251. return <Fragment />;
  252. }
  253. renderConfigurations() {
  254. const {configurations} = this.state;
  255. const {organization} = this.props;
  256. const provider = this.provider;
  257. if (!configurations.length) {
  258. return this.renderEmptyConfigurations();
  259. }
  260. const alertText = getAlertText(configurations);
  261. return (
  262. <Fragment>
  263. {alertText && (
  264. <Alert type="warning" showIcon>
  265. {alertText}
  266. </Alert>
  267. )}
  268. <Panel>
  269. {configurations.map(integration => (
  270. <PanelItem key={integration.id}>
  271. <InstalledIntegration
  272. organization={organization}
  273. provider={provider}
  274. integration={integration}
  275. onRemove={this.onRemove}
  276. onDisable={this.onDisable}
  277. data-test-id={integration.id}
  278. trackIntegrationAnalytics={this.trackIntegrationAnalytics}
  279. requiresUpgrade={!!alertText}
  280. />
  281. </PanelItem>
  282. ))}
  283. </Panel>
  284. </Fragment>
  285. );
  286. }
  287. renderFeatures() {
  288. const {configurations} = this.state;
  289. const {organization} = this.props;
  290. const hasIntegration = configurations ? configurations.length > 0 : false;
  291. const endpoint = `/organizations/${organization.slug}/`;
  292. const hasOrgWrite = organization.access.includes('org:write');
  293. const forms: JsonFormObject[] = [
  294. {
  295. fields: [
  296. {
  297. name: 'githubPRBot',
  298. type: 'boolean',
  299. label: t('Enable Pull Request Bot'),
  300. visible: ({features}) => features.includes('pr-comment-bot'),
  301. help: t(
  302. 'Allow Sentry to comment on pull requests about issues impacting your app.'
  303. ),
  304. disabled: !hasIntegration,
  305. disabledReason: t(
  306. 'You must have a GitHub integration to enable this feature.'
  307. ),
  308. },
  309. ],
  310. },
  311. ];
  312. const initialData = {
  313. githubPRBot: organization.githubPRBot,
  314. };
  315. return (
  316. <Form
  317. apiMethod="PUT"
  318. apiEndpoint={endpoint}
  319. saveOnBlur
  320. allowUndo
  321. initialData={initialData}
  322. onSubmitError={() => addErrorMessage('Unable to save change')}
  323. >
  324. <JsonForm
  325. disabled={!hasOrgWrite}
  326. features={organization.features}
  327. forms={forms}
  328. />
  329. </Form>
  330. );
  331. }
  332. renderBody() {
  333. return (
  334. <Fragment>
  335. <BreadcrumbTitle routes={this.props.routes} title={this.integrationName} />
  336. {this.renderAlert()}
  337. {this.renderTopSection()}
  338. {this.renderTabs()}
  339. {this.state.tab === 'overview'
  340. ? this.renderInformationCard()
  341. : this.state.tab === 'configurations'
  342. ? this.renderConfigurations()
  343. : this.renderFeatures()}
  344. </Fragment>
  345. );
  346. }
  347. }
  348. export default withOrganization(IntegrationDetailedView);
  349. const CapitalizedLink = styled('a')`
  350. text-transform: capitalize;
  351. `;