Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/dashboard/src/service/json-rpc-envvar-client.ts
2500 views
1
/**
2
* Copyright (c) 2023 Gitpod GmbH. All rights reserved.
3
* Licensed under the GNU Affero General Public License (AGPL).
4
* See License.AGPL.txt in the project root for license information.
5
*/
6
7
import { PromiseClient } from "@connectrpc/connect";
8
import { PartialMessage } from "@bufbuild/protobuf";
9
import { EnvironmentVariableService } from "@gitpod/public-api/lib/gitpod/v1/envvar_connect";
10
import {
11
CreateConfigurationEnvironmentVariableRequest,
12
CreateConfigurationEnvironmentVariableResponse,
13
CreateOrganizationEnvironmentVariableRequest,
14
CreateOrganizationEnvironmentVariableResponse,
15
CreateUserEnvironmentVariableRequest,
16
CreateUserEnvironmentVariableResponse,
17
DeleteConfigurationEnvironmentVariableRequest,
18
DeleteConfigurationEnvironmentVariableResponse,
19
DeleteOrganizationEnvironmentVariableRequest,
20
DeleteOrganizationEnvironmentVariableResponse,
21
DeleteUserEnvironmentVariableRequest,
22
DeleteUserEnvironmentVariableResponse,
23
EnvironmentVariableAdmission,
24
ListConfigurationEnvironmentVariablesRequest,
25
ListConfigurationEnvironmentVariablesResponse,
26
ListOrganizationEnvironmentVariablesRequest,
27
ListOrganizationEnvironmentVariablesResponse,
28
ListUserEnvironmentVariablesRequest,
29
ListUserEnvironmentVariablesResponse,
30
ResolveWorkspaceEnvironmentVariablesRequest,
31
ResolveWorkspaceEnvironmentVariablesResponse,
32
UpdateConfigurationEnvironmentVariableRequest,
33
UpdateConfigurationEnvironmentVariableResponse,
34
UpdateOrganizationEnvironmentVariableRequest,
35
UpdateOrganizationEnvironmentVariableResponse,
36
UpdateUserEnvironmentVariableRequest,
37
UpdateUserEnvironmentVariableResponse,
38
} from "@gitpod/public-api/lib/gitpod/v1/envvar_pb";
39
import { converter } from "./public-api";
40
import { getGitpodService } from "./service";
41
import { UserEnvVar, UserEnvVarValue } from "@gitpod/gitpod-protocol";
42
import { ApplicationError, ErrorCodes } from "@gitpod/gitpod-protocol/lib/messaging/error";
43
44
export class JsonRpcEnvvarClient implements PromiseClient<typeof EnvironmentVariableService> {
45
async listUserEnvironmentVariables(
46
req: PartialMessage<ListUserEnvironmentVariablesRequest>,
47
): Promise<ListUserEnvironmentVariablesResponse> {
48
const result = new ListUserEnvironmentVariablesResponse();
49
const userEnvVars = await getGitpodService().server.getAllEnvVars();
50
result.environmentVariables = userEnvVars.map((i) => converter.toUserEnvironmentVariable(i));
51
52
return result;
53
}
54
55
async updateUserEnvironmentVariable(
56
req: PartialMessage<UpdateUserEnvironmentVariableRequest>,
57
): Promise<UpdateUserEnvironmentVariableResponse> {
58
if (!req.environmentVariableId) {
59
throw new ApplicationError(ErrorCodes.BAD_REQUEST, "environmentVariableId is required");
60
}
61
62
const response = new UpdateUserEnvironmentVariableResponse();
63
64
const userEnvVars = await getGitpodService().server.getAllEnvVars();
65
const userEnvVarfound = userEnvVars.find((i) => i.id === req.environmentVariableId);
66
if (userEnvVarfound) {
67
const variable: UserEnvVarValue = {
68
id: req.environmentVariableId,
69
name: req.name ?? userEnvVarfound.name,
70
value: req.value ?? userEnvVarfound.value,
71
repositoryPattern: req.repositoryPattern ?? userEnvVarfound.repositoryPattern,
72
};
73
variable.repositoryPattern = UserEnvVar.normalizeRepoPattern(variable.repositoryPattern);
74
75
await getGitpodService().server.setEnvVar(variable);
76
77
const updatedUserEnvVars = await getGitpodService().server.getAllEnvVars();
78
const updatedUserEnvVar = updatedUserEnvVars.find((i) => i.id === req.environmentVariableId);
79
if (!updatedUserEnvVar) {
80
throw new ApplicationError(ErrorCodes.INTERNAL_SERVER_ERROR, "could not update env variable");
81
}
82
83
response.environmentVariable = converter.toUserEnvironmentVariable(updatedUserEnvVar);
84
return response;
85
}
86
87
throw new ApplicationError(ErrorCodes.NOT_FOUND, "env variable not found");
88
}
89
90
async createUserEnvironmentVariable(
91
req: PartialMessage<CreateUserEnvironmentVariableRequest>,
92
): Promise<CreateUserEnvironmentVariableResponse> {
93
if (!req.name || !req.value || !req.repositoryPattern) {
94
throw new ApplicationError(ErrorCodes.BAD_REQUEST, "name, value and repositoryPattern are required");
95
}
96
97
const response = new CreateUserEnvironmentVariableResponse();
98
99
const variable: UserEnvVarValue = {
100
name: req.name,
101
value: req.value,
102
repositoryPattern: req.repositoryPattern,
103
};
104
variable.repositoryPattern = UserEnvVar.normalizeRepoPattern(variable.repositoryPattern);
105
106
await getGitpodService().server.setEnvVar(variable);
107
108
const updatedUserEnvVars = await getGitpodService().server.getAllEnvVars();
109
const updatedUserEnvVar = updatedUserEnvVars.find(
110
(v) => v.name === variable.name && v.repositoryPattern === variable.repositoryPattern,
111
);
112
if (!updatedUserEnvVar) {
113
throw new ApplicationError(ErrorCodes.INTERNAL_SERVER_ERROR, "could not update env variable");
114
}
115
116
response.environmentVariable = converter.toUserEnvironmentVariable(updatedUserEnvVar);
117
118
return response;
119
}
120
121
async deleteUserEnvironmentVariable(
122
req: PartialMessage<DeleteUserEnvironmentVariableRequest>,
123
): Promise<DeleteUserEnvironmentVariableResponse> {
124
if (!req.environmentVariableId) {
125
throw new ApplicationError(ErrorCodes.BAD_REQUEST, "environmentVariableId is required");
126
}
127
128
const variable: UserEnvVarValue = {
129
id: req.environmentVariableId,
130
name: "",
131
value: "",
132
repositoryPattern: "",
133
};
134
135
await getGitpodService().server.deleteEnvVar(variable);
136
137
const response = new DeleteUserEnvironmentVariableResponse();
138
return response;
139
}
140
141
async listConfigurationEnvironmentVariables(
142
req: PartialMessage<ListConfigurationEnvironmentVariablesRequest>,
143
): Promise<ListConfigurationEnvironmentVariablesResponse> {
144
if (!req.configurationId) {
145
throw new ApplicationError(ErrorCodes.BAD_REQUEST, "configurationId is required");
146
}
147
148
const result = new ListConfigurationEnvironmentVariablesResponse();
149
const projectEnvVars = await getGitpodService().server.getProjectEnvironmentVariables(req.configurationId);
150
result.environmentVariables = projectEnvVars.map((i) => converter.toConfigurationEnvironmentVariable(i));
151
152
return result;
153
}
154
155
async updateConfigurationEnvironmentVariable(
156
req: PartialMessage<UpdateConfigurationEnvironmentVariableRequest>,
157
): Promise<UpdateConfigurationEnvironmentVariableResponse> {
158
if (!req.environmentVariableId) {
159
throw new ApplicationError(ErrorCodes.BAD_REQUEST, "environmentVariableId is required");
160
}
161
if (!req.configurationId) {
162
throw new ApplicationError(ErrorCodes.BAD_REQUEST, "configurationId is required");
163
}
164
165
const response = new UpdateConfigurationEnvironmentVariableResponse();
166
167
const projectEnvVars = await getGitpodService().server.getProjectEnvironmentVariables(req.configurationId);
168
const projectEnvVarfound = projectEnvVars.find((i) => i.id === req.environmentVariableId);
169
if (projectEnvVarfound) {
170
await getGitpodService().server.setProjectEnvironmentVariable(
171
req.configurationId,
172
req.name ?? projectEnvVarfound.name,
173
req.value ?? "",
174
req.admission === EnvironmentVariableAdmission.UNSPECIFIED
175
? projectEnvVarfound.censored
176
: req.admission === EnvironmentVariableAdmission.PREBUILD,
177
req.environmentVariableId,
178
);
179
180
const updatedProjectEnvVars = await getGitpodService().server.getProjectEnvironmentVariables(
181
req.configurationId,
182
);
183
const updatedProjectEnvVar = updatedProjectEnvVars.find((i) => i.id === req.environmentVariableId);
184
if (!updatedProjectEnvVar) {
185
throw new ApplicationError(ErrorCodes.INTERNAL_SERVER_ERROR, "could not update env variable");
186
}
187
188
response.environmentVariable = converter.toConfigurationEnvironmentVariable(updatedProjectEnvVar);
189
return response;
190
}
191
192
throw new ApplicationError(ErrorCodes.NOT_FOUND, "env variable not found");
193
}
194
195
async createConfigurationEnvironmentVariable(
196
req: PartialMessage<CreateConfigurationEnvironmentVariableRequest>,
197
): Promise<CreateConfigurationEnvironmentVariableResponse> {
198
if (!req.configurationId || !req.name || !req.value) {
199
throw new ApplicationError(ErrorCodes.BAD_REQUEST, "configurationId, name and value are required");
200
}
201
202
const response = new CreateConfigurationEnvironmentVariableResponse();
203
204
await getGitpodService().server.setProjectEnvironmentVariable(
205
req.configurationId,
206
req.name,
207
req.value,
208
req.admission === EnvironmentVariableAdmission.PREBUILD,
209
);
210
211
const updatedProjectEnvVars = await getGitpodService().server.getProjectEnvironmentVariables(
212
req.configurationId,
213
);
214
const updatedProjectEnvVar = updatedProjectEnvVars.find((v) => v.name === req.name);
215
if (!updatedProjectEnvVar) {
216
throw new ApplicationError(ErrorCodes.INTERNAL_SERVER_ERROR, "could not create env variable");
217
}
218
219
response.environmentVariable = converter.toConfigurationEnvironmentVariable(updatedProjectEnvVar);
220
221
return response;
222
}
223
224
async deleteConfigurationEnvironmentVariable(
225
req: PartialMessage<DeleteConfigurationEnvironmentVariableRequest>,
226
): Promise<DeleteConfigurationEnvironmentVariableResponse> {
227
if (!req.environmentVariableId) {
228
throw new ApplicationError(ErrorCodes.BAD_REQUEST, "environmentVariableId is required");
229
}
230
231
await getGitpodService().server.deleteProjectEnvironmentVariable(req.environmentVariableId);
232
233
const response = new DeleteConfigurationEnvironmentVariableResponse();
234
return response;
235
}
236
237
async listOrganizationEnvironmentVariables(
238
req: PartialMessage<ListOrganizationEnvironmentVariablesRequest>,
239
): Promise<ListOrganizationEnvironmentVariablesResponse> {
240
throw new ApplicationError(ErrorCodes.BAD_REQUEST, "Unimplemented");
241
}
242
243
async updateOrganizationEnvironmentVariable(
244
req: PartialMessage<UpdateOrganizationEnvironmentVariableRequest>,
245
): Promise<UpdateOrganizationEnvironmentVariableResponse> {
246
throw new ApplicationError(ErrorCodes.BAD_REQUEST, "Unimplemented");
247
}
248
249
async createOrganizationEnvironmentVariable(
250
req: PartialMessage<CreateOrganizationEnvironmentVariableRequest>,
251
): Promise<CreateOrganizationEnvironmentVariableResponse> {
252
throw new ApplicationError(ErrorCodes.BAD_REQUEST, "Unimplemented");
253
}
254
255
async deleteOrganizationEnvironmentVariable(
256
req: PartialMessage<DeleteOrganizationEnvironmentVariableRequest>,
257
): Promise<DeleteOrganizationEnvironmentVariableResponse> {
258
throw new ApplicationError(ErrorCodes.BAD_REQUEST, "Unimplemented");
259
}
260
261
async resolveWorkspaceEnvironmentVariables(
262
req: PartialMessage<ResolveWorkspaceEnvironmentVariablesRequest>,
263
): Promise<ResolveWorkspaceEnvironmentVariablesResponse> {
264
throw new ApplicationError(ErrorCodes.BAD_REQUEST, "Unimplemented");
265
}
266
}
267
268