Skip to content

Commit

Permalink
[server] delete project env vars (#19707)
Browse files Browse the repository at this point in the history
  • Loading branch information
svenefftinge authored May 7, 2024
1 parent 8d1c0b8 commit 0743fe4
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 12 deletions.
6 changes: 0 additions & 6 deletions components/gitpod-db/src/tables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,6 @@ export class GitpodTableDescriptionProvider implements TableDescriptionProvider
deletionColumn: "deleted",
timeColumn: "_lastModified",
},
{
name: "d_b_project_env_var",
primaryKeys: ["id", "projectId"],
deletionColumn: "deleted",
timeColumn: "_lastModified",
},
{
name: "d_b_project_info",
primaryKeys: ["projectId"],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* Copyright (c) 2024 Gitpod GmbH. All rights reserved.
* Licensed under the GNU Affero General Public License (AGPL).
* See License.AGPL.txt in the project root for license information.
*/

import { MigrationInterface, QueryRunner } from "typeorm";

export class DeleteDanglingProjectEnvVars1715064681193 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
"DELETE FROM d_b_project_env_var WHERE projectId IN (SELECT id FROM d_b_project WHERE markedDeleted=true)",
);
}

public async down(queryRunner: QueryRunner): Promise<void> {}
}
7 changes: 1 addition & 6 deletions components/gitpod-db/src/typeorm/project-db-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,12 +223,7 @@ export class ProjectDBImpl extends TransactionalDBImpl<ProjectDB> implements Pro

public async deleteProjectEnvironmentVariable(variableId: string): Promise<void> {
const envVarRepo = await this.getProjectEnvVarRepo();
const envVarWithValue = await envVarRepo.findOne({ id: variableId, deleted: false });
if (!envVarWithValue) {
throw new Error("A environment variable with this name could not be found for this project");
}
envVarWithValue.deleted = true;
await envVarRepo.update({ id: envVarWithValue.id, projectId: envVarWithValue.projectId }, envVarWithValue);
await envVarRepo.delete({ id: variableId });
}

public async getProjectEnvironmentVariableValues(envVars: ProjectEnvVar[]): Promise<ProjectEnvVarWithValue[]> {
Expand Down
12 changes: 12 additions & 0 deletions components/server/src/projects/projects-service.spec.db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { createTestContainer, withTestCtx } from "../test/service-testing-contai
import { OldProjectSettings, ProjectsService } from "./projects-service";
import { daysBefore } from "@gitpod/gitpod-protocol/lib/util/timeutil";
import { SYSTEM_USER } from "../authorization/authorizer";
import { EnvVarService } from "../user/env-var-service";

const expect = chai.expect;

Expand Down Expand Up @@ -99,11 +100,22 @@ describe("ProjectsService", async () => {

it("should deleteProject", async () => {
const ps = container.get(ProjectsService);
const evs = container.get(EnvVarService);
const pdb = container.get<ProjectDB>(ProjectDB);
const project1 = await createTestProject(ps, org, owner);
await evs.addProjectEnvVar(member.id, project1.id, {
name: "key",
value: "value",
censored: false,
});

expect(await pdb.getProjectEnvironmentVariables(project1.id)).to.have.lengthOf(1);

await ps.deleteProject(member.id, project1.id);
let projects = await ps.getProjects(member.id, org.id);
expect(projects.length).to.equal(0);
// have to use db directly to verify the env vars are really deleted, the env var service would throw with project not found.
expect(await pdb.getProjectEnvironmentVariables(project1.id)).to.have.lengthOf(0);

const project2 = await createTestProject(ps, org, owner);
await expectError(ErrorCodes.NOT_FOUND, () => ps.deleteProject(stranger.id, project2.id));
Expand Down
6 changes: 6 additions & 0 deletions components/server/src/projects/projects-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,12 @@ export class ProjectsService {
orgId = project.teamId;
await db.markDeleted(projectId);

// delete env vars
const envVars = await db.getProjectEnvironmentVariables(projectId);
for (const envVar of envVars) {
await db.deleteProjectEnvironmentVariable(envVar.id);
}

await this.auth.removeProjectFromOrg(userId, orgId, projectId);
});
this.analytics.track({
Expand Down

0 comments on commit 0743fe4

Please sign in to comment.