6
6
7
7
import {
8
8
OrganizationSettings ,
9
+ OrgEnvVar ,
10
+ OrgEnvVarWithValue ,
9
11
Team ,
10
12
TeamMemberInfo ,
11
13
TeamMemberRole ,
@@ -25,21 +27,32 @@ import { DBOrgSettings } from "./entity/db-team-settings";
25
27
import { DBUser } from "./entity/db-user" ;
26
28
import { TransactionalDBImpl } from "./transactional-db-impl" ;
27
29
import { TypeORM } from "./typeorm" ;
30
+ import { EncryptionService } from "@gitpod/gitpod-protocol/lib/encryption/encryption-service" ;
31
+ import { DBOrgEnvVar } from "./entity/db-org-env-var" ;
32
+ import { filter } from "../utils" ;
28
33
29
34
@injectable ( )
30
35
export class TeamDBImpl extends TransactionalDBImpl < TeamDB > implements TeamDB {
31
- constructor ( @inject ( TypeORM ) typeorm : TypeORM , @optional ( ) transactionalEM ?: EntityManager ) {
36
+ constructor (
37
+ @inject ( TypeORM ) typeorm : TypeORM ,
38
+ @inject ( EncryptionService ) private readonly encryptionService : EncryptionService ,
39
+ @optional ( ) transactionalEM ?: EntityManager ,
40
+ ) {
32
41
super ( typeorm , transactionalEM ) ;
33
42
}
34
43
35
44
protected createTransactionalDB ( transactionalEM : EntityManager ) : TeamDB {
36
- return new TeamDBImpl ( this . typeorm , transactionalEM ) ;
45
+ return new TeamDBImpl ( this . typeorm , this . encryptionService , transactionalEM ) ;
37
46
}
38
47
39
48
private async getTeamRepo ( ) : Promise < Repository < DBTeam > > {
40
49
return ( await this . getEntityManager ( ) ) . getRepository < DBTeam > ( DBTeam ) ;
41
50
}
42
51
52
+ private async getOrgEnvVarRepo ( ) : Promise < Repository < DBOrgEnvVar > > {
53
+ return ( await this . getEntityManager ( ) ) . getRepository < DBOrgEnvVar > ( DBOrgEnvVar ) ;
54
+ }
55
+
43
56
private async getMembershipRepo ( ) : Promise < Repository < DBTeamMembership > > {
44
57
return ( await this . getEntityManager ( ) ) . getRepository < DBTeamMembership > ( DBTeamMembership ) ;
45
58
}
@@ -408,4 +421,83 @@ export class TeamDBImpl extends TransactionalDBImpl<TeamDB> implements TeamDB {
408
421
) ;
409
422
return result . length === 1 ;
410
423
}
424
+
425
+ public async addOrgEnvironmentVariable ( orgId : string , envVar : OrgEnvVarWithValue ) : Promise < OrgEnvVar > {
426
+ const envVarRepo = await this . getOrgEnvVarRepo ( ) ;
427
+ const insertedEnvVar = await envVarRepo . save ( {
428
+ id : uuidv4 ( ) ,
429
+ orgId,
430
+ name : envVar . name ,
431
+ value : envVar . value ,
432
+ creationTime : new Date ( ) . toISOString ( ) ,
433
+ } ) ;
434
+ return toOrgEnvVar ( insertedEnvVar ) ;
435
+ }
436
+
437
+ public async updateOrgEnvironmentVariable (
438
+ orgId : string ,
439
+ envVar : Partial < OrgEnvVarWithValue > ,
440
+ ) : Promise < OrgEnvVar | undefined > {
441
+ if ( ! envVar . id ) {
442
+ throw new ApplicationError ( ErrorCodes . NOT_FOUND , "An environment variable with this ID could not be found" ) ;
443
+ }
444
+
445
+ return await this . transaction ( async ( _ , ctx ) => {
446
+ const envVarRepo = ctx . entityManager . getRepository < DBOrgEnvVar > ( DBOrgEnvVar ) ;
447
+
448
+ await envVarRepo . update (
449
+ { id : envVar . id , orgId } ,
450
+ filter ( envVar , ( _ , v ) => v !== null && v !== undefined ) ,
451
+ ) ;
452
+
453
+ const found = await envVarRepo . findOne ( { id : envVar . id , orgId } ) ;
454
+ if ( ! found ) {
455
+ return ;
456
+ }
457
+ return toOrgEnvVar ( found ) ;
458
+ } ) ;
459
+ }
460
+
461
+ public async getOrgEnvironmentVariableById ( id : string ) : Promise < OrgEnvVar | undefined > {
462
+ const envVarRepo = await this . getOrgEnvVarRepo ( ) ;
463
+ const envVarWithValue = await envVarRepo . findOne ( { id } ) ;
464
+ if ( ! envVarWithValue ) {
465
+ return undefined ;
466
+ }
467
+ const envVar = toOrgEnvVar ( envVarWithValue ) ;
468
+ return envVar ;
469
+ }
470
+
471
+ public async findOrgEnvironmentVariableByName ( orgId : string , name : string ) : Promise < OrgEnvVar | undefined > {
472
+ const envVarRepo = await this . getOrgEnvVarRepo ( ) ;
473
+ return envVarRepo . findOne ( { orgId, name } ) ;
474
+ }
475
+
476
+ public async getOrgEnvironmentVariables ( orgId : string ) : Promise < OrgEnvVar [ ] > {
477
+ const envVarRepo = await this . getOrgEnvVarRepo ( ) ;
478
+ const envVarsWithValue = await envVarRepo . find ( { orgId } ) ;
479
+ const envVars = envVarsWithValue . map ( toOrgEnvVar ) ;
480
+ return envVars ;
481
+ }
482
+
483
+ public async getOrgEnvironmentVariableValues ( envVars : OrgEnvVar [ ] ) : Promise < OrgEnvVarWithValue [ ] > {
484
+ const envVarRepo = await this . getOrgEnvVarRepo ( ) ;
485
+ const envVarsWithValues = await envVarRepo . findByIds ( envVars ) ;
486
+ return envVarsWithValues ;
487
+ }
488
+
489
+ public async deleteOrgEnvironmentVariable ( id : string ) : Promise < void > {
490
+ const envVarRepo = await this . getOrgEnvVarRepo ( ) ;
491
+ await envVarRepo . delete ( { id } ) ;
492
+ }
493
+ }
494
+
495
+ /**
496
+ * @param envVarWithValue
497
+ * @returns DBOrgEnvVar shape turned into an OrgEnvVar by dropping the "value" property
498
+ */
499
+ function toOrgEnvVar ( envVarWithValue : DBOrgEnvVar ) : OrgEnvVar {
500
+ const envVar = { ...envVarWithValue } ;
501
+ delete ( envVar as any ) [ "value" ] ;
502
+ return envVar ;
411
503
}
0 commit comments