Skip to content

[server] added UpdateTeam #15749

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

Closed
wants to merge 1 commit into from
Closed
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
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 @@ -23,6 +23,7 @@ export interface TeamDB {
findTeamsByUser(userId: string): Promise<Team[]>;
findTeamsByUserAsSoleOwner(userId: string): Promise<Team[]>;
createTeam(userId: string, name: string): Promise<Team>;
updateTeam(teamId: string, team: Partial<Pick<Team, "name">>): Promise<Team>;
addMemberToTeam(userId: string, teamId: string): Promise<"added" | "already_member">;
setTeamMemberRole(userId: string, teamId: string, role: TeamMemberRole): Promise<void>;
setTeamMemberSubscription(userId: string, teamId: string, subscriptionId: string): Promise<void>;
Expand Down
45 changes: 45 additions & 0 deletions components/gitpod-db/src/typeorm/team-db-impl.spec.db.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* Copyright (c) 2022 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 { suite, test, timeout } from "mocha-typescript";
import { testContainer } from "../test-container";
import { UserDB } from "../user-db";
import { TypeORM } from "./typeorm";
import { DBUser } from "./entity/db-user";
import * as chai from "chai";
import { TeamDB } from "../team-db";
import { DBTeam } from "./entity/db-team";
const expect = chai.expect;

@suite(timeout(10000))
export class TeamDBSpec {
private readonly teamDB = testContainer.get<TeamDB>(TeamDB);
private readonly userDB = testContainer.get<UserDB>(UserDB);

async before() {
await this.wipeRepo();
}

async after() {
await this.wipeRepo();
}

async wipeRepo() {
const typeorm = testContainer.get<TypeORM>(TypeORM);
const manager = await typeorm.getConnection();
await manager.getRepository(DBTeam).delete({});
await manager.getRepository(DBUser).delete({});
}

@test()
async testPersistAndUpdate(): Promise<void> {
const user = await this.userDB.newUser();
let team = await this.teamDB.createTeam(user.id, "Test Team");
team.name = "Test Team 2";
team = await this.teamDB.updateTeam(team.id, team);
expect(team.name).to.be.eq("Test Team 2");
}
}
14 changes: 14 additions & 0 deletions components/gitpod-db/src/typeorm/team-db-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,20 @@ export class TeamDBImpl implements TeamDB {
return soleOwnedTeams;
}

public async updateTeam(teamId: string, team: Partial<Pick<Team, "name">>): Promise<Team> {
const teamRepo = await this.getTeamRepo();
const existingTeam = await teamRepo.findOne({ id: teamId, deleted: false, markedDeleted: false });
if (!existingTeam) {
throw new ResponseError(ErrorCodes.NOT_FOUND, "Team not found");
}
const name = team.name && team.name.trim();
if (!name || name.length === 0 || name.length > 32) {
throw new ResponseError(ErrorCodes.INVALID_VALUE, "A team's name must be between 1 and 32 characters long");
}
existingTeam.name = name;
return teamRepo.save(existingTeam);
}

public async createTeam(userId: string, name: string): Promise<Team> {
if (!name) {
throw new ResponseError(ErrorCodes.BAD_REQUEST, "Team name cannot be empty");
Expand Down
1 change: 1 addition & 0 deletions components/gitpod-protocol/src/gitpod-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ export interface GitpodServer extends JsonRpcServer<GitpodClient>, AdminServer,

// Teams
getTeam(teamId: string): Promise<Team>;
updateTeam(teamId: string, team: Partial<Pick<Team, "name">>): Promise<Team>;
getTeams(): Promise<Team[]>;
getTeamMembers(teamId: string): Promise<TeamMemberInfo[]>;
createTeam(name: string): Promise<Team>;
Expand Down
1 change: 1 addition & 0 deletions components/server/src/auth/rate-limiter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ const defaultFunctions: FunctionsConfig = {
getProjectEnvironmentVariables: { group: "default", points: 1 },
deleteProjectEnvironmentVariable: { group: "default", points: 1 },
getTeam: { group: "default", points: 1 },
updateTeam: { group: "default", points: 1 },
getTeams: { group: "default", points: 1 },
getTeamMembers: { group: "default", points: 1 },
createTeam: { group: "default", points: 1 },
Expand Down
18 changes: 18 additions & 0 deletions components/server/src/workspace/gitpod-server-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2061,6 +2061,24 @@ export class GitpodServerImpl implements GitpodServerWithTracing, Disposable {
return team;
}

public async updateTeam(ctx: TraceContext, teamId: string, team: Partial<Pick<Team, "name">>): Promise<Team> {
traceAPIParams(ctx, { teamId });

if (!teamId || !uuidValidate(teamId)) {
throw new ResponseError(ErrorCodes.BAD_REQUEST, "team ID must be a valid UUID");
}
this.checkUser("updateTeam");
const existingTeam = await this.teamDB.findTeamById(teamId);
if (!existingTeam) {
throw new ResponseError(ErrorCodes.NOT_FOUND, `Team ${teamId} does not exist`);
}
const members = await this.teamDB.findMembersByTeam(teamId);
await this.guardAccess({ kind: "team", subject: existingTeam, members }, "update");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For others reading this, the update action is only allowed if you're the team owner.


const updatedTeam = await this.teamDB.updateTeam(teamId, team);
return updatedTeam;
}

public async getTeamMembers(ctx: TraceContext, teamId: string): Promise<TeamMemberInfo[]> {
traceAPIParams(ctx, { teamId });

Expand Down