|
| 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 { Code, ConnectError, 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 | + CreateUserEnvironmentVariableRequest, |
| 14 | + CreateUserEnvironmentVariableResponse, |
| 15 | + DeleteConfigurationEnvironmentVariableRequest, |
| 16 | + DeleteConfigurationEnvironmentVariableResponse, |
| 17 | + DeleteUserEnvironmentVariableRequest, |
| 18 | + DeleteUserEnvironmentVariableResponse, |
| 19 | + EnvironmentVariableAdmission, |
| 20 | + ListConfigurationEnvironmentVariablesRequest, |
| 21 | + ListConfigurationEnvironmentVariablesResponse, |
| 22 | + ListUserEnvironmentVariablesRequest, |
| 23 | + ListUserEnvironmentVariablesResponse, |
| 24 | + ResolveWorkspaceEnvironmentVariablesRequest, |
| 25 | + ResolveWorkspaceEnvironmentVariablesResponse, |
| 26 | + UpdateConfigurationEnvironmentVariableRequest, |
| 27 | + UpdateConfigurationEnvironmentVariableResponse, |
| 28 | + UpdateUserEnvironmentVariableRequest, |
| 29 | + UpdateUserEnvironmentVariableResponse, |
| 30 | +} from "@gitpod/public-api/lib/gitpod/v1/envvar_pb"; |
| 31 | +import { converter } from "./public-api"; |
| 32 | +import { getGitpodService } from "./service"; |
| 33 | +import { UserEnvVar, UserEnvVarValue } from "@gitpod/gitpod-protocol"; |
| 34 | + |
| 35 | +export class JsonRpcEnvvarClient implements PromiseClient<typeof EnvironmentVariableService> { |
| 36 | + async listUserEnvironmentVariables( |
| 37 | + req: PartialMessage<ListUserEnvironmentVariablesRequest>, |
| 38 | + ): Promise<ListUserEnvironmentVariablesResponse> { |
| 39 | + const result = new ListUserEnvironmentVariablesResponse(); |
| 40 | + const userEnvVars = await getGitpodService().server.getAllEnvVars(); |
| 41 | + result.environmentVariables = userEnvVars.map((i) => converter.toUserEnvironmentVariable(i)); |
| 42 | + |
| 43 | + return result; |
| 44 | + } |
| 45 | + |
| 46 | + async updateUserEnvironmentVariable( |
| 47 | + req: PartialMessage<UpdateUserEnvironmentVariableRequest>, |
| 48 | + ): Promise<UpdateUserEnvironmentVariableResponse> { |
| 49 | + if (!req.envVarId) { |
| 50 | + throw new ConnectError("id is not set", Code.InvalidArgument); |
| 51 | + } |
| 52 | + |
| 53 | + const response = new UpdateUserEnvironmentVariableResponse(); |
| 54 | + |
| 55 | + const userEnvVars = await getGitpodService().server.getAllEnvVars(); |
| 56 | + const userEnvVarfound = userEnvVars.find((i) => i.id === req.envVarId); |
| 57 | + if (userEnvVarfound) { |
| 58 | + const variable: UserEnvVarValue = { |
| 59 | + name: req.name ?? userEnvVarfound.name, |
| 60 | + value: req.value ?? userEnvVarfound.value, |
| 61 | + repositoryPattern: req.repositoryPattern ?? userEnvVarfound.repositoryPattern, |
| 62 | + }; |
| 63 | + variable.repositoryPattern = UserEnvVar.normalizeRepoPattern(variable.repositoryPattern); |
| 64 | + |
| 65 | + await getGitpodService().server.setEnvVar(variable); |
| 66 | + |
| 67 | + const updatedUserEnvVars = await getGitpodService().server.getAllEnvVars(); |
| 68 | + const updatedUserEnvVar = updatedUserEnvVars.find((i) => i.id === req.envVarId); |
| 69 | + if (!updatedUserEnvVar) { |
| 70 | + throw new ConnectError("could not update env variable", Code.Internal); |
| 71 | + } |
| 72 | + |
| 73 | + response.environmentVariable = converter.toUserEnvironmentVariable(updatedUserEnvVar); |
| 74 | + return response; |
| 75 | + } |
| 76 | + |
| 77 | + throw new ConnectError("env variable not found", Code.InvalidArgument); |
| 78 | + } |
| 79 | + |
| 80 | + async createUserEnvironmentVariable( |
| 81 | + req: PartialMessage<CreateUserEnvironmentVariableRequest>, |
| 82 | + ): Promise<CreateUserEnvironmentVariableResponse> { |
| 83 | + if (!req.name || !req.value || !req.repositoryPattern) { |
| 84 | + throw new ConnectError("invalid argument", Code.InvalidArgument); |
| 85 | + } |
| 86 | + |
| 87 | + const response = new CreateUserEnvironmentVariableResponse(); |
| 88 | + |
| 89 | + const variable: UserEnvVarValue = { |
| 90 | + name: req.name, |
| 91 | + value: req.value, |
| 92 | + repositoryPattern: req.repositoryPattern, |
| 93 | + }; |
| 94 | + variable.repositoryPattern = UserEnvVar.normalizeRepoPattern(variable.repositoryPattern); |
| 95 | + |
| 96 | + await getGitpodService().server.setEnvVar(variable); |
| 97 | + |
| 98 | + const updatedUserEnvVars = await getGitpodService().server.getAllEnvVars(); |
| 99 | + const updatedUserEnvVar = updatedUserEnvVars.find( |
| 100 | + (v) => v.name === variable.name && v.repositoryPattern === variable.repositoryPattern, |
| 101 | + ); |
| 102 | + if (!updatedUserEnvVar) { |
| 103 | + throw new ConnectError("could not update env variable", Code.Internal); |
| 104 | + } |
| 105 | + |
| 106 | + response.environmentVariable = converter.toUserEnvironmentVariable(updatedUserEnvVar); |
| 107 | + |
| 108 | + return response; |
| 109 | + } |
| 110 | + |
| 111 | + async deleteUserEnvironmentVariable( |
| 112 | + req: PartialMessage<DeleteUserEnvironmentVariableRequest>, |
| 113 | + ): Promise<DeleteUserEnvironmentVariableResponse> { |
| 114 | + if (!req.envVarId) { |
| 115 | + throw new ConnectError("invalid argument", Code.InvalidArgument); |
| 116 | + } |
| 117 | + |
| 118 | + const variable: UserEnvVarValue = { |
| 119 | + id: req.envVarId, |
| 120 | + name: "", |
| 121 | + value: "", |
| 122 | + repositoryPattern: "", |
| 123 | + }; |
| 124 | + |
| 125 | + await getGitpodService().server.deleteEnvVar(variable); |
| 126 | + |
| 127 | + const response = new DeleteUserEnvironmentVariableResponse(); |
| 128 | + return response; |
| 129 | + } |
| 130 | + |
| 131 | + async listConfigurationEnvironmentVariables( |
| 132 | + req: PartialMessage<ListConfigurationEnvironmentVariablesRequest>, |
| 133 | + ): Promise<ListConfigurationEnvironmentVariablesResponse> { |
| 134 | + if (!req.configurationId) { |
| 135 | + throw new ConnectError("configurationId is not set", Code.InvalidArgument); |
| 136 | + } |
| 137 | + |
| 138 | + const result = new ListConfigurationEnvironmentVariablesResponse(); |
| 139 | + const projectEnvVars = await getGitpodService().server.getProjectEnvironmentVariables(req.configurationId); |
| 140 | + result.environmentVariables = projectEnvVars.map((i) => converter.toConfigurationEnvironmentVariable(i)); |
| 141 | + |
| 142 | + return result; |
| 143 | + } |
| 144 | + |
| 145 | + async updateConfigurationEnvironmentVariable( |
| 146 | + req: PartialMessage<UpdateConfigurationEnvironmentVariableRequest>, |
| 147 | + ): Promise<UpdateConfigurationEnvironmentVariableResponse> { |
| 148 | + if (!req.envVarId) { |
| 149 | + throw new ConnectError("envVarId is not set", Code.InvalidArgument); |
| 150 | + } |
| 151 | + if (!req.configurationId) { |
| 152 | + throw new ConnectError("configurationId is not set", Code.InvalidArgument); |
| 153 | + } |
| 154 | + |
| 155 | + const response = new UpdateConfigurationEnvironmentVariableResponse(); |
| 156 | + |
| 157 | + const projectEnvVars = await getGitpodService().server.getProjectEnvironmentVariables(req.configurationId); |
| 158 | + const projectEnvVarfound = projectEnvVars.find((i) => i.id === req.envVarId); |
| 159 | + if (projectEnvVarfound) { |
| 160 | + await getGitpodService().server.setProjectEnvironmentVariable( |
| 161 | + req.configurationId, |
| 162 | + req.name ?? projectEnvVarfound.name, |
| 163 | + req.value ?? "", |
| 164 | + (req.admission === EnvironmentVariableAdmission.PREBUILD ? true : false) ?? projectEnvVarfound.censored, |
| 165 | + ); |
| 166 | + |
| 167 | + const updatedProjectEnvVars = await getGitpodService().server.getProjectEnvironmentVariables( |
| 168 | + req.configurationId, |
| 169 | + ); |
| 170 | + const updatedProjectEnvVar = updatedProjectEnvVars.find((i) => i.id === req.envVarId); |
| 171 | + if (!updatedProjectEnvVar) { |
| 172 | + throw new ConnectError("could not update env variable", Code.Internal); |
| 173 | + } |
| 174 | + |
| 175 | + response.environmentVariable = converter.toConfigurationEnvironmentVariable(updatedProjectEnvVar); |
| 176 | + return response; |
| 177 | + } |
| 178 | + |
| 179 | + throw new ConnectError("env variable not found", Code.InvalidArgument); |
| 180 | + } |
| 181 | + |
| 182 | + async createConfigurationEnvironmentVariable( |
| 183 | + req: PartialMessage<CreateConfigurationEnvironmentVariableRequest>, |
| 184 | + ): Promise<CreateConfigurationEnvironmentVariableResponse> { |
| 185 | + if (!req.configurationId || !req.name || !req.value) { |
| 186 | + throw new ConnectError("invalid argument", Code.InvalidArgument); |
| 187 | + } |
| 188 | + |
| 189 | + const response = new CreateConfigurationEnvironmentVariableResponse(); |
| 190 | + |
| 191 | + await getGitpodService().server.setProjectEnvironmentVariable( |
| 192 | + req.configurationId, |
| 193 | + req.name, |
| 194 | + req.value, |
| 195 | + req.admission === EnvironmentVariableAdmission.PREBUILD ? true : false, |
| 196 | + ); |
| 197 | + |
| 198 | + const updatedProjectEnvVars = await getGitpodService().server.getProjectEnvironmentVariables( |
| 199 | + req.configurationId, |
| 200 | + ); |
| 201 | + const updatedProjectEnvVar = updatedProjectEnvVars.find((v) => v.name === req.name); |
| 202 | + if (!updatedProjectEnvVar) { |
| 203 | + throw new ConnectError("could not create env variable", Code.Internal); |
| 204 | + } |
| 205 | + |
| 206 | + response.environmentVariable = converter.toConfigurationEnvironmentVariable(updatedProjectEnvVar); |
| 207 | + |
| 208 | + return response; |
| 209 | + } |
| 210 | + |
| 211 | + async deleteConfigurationEnvironmentVariable( |
| 212 | + req: PartialMessage<DeleteConfigurationEnvironmentVariableRequest>, |
| 213 | + ): Promise<DeleteConfigurationEnvironmentVariableResponse> { |
| 214 | + if (!req.envVarId) { |
| 215 | + throw new ConnectError("invalid argument", Code.InvalidArgument); |
| 216 | + } |
| 217 | + |
| 218 | + await getGitpodService().server.deleteProjectEnvironmentVariable(req.envVarId); |
| 219 | + |
| 220 | + const response = new DeleteConfigurationEnvironmentVariableResponse(); |
| 221 | + return response; |
| 222 | + } |
| 223 | + |
| 224 | + async resolveWorkspaceEnvironmentVariables( |
| 225 | + req: PartialMessage<ResolveWorkspaceEnvironmentVariablesRequest>, |
| 226 | + ): Promise<ResolveWorkspaceEnvironmentVariablesResponse> { |
| 227 | + throw new Error("Not implemented"); |
| 228 | + } |
| 229 | +} |
0 commit comments