integrationDetailedView.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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'];
  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. const [domainName, orgName] = integration.domainName.split('/');
  176. if (integration.accountType === 'User') {
  177. url = `https://${domainName}/settings/installations/`;
  178. } else {
  179. url = `https://${domainName}/organizations/${orgName}/settings/installations/`;
  180. }
  181. window.open(url, '_blank');
  182. };
  183. handleExternalInstall = () => {
  184. this.trackIntegrationAnalytics('integrations.installation_start');
  185. };
  186. renderAlert() {
  187. return (
  188. <FirstPartyIntegrationAlert
  189. integrations={this.state.configurations ?? []}
  190. hideCTA
  191. />
  192. );
  193. }
  194. renderAdditionalCTA() {
  195. return (
  196. <FirstPartyIntegrationAdditionalCTA
  197. integrations={this.state.configurations ?? []}
  198. />
  199. );
  200. }
  201. renderTopButton(disabledFromFeatures: boolean, userHasAccess: boolean) {
  202. const {organization} = this.props;
  203. const provider = this.provider;
  204. const {metadata} = provider;
  205. const size = 'sm' as const;
  206. const priority = 'primary' as const;
  207. const buttonProps = {
  208. style: {marginBottom: space(1)},
  209. size,
  210. priority,
  211. 'data-test-id': 'install-button',
  212. disabled: disabledFromFeatures,
  213. organization,
  214. };
  215. if (!userHasAccess) {
  216. return this.renderRequestIntegrationButton();
  217. }
  218. if (provider.canAdd) {
  219. return (
  220. <AddIntegrationButton
  221. provider={provider}
  222. onAddIntegration={this.onInstall}
  223. installStatus={this.installationStatus}
  224. analyticsParams={{
  225. view: 'integrations_directory_integration_detail',
  226. already_installed: this.installationStatus !== 'Not Installed',
  227. }}
  228. {...buttonProps}
  229. />
  230. );
  231. }
  232. if (metadata.aspects.externalInstall) {
  233. return (
  234. <Button
  235. icon={<IconOpen />}
  236. href={metadata.aspects.externalInstall.url}
  237. onClick={this.handleExternalInstall}
  238. external
  239. {...buttonProps}
  240. >
  241. {metadata.aspects.externalInstall.buttonText}
  242. </Button>
  243. );
  244. }
  245. // This should never happen but we can't return undefined without some refactoring.
  246. return <Fragment />;
  247. }
  248. renderConfigurations() {
  249. const {configurations} = this.state;
  250. const {organization} = this.props;
  251. const provider = this.provider;
  252. if (!configurations.length) {
  253. return this.renderEmptyConfigurations();
  254. }
  255. const alertText = getAlertText(configurations);
  256. return (
  257. <Fragment>
  258. {alertText && (
  259. <Alert type="warning" showIcon>
  260. {alertText}
  261. </Alert>
  262. )}
  263. <Panel>
  264. {configurations.map(integration => (
  265. <PanelItem key={integration.id}>
  266. <InstalledIntegration
  267. organization={organization}
  268. provider={provider}
  269. integration={integration}
  270. onRemove={this.onRemove}
  271. onDisable={this.onDisable}
  272. data-test-id={integration.id}
  273. trackIntegrationAnalytics={this.trackIntegrationAnalytics}
  274. requiresUpgrade={!!alertText}
  275. />
  276. </PanelItem>
  277. ))}
  278. </Panel>
  279. </Fragment>
  280. );
  281. }
  282. renderFeatures() {
  283. const {configurations} = this.state;
  284. const {organization} = this.props;
  285. const hasIntegration = configurations ? configurations.length > 0 : false;
  286. const endpoint = `/organizations/${organization.slug}/`;
  287. const hasOrgWrite = organization.access.includes('org:write');
  288. const forms: JsonFormObject[] = [
  289. {
  290. fields: [
  291. {
  292. name: 'githubPRBot',
  293. type: 'boolean',
  294. label: t('Enable Comments on Suspect Pull Requests'),
  295. help: t(
  296. 'Allow Sentry to comment on recent pull requests suspected of causing issues.'
  297. ),
  298. disabled: !hasIntegration,
  299. disabledReason: t(
  300. 'You must have a GitHub integration to enable this feature.'
  301. ),
  302. },
  303. {
  304. name: 'githubOpenPRBot',
  305. type: 'boolean',
  306. label: t('Enable Comments on Open Pull Requests'),
  307. help: t(
  308. 'Allow Sentry to comment on open pull requests to show recent error and performance issues for the code being changed.'
  309. ),
  310. disabled: !hasIntegration,
  311. disabledReason: t(
  312. 'You must have a GitHub integration to enable this feature.'
  313. ),
  314. visible: ({features}) => features.includes('integrations-open-pr-comment'),
  315. },
  316. ],
  317. },
  318. ];
  319. const initialData = {
  320. githubPRBot: organization.githubPRBot,
  321. githubOpenPRBot: organization.githubOpenPRBot,
  322. };
  323. return (
  324. <Form
  325. apiMethod="PUT"
  326. apiEndpoint={endpoint}
  327. saveOnBlur
  328. allowUndo
  329. initialData={initialData}
  330. onSubmitError={() => addErrorMessage('Unable to save change')}
  331. >
  332. <JsonForm
  333. disabled={!hasOrgWrite}
  334. features={organization.features}
  335. forms={forms}
  336. />
  337. </Form>
  338. );
  339. }
  340. renderBody() {
  341. return (
  342. <Fragment>
  343. <BreadcrumbTitle routes={this.props.routes} title={this.integrationName} />
  344. {this.renderAlert()}
  345. {this.renderTopSection()}
  346. {this.renderTabs()}
  347. {this.state.tab === 'overview'
  348. ? this.renderInformationCard()
  349. : this.state.tab === 'configurations'
  350. ? this.renderConfigurations()
  351. : this.renderFeatures()}
  352. </Fragment>
  353. );
  354. }
  355. }
  356. export default withOrganization(IntegrationDetailedView);
  357. const CapitalizedLink = styled('a')`
  358. text-transform: capitalize;
  359. `;