index.tsx 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. import {RouteComponentProps} from 'react-router';
  2. import styled from '@emotion/styled';
  3. import {addErrorMessage} from 'sentry/actionCreators/indicator';
  4. import {installSentryApp} from 'sentry/actionCreators/sentryAppInstallations';
  5. import {Alert} from 'sentry/components/alert';
  6. import OrganizationAvatar from 'sentry/components/avatar/organizationAvatar';
  7. import SelectControl from 'sentry/components/forms/controls/selectControl';
  8. import FieldGroup from 'sentry/components/forms/fieldGroup';
  9. import SentryAppDetailsModal from 'sentry/components/modals/sentryAppDetailsModal';
  10. import NarrowLayout from 'sentry/components/narrowLayout';
  11. import {t, tct} from 'sentry/locale';
  12. import {Organization, SentryApp, SentryAppInstallation} from 'sentry/types';
  13. import {trackIntegrationAnalytics} from 'sentry/utils/integrationUtil';
  14. import {addQueryParamsToExistingUrl} from 'sentry/utils/queryString';
  15. import AsyncView from 'sentry/views/asyncView';
  16. import {OrganizationContext} from '../organizationContext';
  17. type Props = RouteComponentProps<{sentryAppSlug: string}, {}>;
  18. type State = AsyncView['state'] & {
  19. organization: Organization | null;
  20. organizations: Organization[];
  21. reloading: boolean;
  22. selectedOrgSlug: string | null;
  23. sentryApp: SentryApp;
  24. };
  25. export default class SentryAppExternalInstallation extends AsyncView<Props, State> {
  26. disableErrorReport = false;
  27. getDefaultState() {
  28. const state = super.getDefaultState();
  29. return {
  30. ...state,
  31. selectedOrgSlug: null,
  32. organization: null,
  33. organizations: [],
  34. reloading: false,
  35. };
  36. }
  37. getEndpoints(): ReturnType<AsyncView['getEndpoints']> {
  38. return [
  39. ['organizations', '/organizations/'],
  40. ['sentryApp', `/sentry-apps/${this.sentryAppSlug}/`],
  41. ];
  42. }
  43. getTitle() {
  44. return t('Choose Installation Organization');
  45. }
  46. get sentryAppSlug() {
  47. return this.props.params.sentryAppSlug;
  48. }
  49. get isSingleOrg() {
  50. return this.state.organizations.length === 1;
  51. }
  52. get isSentryAppInternal() {
  53. const {sentryApp} = this.state;
  54. return sentryApp && sentryApp.status === 'internal';
  55. }
  56. get isSentryAppUnavailableForOrg() {
  57. const {sentryApp, selectedOrgSlug} = this.state;
  58. // if the app is unpublished for a different org
  59. return (
  60. selectedOrgSlug &&
  61. sentryApp?.owner?.slug !== selectedOrgSlug &&
  62. sentryApp.status === 'unpublished'
  63. );
  64. }
  65. get disableInstall() {
  66. const {reloading, isInstalled} = this.state;
  67. return isInstalled || reloading || this.isSentryAppUnavailableForOrg;
  68. }
  69. hasAccess = (org: Organization) => org.access.includes('org:integrations');
  70. onClose = () => {
  71. // if we came from somewhere, go back there. Otherwise, back to the integrations page
  72. const {selectedOrgSlug} = this.state;
  73. const newUrl = document.referrer || `/settings/${selectedOrgSlug}/integrations/`;
  74. window.location.assign(newUrl);
  75. };
  76. onInstall = async (): Promise<any | undefined> => {
  77. const {organization, sentryApp} = this.state;
  78. if (!organization || !sentryApp) {
  79. return undefined;
  80. }
  81. trackIntegrationAnalytics('integrations.installation_start', {
  82. integration_type: 'sentry_app',
  83. integration: sentryApp.slug,
  84. view: 'external_install',
  85. integration_status: sentryApp.status,
  86. organization,
  87. });
  88. const install = await installSentryApp(this.api, organization.slug, sentryApp);
  89. // installation is complete if the status is installed
  90. if (install.status === 'installed') {
  91. trackIntegrationAnalytics('integrations.installation_complete', {
  92. integration_type: 'sentry_app',
  93. integration: sentryApp.slug,
  94. view: 'external_install',
  95. integration_status: sentryApp.status,
  96. organization,
  97. });
  98. }
  99. if (sentryApp.redirectUrl) {
  100. const queryParams = {
  101. installationId: install.uuid,
  102. code: install.code,
  103. orgSlug: organization.slug,
  104. };
  105. const redirectUrl = addQueryParamsToExistingUrl(sentryApp.redirectUrl, queryParams);
  106. return window.location.assign(redirectUrl);
  107. }
  108. return this.onClose();
  109. };
  110. onSelectOrg = async (orgSlug: string) => {
  111. this.setState({selectedOrgSlug: orgSlug, reloading: true});
  112. try {
  113. const [organization, installations]: [Organization, SentryAppInstallation[]] =
  114. await Promise.all([
  115. this.api.requestPromise(`/organizations/${orgSlug}/`),
  116. this.api.requestPromise(`/organizations/${orgSlug}/sentry-app-installations/`),
  117. ]);
  118. const isInstalled = installations
  119. .map(install => install.app.slug)
  120. .includes(this.sentryAppSlug);
  121. // all state fields should be set at the same time so analytics in SentryAppDetailsModal works properly
  122. this.setState({organization, isInstalled, reloading: false});
  123. } catch (err) {
  124. addErrorMessage(t('Failed to retrieve organization or integration details'));
  125. this.setState({reloading: false});
  126. }
  127. };
  128. onRequestSuccess = ({stateKey, data}) => {
  129. // if only one org, we can immediately update our selected org
  130. if (stateKey === 'organizations' && data.length === 1) {
  131. this.onSelectOrg(data[0].slug);
  132. }
  133. };
  134. getOptions() {
  135. return this.state.organizations.map(org => ({
  136. value: org.slug,
  137. label: (
  138. <div key={org.slug}>
  139. <OrganizationAvatar organization={org} />
  140. <OrgNameHolder>{org.slug}</OrgNameHolder>
  141. </div>
  142. ),
  143. }));
  144. }
  145. renderInternalAppError() {
  146. const {sentryApp} = this.state;
  147. return (
  148. <Alert type="error" showIcon>
  149. {tct(
  150. 'Integration [sentryAppName] is an internal integration. Internal integrations are automatically installed',
  151. {
  152. sentryAppName: <strong>{sentryApp.name}</strong>,
  153. }
  154. )}
  155. </Alert>
  156. );
  157. }
  158. checkAndRenderError() {
  159. const {organization, selectedOrgSlug, isInstalled, sentryApp} = this.state;
  160. if (selectedOrgSlug && organization && !this.hasAccess(organization)) {
  161. return (
  162. <Alert type="error" showIcon>
  163. <p>
  164. {tct(
  165. `You do not have permission to install integrations in
  166. [organization]. Ask an organization owner or manager to
  167. visit this page to finish installing this integration.`,
  168. {organization: <strong>{organization.slug}</strong>}
  169. )}
  170. </p>
  171. <InstallLink>{window.location.href}</InstallLink>
  172. </Alert>
  173. );
  174. }
  175. if (isInstalled && organization) {
  176. return (
  177. <Alert type="error" showIcon>
  178. {tct('Integration [sentryAppName] already installed for [organization]', {
  179. organization: <strong>{organization.name}</strong>,
  180. sentryAppName: <strong>{sentryApp.name}</strong>,
  181. })}
  182. </Alert>
  183. );
  184. }
  185. if (this.isSentryAppUnavailableForOrg) {
  186. // use the slug of the owner if we have it, otherwise use 'another organization'
  187. const ownerSlug = sentryApp?.owner?.slug ?? 'another organization';
  188. return (
  189. <Alert type="error" showIcon>
  190. {tct(
  191. 'Integration [sentryAppName] is an unpublished integration for [otherOrg]. An unpublished integration can only be installed on the organization which created it.',
  192. {
  193. sentryAppName: <strong>{sentryApp.name}</strong>,
  194. otherOrg: <strong>{ownerSlug}</strong>,
  195. }
  196. )}
  197. </Alert>
  198. );
  199. }
  200. return null;
  201. }
  202. renderMultiOrgView() {
  203. const {selectedOrgSlug, sentryApp} = this.state;
  204. return (
  205. <div>
  206. <p>
  207. {tct(
  208. 'Please pick a specific [organization:organization] to install [sentryAppName]',
  209. {
  210. organization: <strong />,
  211. sentryAppName: <strong>{sentryApp.name}</strong>,
  212. }
  213. )}
  214. </p>
  215. <FieldGroup label={t('Organization')} inline={false} stacked required>
  216. {() => (
  217. <SelectControl
  218. onChange={({value}) => this.onSelectOrg(value)}
  219. value={selectedOrgSlug}
  220. placeholder={t('Select an organization')}
  221. options={this.getOptions()}
  222. />
  223. )}
  224. </FieldGroup>
  225. </div>
  226. );
  227. }
  228. renderSingleOrgView() {
  229. const {organizations, sentryApp} = this.state;
  230. // pull the name out of organizations since state.organization won't be loaded initially
  231. const organizationName = organizations[0].name;
  232. return (
  233. <div>
  234. <p>
  235. {tct('You are installing [sentryAppName] for organization [organization]', {
  236. organization: <strong>{organizationName}</strong>,
  237. sentryAppName: <strong>{sentryApp.name}</strong>,
  238. })}
  239. </p>
  240. </div>
  241. );
  242. }
  243. renderMainContent() {
  244. const {organization, sentryApp} = this.state;
  245. return (
  246. <div>
  247. <OrgViewHolder>
  248. {this.isSingleOrg ? this.renderSingleOrgView() : this.renderMultiOrgView()}
  249. </OrgViewHolder>
  250. {this.checkAndRenderError()}
  251. {organization && (
  252. <OrganizationContext.Provider value={organization}>
  253. <SentryAppDetailsModal
  254. sentryApp={sentryApp}
  255. organization={organization}
  256. onInstall={this.onInstall}
  257. closeModal={this.onClose}
  258. isInstalled={this.disableInstall}
  259. />
  260. </OrganizationContext.Provider>
  261. )}
  262. </div>
  263. );
  264. }
  265. renderBody() {
  266. return (
  267. <NarrowLayout>
  268. <Content>
  269. <h3>{t('Finish integration installation')}</h3>
  270. {this.isSentryAppInternal
  271. ? this.renderInternalAppError()
  272. : this.renderMainContent()}
  273. </Content>
  274. </NarrowLayout>
  275. );
  276. }
  277. }
  278. const InstallLink = styled('pre')`
  279. margin-bottom: 0;
  280. background: #fbe3e1;
  281. `;
  282. const OrgNameHolder = styled('span')`
  283. margin-left: 5px;
  284. `;
  285. const Content = styled('div')`
  286. margin-bottom: 40px;
  287. `;
  288. const OrgViewHolder = styled('div')`
  289. margin-bottom: 20px;
  290. `;