organization.tsx 4.9 KB

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