-
Notifications
You must be signed in to change notification settings - Fork 1.3k
[public-api] Migrate envvarService #19067
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
231 changes: 231 additions & 0 deletions
231
components/dashboard/src/service/json-rpc-envvar-client.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,231 @@ | ||
/** | ||
* Copyright (c) 2023 Gitpod GmbH. All rights reserved. | ||
* Licensed under the GNU Affero General Public License (AGPL). | ||
* See License.AGPL.txt in the project root for license information. | ||
*/ | ||
|
||
import { PromiseClient } from "@connectrpc/connect"; | ||
import { PartialMessage } from "@bufbuild/protobuf"; | ||
import { EnvironmentVariableService } from "@gitpod/public-api/lib/gitpod/v1/envvar_connect"; | ||
import { | ||
CreateConfigurationEnvironmentVariableRequest, | ||
CreateConfigurationEnvironmentVariableResponse, | ||
CreateUserEnvironmentVariableRequest, | ||
CreateUserEnvironmentVariableResponse, | ||
DeleteConfigurationEnvironmentVariableRequest, | ||
DeleteConfigurationEnvironmentVariableResponse, | ||
DeleteUserEnvironmentVariableRequest, | ||
DeleteUserEnvironmentVariableResponse, | ||
EnvironmentVariableAdmission, | ||
ListConfigurationEnvironmentVariablesRequest, | ||
ListConfigurationEnvironmentVariablesResponse, | ||
ListUserEnvironmentVariablesRequest, | ||
ListUserEnvironmentVariablesResponse, | ||
ResolveWorkspaceEnvironmentVariablesRequest, | ||
ResolveWorkspaceEnvironmentVariablesResponse, | ||
UpdateConfigurationEnvironmentVariableRequest, | ||
UpdateConfigurationEnvironmentVariableResponse, | ||
UpdateUserEnvironmentVariableRequest, | ||
UpdateUserEnvironmentVariableResponse, | ||
} from "@gitpod/public-api/lib/gitpod/v1/envvar_pb"; | ||
import { converter } from "./public-api"; | ||
import { getGitpodService } from "./service"; | ||
import { UserEnvVar, UserEnvVarValue } from "@gitpod/gitpod-protocol"; | ||
import { ApplicationError, ErrorCodes } from "@gitpod/gitpod-protocol/lib/messaging/error"; | ||
|
||
export class JsonRpcEnvvarClient implements PromiseClient<typeof EnvironmentVariableService> { | ||
async listUserEnvironmentVariables( | ||
req: PartialMessage<ListUserEnvironmentVariablesRequest>, | ||
): Promise<ListUserEnvironmentVariablesResponse> { | ||
const result = new ListUserEnvironmentVariablesResponse(); | ||
const userEnvVars = await getGitpodService().server.getAllEnvVars(); | ||
result.environmentVariables = userEnvVars.map((i) => converter.toUserEnvironmentVariable(i)); | ||
|
||
return result; | ||
} | ||
|
||
async updateUserEnvironmentVariable( | ||
req: PartialMessage<UpdateUserEnvironmentVariableRequest>, | ||
): Promise<UpdateUserEnvironmentVariableResponse> { | ||
if (!req.envVarId) { | ||
throw new ApplicationError(ErrorCodes.BAD_REQUEST, "envVarId is required"); | ||
} | ||
|
||
const response = new UpdateUserEnvironmentVariableResponse(); | ||
|
||
const userEnvVars = await getGitpodService().server.getAllEnvVars(); | ||
const userEnvVarfound = userEnvVars.find((i) => i.id === req.envVarId); | ||
if (userEnvVarfound) { | ||
const variable: UserEnvVarValue = { | ||
id: req.envVarId, | ||
name: req.name ?? userEnvVarfound.name, | ||
value: req.value ?? userEnvVarfound.value, | ||
repositoryPattern: req.repositoryPattern ?? userEnvVarfound.repositoryPattern, | ||
}; | ||
variable.repositoryPattern = UserEnvVar.normalizeRepoPattern(variable.repositoryPattern); | ||
|
||
await getGitpodService().server.setEnvVar(variable); | ||
|
||
const updatedUserEnvVars = await getGitpodService().server.getAllEnvVars(); | ||
const updatedUserEnvVar = updatedUserEnvVars.find((i) => i.id === req.envVarId); | ||
if (!updatedUserEnvVar) { | ||
throw new ApplicationError(ErrorCodes.INTERNAL_SERVER_ERROR, "could not update env variable"); | ||
} | ||
|
||
response.environmentVariable = converter.toUserEnvironmentVariable(updatedUserEnvVar); | ||
return response; | ||
} | ||
|
||
throw new ApplicationError(ErrorCodes.NOT_FOUND, "env variable not found"); | ||
} | ||
|
||
async createUserEnvironmentVariable( | ||
req: PartialMessage<CreateUserEnvironmentVariableRequest>, | ||
): Promise<CreateUserEnvironmentVariableResponse> { | ||
if (!req.name || !req.value || !req.repositoryPattern) { | ||
throw new ApplicationError(ErrorCodes.BAD_REQUEST, "name, value and repositoryPattern are required"); | ||
} | ||
|
||
const response = new CreateUserEnvironmentVariableResponse(); | ||
|
||
const variable: UserEnvVarValue = { | ||
name: req.name, | ||
value: req.value, | ||
repositoryPattern: req.repositoryPattern, | ||
}; | ||
variable.repositoryPattern = UserEnvVar.normalizeRepoPattern(variable.repositoryPattern); | ||
|
||
await getGitpodService().server.setEnvVar(variable); | ||
|
||
const updatedUserEnvVars = await getGitpodService().server.getAllEnvVars(); | ||
const updatedUserEnvVar = updatedUserEnvVars.find( | ||
(v) => v.name === variable.name && v.repositoryPattern === variable.repositoryPattern, | ||
); | ||
if (!updatedUserEnvVar) { | ||
throw new ApplicationError(ErrorCodes.INTERNAL_SERVER_ERROR, "could not update env variable"); | ||
} | ||
|
||
response.environmentVariable = converter.toUserEnvironmentVariable(updatedUserEnvVar); | ||
|
||
return response; | ||
} | ||
|
||
async deleteUserEnvironmentVariable( | ||
req: PartialMessage<DeleteUserEnvironmentVariableRequest>, | ||
): Promise<DeleteUserEnvironmentVariableResponse> { | ||
if (!req.envVarId) { | ||
throw new ApplicationError(ErrorCodes.BAD_REQUEST, "envVarId is required"); | ||
} | ||
|
||
const variable: UserEnvVarValue = { | ||
id: req.envVarId, | ||
name: "", | ||
value: "", | ||
repositoryPattern: "", | ||
}; | ||
|
||
await getGitpodService().server.deleteEnvVar(variable); | ||
|
||
const response = new DeleteUserEnvironmentVariableResponse(); | ||
return response; | ||
} | ||
|
||
async listConfigurationEnvironmentVariables( | ||
req: PartialMessage<ListConfigurationEnvironmentVariablesRequest>, | ||
): Promise<ListConfigurationEnvironmentVariablesResponse> { | ||
if (!req.configurationId) { | ||
throw new ApplicationError(ErrorCodes.BAD_REQUEST, "configurationId is required"); | ||
} | ||
|
||
const result = new ListConfigurationEnvironmentVariablesResponse(); | ||
const projectEnvVars = await getGitpodService().server.getProjectEnvironmentVariables(req.configurationId); | ||
result.environmentVariables = projectEnvVars.map((i) => converter.toConfigurationEnvironmentVariable(i)); | ||
|
||
return result; | ||
} | ||
|
||
async updateConfigurationEnvironmentVariable( | ||
req: PartialMessage<UpdateConfigurationEnvironmentVariableRequest>, | ||
): Promise<UpdateConfigurationEnvironmentVariableResponse> { | ||
if (!req.envVarId) { | ||
throw new ApplicationError(ErrorCodes.BAD_REQUEST, "envVarId is required"); | ||
} | ||
if (!req.configurationId) { | ||
throw new ApplicationError(ErrorCodes.BAD_REQUEST, "configurationId is required"); | ||
} | ||
|
||
const response = new UpdateConfigurationEnvironmentVariableResponse(); | ||
|
||
const projectEnvVars = await getGitpodService().server.getProjectEnvironmentVariables(req.configurationId); | ||
const projectEnvVarfound = projectEnvVars.find((i) => i.id === req.envVarId); | ||
if (projectEnvVarfound) { | ||
jeanp413 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
await getGitpodService().server.setProjectEnvironmentVariable( | ||
req.configurationId, | ||
req.name ?? projectEnvVarfound.name, | ||
req.value ?? "", | ||
(req.admission === EnvironmentVariableAdmission.PREBUILD ? true : false) ?? projectEnvVarfound.censored, | ||
); | ||
|
||
const updatedProjectEnvVars = await getGitpodService().server.getProjectEnvironmentVariables( | ||
req.configurationId, | ||
); | ||
const updatedProjectEnvVar = updatedProjectEnvVars.find((i) => i.id === req.envVarId); | ||
if (!updatedProjectEnvVar) { | ||
throw new ApplicationError(ErrorCodes.INTERNAL_SERVER_ERROR, "could not update env variable"); | ||
} | ||
|
||
response.environmentVariable = converter.toConfigurationEnvironmentVariable(updatedProjectEnvVar); | ||
return response; | ||
} | ||
|
||
throw new ApplicationError(ErrorCodes.NOT_FOUND, "env variable not found"); | ||
} | ||
|
||
async createConfigurationEnvironmentVariable( | ||
req: PartialMessage<CreateConfigurationEnvironmentVariableRequest>, | ||
): Promise<CreateConfigurationEnvironmentVariableResponse> { | ||
if (!req.configurationId || !req.name || !req.value) { | ||
throw new ApplicationError(ErrorCodes.BAD_REQUEST, "configurationId, name and value are required"); | ||
} | ||
|
||
const response = new CreateConfigurationEnvironmentVariableResponse(); | ||
|
||
await getGitpodService().server.setProjectEnvironmentVariable( | ||
req.configurationId, | ||
req.name, | ||
req.value, | ||
req.admission === EnvironmentVariableAdmission.PREBUILD ? true : false, | ||
); | ||
|
||
const updatedProjectEnvVars = await getGitpodService().server.getProjectEnvironmentVariables( | ||
req.configurationId, | ||
); | ||
const updatedProjectEnvVar = updatedProjectEnvVars.find((v) => v.name === req.name); | ||
if (!updatedProjectEnvVar) { | ||
throw new ApplicationError(ErrorCodes.INTERNAL_SERVER_ERROR, "could not create env variable"); | ||
} | ||
|
||
response.environmentVariable = converter.toConfigurationEnvironmentVariable(updatedProjectEnvVar); | ||
|
||
return response; | ||
} | ||
|
||
async deleteConfigurationEnvironmentVariable( | ||
req: PartialMessage<DeleteConfigurationEnvironmentVariableRequest>, | ||
): Promise<DeleteConfigurationEnvironmentVariableResponse> { | ||
if (!req.envVarId) { | ||
throw new ApplicationError(ErrorCodes.BAD_REQUEST, "envVarId is required"); | ||
} | ||
|
||
await getGitpodService().server.deleteProjectEnvironmentVariable(req.envVarId); | ||
|
||
const response = new DeleteConfigurationEnvironmentVariableResponse(); | ||
return response; | ||
} | ||
|
||
async resolveWorkspaceEnvironmentVariables( | ||
req: PartialMessage<ResolveWorkspaceEnvironmentVariablesRequest>, | ||
): Promise<ResolveWorkspaceEnvironmentVariablesResponse> { | ||
throw new ApplicationError(ErrorCodes.BAD_REQUEST, "Unimplemented"); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.