Path: blob/main/components/dashboard/src/service/json-rpc-envvar-client.ts
2500 views
/**1* Copyright (c) 2023 Gitpod GmbH. All rights reserved.2* Licensed under the GNU Affero General Public License (AGPL).3* See License.AGPL.txt in the project root for license information.4*/56import { PromiseClient } from "@connectrpc/connect";7import { PartialMessage } from "@bufbuild/protobuf";8import { EnvironmentVariableService } from "@gitpod/public-api/lib/gitpod/v1/envvar_connect";9import {10CreateConfigurationEnvironmentVariableRequest,11CreateConfigurationEnvironmentVariableResponse,12CreateOrganizationEnvironmentVariableRequest,13CreateOrganizationEnvironmentVariableResponse,14CreateUserEnvironmentVariableRequest,15CreateUserEnvironmentVariableResponse,16DeleteConfigurationEnvironmentVariableRequest,17DeleteConfigurationEnvironmentVariableResponse,18DeleteOrganizationEnvironmentVariableRequest,19DeleteOrganizationEnvironmentVariableResponse,20DeleteUserEnvironmentVariableRequest,21DeleteUserEnvironmentVariableResponse,22EnvironmentVariableAdmission,23ListConfigurationEnvironmentVariablesRequest,24ListConfigurationEnvironmentVariablesResponse,25ListOrganizationEnvironmentVariablesRequest,26ListOrganizationEnvironmentVariablesResponse,27ListUserEnvironmentVariablesRequest,28ListUserEnvironmentVariablesResponse,29ResolveWorkspaceEnvironmentVariablesRequest,30ResolveWorkspaceEnvironmentVariablesResponse,31UpdateConfigurationEnvironmentVariableRequest,32UpdateConfigurationEnvironmentVariableResponse,33UpdateOrganizationEnvironmentVariableRequest,34UpdateOrganizationEnvironmentVariableResponse,35UpdateUserEnvironmentVariableRequest,36UpdateUserEnvironmentVariableResponse,37} from "@gitpod/public-api/lib/gitpod/v1/envvar_pb";38import { converter } from "./public-api";39import { getGitpodService } from "./service";40import { UserEnvVar, UserEnvVarValue } from "@gitpod/gitpod-protocol";41import { ApplicationError, ErrorCodes } from "@gitpod/gitpod-protocol/lib/messaging/error";4243export class JsonRpcEnvvarClient implements PromiseClient<typeof EnvironmentVariableService> {44async listUserEnvironmentVariables(45req: PartialMessage<ListUserEnvironmentVariablesRequest>,46): Promise<ListUserEnvironmentVariablesResponse> {47const result = new ListUserEnvironmentVariablesResponse();48const userEnvVars = await getGitpodService().server.getAllEnvVars();49result.environmentVariables = userEnvVars.map((i) => converter.toUserEnvironmentVariable(i));5051return result;52}5354async updateUserEnvironmentVariable(55req: PartialMessage<UpdateUserEnvironmentVariableRequest>,56): Promise<UpdateUserEnvironmentVariableResponse> {57if (!req.environmentVariableId) {58throw new ApplicationError(ErrorCodes.BAD_REQUEST, "environmentVariableId is required");59}6061const response = new UpdateUserEnvironmentVariableResponse();6263const userEnvVars = await getGitpodService().server.getAllEnvVars();64const userEnvVarfound = userEnvVars.find((i) => i.id === req.environmentVariableId);65if (userEnvVarfound) {66const variable: UserEnvVarValue = {67id: req.environmentVariableId,68name: req.name ?? userEnvVarfound.name,69value: req.value ?? userEnvVarfound.value,70repositoryPattern: req.repositoryPattern ?? userEnvVarfound.repositoryPattern,71};72variable.repositoryPattern = UserEnvVar.normalizeRepoPattern(variable.repositoryPattern);7374await getGitpodService().server.setEnvVar(variable);7576const updatedUserEnvVars = await getGitpodService().server.getAllEnvVars();77const updatedUserEnvVar = updatedUserEnvVars.find((i) => i.id === req.environmentVariableId);78if (!updatedUserEnvVar) {79throw new ApplicationError(ErrorCodes.INTERNAL_SERVER_ERROR, "could not update env variable");80}8182response.environmentVariable = converter.toUserEnvironmentVariable(updatedUserEnvVar);83return response;84}8586throw new ApplicationError(ErrorCodes.NOT_FOUND, "env variable not found");87}8889async createUserEnvironmentVariable(90req: PartialMessage<CreateUserEnvironmentVariableRequest>,91): Promise<CreateUserEnvironmentVariableResponse> {92if (!req.name || !req.value || !req.repositoryPattern) {93throw new ApplicationError(ErrorCodes.BAD_REQUEST, "name, value and repositoryPattern are required");94}9596const response = new CreateUserEnvironmentVariableResponse();9798const variable: UserEnvVarValue = {99name: req.name,100value: req.value,101repositoryPattern: req.repositoryPattern,102};103variable.repositoryPattern = UserEnvVar.normalizeRepoPattern(variable.repositoryPattern);104105await getGitpodService().server.setEnvVar(variable);106107const updatedUserEnvVars = await getGitpodService().server.getAllEnvVars();108const updatedUserEnvVar = updatedUserEnvVars.find(109(v) => v.name === variable.name && v.repositoryPattern === variable.repositoryPattern,110);111if (!updatedUserEnvVar) {112throw new ApplicationError(ErrorCodes.INTERNAL_SERVER_ERROR, "could not update env variable");113}114115response.environmentVariable = converter.toUserEnvironmentVariable(updatedUserEnvVar);116117return response;118}119120async deleteUserEnvironmentVariable(121req: PartialMessage<DeleteUserEnvironmentVariableRequest>,122): Promise<DeleteUserEnvironmentVariableResponse> {123if (!req.environmentVariableId) {124throw new ApplicationError(ErrorCodes.BAD_REQUEST, "environmentVariableId is required");125}126127const variable: UserEnvVarValue = {128id: req.environmentVariableId,129name: "",130value: "",131repositoryPattern: "",132};133134await getGitpodService().server.deleteEnvVar(variable);135136const response = new DeleteUserEnvironmentVariableResponse();137return response;138}139140async listConfigurationEnvironmentVariables(141req: PartialMessage<ListConfigurationEnvironmentVariablesRequest>,142): Promise<ListConfigurationEnvironmentVariablesResponse> {143if (!req.configurationId) {144throw new ApplicationError(ErrorCodes.BAD_REQUEST, "configurationId is required");145}146147const result = new ListConfigurationEnvironmentVariablesResponse();148const projectEnvVars = await getGitpodService().server.getProjectEnvironmentVariables(req.configurationId);149result.environmentVariables = projectEnvVars.map((i) => converter.toConfigurationEnvironmentVariable(i));150151return result;152}153154async updateConfigurationEnvironmentVariable(155req: PartialMessage<UpdateConfigurationEnvironmentVariableRequest>,156): Promise<UpdateConfigurationEnvironmentVariableResponse> {157if (!req.environmentVariableId) {158throw new ApplicationError(ErrorCodes.BAD_REQUEST, "environmentVariableId is required");159}160if (!req.configurationId) {161throw new ApplicationError(ErrorCodes.BAD_REQUEST, "configurationId is required");162}163164const response = new UpdateConfigurationEnvironmentVariableResponse();165166const projectEnvVars = await getGitpodService().server.getProjectEnvironmentVariables(req.configurationId);167const projectEnvVarfound = projectEnvVars.find((i) => i.id === req.environmentVariableId);168if (projectEnvVarfound) {169await getGitpodService().server.setProjectEnvironmentVariable(170req.configurationId,171req.name ?? projectEnvVarfound.name,172req.value ?? "",173req.admission === EnvironmentVariableAdmission.UNSPECIFIED174? projectEnvVarfound.censored175: req.admission === EnvironmentVariableAdmission.PREBUILD,176req.environmentVariableId,177);178179const updatedProjectEnvVars = await getGitpodService().server.getProjectEnvironmentVariables(180req.configurationId,181);182const updatedProjectEnvVar = updatedProjectEnvVars.find((i) => i.id === req.environmentVariableId);183if (!updatedProjectEnvVar) {184throw new ApplicationError(ErrorCodes.INTERNAL_SERVER_ERROR, "could not update env variable");185}186187response.environmentVariable = converter.toConfigurationEnvironmentVariable(updatedProjectEnvVar);188return response;189}190191throw new ApplicationError(ErrorCodes.NOT_FOUND, "env variable not found");192}193194async createConfigurationEnvironmentVariable(195req: PartialMessage<CreateConfigurationEnvironmentVariableRequest>,196): Promise<CreateConfigurationEnvironmentVariableResponse> {197if (!req.configurationId || !req.name || !req.value) {198throw new ApplicationError(ErrorCodes.BAD_REQUEST, "configurationId, name and value are required");199}200201const response = new CreateConfigurationEnvironmentVariableResponse();202203await getGitpodService().server.setProjectEnvironmentVariable(204req.configurationId,205req.name,206req.value,207req.admission === EnvironmentVariableAdmission.PREBUILD,208);209210const updatedProjectEnvVars = await getGitpodService().server.getProjectEnvironmentVariables(211req.configurationId,212);213const updatedProjectEnvVar = updatedProjectEnvVars.find((v) => v.name === req.name);214if (!updatedProjectEnvVar) {215throw new ApplicationError(ErrorCodes.INTERNAL_SERVER_ERROR, "could not create env variable");216}217218response.environmentVariable = converter.toConfigurationEnvironmentVariable(updatedProjectEnvVar);219220return response;221}222223async deleteConfigurationEnvironmentVariable(224req: PartialMessage<DeleteConfigurationEnvironmentVariableRequest>,225): Promise<DeleteConfigurationEnvironmentVariableResponse> {226if (!req.environmentVariableId) {227throw new ApplicationError(ErrorCodes.BAD_REQUEST, "environmentVariableId is required");228}229230await getGitpodService().server.deleteProjectEnvironmentVariable(req.environmentVariableId);231232const response = new DeleteConfigurationEnvironmentVariableResponse();233return response;234}235236async listOrganizationEnvironmentVariables(237req: PartialMessage<ListOrganizationEnvironmentVariablesRequest>,238): Promise<ListOrganizationEnvironmentVariablesResponse> {239throw new ApplicationError(ErrorCodes.BAD_REQUEST, "Unimplemented");240}241242async updateOrganizationEnvironmentVariable(243req: PartialMessage<UpdateOrganizationEnvironmentVariableRequest>,244): Promise<UpdateOrganizationEnvironmentVariableResponse> {245throw new ApplicationError(ErrorCodes.BAD_REQUEST, "Unimplemented");246}247248async createOrganizationEnvironmentVariable(249req: PartialMessage<CreateOrganizationEnvironmentVariableRequest>,250): Promise<CreateOrganizationEnvironmentVariableResponse> {251throw new ApplicationError(ErrorCodes.BAD_REQUEST, "Unimplemented");252}253254async deleteOrganizationEnvironmentVariable(255req: PartialMessage<DeleteOrganizationEnvironmentVariableRequest>,256): Promise<DeleteOrganizationEnvironmentVariableResponse> {257throw new ApplicationError(ErrorCodes.BAD_REQUEST, "Unimplemented");258}259260async resolveWorkspaceEnvironmentVariables(261req: PartialMessage<ResolveWorkspaceEnvironmentVariablesRequest>,262): Promise<ResolveWorkspaceEnvironmentVariablesResponse> {263throw new ApplicationError(ErrorCodes.BAD_REQUEST, "Unimplemented");264}265}266267268