organization.tsx 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. // XXX(epurkhiser): Ensure the LatestContextStore is initialized before we set
  2. // the active org. Otherwise we will trigger an action that does nothing
  3. import 'sentry/stores/latestContextStore';
  4. import * as Sentry from '@sentry/react';
  5. import {addErrorMessage} from 'sentry/actionCreators/indicator';
  6. import {setActiveOrganization} from 'sentry/actionCreators/organizations';
  7. import type {ResponseMeta} from 'sentry/api';
  8. import {Client} from 'sentry/api';
  9. import OrganizationStore from 'sentry/stores/organizationStore';
  10. import PageFiltersStore from 'sentry/stores/pageFiltersStore';
  11. import ProjectsStore from 'sentry/stores/projectsStore';
  12. import TeamStore from 'sentry/stores/teamStore';
  13. import type {Organization, Project, Team} from 'sentry/types';
  14. import {getPreloadedDataPromise} from 'sentry/utils/getPreloadedData';
  15. import parseLinkHeader from 'sentry/utils/parseLinkHeader';
  16. async function fetchOrg(
  17. api: Client,
  18. slug: string,
  19. isInitialFetch?: boolean
  20. ): Promise<Organization> {
  21. const [org] = await getPreloadedDataPromise(
  22. 'organization',
  23. slug,
  24. () =>
  25. // This data should get preloaded in static/sentry/index.ejs
  26. // If this url changes make sure to update the preload
  27. api.requestPromise(`/organizations/${slug}/`, {
  28. includeAllArgs: true,
  29. query: {detailed: 0},
  30. }),
  31. isInitialFetch
  32. );
  33. if (!org) {
  34. throw new Error('retrieved organization is falsey');
  35. }
  36. OrganizationStore.onUpdate(org, {replace: true});
  37. setActiveOrganization(org);
  38. Sentry.configureScope(scope => {
  39. // XXX(dcramer): this is duplicated in sdk.py on the backend
  40. scope.setTag('organization', org.id);
  41. scope.setTag('organization.slug', org.slug);
  42. scope.setContext('organization', {id: org.id, slug: org.slug});
  43. });
  44. return org;
  45. }
  46. async function fetchProjectsAndTeams(
  47. slug: string,
  48. isInitialFetch?: boolean
  49. ): Promise<
  50. [
  51. [Project[], string | undefined, XMLHttpRequest | ResponseMeta | undefined],
  52. [Team[], string | undefined, XMLHttpRequest | ResponseMeta | undefined],
  53. ]
  54. > {
  55. // Create a new client so the request is not cancelled
  56. const uncancelableApi = new Client();
  57. const projectsPromise = getPreloadedDataPromise(
  58. 'projects',
  59. slug,
  60. () =>
  61. // This data should get preloaded in static/sentry/index.ejs
  62. // If this url changes make sure to update the preload
  63. uncancelableApi.requestPromise(`/organizations/${slug}/projects/`, {
  64. includeAllArgs: true,
  65. query: {
  66. all_projects: 1,
  67. collapse: 'latestDeploys',
  68. },
  69. }),
  70. isInitialFetch
  71. );
  72. const teamsPromise = getPreloadedDataPromise(
  73. 'teams',
  74. slug,
  75. // This data should get preloaded in static/sentry/index.ejs
  76. // If this url changes make sure to update the preload
  77. () =>
  78. uncancelableApi.requestPromise(`/organizations/${slug}/teams/`, {
  79. includeAllArgs: true,
  80. }),
  81. isInitialFetch
  82. );
  83. try {
  84. return await Promise.all([projectsPromise, teamsPromise]);
  85. } catch (err) {
  86. // It's possible these requests fail with a 403 if the user has a role with
  87. // insufficient access to projects and teams, but *can* access org details
  88. // (e.g. billing). An example of this is in org settings.
  89. //
  90. // Ignore 403s and bubble up other API errors
  91. if (err.status !== 403) {
  92. throw err;
  93. }
  94. }
  95. return [
  96. [[], undefined, undefined],
  97. [[], undefined, undefined],
  98. ];
  99. }
  100. /**
  101. * Fetches an organization's details
  102. *
  103. * @param api A reference to the api client
  104. * @param slug The organization slug
  105. * @param silent Should we silently update the organization (do not clear the
  106. * current organization in the store)
  107. */
  108. export function fetchOrganizationDetails(
  109. api: Client,
  110. slug: string,
  111. silent: boolean,
  112. isInitialFetch?: boolean
  113. ) {
  114. if (!silent) {
  115. OrganizationStore.reset();
  116. ProjectsStore.reset();
  117. TeamStore.reset();
  118. PageFiltersStore.onReset();
  119. }
  120. const loadOrganization = async () => {
  121. try {
  122. await fetchOrg(api, slug, isInitialFetch);
  123. } catch (err) {
  124. if (!err) {
  125. return;
  126. }
  127. OrganizationStore.onFetchOrgError(err);
  128. if (err.status === 403 || err.status === 401) {
  129. const errMessage =
  130. typeof err.responseJSON?.detail === 'string'
  131. ? err.responseJSON?.detail
  132. : typeof err.responseJSON?.detail?.message === 'string'
  133. ? err.responseJSON?.detail.message
  134. : null;
  135. if (errMessage) {
  136. addErrorMessage(errMessage);
  137. }
  138. return;
  139. }
  140. Sentry.captureException(err);
  141. }
  142. };
  143. const loadTeamsAndProjects = async () => {
  144. const [[projects], [teams, , resp]] = await fetchProjectsAndTeams(
  145. slug,
  146. isInitialFetch
  147. );
  148. ProjectsStore.loadInitialData(projects ?? []);
  149. const teamPageLinks = resp?.getResponseHeader('Link');
  150. if (teamPageLinks) {
  151. const paginationObject = parseLinkHeader(teamPageLinks);
  152. const hasMore = paginationObject?.next?.results ?? false;
  153. const cursor = paginationObject.next?.cursor;
  154. TeamStore.loadInitialData(teams, hasMore, cursor);
  155. } else {
  156. TeamStore.loadInitialData(teams);
  157. }
  158. };
  159. return Promise.all([loadOrganization(), loadTeamsAndProjects()]);
  160. }