index.tsx 10.0 KB

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