Skip to content

Commit 040c830

Browse files
bsofiatodelvhwxiaoguang
authored
Inclusion of rename organization api (#33303)
This adds an endpoint (`/orgs/{org}/rename`) to rename organizations. I've modeled the endpoint using the rename user endpoint -- `/admin/users/{username}/rename` -- as base. It is the 1st time I wrote a new API endpoint (I've tried to follow the rename users endpoint code while writing it). So feel free to ping me if there is something wrong or missing. Resolves #32995 --------- Signed-off-by: Bruno Sofiato <[email protected]> Co-authored-by: delvh <[email protected]> Co-authored-by: wxiaoguang <[email protected]>
1 parent 5b83203 commit 040c830

File tree

7 files changed

+250
-151
lines changed

7 files changed

+250
-151
lines changed

modules/structs/org.go

+9
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,12 @@ type EditOrgOption struct {
5757
Visibility string `json:"visibility" binding:"In(,public,limited,private)"`
5858
RepoAdminChangeTeamAccess *bool `json:"repo_admin_change_team_access"`
5959
}
60+
61+
// RenameOrgOption options when renaming an organization
62+
type RenameOrgOption struct {
63+
// New username for this org. This name cannot be in use yet by any other user.
64+
//
65+
// required: true
66+
// unique: true
67+
NewName string `json:"new_name" binding:"Required"`
68+
}

routers/api/v1/admin/user.go

+4-14
Original file line numberDiff line numberDiff line change
@@ -477,26 +477,16 @@ func RenameUser(ctx *context.APIContext) {
477477
return
478478
}
479479

480-
oldName := ctx.ContextUser.Name
481480
newName := web.GetForm(ctx).(*api.RenameUserOption).NewName
482481

483-
// Check if user name has been changed
482+
// Check if username has been changed
484483
if err := user_service.RenameUser(ctx, ctx.ContextUser, newName); err != nil {
485-
switch {
486-
case user_model.IsErrUserAlreadyExist(err):
487-
ctx.Error(http.StatusUnprocessableEntity, "", ctx.Tr("form.username_been_taken"))
488-
case db.IsErrNameReserved(err):
489-
ctx.Error(http.StatusUnprocessableEntity, "", ctx.Tr("user.form.name_reserved", newName))
490-
case db.IsErrNamePatternNotAllowed(err):
491-
ctx.Error(http.StatusUnprocessableEntity, "", ctx.Tr("user.form.name_pattern_not_allowed", newName))
492-
case db.IsErrNameCharsNotAllowed(err):
493-
ctx.Error(http.StatusUnprocessableEntity, "", ctx.Tr("user.form.name_chars_not_allowed", newName))
494-
default:
484+
if user_model.IsErrUserAlreadyExist(err) || db.IsErrNameReserved(err) || db.IsErrNamePatternNotAllowed(err) || db.IsErrNameCharsNotAllowed(err) {
485+
ctx.Error(http.StatusUnprocessableEntity, "", err)
486+
} else {
495487
ctx.ServerError("ChangeUserName", err)
496488
}
497489
return
498490
}
499-
500-
log.Trace("User name changed: %s -> %s", oldName, newName)
501491
ctx.Status(http.StatusNoContent)
502492
}

routers/api/v1/api.go

+1
Original file line numberDiff line numberDiff line change
@@ -1530,6 +1530,7 @@ func Routes() *web.Router {
15301530
m.Combo("").Get(org.Get).
15311531
Patch(reqToken(), reqOrgOwnership(), bind(api.EditOrgOption{}), org.Edit).
15321532
Delete(reqToken(), reqOrgOwnership(), org.Delete)
1533+
m.Post("/rename", reqToken(), reqOrgOwnership(), bind(api.RenameOrgOption{}), org.Rename)
15331534
m.Combo("/repos").Get(user.ListOrgRepos).
15341535
Post(reqToken(), bind(api.CreateRepoOption{}), repo.CreateOrgRepo)
15351536
m.Group("/members", func() {

routers/api/v1/org/org.go

+38
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,44 @@ func Get(ctx *context.APIContext) {
315315
ctx.JSON(http.StatusOK, org)
316316
}
317317

318+
func Rename(ctx *context.APIContext) {
319+
// swagger:operation POST /orgs/{org}/rename organization renameOrg
320+
// ---
321+
// summary: Rename an organization
322+
// produces:
323+
// - application/json
324+
// parameters:
325+
// - name: org
326+
// in: path
327+
// description: existing org name
328+
// type: string
329+
// required: true
330+
// - name: body
331+
// in: body
332+
// required: true
333+
// schema:
334+
// "$ref": "#/definitions/RenameOrgOption"
335+
// responses:
336+
// "204":
337+
// "$ref": "#/responses/empty"
338+
// "403":
339+
// "$ref": "#/responses/forbidden"
340+
// "422":
341+
// "$ref": "#/responses/validationError"
342+
343+
form := web.GetForm(ctx).(*api.RenameOrgOption)
344+
orgUser := ctx.Org.Organization.AsUser()
345+
if err := user_service.RenameUser(ctx, orgUser, form.NewName); err != nil {
346+
if user_model.IsErrUserAlreadyExist(err) || db.IsErrNameReserved(err) || db.IsErrNamePatternNotAllowed(err) || db.IsErrNameCharsNotAllowed(err) {
347+
ctx.Error(http.StatusUnprocessableEntity, "RenameOrg", err)
348+
} else {
349+
ctx.ServerError("RenameOrg", err)
350+
}
351+
return
352+
}
353+
ctx.Status(http.StatusNoContent)
354+
}
355+
318356
// Edit change an organization's information
319357
func Edit(ctx *context.APIContext) {
320358
// swagger:operation PATCH /orgs/{org} organization orgEdit

routers/api/v1/swagger/options.go

+3
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,9 @@ type swaggerParameterBodies struct {
208208
// in:body
209209
CreateVariableOption api.CreateVariableOption
210210

211+
// in:body
212+
RenameOrgOption api.RenameOrgOption
213+
211214
// in:body
212215
UpdateVariableOption api.UpdateVariableOption
213216
}

templates/swagger/v1_json.tmpl

+56
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)