-
Notifications
You must be signed in to change notification settings - Fork 1.3k
[Orgs] Persist slug
#16923
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
Merged
Merged
[Orgs] Persist slug
#16923
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
components/dashboard/src/data/organizations/update-org-mutation.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/** | ||
* Copyright (c) 2023 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 { Organization } from "@gitpod/gitpod-protocol"; | ||
import { useMutation } from "@tanstack/react-query"; | ||
import { getGitpodService } from "../../service/service"; | ||
import { useCurrentOrg, useOrganizationsInvalidator } from "./orgs-query"; | ||
|
||
type UpdateOrgArgs = Pick<Organization, "name" | "slug">; | ||
|
||
export const useUpdateOrgMutation = () => { | ||
const org = useCurrentOrg().data; | ||
const invalidateOrgs = useOrganizationsInvalidator(); | ||
|
||
return useMutation<Organization, Error, UpdateOrgArgs>({ | ||
mutationFn: async ({ name, slug }) => { | ||
if (!org) { | ||
throw new Error("No current organization selected"); | ||
} | ||
|
||
return await getGitpodService().server.updateTeam(org.id, { name, slug }); | ||
}, | ||
onSuccess(updatedOrg) { | ||
// TODO: Update query cache with new org prior to invalidation so it's reflected immediately | ||
invalidateOrgs(); | ||
}, | ||
}); | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,13 +9,15 @@ import { inject, injectable } from "inversify"; | |
import { TypeORM } from "./typeorm"; | ||
import { Repository } from "typeorm"; | ||
import { v4 as uuidv4 } from "uuid"; | ||
import { randomBytes } from "crypto"; | ||
import { TeamDB } from "../team-db"; | ||
import { DBTeam } from "./entity/db-team"; | ||
import { DBTeamMembership } from "./entity/db-team-membership"; | ||
import { DBUser } from "./entity/db-user"; | ||
import { DBTeamMembershipInvite } from "./entity/db-team-membership-invite"; | ||
import { ResponseError } from "vscode-jsonrpc"; | ||
import { ErrorCodes } from "@gitpod/gitpod-protocol/lib/messaging/error"; | ||
import slugify from "slugify"; | ||
|
||
@injectable() | ||
export class TeamDBImpl implements TeamDB { | ||
|
@@ -121,18 +123,53 @@ export class TeamDBImpl implements TeamDB { | |
return soleOwnedTeams; | ||
} | ||
|
||
public async updateTeam(teamId: string, team: 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, "Organization not found"); | ||
} | ||
public async updateTeam(teamId: string, team: Pick<Team, "name" | "slug">): Promise<Team> { | ||
const name = team.name && team.name.trim(); | ||
if (!name || name.length === 0 || name.length > 32) { | ||
throw new ResponseError(ErrorCodes.INVALID_VALUE, "The name must be between 1 and 32 characters long"); | ||
const slug = team.slug && team.slug.trim(); | ||
if (!name && !slug) { | ||
throw new ResponseError(ErrorCodes.BAD_REQUEST, "No update provided"); | ||
} | ||
existingTeam.name = name; | ||
return teamRepo.save(existingTeam); | ||
|
||
// Storing entry in a TX to avoid potential slug dupes caused by racing requests. | ||
const em = await this.getEntityManager(); | ||
return await em.transaction<DBTeam>(async (em) => { | ||
const teamRepo = em.getRepository<DBTeam>(DBTeam); | ||
|
||
const existingTeam = await teamRepo.findOne({ id: teamId, deleted: false, markedDeleted: false }); | ||
if (!existingTeam) { | ||
throw new ResponseError(ErrorCodes.NOT_FOUND, "Organization not found"); | ||
} | ||
|
||
// no changes | ||
if (existingTeam.name === name && existingTeam.slug === slug) { | ||
return existingTeam; | ||
} | ||
|
||
if (!!name) { | ||
if (name.length > 32) { | ||
throw new ResponseError( | ||
ErrorCodes.INVALID_VALUE, | ||
"The name must be between 1 and 32 characters long", | ||
); | ||
} | ||
existingTeam.name = name; | ||
} | ||
if (!!slug && existingTeam.slug != slug) { | ||
if (slug.length > 63) { | ||
throw new ResponseError(ErrorCodes.INVALID_VALUE, "Slug must be between 1 and 63 characters long"); | ||
} | ||
if (!/^[A-Za-z0-9-]+$/.test(slug)) { | ||
throw new ResponseError(ErrorCodes.BAD_REQUEST, "Slug must contain only letters, or numbers"); | ||
} | ||
const anotherTeamWithThatSlug = await teamRepo.findOne({ slug, deleted: false, markedDeleted: false }); | ||
if (anotherTeamWithThatSlug) { | ||
throw new ResponseError(ErrorCodes.INVALID_VALUE, "Slug must be unique"); | ||
} | ||
existingTeam.slug = slug; | ||
} | ||
|
||
return teamRepo.save(existingTeam); | ||
}); | ||
} | ||
|
||
public async createTeam(userId: string, name: string): Promise<Team> { | ||
|
@@ -146,13 +183,27 @@ export class TeamDBImpl implements TeamDB { | |
); | ||
} | ||
|
||
const teamRepo = await this.getTeamRepo(); | ||
const team: Team = { | ||
id: uuidv4(), | ||
name, | ||
creationTime: new Date().toISOString(), | ||
}; | ||
await teamRepo.save(team); | ||
let slug = slugify(name, { lower: true }); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm guessing we've ensured that the resulting slug from this function does actually conform to the regex which we're enforcing when doing the update. |
||
|
||
// Storing new entry in a TX to avoid potential dupes caused by racing requests. | ||
const em = await this.getEntityManager(); | ||
const team = await em.transaction<DBTeam>(async (em) => { | ||
const teamRepo = em.getRepository<DBTeam>(DBTeam); | ||
|
||
const existingTeam = await teamRepo.findOne({ slug, deleted: false, markedDeleted: false }); | ||
if (!!existingTeam) { | ||
slug = slug + "-" + randomBytes(4).toString("hex"); | ||
} | ||
|
||
const team: Team = { | ||
id: uuidv4(), | ||
name, | ||
slug, | ||
creationTime: new Date().toISOString(), | ||
}; | ||
return await teamRepo.save(team); | ||
}); | ||
|
||
const membershipRepo = await this.getMembershipRepo(); | ||
await membershipRepo.save({ | ||
id: uuidv4(), | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
praise: Thanks @AlexTugarev for using the new form components! Thanks also @selfcontained for initially adding[1] the
htmlFor
element.