|
7 | 7 | import { inject, injectable } from "inversify";
|
8 | 8 | import { TypeORM } from "./typeorm";
|
9 | 9 | import { Repository } from "typeorm";
|
| 10 | +import { v4 as uuidv4 } from 'uuid'; |
| 11 | +import { PartialProject, Project, ProjectEnvVar } from "@gitpod/gitpod-protocol"; |
| 12 | +import { EncryptionService } from "@gitpod/gitpod-protocol/lib/encryption/encryption-service"; |
| 13 | +import { censor } from '@gitpod/gitpod-protocol/lib/util/censor'; |
10 | 14 | import { ProjectDB } from "../project-db";
|
11 | 15 | import { DBProject } from "./entity/db-project";
|
12 |
| -import { PartialProject, Project } from "@gitpod/gitpod-protocol"; |
| 16 | +import { DBProjectEnvVar } from "./entity/db-project-env-vars"; |
13 | 17 |
|
14 | 18 | @injectable()
|
15 | 19 | export class ProjectDBImpl implements ProjectDB {
|
16 | 20 | @inject(TypeORM) typeORM: TypeORM;
|
| 21 | + @inject(EncryptionService) protected readonly encryptionService: EncryptionService; |
17 | 22 |
|
18 | 23 | protected async getEntityManager() {
|
19 | 24 | return (await this.typeORM.getConnection()).manager;
|
20 | 25 | }
|
21 | 26 |
|
22 |
| - async getRepo(): Promise<Repository<DBProject>> { |
| 27 | + protected async getRepo(): Promise<Repository<DBProject>> { |
23 | 28 | return (await this.getEntityManager()).getRepository<DBProject>(DBProject);
|
24 | 29 | }
|
25 | 30 |
|
| 31 | + protected async getProjectEnvVarRepo(): Promise<Repository<DBProjectEnvVar>> { |
| 32 | + return (await this.getEntityManager()).getRepository<DBProjectEnvVar>(DBProjectEnvVar); |
| 33 | + } |
| 34 | + |
26 | 35 | public async findProjectById(projectId: string): Promise<Project | undefined> {
|
27 | 36 | const repo = await this.getRepo();
|
28 | 37 | return repo.findOne({ id: projectId, markedDeleted: false });
|
@@ -93,4 +102,39 @@ export class ProjectDBImpl implements ProjectDB {
|
93 | 102 | await repo.save(project);
|
94 | 103 | }
|
95 | 104 | }
|
| 105 | + |
| 106 | + public async setProjectEnvironmentVariable(projectId: string, name: string, value: string): Promise<void>{ |
| 107 | + const envVarRepo = await this.getProjectEnvVarRepo(); |
| 108 | + let envVar = await envVarRepo.findOne({ projectId, name, deleted: false }); |
| 109 | + if (envVar) { |
| 110 | + envVar.value = value; |
| 111 | + } else { |
| 112 | + envVar = { |
| 113 | + id: uuidv4(), |
| 114 | + projectId, |
| 115 | + name, |
| 116 | + value, |
| 117 | + creationTime: new Date().toISOString(), |
| 118 | + deleted: false, |
| 119 | + }; |
| 120 | + } |
| 121 | + await envVarRepo.save(envVar); |
| 122 | + } |
| 123 | + |
| 124 | + public async getProjectEnvironmentVariables(projectId: string): Promise<ProjectEnvVar[]> { |
| 125 | + const envVarRepo = await this.getProjectEnvVarRepo(); |
| 126 | + const envVarsWithValue = await envVarRepo.find({ projectId, deleted: false }); |
| 127 | + const envVars = envVarsWithValue.map(v => (censor(v, 'value') as any as ProjectEnvVar)); |
| 128 | + return envVars; |
| 129 | + } |
| 130 | + |
| 131 | + public async deleteProjectEnvironmentVariable(projectId: string, name: string): Promise<void> { |
| 132 | + const envVarRepo = await this.getProjectEnvVarRepo(); |
| 133 | + const envVar = await envVarRepo.findOne({ projectId, name, deleted: false }); |
| 134 | + if (!envVar) { |
| 135 | + throw new Error('A environment variable with this name could not be found for this project'); |
| 136 | + } |
| 137 | + envVar.deleted = true; |
| 138 | + envVarRepo.update(envVar.id, envVar); |
| 139 | + } |
96 | 140 | }
|
0 commit comments