sentryAppExternalInstallation.tsx 9.7 KB

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