Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions components/gitpod-db/.eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@
},
"rules": {
"no-var": "error",
"no-void": "error",
"prefer-const": "error",
"@typescript-eslint/ban-ts-comment": "off",
"@typescript-eslint/ban-types": "off",
"@typescript-eslint/explicit-module-boundary-types": "off",
"@typescript-eslint/no-empty-function": "off",
"@typescript-eslint/no-empty-interface": "off",
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/no-floating-promises": "error",
"@typescript-eslint/no-non-null-asserted-optional-chain": "off",
"@typescript-eslint/no-inferrable-types": "off",
"@typescript-eslint/no-misused-promises": [
Expand All @@ -28,6 +30,7 @@
"@typescript-eslint/no-namespace": "off",
"@typescript-eslint/no-non-null-assertion": "off",
"@typescript-eslint/no-this-alias": "off",
"@typescript-eslint/no-unsafe-argument": "error",
"@typescript-eslint/no-unused-vars": [
"error",
{
Expand Down
3 changes: 2 additions & 1 deletion components/gitpod-db/src/migrate-migrations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import { TypeORM } from "./typeorm/typeorm";
import { Config } from "./config";
import { MigrateMigrations0_2_0 } from "./typeorm/migrate-migrations-0_2_0";
import { log } from "@gitpod/gitpod-protocol/lib/util/logging";

async function migrateMigrationsTable() {
const config = new Config();
Expand All @@ -17,7 +18,7 @@ async function migrateMigrationsTable() {
const migration_0_2_0 = new MigrateMigrations0_2_0();
await migration_0_2_0.up(runner);

conn.close();
conn.close().catch((err) => log.error("cannot close connection", err));
console.log("successfully migrated 'migrations' table.");
}

Expand Down
17 changes: 6 additions & 11 deletions components/gitpod-db/src/periodic-deleter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,13 @@ export class PeriodicDbDeleter {
const pendingDeletions: Promise<void>[] = [];
for (const { deletions } of toBeDeleted.reverse()) {
for (const deletion of deletions) {
pendingDeletions.push(
this.query(deletion).catch((err) =>
log.error(
`[PeriodicDbDeleter] sync error`,
{
periodicDeleterTickId: tickID,
query: deletion,
},
err,
),
),
const promise: Promise<void> = this.query(deletion).catch((err) =>
log.error(`[PeriodicDbDeleter] sync error`, err, {
periodicDeleterTickId: tickID,
query: deletion,
}),
);
pendingDeletions.push(promise);
}
}
await Promise.all(pendingDeletions);
Expand Down
4 changes: 2 additions & 2 deletions components/gitpod-db/src/redis/publisher.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ class TestRedisPublisher {

@test public publishInstanceUpdate() {
const publisher = this.container.get(RedisPublisher);
expect(() => {
publisher.publishInstanceUpdate({
expect(async () => {
await publisher.publishInstanceUpdate({
ownerID: "123-owner",
instanceID: "123",
workspaceID: "foo-bar-123",
Expand Down
6 changes: 1 addition & 5 deletions components/gitpod-db/src/tables.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,7 @@ class TablesSpec {
@test(timeout(10000))
public async createAndFindATeam() {
const thing = new GitpodTableDescriptionProvider();
try {
thing.getSortedTables();
} catch (error) {
expect.fail(error);
}
expect(() => thing.getSortedTables()).to.not.throw();
}
}

Expand Down
2 changes: 1 addition & 1 deletion components/gitpod-db/src/test/reset-db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export async function resetDB(typeorm: TypeORM) {
const conn = await typeorm.getConnection();
const users = await conn.getRepository(DBUser).find();
// delete all users except the builtin users
conn.getRepository(DBUser).remove(users.filter((u) => !isBuiltinUser(u.id)));
await conn.getRepository(DBUser).remove(users.filter((u) => !isBuiltinUser(u.id)));

const deletions = conn.entityMetadatas
.filter((meta) => meta.tableName !== "d_b_user")
Expand Down
1 change: 1 addition & 0 deletions components/gitpod-db/src/traced-db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export class DBWithTracing<T> {
public trace(ctx: TraceContext): T {
return new Proxy(this.db, {
get: (_target: any, name: string) => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
const f = Reflect.get(_target, name);
if (!f) {
return undefined;
Expand Down
2 changes: 1 addition & 1 deletion components/gitpod-db/src/typeorm/team-db-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ export class TeamDBImpl extends TransactionalDBImpl<TeamDB> implements TeamDB {
const orgSettings = await orgSettingsRepo.findOne({ where: { orgId } });
if (orgSettings) {
orgSettings.deleted = true;
orgSettingsRepo.save(orgSettings);
await orgSettingsRepo.save(orgSettings);
}
}

Expand Down
3 changes: 3 additions & 0 deletions components/gitpod-db/src/typeorm/transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export namespace Transformer {
},
from(value: any): any {
// From TIMESTAMP to ISO string
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
return new Date(Date.parse(value)).toISOString();
},
};
Expand All @@ -74,6 +75,7 @@ export namespace Transformer {
return JSON.stringify(value || defaultValue);
},
from(value: any): any {
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
return JSON.parse(value);
},
};
Expand All @@ -85,6 +87,7 @@ export namespace Transformer {
return encryptionServiceProvider().encrypt(value);
},
from(value: any): any {
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
return encryptionServiceProvider().decrypt(value);
},
};
Expand Down
3 changes: 2 additions & 1 deletion components/gitpod-db/src/typeorm/user-db-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,7 @@ export class TypeORMUserDBImpl extends TransactionalDBImpl<UserDB> implements Us
}
const res = await userRepo.query(query);
const count = res[0].cnt;
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
return Number.parseInt(count);
}

Expand Down Expand Up @@ -598,7 +599,7 @@ export class TypeORMUserDBImpl extends TransactionalDBImpl<UserDB> implements Us
}
async revoke(accessTokenToken: OAuthToken): Promise<void> {
const tokenHash = crypto.createHash("sha256").update(accessTokenToken.accessToken, "utf8").digest("hex");
this.deleteGitpodToken(tokenHash);
await this.deleteGitpodToken(tokenHash);
}
async isRefreshTokenRevoked(refreshToken: OAuthToken): Promise<boolean> {
return Date.now() > (refreshToken.refreshTokenExpiresAt?.getTime() ?? 0);
Expand Down
40 changes: 15 additions & 25 deletions components/gitpod-db/src/typeorm/workspace-db-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import {
AdminGetWorkspacesQuery,
CommitContext,
PrebuildInfo,
PrebuiltWorkspace,
PrebuiltWorkspaceState,
Expand Down Expand Up @@ -143,9 +144,10 @@ export class TypeORMWorkspaceDBImpl extends TransactionalDBImpl<WorkspaceDB> imp

// `cloneUrl` is stored redundandly to optimize for `getWorkspaceCountByCloneURL`.
// As clone URLs are lesser constrained we want to shorten the value to work well with the indexed column.
const cloneUrl: string = this.toCloneUrl255((workspace as any).context?.repository?.cloneUrl || "");

dbWorkspace.cloneUrl = cloneUrl;
if (CommitContext.is(dbWorkspace.context)) {
const cloneUrl = this.toCloneUrl255(dbWorkspace.context.repository.cloneUrl);
dbWorkspace.cloneUrl = cloneUrl;
}
return await workspaceRepo.save(dbWorkspace);
}

Expand All @@ -167,17 +169,12 @@ export class TypeORMWorkspaceDBImpl extends TransactionalDBImpl<WorkspaceDB> imp
}

public async findByInstanceId(instanceId: string): Promise<MaybeWorkspace> {
const workspaceRepo = await this.getWorkspaceRepo();
const maybeRawWorkspaces = (await workspaceRepo.query(
`SELECT ws.* FROM d_b_workspace as ws
LEFT OUTER JOIN d_b_workspace_instance as wsi ON wsi.workspaceId = ws.id
WHERE wsi.id = ?;`,
[instanceId],
)) as object[];
if (!maybeRawWorkspaces || maybeRawWorkspaces.length !== 1) {
const instanceRepo = await this.getWorkspaceInstanceRepo();
const instance = await instanceRepo.findOne(instanceId);
if (!instance) {
return undefined;
}
return this.makeWorkspace(maybeRawWorkspaces[0]);
return this.findById(instance.workspaceId);
}

public async find(options: FindWorkspacesOptions): Promise<WorkspaceInfo[]> {
Expand Down Expand Up @@ -265,16 +262,6 @@ export class TypeORMWorkspaceDBImpl extends TransactionalDBImpl<WorkspaceDB> imp
});
}

private makeWorkspace(raw: any): DBWorkspace | undefined {
if (!raw) return undefined;
return {
...raw,
config: JSON.parse(raw.config),
context: JSON.parse(raw.context),
pinned: (raw.pinned && JSON.parse(raw.pinned)) || undefined,
};
}

public async updateLastHeartbeat(
instanceId: string,
userId: string,
Expand All @@ -285,7 +272,7 @@ export class TypeORMWorkspaceDBImpl extends TransactionalDBImpl<WorkspaceDB> imp
"INSERT INTO d_b_workspace_instance_user(instanceId, userId, lastSeen) VALUES (?, ?, timestamp ?) ON DUPLICATE KEY UPDATE lastSeen = timestamp ?, wasClosed = ?";
const lastSeen = this.toTimestampString(newHeartbeat);
const workspaceInstanceUserRepo = await this.getWorkspaceInstanceUserRepo();
workspaceInstanceUserRepo.query(query, [instanceId, userId, lastSeen, lastSeen, wasClosed || false]);
await workspaceInstanceUserRepo.query(query, [instanceId, userId, lastSeen, lastSeen, wasClosed || false]);
}

private toTimestampString(date: Date) {
Expand All @@ -302,6 +289,7 @@ export class TypeORMWorkspaceDBImpl extends TransactionalDBImpl<WorkspaceDB> imp

if (result && result.length > 0 && result[0].lastSeen) {
return {
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
lastSeen: new Date(result[0].lastSeen),
wasClosed: Boolean(result[0].wasClosed),
};
Expand Down Expand Up @@ -401,7 +389,7 @@ export class TypeORMWorkspaceDBImpl extends TransactionalDBImpl<WorkspaceDB> imp
userId?: string,
includeStopping: boolean = false,
): Promise<RunningWorkspaceInfo[]> {
const params: any = {};
const params: { region?: string } = {};
const conditions = ["wsi.phasePersisted != 'stopped'", "wsi.deleted != TRUE"];
if (!includeStopping) {
// This excludes instances in a 'stopping' phase
Expand All @@ -411,7 +399,7 @@ export class TypeORMWorkspaceDBImpl extends TransactionalDBImpl<WorkspaceDB> imp
params.region = workspaceClusterName;
conditions.push("wsi.region = :region");
}
const joinParams: any = {};
const joinParams: { userId?: string } = {};
const joinConditions = [];
if (userId) {
joinParams.userId = userId;
Expand Down Expand Up @@ -501,6 +489,7 @@ export class TypeORMWorkspaceDBImpl extends TransactionalDBImpl<WorkspaceDB> imp
resultSessions.push({
workspace: {
id: session.ws_id,
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
context: JSON.parse(session.ws_context),
contextURL: session.ws_contextURL,
type: session.ws_type,
Expand Down Expand Up @@ -980,6 +969,7 @@ export class TypeORMWorkspaceDBImpl extends TransactionalDBImpl<WorkspaceDB> imp
: "wsi.id = (SELECT i.id FROM d_b_workspace_instance AS i WHERE i.workspaceId = ws.id ORDER BY i.creationTime DESC LIMIT 1)"
}`,
)
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
.where(whereConditions.join(" AND "), whereConditionParams)
.orderBy(orderField, orderDir)
.take(limit)
Expand Down
4 changes: 2 additions & 2 deletions components/gitpod-db/src/wait-for-db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ function connectOrReschedule(attempt: number) {
}
}

function rescheduleConnectionAttempt(attempt: number, err: Error) {
function rescheduleConnectionAttempt(attempt: number, err: unknown) {
if (attempt == totalAttempts) {
console.log(`Could not connect within ${totalAttempts} attempts. Stopping.`);
console.log(`Could not connect within ${totalAttempts} attempts. Stopping.`, err);
process.exit(1);
}
console.log(`Connection attempt ${attempt}/${totalAttempts} failed. Retrying in ${retryPeriod / 1000} seconds.`);
Expand Down
36 changes: 33 additions & 3 deletions components/gitpod-db/src/workspace-db.spec.db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,13 @@ class WorkspaceDBSpec {
tasks: [],
},
projectId: this.projectAID,
context: { title: "example" },
context: <CommitContext>{
title: "example",
repository: {
cloneUrl: "https://github.com/gitpod-io/gitpod",
},
revision: "abc",
},
contextURL: "example.org",
description: "blabla",
ownerId: this.userId,
Expand Down Expand Up @@ -105,7 +111,13 @@ class WorkspaceDBSpec {
tasks: [],
},
projectId: this.projectBID,
context: { title: "example" },
context: <CommitContext>{
title: "example",
repository: {
cloneUrl: "https://github.com/gitpod-io/gitpod",
},
revision: "abc",
},
contextURL: "https://github.com/gitpod-io/gitpod",
description: "Gitpod",
ownerId: this.userId,
Expand Down Expand Up @@ -145,7 +157,13 @@ class WorkspaceDBSpec {
image: "",
tasks: [],
},
context: { title: "example" },
context: <CommitContext>{
title: "example",
repository: {
cloneUrl: "https://github.com/gitpod-io/gitpod",
},
revision: "abc",
},
contextURL: "example.org",
description: "blabla",
ownerId: this.userId,
Expand Down Expand Up @@ -206,6 +224,16 @@ class WorkspaceDBSpec {
fail("Rollback failed");
}

@test(timeout(10000))
public async testFindByInstanceId() {
await this.db.transaction(async (db) => {
await Promise.all([db.store(this.ws), db.storeInstance(this.wsi1)]);
const dbResult = await db.findByInstanceId(this.wsi1.id);
const expected = await db.findById(this.wsi1.workspaceId);
expect(dbResult).to.deep.eq(expected);
});
}

@test(timeout(10000))
public async testFindPrebuildsForGC_oldPrebuildNoUsage() {
await this.createPrebuild(2);
Expand Down Expand Up @@ -583,6 +611,7 @@ class WorkspaceDBSpec {
repository: {
cloneUrl: inactiveRepo,
},
revision: "abc",
},
config: {},
type: "regular",
Expand All @@ -599,6 +628,7 @@ class WorkspaceDBSpec {
repository: {
cloneUrl: activeRepo,
},
revision: "abc",
},
config: {},
type: "regular",
Expand Down
3 changes: 3 additions & 0 deletions components/gitpod-protocol/.eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@
},
"rules": {
"no-var": "error",
"no-void": "error",
"prefer-const": "error",
"@typescript-eslint/ban-ts-comment": "off",
"@typescript-eslint/ban-types": "off",
"@typescript-eslint/explicit-module-boundary-types": "off",
"@typescript-eslint/no-empty-function": "off",
"@typescript-eslint/no-empty-interface": "off",
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/no-floating-promises": "error",
"@typescript-eslint/no-non-null-asserted-optional-chain": "off",
"@typescript-eslint/no-inferrable-types": "off",
"@typescript-eslint/no-misused-promises": [
Expand All @@ -28,6 +30,7 @@
"@typescript-eslint/no-namespace": "off",
"@typescript-eslint/no-non-null-assertion": "off",
"@typescript-eslint/no-this-alias": "off",
"@typescript-eslint/no-unsafe-argument": "error",
"@typescript-eslint/no-unused-vars": [
"error",
{
Expand Down
3 changes: 2 additions & 1 deletion components/gitpod-protocol/src/gitpod-file-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { WorkspaceConfig, PortRangeConfig } from "./protocol";
export type MaybeConfig = WorkspaceConfig | undefined;

const schema = require("../data/gitpod-schema.json");
const validate = new Ajv().compile(schema);
const validate = new Ajv().compile(schema as object);
const defaultParseOptions = {
acceptPortRanges: false,
};
Expand All @@ -33,6 +33,7 @@ export class GitpodFileParser {
};
try {
const parsedConfig = yaml.safeLoad(content) as any;
// eslint-disable-next-line @typescript-eslint/no-floating-promises
validate(parsedConfig);
const validationErrors = validate.errors ? validate.errors.map((e) => e.message || e.keyword) : undefined;
if (validationErrors && validationErrors.length > 0) {
Expand Down
Loading