Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove team when sole owner and remove projects #6721

Merged
merged 1 commit into from
Nov 18, 2021
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
50 changes: 50 additions & 0 deletions components/gitpod-db/src/team-db.spec.db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,56 @@ import { DBIdentity } from './typeorm/entity/db-identity';
expect(members[0].primaryEmail).to.eq('[email protected]');
}

@test(timeout(15000))
public async findTeamWhenUserIsSoleOwner() {
const user = await this.userDb.newUser();
user.identities.push({ authProviderId: 'GitHub', authId: '2345', authName: 'Nana', primaryEmail: '[email protected]' });
await this.userDb.storeUser(user);

const ownTeam = await this.db.createTeam(user.id, 'My Own Team');

const teams = await this.db.findTeamsByUserAsSoleOwner(user.id);

expect(teams.length).to.eq(1);
expect(teams[0].id).to.eq(ownTeam.id);

}

@test(timeout(10000))
public async findTeamWhenUserIsSoleOwnerWithMembers() {
const user = await this.userDb.newUser();
user.identities.push({ authProviderId: 'GitHub', authId: '2345', authName: 'Nana', primaryEmail: '[email protected]' });
await this.userDb.storeUser(user);
const user2 = await this.userDb.newUser();
user2.identities.push({ authProviderId: 'GitLab', authId: '4567', authName: 'Dudu', primaryEmail: '[email protected]' });
await this.userDb.storeUser(user2);

const ownTeam = await this.db.createTeam(user.id, 'My Own Team With Members');
await this.db.addMemberToTeam(user2.id, ownTeam.id);
const teams = await this.db.findTeamsByUserAsSoleOwner(user.id);

expect(teams.length).to.eq(1);
expect(teams[0].id).to.eq(ownTeam.id);

}

@test(timeout(10000))
public async findNoTeamWhenCoOwned() {
const user = await this.userDb.newUser();
user.identities.push({ authProviderId: 'GitHub', authId: '2345', authName: 'Nana', primaryEmail: '[email protected]' });
await this.userDb.storeUser(user);
const user2 = await this.userDb.newUser();
user2.identities.push({ authProviderId: 'GitLab', authId: '4567', authName: 'Dudu', primaryEmail: '[email protected]' });
await this.userDb.storeUser(user2);

const jointTeam = await this.db.createTeam(user.id, 'Joint Team');
await this.db.addMemberToTeam(user2.id, jointTeam.id);
await this.db.setTeamMemberRole(user2.id, jointTeam.id, 'owner');

const teams = await this.db.findTeamsByUserAsSoleOwner(user.id);

expect(teams.length).to.eq(0);
}
}

module.exports = new TeamDBSpec()
1 change: 1 addition & 0 deletions components/gitpod-db/src/team-db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export interface TeamDB {
findTeamById(teamId: string): Promise<Team | undefined>;
findMembersByTeam(teamId: string): Promise<TeamMemberInfo[]>;
findTeamsByUser(userId: string): Promise<Team[]>;
findTeamsByUserAsSoleOwner(userId: string): Promise<Team[]>;
createTeam(userId: string, name: string): Promise<Team>;
addMemberToTeam(userId: string, teamId: string): Promise<void>;
setTeamMemberRole(userId: string, teamId: string, role: TeamMemberRole): Promise<void>;
Expand Down
17 changes: 16 additions & 1 deletion components/gitpod-db/src/typeorm/team-db-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,22 @@ export class TeamDBImpl implements TeamDB {
const membershipRepo = await this.getMembershipRepo();
const memberships = await membershipRepo.find({ userId, deleted: false });
const teams = await teamRepo.findByIds(memberships.map(m => m.teamId));
return teams.filter(t => !t.deleted);
return teams.filter(t => !t.markedDeleted);
}

public async findTeamsByUserAsSoleOwner(userId: string): Promise<Team[]> {
// Find the memberships of this user,
// and among the memberships, get the teams where the user is the sole owner
const soleOwnedTeams = [];
const userTeams = await this.findTeamsByUser(userId);
for (const team of userTeams) {
const memberships = await this.findMembersByTeam(team.id);
const ownerships = memberships.filter(m => m.role === 'owner');
if (ownerships.length === 1 && ownerships[0].userId === userId) {
soleOwnedTeams.push(team);
}
}
return soleOwnedTeams;
}

public async createTeam(userId: string, name: string): Promise<Team> {
Expand Down
24 changes: 23 additions & 1 deletion components/server/src/user/user-deletion-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

import { injectable, inject } from "inversify";
import { UserDB, WorkspaceDB, UserStorageResourcesDB, TeamDB } from '@gitpod/gitpod-db/lib';
import { UserDB, WorkspaceDB, UserStorageResourcesDB, TeamDB, ProjectDB } from '@gitpod/gitpod-db/lib';
import { User, Workspace } from "@gitpod/gitpod-protocol";
import { StorageClient } from "../storage/storage-client";
import { log } from '@gitpod/gitpod-protocol/lib/util/logging';
Expand All @@ -23,6 +23,7 @@ export class UserDeletionService {
@inject(WorkspaceDB) protected readonly workspaceDb: WorkspaceDB;
@inject(UserStorageResourcesDB) protected readonly userStorageResourcesDb: UserStorageResourcesDB;
@inject(TeamDB) protected readonly teamDb: TeamDB;
@inject(ProjectDB) protected readonly projectDb: ProjectDB;
@inject(StorageClient) protected readonly storageClient: StorageClient;
@inject(WorkspaceManagerClientProvider) protected readonly workspaceManagerClientProvider: WorkspaceManagerClientProvider;
@inject(WorkspaceDeletionService) protected readonly workspaceDeletionService: WorkspaceDeletionService;
Expand Down Expand Up @@ -74,8 +75,12 @@ export class UserDeletionService {
this.userStorageResourcesDb.deleteAllForUser(user.id),
// Bucket
this.deleteUserBucket(id),
// Teams owned only by this user
this.deleteSoleOwnedTeams(id),
// Team memberships
this.deleteTeamMemberships(id),
// User projects
this.deleteUserProjects(id),
]);

// Track the deletion Event for Analytics Purposes
Expand Down Expand Up @@ -140,6 +145,23 @@ export class UserDeletionService {
await Promise.all(teams.map(t => this.teamDb.removeMemberFromTeam(userId, t.id)));
}

protected async deleteSoleOwnedTeams(userId: string) {
const ownedTeams = await this.teamDb.findTeamsByUserAsSoleOwner(userId);

for (const team of ownedTeams) {
const teamProjects = await this.projectDb.findTeamProjects(team.id);
await Promise.all(teamProjects.map(project => this.projectDb.markDeleted(project.id)));
}

await Promise.all(ownedTeams.map(t => this.teamDb.deleteTeam(t.id)));
}

protected async deleteUserProjects(id: string) {
const userProjects = await this.projectDb.findUserProjects(id);

await Promise.all(userProjects.map(project => this.projectDb.markDeleted(project.id)));
}

anonymizeWorkspace(ws: Workspace) {
ws.context.title = 'deleted-title';
ws.context.normalizedContextURL = 'deleted-normalizedContextURL';
Expand Down