-
-
Notifications
You must be signed in to change notification settings - Fork 4k
/
organization.tsx
96 lines (86 loc) · 3.06 KB
/
organization.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import * as Sentry from '@sentry/react';
import {Client} from 'app/api';
import {addErrorMessage} from 'app/actionCreators/indicator';
import {setActiveOrganization} from 'app/actionCreators/organizations';
import GlobalSelectionActions from 'app/actions/globalSelectionActions';
import OrganizationActions from 'app/actions/organizationActions';
import ProjectActions from 'app/actions/projectActions';
import ProjectsStore from 'app/stores/projectsStore';
import TeamActions from 'app/actions/teamActions';
import TeamStore from 'app/stores/teamStore';
/**
* Fetches an organization's details with an option for the detailed representation
* with teams and projects
*
* @param api A reference to the api client
* @param slug The organization slug
* @param detailed Whether or not the detailed org details should be retrieved
* @param silent Should we silently update the organization (do not clear the
* current organization in the store)
*/
export async function fetchOrganizationDetails(
api: Client,
slug: string,
detailed: boolean,
silent: boolean
) {
if (!silent) {
OrganizationActions.fetchOrg();
ProjectActions.reset();
GlobalSelectionActions.reset();
}
try {
const org = await api.requestPromise(`/organizations/${slug}/`, {
query: {detailed: detailed ? 1 : 0},
});
if (!org) {
OrganizationActions.fetchOrgError(new Error('retrieved organization is falsey'));
return;
}
OrganizationActions.update(org, {replace: true});
setActiveOrganization(org);
if (detailed) {
// TODO(davidenwang): Change these to actions after organization.projects
// and organization.teams no longer exists. Currently if they were changed
// to actions it would cause OrganizationContext to rerender many times
TeamStore.loadInitialData(org.teams);
ProjectsStore.loadInitialData(org.projects);
} else {
// create a new client so the request is not cancelled
const uncancelableApi = new Client();
try {
const [projects, teams] = await Promise.all([
uncancelableApi.requestPromise(`/organizations/${slug}/projects/`, {
query: {
all_projects: 1,
},
}),
uncancelableApi.requestPromise(`/organizations/${slug}/teams/`),
]);
ProjectActions.loadProjects(projects);
TeamActions.loadTeams(teams);
} catch (err) {
// It's possible these requests fail with a 403 if the user has a role with insufficient access
// to projects and teams, but *can* access org details (e.g. billing).
// An example of this is in org settings.
//
// Ignore 403s and bubble up other API errors
if (err.status !== 403) {
throw err;
}
}
}
} catch (err) {
if (!err) {
return;
}
OrganizationActions.fetchOrgError(err);
if (err.status === 403 || err.status === 401) {
if (err.responseJSON?.detail) {
addErrorMessage(err.responseJSON.detail);
}
return;
}
Sentry.captureException(err);
}
}