Skip to content

Commit 599e7b9

Browse files
committed
Migrate envvarService
1 parent f7e11f5 commit 599e7b9

File tree

9 files changed

+587
-30
lines changed

9 files changed

+587
-30
lines changed

components/dashboard/src/data/projects/set-project-env-var-mutation.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,22 @@
55
*/
66

77
import { useMutation } from "@tanstack/react-query";
8-
import { getGitpodService } from "../../service/service";
8+
import { envVarClient } from "../../service/public-api";
9+
import { EnvironmentVariableAdmission } from "@gitpod/public-api/lib/gitpod/v1/envvar_pb";
910

1011
type SetProjectEnvVarArgs = {
1112
projectId: string;
1213
name: string;
1314
value: string;
14-
censored: boolean;
15+
admission: EnvironmentVariableAdmission;
1516
};
1617
export const useSetProjectEnvVar = () => {
17-
return useMutation<void, Error, SetProjectEnvVarArgs>(async ({ projectId, name, value, censored }) => {
18-
return getGitpodService().server.setProjectEnvironmentVariable(projectId, name, value, censored);
18+
return useMutation<void, Error, SetProjectEnvVarArgs>(async ({ projectId, name, value, admission }) => {
19+
await envVarClient.createConfigurationEnvironmentVariable({
20+
name,
21+
value,
22+
configurationId: projectId,
23+
admission,
24+
});
1925
});
2026
};

components/dashboard/src/projects/ProjectVariables.tsx

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* See License.AGPL.txt in the project root for license information.
55
*/
66

7-
import { Project, ProjectEnvVar } from "@gitpod/gitpod-protocol";
7+
import { Project } from "@gitpod/gitpod-protocol";
88
import { useCallback, useEffect, useState } from "react";
99
import { Redirect } from "react-router";
1010
import Alert from "../components/Alert";
@@ -13,23 +13,29 @@ import InfoBox from "../components/InfoBox";
1313
import { Item, ItemField, ItemFieldContextMenu, ItemsList } from "../components/ItemsList";
1414
import Modal, { ModalBody, ModalFooter, ModalFooterAlert, ModalHeader } from "../components/Modal";
1515
import { Heading2, Subheading } from "../components/typography/headings";
16-
import { getGitpodService } from "../service/service";
1716
import { useCurrentProject } from "./project-context";
1817
import { ProjectSettingsPage } from "./ProjectSettings";
1918
import { Button } from "../components/Button";
2019
import { useSetProjectEnvVar } from "../data/projects/set-project-env-var-mutation";
20+
import { envVarClient } from "../service/public-api";
21+
import {
22+
ConfigurationEnvironmentVariable,
23+
EnvironmentVariableAdmission,
24+
} from "@gitpod/public-api/lib/gitpod/v1/envvar_pb";
2125

2226
export default function ProjectVariablesPage() {
2327
const { project, loading } = useCurrentProject();
24-
const [envVars, setEnvVars] = useState<ProjectEnvVar[]>([]);
28+
const [envVars, setEnvVars] = useState<ConfigurationEnvironmentVariable[]>([]);
2529
const [showAddVariableModal, setShowAddVariableModal] = useState<boolean>(false);
2630

2731
const updateEnvVars = async () => {
2832
if (!project) {
2933
return;
3034
}
31-
const vars = await getGitpodService().server.getProjectEnvironmentVariables(project.id);
32-
const sortedVars = vars.sort((a, b) => (a.name.toLowerCase() > b.name.toLowerCase() ? 1 : -1));
35+
const resp = await envVarClient.listConfigurationEnvironmentVariables({ configurationId: project.id });
36+
const sortedVars = resp.environmentVariables.sort((a, b) =>
37+
a.name.toLowerCase() > b.name.toLowerCase() ? 1 : -1,
38+
);
3339
setEnvVars(sortedVars);
3440
};
3541

@@ -39,7 +45,7 @@ export default function ProjectVariablesPage() {
3945
}, [project]);
4046

4147
const deleteEnvVar = async (variableId: string) => {
42-
await getGitpodService().server.deleteProjectEnvironmentVariable(variableId);
48+
await envVarClient.deleteConfigurationEnvironmentVariable({ envVarId: variableId });
4349
updateEnvVars();
4450
};
4551

@@ -85,7 +91,11 @@ export default function ProjectVariablesPage() {
8591
return (
8692
<Item key={variable.id} className="grid grid-cols-3 items-center">
8793
<ItemField className="truncate">{variable.name}</ItemField>
88-
<ItemField>{variable.censored ? "Hidden" : "Visible"}</ItemField>
94+
<ItemField>
95+
{variable.admission === EnvironmentVariableAdmission.PREBUILD
96+
? "Hidden"
97+
: "Visible"}
98+
</ItemField>
8999
<ItemField className="flex justify-end">
90100
<ItemFieldContextMenu
91101
menuEntries={[
@@ -110,7 +120,7 @@ export default function ProjectVariablesPage() {
110120
function AddVariableModal(props: { project?: Project; onClose: () => void }) {
111121
const [name, setName] = useState<string>("");
112122
const [value, setValue] = useState<string>("");
113-
const [censored, setCensored] = useState<boolean>(true);
123+
const [admission, setAdmission] = useState<EnvironmentVariableAdmission>(EnvironmentVariableAdmission.PREBUILD);
114124
const setProjectEnvVar = useSetProjectEnvVar();
115125

116126
const addVariable = useCallback(async () => {
@@ -123,11 +133,11 @@ function AddVariableModal(props: { project?: Project; onClose: () => void }) {
123133
projectId: props.project.id,
124134
name,
125135
value,
126-
censored,
136+
admission,
127137
},
128138
{ onSuccess: props.onClose },
129139
);
130-
}, [censored, name, props.onClose, props.project, setProjectEnvVar, value]);
140+
}, [admission, name, props.onClose, props.project, setProjectEnvVar, value]);
131141

132142
return (
133143
<Modal visible onClose={props.onClose} onSubmit={addVariable}>
@@ -164,10 +174,16 @@ function AddVariableModal(props: { project?: Project; onClose: () => void }) {
164174
<CheckboxInputField
165175
label="Hide Variable in Workspaces"
166176
hint="Unset this environment variable so that it's not accessible from the terminal in workspaces."
167-
checked={censored}
168-
onChange={() => setCensored(!censored)}
177+
checked={admission === EnvironmentVariableAdmission.PREBUILD}
178+
onChange={() =>
179+
setAdmission(
180+
admission === EnvironmentVariableAdmission.PREBUILD
181+
? EnvironmentVariableAdmission.EVERYWHERE
182+
: EnvironmentVariableAdmission.PREBUILD,
183+
)
184+
}
169185
/>
170-
{!censored && (
186+
{admission === EnvironmentVariableAdmission.EVERYWHERE && (
171187
<div className="mt-4">
172188
<InfoBox>
173189
This variable will be visible to anyone who starts a Gitpod workspace for your repository.
Lines changed: 229 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,229 @@
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+
}

components/dashboard/src/service/public-api.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ import { JsonRpcOrganizationClient } from "./json-rpc-organization-client";
2525
import { JsonRpcWorkspaceClient } from "./json-rpc-workspace-client";
2626
import { JsonRpcAuthProviderClient } from "./json-rpc-authprovider-client";
2727
import { AuthProviderService } from "@gitpod/public-api/lib/gitpod/v1/authprovider_connect";
28+
import { EnvironmentVariableService } from "@gitpod/public-api/lib/gitpod/v1/envvar_connect";
29+
import { JsonRpcEnvvarClient } from "./json-rpc-envvar-client";
2830

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

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

58+
export const envVarClient = createServiceClient(EnvironmentVariableService, new JsonRpcEnvvarClient());
59+
5660
export async function listAllProjects(opts: { orgId: string }): Promise<ProtocolProject[]> {
5761
let pagination = {
5862
page: 1,

0 commit comments

Comments
 (0)