-
Notifications
You must be signed in to change notification settings - Fork 2
/
store.ts
192 lines (176 loc) · 5.99 KB
/
store.ts
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import { create } from 'zustand';
import { CurrentUser, MinderContext, Project, StacklokProviders, User } from './types';
import { parseYaml } from './utils/general';
type GlobalState = {
minderContext: MinderContext;
currentProject: Project;
currentUser: CurrentUser;
projects: Project[];
projectRules: any[];
projectProfiles: any[];
remoteRepos: any[];
localRepos: any[];
errorBannerMessage?: string;
setErrorBannerMessage: (msg: string) => void;
setMinderContext: (projectID: string, access_token: string, provider: StacklokProviders) => void;
setProjects: (projects: Project[]) => void;
setCurrentUser: (user: CurrentUser) => void;
addToLocalRepos: (owner: string, repo_id: string, name: string) => void;
addRuleToControlPlane: (ruleData: string) => void;
addProfileToControlPlane: (ruleData: string) => void;
fetchData: (projectID: string, access_token: string) => void;
}
const initialCurrentProject: Project = {
projectId: '',
name: '',
description: '',
createdAt: '',
updatedAt: '',
}
const initialUser: CurrentUser = {
id: 0,
name: '',
email: '',
}
const initialMinderContext: MinderContext = {
provider: StacklokProviders.GH,
projectId: '',
accessToken: '',
}
export const useStore = create<GlobalState>((set, get) => ({
minderContext: {...initialMinderContext},
projects: [],
currentProject: {...initialCurrentProject},
currentUser: {...initialUser},
projectRules: [],
projectProfiles: [],
remoteRepos: [],
localRepos:[],
setErrorBannerMessage: (msg: string) => {
set({errorBannerMessage: msg});
},
setMinderContext: (projectId: string, accessToken: string, provider: StacklokProviders) => {
set({minderContext: {projectId, provider, accessToken}});
},
setCurrentUser: (user: CurrentUser) => {
set({currentUser: user});
},
setProjects: (projects: Project[]) => {
set({projects})
},
addRuleToControlPlane: async (ruleData: string) => {
const context = get().minderContext;
const ruleType = parseYaml(ruleData);
const body = JSON.stringify({
context: {
project_id: context.projectId,
provider: context.provider,
},
ruleType,
})
const response = await fetch('/api/rules/create', {
method: 'POST',
headers: new Headers({
'Content-Type': 'application/json',
Accept: 'application/json',
Authorization: `bearer ${context.accessToken}`,
}),
body,
})
const createdRuleData = await response.json();
const rule = createdRuleData.ruleType;
set((state) => ({
projectRules: [
...state.projectRules,
rule,
]
}))
},
addProfileToControlPlane: async (profileData: string) => {
const context = get().minderContext;
const profile = parseYaml(profileData);
const body = JSON.stringify({
context: {
project_id: context.projectId,
provider: context.provider,
},
profile,
})
// TODO: handle errors
const response = await fetch('/api/profiles/create', {
method: 'POST',
headers: new Headers({
'Content-Type': 'application/json',
Accept: 'application/json',
Authorization: `bearer ${context.accessToken}`,
}),
body,
})
const createdProfileData = await response.json();
const createdProfile = createdProfileData.profile;
set((state) => ({
projectProfiles: [
...state.projectProfiles,
createdProfile,
]
}))
},
addToLocalRepos: async (owner: string, repo_id: string, name: string) => {
const context = get().minderContext;
const body = JSON.stringify({
context: {
provider: context.provider,
project: context.projectId,
},
repository: {
owner,
repo_id,
name,
},
});
const response = await fetch('/api/repos/register', {
method: 'POST',
headers: new Headers({
'Content-Type': 'application/json',
Accept: 'application/json',
Authorization: `bearer ${context.accessToken}`,
}),
body,
})
const repo = await response.json();
const addedRepo = {...repo.result.repository, context: {provider: context.provider, project: context.projectId}};
set((state) => ({
localRepos: [
...state.localRepos,
addedRepo
]
}))
},
fetchData: async (projectID: string, access_token: string) => {
const headers = new Headers();
headers.append('authorization', `bearer ${access_token}`);
const requestOptions = {
method: 'GET',
headers: headers,
};
// Fetch repos
const remoteReposURL = '/api/repos/listremote?' + new URLSearchParams({project_id: projectID}).toString();
const localReposURL = '/api/repos/listlocal?' + new URLSearchParams({project_id: projectID}).toString();
const remoteReposResponse = await fetch(remoteReposURL, requestOptions);
const localReposResponse = await fetch(localReposURL, requestOptions);
const remoteReposData = await remoteReposResponse.json();
const localReposData = await localReposResponse.json();
set({remoteRepos: remoteReposData.results || []});
set({localRepos: localReposData.results || []});
// Fetch rules
const rulesURL = '/api/rules/list?' + new URLSearchParams({project_id: projectID}).toString()
const rulesResponse = await fetch(rulesURL, requestOptions);
const rulesList = await rulesResponse.json();
set({ projectRules: rulesList.ruleTypes || [] });
// Fetch Profiles
const profilesListURL = '/api/profiles/list?' + new URLSearchParams({project_id: projectID}).toString()
const profilesResponse = await fetch(profilesListURL, requestOptions);
const profilesList = await profilesResponse.json();
set({projectProfiles: profilesList.profiles || []});
},
}))