Skip to content

Commit

Permalink
Migrate envvarService
Browse files Browse the repository at this point in the history
  • Loading branch information
jeanp413 committed Nov 14, 2023
1 parent 0e00e3d commit 2d8b9cb
Show file tree
Hide file tree
Showing 13 changed files with 1,676 additions and 11 deletions.
68 changes: 68 additions & 0 deletions components/dashboard/src/service/json-rpc-envvar-client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/**
* 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 { Code, ConnectError, PromiseClient } from "@connectrpc/connect";
import { PartialMessage } from "@bufbuild/protobuf";
import { EnvironmentVariableService } from "@gitpod/public-api/lib/gitpod/v1/envvar_connect";
import {
ListEnvironmentVariablesRequest,
ListEnvironmentVariablesResponse,
UpdateEnvironmentVariableRequest,
UpdateEnvironmentVariableResponse,
DeleteEnvironmentVariableRequest,
DeleteEnvironmentVariableResponse,
} 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";

export class JsonRpcEnvvarClient implements PromiseClient<typeof EnvironmentVariableService> {
async listEnvironmentVariables(
req: PartialMessage<ListEnvironmentVariablesRequest>,
): Promise<ListEnvironmentVariablesResponse> {
const userEnvVars = await getGitpodService().server.getAllEnvVars();
const result = new ListEnvironmentVariablesResponse();
result.environmentVariables = userEnvVars.map((i) => converter.toUserEnvironmentVariable(i));
return result;
}

async updateEnvironmentVariable(
req: PartialMessage<UpdateEnvironmentVariableRequest>,
): Promise<UpdateEnvironmentVariableResponse> {
await getGitpodService().server.setEnvVar({
name: req.envvarName || "",
value: req.envvarValue || "",
repositoryPattern: req.envvarRepositoryPattern || "",
});

req.envvarRepositoryPattern = UserEnvVar.normalizeRepoPattern(req.envvarRepositoryPattern || "");
const updatedUserEnvVars = await getGitpodService().server.getAllEnvVars();
const updatedUserEnvVar = updatedUserEnvVars.find(
(v) => v.name === req.envvarName && v.repositoryPattern === req.envvarRepositoryPattern,
);
if (!updatedUserEnvVar) {
throw new ConnectError("env variable not found", Code.Internal);
}

const response = new UpdateEnvironmentVariableResponse();
response.environmentVariable = converter.toUserEnvironmentVariable(updatedUserEnvVar);
return response;
}

async deleteEnvironmentVariable(
req: PartialMessage<DeleteEnvironmentVariableRequest>,
): Promise<DeleteEnvironmentVariableResponse> {
const variable: UserEnvVarValue = {
name: req.envvarName || "",
value: "",
repositoryPattern: req.envvarRepositoryPattern || "",
};
await getGitpodService().server.deleteEnvVar(variable);

const response = new DeleteEnvironmentVariableResponse();
return response;
}
}
4 changes: 4 additions & 0 deletions components/dashboard/src/service/public-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import { JsonRpcOrganizationClient } from "./json-rpc-organization-client";
import { JsonRpcWorkspaceClient } from "./json-rpc-workspace-client";
import { JsonRpcAuthProviderClient } from "./json-rpc-authprovider-client";
import { AuthProviderService } from "@gitpod/public-api/lib/gitpod/v1/authprovider_connect";
import { EnvironmentVariableService } from "@gitpod/public-api/lib/gitpod/v1/envvar_connect";
import { JsonRpcEnvvarClient } from "./json-rpc-envvar-client";

const transport = createConnectTransport({
baseUrl: `${window.location.protocol}//${window.location.host}/public-api`,
Expand Down Expand Up @@ -53,6 +55,8 @@ export const configurationClient = createServiceClient(ConfigurationService);

export const authProviderClient = createServiceClient(AuthProviderService, new JsonRpcAuthProviderClient());

export const envvarClient = createServiceClient(EnvironmentVariableService, new JsonRpcEnvvarClient());

export async function listAllProjects(opts: { orgId: string }): Promise<ProtocolProject[]> {
let pagination = {
page: 1,
Expand Down
19 changes: 13 additions & 6 deletions components/dashboard/src/user-settings/EnvironmentVariables.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import { useCallback, useEffect, useRef, useState } from "react";
import ConfirmationModal from "../components/ConfirmationModal";
import { Item, ItemField, ItemsList } from "../components/ItemsList";
import Modal, { ModalBody, ModalFooter, ModalHeader } from "../components/Modal";
import { getGitpodService } from "../service/service";
import { PageWithSettingsSubMenu } from "./PageWithSettingsSubMenu";
import { EnvironmentVariableEntry } from "./EnvironmentVariableEntry";
import { Button } from "../components/Button";
import { Heading2, Subheading } from "../components/typography/headings";
import { envvarClient } from "../service/public-api";

interface EnvVarModalProps {
envVar: UserEnvVarValue;
Expand Down Expand Up @@ -150,9 +150,9 @@ export default function EnvVars() {
const [isAddEnvVarModalVisible, setAddEnvVarModalVisible] = useState(false);
const [isDeleteEnvVarModalVisible, setDeleteEnvVarModalVisible] = useState(false);
const update = async () => {
await getGitpodService()
.server.getAllEnvVars()
.then((r) => setEnvVars(r.sort(sortEnvVars)));
await envvarClient
.listEnvironmentVariables({})
.then((r) => setEnvVars(r.environmentVariables.sort(sortEnvVars)));
};

useEffect(() => {
Expand All @@ -178,12 +178,19 @@ export default function EnvVars() {
};

const save = async (variable: UserEnvVarValue) => {
await getGitpodService().server.setEnvVar(variable);
await envvarClient.updateEnvironmentVariable({
envvarName: variable.name,
envvarValue: variable.value,
envvarRepositoryPattern: variable.repositoryPattern,
});
await update();
};

const deleteVariable = async (variable: UserEnvVarValue) => {
await getGitpodService().server.deleteEnvVar(variable);
await envvarClient.deleteEnvironmentVariable({
envvarName: variable.name,
envvarRepositoryPattern: variable.repositoryPattern,
});
await update();
};

Expand Down
21 changes: 16 additions & 5 deletions components/gitpod-protocol/src/public-api-converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import {
WorkspacePort_Protocol,
WorkspaceStatus,
} from "@gitpod/public-api/lib/gitpod/v1/workspace_pb";
import { UserEnvironmentVariable } from "@gitpod/public-api/lib/gitpod/v1/envvar_pb";
import { ContextURL } from "./context-url";
import { ApplicationError, ErrorCode, ErrorCodes } from "./messaging/error";
import {
Expand All @@ -50,6 +51,7 @@ import {
WithPrebuild,
WorkspaceContext,
WorkspaceInfo,
UserEnvVarValue,
} from "./protocol";
import {
OrgMemberInfo,
Expand Down Expand Up @@ -104,7 +106,7 @@ export class PublicAPIConverter {
status.admission = this.toAdmission(arg.workspace.shareable);
status.gitStatus = this.toGitStatus(arg.workspace);
workspace.status = status;
workspace.additionalEnvironmentVariables = this.toEnvironmentVariables(arg.workspace.context);
workspace.additionalEnvironmentVariables = this.toWorkspaceEnvironmentVariables(arg.workspace.context);

if (arg.latestInstance) {
return this.toWorkspace(arg.latestInstance, workspace);
Expand Down Expand Up @@ -221,17 +223,26 @@ export class PublicAPIConverter {
return new ApplicationError(code, reason.message, new TrustedValue(data));
}

toEnvironmentVariables(context: WorkspaceContext): WorkspaceEnvironmentVariable[] {
toWorkspaceEnvironmentVariables(context: WorkspaceContext): WorkspaceEnvironmentVariable[] {
if (WithEnvvarsContext.is(context)) {
return context.envvars.map((envvar) => this.toEnvironmentVariable(envvar));
return context.envvars.map((envvar) => this.toWorkspaceEnvironmentVariable(envvar));
}
return [];
}

toEnvironmentVariable(envVar: EnvVarWithValue): WorkspaceEnvironmentVariable {
toWorkspaceEnvironmentVariable(envVar: EnvVarWithValue): WorkspaceEnvironmentVariable {
const result = new WorkspaceEnvironmentVariable();
result.name = envVar.name;
envVar.value = envVar.value;
result.value = envVar.value;
return result;
}

toUserEnvironmentVariable(envVar: UserEnvVarValue): UserEnvironmentVariable {
const result = new UserEnvironmentVariable();
result.id = envVar.id || "";
result.name = envVar.name;
result.value = envVar.value;
result.repositoryPattern = envVar.repositoryPattern;
return result;
}

Expand Down
59 changes: 59 additions & 0 deletions components/public-api/gitpod/v1/envvar.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
syntax = "proto3";

package gitpod.v1;

option go_package = "github.com/gitpod-io/gitpod/components/public-api/go/v1";

service EnvironmentVariableService {
// ListEnvironmentVariables returns all environment variables for the
// authenticated user.
//
// +return NOT_FOUND User does not have access to a workspace with the given
// ID +return NOT_FOUND Workspace does not exist
rpc ListEnvironmentVariables(ListEnvironmentVariablesRequest)
returns (ListEnvironmentVariablesResponse) {}

// UpdateEnvironmentVariable updates an environment variable for the
// authenticated user.
//
// workspace_id +return NOT_FOUND Workspace does not exist
rpc UpdateEnvironmentVariable(UpdateEnvironmentVariableRequest)
returns (UpdateEnvironmentVariableResponse) {}

// DeleteEnvironmentVariable deletes an environment variable for the
// authenticated user.
//
// workspace_id +return NOT_FOUND Workspace does not exist
rpc DeleteEnvironmentVariable(DeleteEnvironmentVariableRequest)
returns (DeleteEnvironmentVariableResponse) {}
}

message UserEnvironmentVariable {
string id = 1;
string name = 2;
string value = 3;
string repository_pattern = 4;
}

message ListEnvironmentVariablesRequest { optional string workspace_id = 1; }

message ListEnvironmentVariablesResponse {
repeated UserEnvironmentVariable environment_variables = 1;
}

message UpdateEnvironmentVariableRequest {
optional string envvar_name = 1;
optional string envvar_value = 2;
optional string envvar_repository_pattern = 3;
}

message UpdateEnvironmentVariableResponse {
UserEnvironmentVariable environment_variable = 1;
}

message DeleteEnvironmentVariableRequest {
string envvar_name = 1;
string envvar_repository_pattern = 2;
}

message DeleteEnvironmentVariableResponse {}
Loading

0 comments on commit 2d8b9cb

Please sign in to comment.