Skip to content

Commit cc9ecec

Browse files
committed
💄
1 parent 7a426dc commit cc9ecec

File tree

8 files changed

+284
-299
lines changed

8 files changed

+284
-299
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@ export interface ProjectDB extends TransactionalDB<ProjectDB> {
2323
storeProject(project: Project): Promise<Project>;
2424
updateProject(partialProject: PartialProject): Promise<void>;
2525
markDeleted(projectId: string): Promise<void>;
26-
setProjectEnvironmentVariable(projectId: string, name: string, value: string, censored: boolean): Promise<void>;
26+
findProjectEnvironmentVariable(
27+
projectId: string,
28+
envVar: ProjectEnvVarWithValue,
29+
): Promise<ProjectEnvVar | undefined>;
30+
setProjectEnvironmentVariable(projectId: string, envVar: ProjectEnvVarWithValue): Promise<void>;
2731
getProjectEnvironmentVariables(projectId: string): Promise<ProjectEnvVar[]>;
2832
getProjectEnvironmentVariableById(variableId: string): Promise<ProjectEnvVar | undefined>;
2933
deleteProjectEnvironmentVariable(variableId: string): Promise<void>;

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

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import { DBProjectUsage } from "./entity/db-project-usage";
1717
import { TransactionalDBImpl } from "./transactional-db-impl";
1818
import { TypeORM } from "./typeorm";
1919

20-
function toProjectEnvVar(envVarWithValue: ProjectEnvVarWithValue): ProjectEnvVar {
20+
function toProjectEnvVar(envVarWithValue: DBProjectEnvVar): ProjectEnvVar {
2121
const envVar = { ...envVarWithValue };
2222
delete (envVar as any)["value"];
2323
return envVar;
@@ -160,35 +160,38 @@ export class ProjectDBImpl extends TransactionalDBImpl<ProjectDB> implements Pro
160160
}
161161
}
162162

163-
public async setProjectEnvironmentVariable(
163+
public async findProjectEnvironmentVariable(
164164
projectId: string,
165-
name: string,
166-
value: string,
167-
censored: boolean,
168-
): Promise<void> {
169-
if (!name) {
165+
envVar: ProjectEnvVarWithValue,
166+
): Promise<ProjectEnvVar | undefined> {
167+
const envVarRepo = await this.getProjectEnvVarRepo();
168+
return envVarRepo.findOne({ projectId, name: envVar.name, deleted: false });
169+
}
170+
171+
public async setProjectEnvironmentVariable(projectId: string, envVar: ProjectEnvVarWithValue): Promise<void> {
172+
if (!envVar.name) {
170173
throw new Error("Variable name cannot be empty");
171174
}
172-
if (!/^[a-zA-Z_][a-zA-Z0-9_]*$/.test(name)) {
175+
if (!/^[a-zA-Z_][a-zA-Z0-9_]*$/.test(envVar.name)) {
173176
throw new Error(
174177
"Please choose a variable name containing only letters, numbers, or _, and which doesn't start with a number",
175178
);
176179
}
177180
const envVarRepo = await this.getProjectEnvVarRepo();
178-
const envVarWithValue = await envVarRepo.findOne({ projectId, name, deleted: false });
181+
const envVarWithValue = await envVarRepo.findOne({ projectId, name: envVar.name, deleted: false });
179182
if (envVarWithValue) {
180183
await envVarRepo.update(
181184
{ id: envVarWithValue.id, projectId: envVarWithValue.projectId },
182-
{ value, censored },
185+
{ value: envVar.value, censored: envVar.censored },
183186
);
184187
return;
185188
}
186189
await envVarRepo.save({
187190
id: uuidv4(),
188191
projectId,
189-
name,
190-
value,
191-
censored,
192+
name: envVar.name,
193+
value: envVar.value,
194+
censored: envVar.censored,
192195
creationTime: new Date().toISOString(),
193196
deleted: false,
194197
});

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

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
TokenEntry,
1616
User,
1717
UserEnvVar,
18+
UserEnvVarValue,
1819
UserSSHPublicKey,
1920
} from "@gitpod/gitpod-protocol";
2021
import { EncryptionService } from "@gitpod/gitpod-protocol/lib/encryption/encryption-service";
@@ -28,7 +29,7 @@ import {
2829
OAuthUser,
2930
} from "@jmondi/oauth2-server";
3031
import { inject, injectable, optional } from "inversify";
31-
import { EntityManager, Repository } from "typeorm";
32+
import { EntityManager, Equal, Not, Repository } from "typeorm";
3233
import { v4 as uuidv4 } from "uuid";
3334
import {
3435
BUILTIN_WORKSPACE_PROBE_USER_ID,
@@ -396,9 +397,27 @@ export class TypeORMUserDBImpl extends TransactionalDBImpl<UserDB> implements Us
396397
return Number.parseInt(count);
397398
}
398399

399-
public async setEnvVar(envVar: UserEnvVar): Promise<void> {
400+
public async findEnvVar(userId: string, envVar: UserEnvVarValue): Promise<UserEnvVar | undefined> {
400401
const repo = await this.getUserEnvVarRepo();
401-
await repo.save(envVar);
402+
return repo.findOne({
403+
where: {
404+
userId,
405+
name: envVar.name,
406+
repositoryPattern: envVar.repositoryPattern,
407+
deleted: Not(Equal(true)),
408+
},
409+
});
410+
}
411+
412+
public async setEnvVar(userId: string, envVar: UserEnvVarValue): Promise<void> {
413+
const repo = await this.getUserEnvVarRepo();
414+
await repo.save({
415+
id: envVar.id || uuidv4(),
416+
userId,
417+
name: envVar.name,
418+
repositoryPattern: envVar.repositoryPattern,
419+
value: envVar.value,
420+
});
402421
}
403422

404423
public async getEnvVars(userId: string): Promise<UserEnvVar[]> {
@@ -408,9 +427,8 @@ export class TypeORMUserDBImpl extends TransactionalDBImpl<UserDB> implements Us
408427
}
409428

410429
public async deleteEnvVar(envVar: UserEnvVar): Promise<void> {
411-
envVar.deleted = true;
412430
const repo = await this.getUserEnvVarRepo();
413-
await repo.save(envVar);
431+
await repo.update({ userId: envVar.userId, id: envVar.id }, { deleted: true });
414432
}
415433

416434
public async hasSSHPublicKey(userId: string): Promise<boolean> {

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
TokenEntry,
1515
User,
1616
UserEnvVar,
17+
UserEnvVarValue,
1718
UserSSHPublicKey,
1819
} from "@gitpod/gitpod-protocol";
1920
import { OAuthTokenRepository, OAuthUserRepository } from "@jmondi/oauth2-server";
@@ -112,7 +113,8 @@ export interface UserDB extends OAuthUserRepository, OAuthTokenRepository, Trans
112113
*/
113114
findUsersByEmail(email: string): Promise<User[]>;
114115

115-
setEnvVar(envVar: UserEnvVar): Promise<void>;
116+
findEnvVar(userId: string, envVar: UserEnvVarValue): Promise<UserEnvVar | undefined>;
117+
setEnvVar(userId: string, envVar: UserEnvVarValue): Promise<void>;
116118
deleteEnvVar(envVar: UserEnvVar): Promise<void>;
117119
getEnvVars(userId: string): Promise<UserEnvVar[]>;
118120

components/gitpod-protocol/src/protocol.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -396,12 +396,14 @@ export interface EnvVarWithValue {
396396
}
397397

398398
export interface ProjectEnvVarWithValue extends EnvVarWithValue {
399-
id: string;
400-
projectId: string;
399+
id?: string;
401400
censored: boolean;
402401
}
403402

404-
export type ProjectEnvVar = Omit<ProjectEnvVarWithValue, "value">;
403+
export interface ProjectEnvVar extends Omit<ProjectEnvVarWithValue, "value"> {
404+
id: string;
405+
projectId: string;
406+
}
405407

406408
export interface UserEnvVarValue extends EnvVarWithValue {
407409
id?: string;

0 commit comments

Comments
 (0)