Skip to content

Commit ce66d27

Browse files
author
Simon Emms
committed
[installer-admin]: add the database bindings
1 parent e5f8ae2 commit ce66d27

File tree

8 files changed

+125
-5
lines changed

8 files changed

+125
-5
lines changed

components/gitpod-db/src/container-module.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ import { ProjectDBImpl } from './typeorm/project-db-impl';
5757
import { EntityManager } from 'typeorm';
5858
import { OssAllowListDB } from './oss-allowlist-db';
5959
import { OssAllowListDBImpl } from './typeorm/oss-allowlist-db-impl';
60+
import { TypeORMInstallationAdminImpl } from './typeorm/installation-admin-db-impl';
61+
import { InstallationAdminDB } from './installation-admin-db';
6062

6163
// THE DB container module that contains all DB implementations
6264
export const dbContainerModule = new ContainerModule((bind, unbind, isBound, rebind) => {
@@ -72,6 +74,9 @@ export const dbContainerModule = new ContainerModule((bind, unbind, isBound, reb
7274
bind(TermsAcceptanceDB).toService(TermsAcceptanceDBImpl);
7375
bindDbWithTracing(TracedUserDB, bind, UserDB).inSingletonScope();
7476

77+
bind(TypeORMInstallationAdminImpl).toSelf().inSingletonScope();
78+
bind(InstallationAdminDB).toService(TypeORMInstallationAdminImpl);
79+
7580
bind(AuthProviderEntryDB).to(AuthProviderEntryDBImpl).inSingletonScope();
7681

7782
bind(TypeORMWorkspaceDBImpl).toSelf().inSingletonScope();

components/gitpod-db/src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,5 @@ export * from "./edu-email-domain-db";
3636
export * from "./email-domain-filter-db";
3737
export * from "./typeorm/entity/db-account-entry";
3838
export * from "./project-db";
39-
export * from "./team-db";
39+
export * from "./team-db";
40+
export * from "./installation-admin-db";
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* Copyright (c) 2022 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 { InstallationAdmin } from "@gitpod/gitpod-protocol";
8+
9+
export const InstallationAdminDB = Symbol('InstallationAdminDB');
10+
export interface InstallationAdminDB {
11+
getData(): Promise<InstallationAdmin>
12+
}
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+
import { InstallationAdmin } from "@gitpod/gitpod-protocol";
8+
import { Entity, Column, PrimaryColumn } from "typeorm";
9+
import { TypeORM } from "../typeorm";
10+
11+
@Entity()
12+
export class DBInstallationAdmin implements InstallationAdmin {
13+
@PrimaryColumn(TypeORM.UUID_COLUMN_TYPE)
14+
id: string;
15+
16+
@Column()
17+
sendTelemetry: boolean
18+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**
2+
* Copyright (c) 2022 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 { inject, injectable, } from 'inversify';
8+
import { InstallationAdmin } from '@gitpod/gitpod-protocol';
9+
import { Repository } from 'typeorm';
10+
import { TypeORM } from './typeorm';
11+
import { InstallationAdminDB } from '../installation-admin-db';
12+
import { DBInstallationAdmin } from './entity/db-installation-admin';
13+
14+
@injectable()
15+
export class TypeORMInstallationAdminImpl implements InstallationAdminDB {
16+
@inject(TypeORM) typeORM: TypeORM;
17+
18+
protected async getEntityManager() {
19+
return (await this.typeORM.getConnection()).manager;
20+
}
21+
22+
protected async createDefaultRecord(): Promise<InstallationAdmin> {
23+
const record = new DBInstallationAdmin();
24+
25+
record.sendTelemetry = true;
26+
27+
const repo = await this.getInstallationAdminRepo()
28+
return repo.save(record);
29+
}
30+
31+
async getInstallationAdminRepo(): Promise<Repository<DBInstallationAdmin>> {
32+
return (await this.getEntityManager()).getRepository<DBInstallationAdmin>(DBInstallationAdmin);
33+
}
34+
35+
/**
36+
* Get Data
37+
*
38+
* Returns the first record found or creates a
39+
* new record.
40+
*
41+
* @returns Promise<InstallationAdmin>
42+
*/
43+
async getData(): Promise<InstallationAdmin> {
44+
const repo = await this.getInstallationAdminRepo()
45+
const [record] = await repo.find()
46+
47+
if (record) {
48+
return record;
49+
}
50+
51+
/* Record not found - create one */
52+
return this.createDefaultRecord();
53+
}
54+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* Copyright (c) 2022 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 { MigrationInterface, QueryRunner } from "typeorm";
8+
import { tableExists } from "./helper/helper";
9+
10+
export class InstallationAdminTable1642422506330 implements MigrationInterface {
11+
12+
public async up(queryRunner: QueryRunner): Promise<void> {
13+
await queryRunner.query("CREATE TABLE IF NOT EXISTS `d_b_installation_admin` (`id` char(128) NOT NULL, `sendTelemetry` tinyint(4) NOT NULL DEFAULT 0, `_lastModified` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), PRIMARY KEY(`id`)) ENGINE=InnoDB");
14+
}
15+
16+
public async down(queryRunner: QueryRunner): Promise<void> {
17+
if (await tableExists(queryRunner, "d_b_installation_admin")) {
18+
await queryRunner.query("DROP TABLE `d_b_installation_admin`");
19+
}
20+
}
21+
22+
}

components/gitpod-protocol/src/teams-projects-protocol.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,11 @@ export interface Team {
117117
deleted?: boolean;
118118
}
119119

120+
export interface InstallationAdmin {
121+
id: string;
122+
sendTelemetry: boolean;
123+
}
124+
120125
export type TeamMemberRole = "owner" | "member";
121126

122127
export interface TeamMemberInfo {

components/server/src/installation-admin/installation-admin-controller.ts

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

7-
import { injectable } from 'inversify';
7+
import { injectable, inject } from 'inversify';
88
import * as express from 'express';
9+
import { InstallationAdminDB } from '@gitpod/gitpod-db/lib';
910

1011
@injectable()
1112
export class InstallationAdminController {
13+
@inject(InstallationAdminDB) protected readonly installationAdminDb: InstallationAdminDB;
14+
1215
get apiRouter(): express.Router {
1316
const router = express.Router();
1417

1518
router.get('/data', async (req: express.Request, res: express.Response) => {
16-
res.send({
17-
sendTelemetry: false
18-
});
19+
const data = await this.installationAdminDb.getData();
20+
21+
res.send(data);
1922
});
2023

2124
return router;

0 commit comments

Comments
 (0)