integrationDetailedView.tsx 12 KB

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