Skip to content

Commit cb6cee0

Browse files
committed
update
1 parent 7eed6d8 commit cb6cee0

File tree

11 files changed

+220
-118
lines changed

11 files changed

+220
-118
lines changed

components/dashboard/src/data/setup.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,12 @@ import * as WorkspaceClasses from "@gitpod/public-api/lib/gitpod/v1/workspace_pb
2222
import * as PaginationClasses from "@gitpod/public-api/lib/gitpod/v1/pagination_pb";
2323
import * as ConfigurationClasses from "@gitpod/public-api/lib/gitpod/v1/configuration_pb";
2424
import * as AuthProviderClasses from "@gitpod/public-api/lib/gitpod/v1/authprovider_pb";
25+
import * as EnvVarClasses from "@gitpod/public-api/lib/gitpod/v1/envvar_pb";
2526

2627
// This is used to version the cache
2728
// If data we cache changes in a non-backwards compatible way, increment this version
2829
// That will bust any previous cache versions a client may have stored
29-
const CACHE_VERSION = "5";
30+
const CACHE_VERSION = "6";
3031

3132
export function noPersistence(queryKey: QueryKey): QueryKey {
3233
return [...queryKey, "no-persistence"];
@@ -145,6 +146,7 @@ function initializeMessages() {
145146
...Object.values(PaginationClasses),
146147
...Object.values(ConfigurationClasses),
147148
...Object.values(AuthProviderClasses),
149+
...Object.values(EnvVarClasses),
148150
];
149151
for (const c of constr) {
150152
if ((c as any).prototype instanceof Message) {

components/dashboard/src/service/json-rpc-envvar-client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,6 @@ export class JsonRpcEnvvarClient implements PromiseClient<typeof EnvironmentVari
224224
async resolveWorkspaceEnvironmentVariables(
225225
req: PartialMessage<ResolveWorkspaceEnvironmentVariablesRequest>,
226226
): Promise<ResolveWorkspaceEnvironmentVariablesResponse> {
227-
throw new Error("Not implemented");
227+
throw new ConnectError("Unimplemented", Code.Unimplemented);
228228
}
229229
}

components/gitpod-db/src/project-db.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,11 @@ export interface ProjectDB extends TransactionalDB<ProjectDB> {
2020
projectId: string,
2121
envVar: ProjectEnvVarWithValue,
2222
): Promise<ProjectEnvVar | undefined>;
23-
addProjectEnvironmentVariable(projectId: string, envVar: ProjectEnvVarWithValue): Promise<void>;
24-
updateProjectEnvironmentVariable(projectId: string, envVar: Required<ProjectEnvVarWithValue>): Promise<void>;
23+
addProjectEnvironmentVariable(projectId: string, envVar: ProjectEnvVarWithValue): Promise<ProjectEnvVar>;
24+
updateProjectEnvironmentVariable(
25+
projectId: string,
26+
envVar: Partial<ProjectEnvVarWithValue>,
27+
): Promise<ProjectEnvVar | undefined>;
2528
getProjectEnvironmentVariables(projectId: string): Promise<ProjectEnvVar[]>;
2629
getProjectEnvironmentVariableById(variableId: string): Promise<ProjectEnvVar | undefined>;
2730
deleteProjectEnvironmentVariable(variableId: string): Promise<void>;

components/gitpod-db/src/typeorm/project-db-impl.ts

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import { DBProjectInfo } from "./entity/db-project-info";
1616
import { DBProjectUsage } from "./entity/db-project-usage";
1717
import { TransactionalDBImpl } from "./transactional-db-impl";
1818
import { TypeORM } from "./typeorm";
19+
import { ApplicationError, ErrorCodes } from "@gitpod/gitpod-protocol/lib/messaging/error";
20+
import { filter } from "../utils";
1921

2022
function toProjectEnvVar(envVarWithValue: DBProjectEnvVar): ProjectEnvVar {
2123
const envVar = { ...envVarWithValue };
@@ -145,9 +147,12 @@ export class ProjectDBImpl extends TransactionalDBImpl<ProjectDB> implements Pro
145147
return envVarRepo.findOne({ projectId, name: envVar.name, deleted: false });
146148
}
147149

148-
public async addProjectEnvironmentVariable(projectId: string, envVar: ProjectEnvVarWithValue): Promise<void> {
150+
public async addProjectEnvironmentVariable(
151+
projectId: string,
152+
envVar: ProjectEnvVarWithValue,
153+
): Promise<ProjectEnvVar> {
149154
const envVarRepo = await this.getProjectEnvVarRepo();
150-
await envVarRepo.save({
155+
const insertedEnvVar = await envVarRepo.save({
151156
id: uuidv4(),
152157
projectId,
153158
name: envVar.name,
@@ -156,14 +161,31 @@ export class ProjectDBImpl extends TransactionalDBImpl<ProjectDB> implements Pro
156161
creationTime: new Date().toISOString(),
157162
deleted: false,
158163
});
164+
return toProjectEnvVar(insertedEnvVar);
159165
}
160166

161167
public async updateProjectEnvironmentVariable(
162168
projectId: string,
163-
envVar: Required<ProjectEnvVarWithValue>,
164-
): Promise<void> {
165-
const envVarRepo = await this.getProjectEnvVarRepo();
166-
await envVarRepo.update({ id: envVar.id, projectId }, { value: envVar.value, censored: envVar.censored });
169+
envVar: Partial<ProjectEnvVarWithValue>,
170+
): Promise<ProjectEnvVar | undefined> {
171+
if (!envVar.id) {
172+
throw new ApplicationError(ErrorCodes.NOT_FOUND, "An environment variable with this ID could not be found");
173+
}
174+
175+
return await this.transaction(async (_, ctx) => {
176+
const envVarRepo = ctx.entityManager.getRepository<DBProjectEnvVar>(DBProjectEnvVar);
177+
178+
await envVarRepo.update(
179+
{ id: envVar.id, projectId },
180+
filter(envVar, (_, v) => v !== null && v !== undefined),
181+
);
182+
183+
const found = await envVarRepo.findOne({ id: envVar.id, projectId, deleted: false });
184+
if (!found) {
185+
return;
186+
}
187+
return toProjectEnvVar(found);
188+
});
167189
}
168190

169191
public async getProjectEnvironmentVariables(projectId: string): Promise<ProjectEnvVar[]> {

components/gitpod-db/src/typeorm/user-db-impl.ts

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ import { DataCache } from "../data-cache";
5050
import { TransactionalDBImpl } from "./transactional-db-impl";
5151
import { TypeORM } from "./typeorm";
5252
import { ApplicationError, ErrorCodes } from "@gitpod/gitpod-protocol/lib/messaging/error";
53+
import { filter } from "../utils";
5354

5455
// OAuth token expiry
5556
const tokenExpiryInFuture = new DateInterval("7d");
@@ -395,9 +396,9 @@ export class TypeORMUserDBImpl extends TransactionalDBImpl<UserDB> implements Us
395396
});
396397
}
397398

398-
public async addEnvVar(userId: string, envVar: UserEnvVarValue): Promise<void> {
399+
public async addEnvVar(userId: string, envVar: UserEnvVarValue): Promise<UserEnvVar> {
399400
const repo = await this.getUserEnvVarRepo();
400-
await repo.save({
401+
return await repo.save({
401402
id: uuidv4(),
402403
userId,
403404
name: envVar.name,
@@ -406,15 +407,22 @@ export class TypeORMUserDBImpl extends TransactionalDBImpl<UserDB> implements Us
406407
});
407408
}
408409

409-
public async updateEnvVar(userId: string, envVar: Required<UserEnvVarValue>): Promise<void> {
410-
const repo = await this.getUserEnvVarRepo();
411-
await repo.update(
412-
{
413-
id: envVar.id,
414-
userId: userId,
415-
},
416-
{ name: envVar.name, repositoryPattern: envVar.repositoryPattern, value: envVar.value },
417-
);
410+
public async updateEnvVar(userId: string, envVar: Partial<UserEnvVarValue>): Promise<UserEnvVar | undefined> {
411+
if (!envVar.id) {
412+
throw new ApplicationError(ErrorCodes.NOT_FOUND, "An environment variable with this ID could not be found");
413+
}
414+
415+
return await this.transaction(async (_, ctx) => {
416+
const envVarRepo = ctx.entityManager.getRepository<DBUserEnvVar>(DBUserEnvVar);
417+
418+
await envVarRepo.update(
419+
{ id: envVar.id, userId },
420+
filter(envVar, (_, v) => v !== null && v !== undefined),
421+
);
422+
423+
const found = await envVarRepo.findOne({ id: envVar.id, userId, deleted: false });
424+
return found;
425+
});
418426
}
419427

420428
public async getEnvVars(userId: string): Promise<UserEnvVar[]> {

components/gitpod-db/src/user-db.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,8 @@ export interface UserDB extends OAuthUserRepository, OAuthTokenRepository, Trans
114114
findUsersByEmail(email: string): Promise<User[]>;
115115

116116
findEnvVar(userId: string, envVar: UserEnvVarValue): Promise<UserEnvVar | undefined>;
117-
addEnvVar(userId: string, envVar: UserEnvVarValue): Promise<void>;
118-
updateEnvVar(userId: string, envVar: UserEnvVarValue): Promise<void>;
117+
addEnvVar(userId: string, envVar: UserEnvVarValue): Promise<UserEnvVar>;
118+
updateEnvVar(userId: string, envVar: Partial<UserEnvVarValue>): Promise<UserEnvVar | undefined>;
119119
deleteEnvVar(envVar: UserEnvVar): Promise<void>;
120120
getEnvVars(userId: string): Promise<UserEnvVar[]>;
121121

components/gitpod-db/src/utils.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* Copyright (c) 2021 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+
export function filter(
8+
obj: { [key: string]: any },
9+
predicate: (key: string, value: any) => boolean,
10+
): { [key: string]: any } {
11+
const result = Object.create(null);
12+
for (const [key, value] of Object.entries(obj)) {
13+
if (predicate(key, value)) {
14+
result[key] = value;
15+
}
16+
}
17+
return result;
18+
}

components/gitpod-protocol/src/public-api-converter.spec.ts

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import { Timestamp } from "@bufbuild/protobuf";
99
import {
1010
AdmissionLevel,
11+
WorkspaceEnvironmentVariable,
1112
WorkspacePhase_Phase,
1213
WorkspacePort_Policy,
1314
WorkspacePort_Protocol,
@@ -21,12 +22,17 @@ import {
2122
PrebuildSettings,
2223
WorkspaceSettings,
2324
} from "@gitpod/public-api/lib/gitpod/v1/configuration_pb";
24-
import { AuthProviderEntry, AuthProviderInfo } from "./protocol";
25+
import { AuthProviderEntry, AuthProviderInfo, ProjectEnvVar, UserEnvVarValue, WithEnvvarsContext } from "./protocol";
2526
import {
2627
AuthProvider,
2728
AuthProviderDescription,
2829
AuthProviderType,
2930
} from "@gitpod/public-api/lib/gitpod/v1/authprovider_pb";
31+
import {
32+
ConfigurationEnvironmentVariable,
33+
EnvironmentVariableAdmission,
34+
UserEnvironmentVariable,
35+
} from "@gitpod/public-api/lib/gitpod/v1/envvar_pb";
3036

3137
describe("PublicAPIConverter", () => {
3238
const converter = new PublicAPIConverter();
@@ -808,4 +814,59 @@ describe("PublicAPIConverter", () => {
808814
}
809815
});
810816
});
817+
818+
describe("toWorkspaceEnvironmentVariables", () => {
819+
const wsCtx: WithEnvvarsContext = {
820+
title: "title",
821+
envvars: [
822+
{
823+
name: "FOO",
824+
value: "bar",
825+
},
826+
],
827+
};
828+
const envVars = [new WorkspaceEnvironmentVariable({ name: "FOO", value: "bar" })];
829+
it("should convert workspace environment variable types", () => {
830+
const result = converter.toWorkspaceEnvironmentVariables(wsCtx);
831+
expect(result).to.deep.equal(envVars);
832+
});
833+
});
834+
835+
describe("toUserEnvironmentVariable", () => {
836+
const envVar: UserEnvVarValue = {
837+
id: "1",
838+
name: "FOO",
839+
value: "bar",
840+
repositoryPattern: "*/*",
841+
};
842+
const userEnvVar = new UserEnvironmentVariable({
843+
id: "1",
844+
name: "FOO",
845+
value: "bar",
846+
repositoryPattern: "*/*",
847+
});
848+
it("should convert user environment variable types", () => {
849+
const result = converter.toUserEnvironmentVariable(envVar);
850+
expect(result).to.deep.equal(userEnvVar);
851+
});
852+
});
853+
854+
describe("toConfigurationEnvironmentVariable", () => {
855+
const envVar: ProjectEnvVar = {
856+
id: "1",
857+
name: "FOO",
858+
censored: true,
859+
projectId: "1",
860+
};
861+
const userEnvVar = new ConfigurationEnvironmentVariable({
862+
id: "1",
863+
name: "FOO",
864+
admission: EnvironmentVariableAdmission.PREBUILD,
865+
configurationId: "1",
866+
});
867+
it("should convert configuration environment variable types", () => {
868+
const result = converter.toConfigurationEnvironmentVariable(envVar);
869+
expect(result).to.deep.equal(userEnvVar);
870+
});
871+
});
811872
});

0 commit comments

Comments
 (0)