From ff24828608cf905ba82972f9aeb7724573ce7420 Mon Sep 17 00:00:00 2001 From: Bruno Sofiato Date: Thu, 16 Jan 2025 10:15:59 -0300 Subject: [PATCH] Inclusion of rename organization api Signed-off-by: Bruno Sofiato --- modules/structs/org.go | 9 +++++ routers/api/v1/api.go | 1 + routers/api/v1/org/org.go | 41 ++++++++++++++++++++++ routers/api/v1/swagger/options.go | 3 ++ templates/swagger/v1_json.tmpl | 56 +++++++++++++++++++++++++++++++ 5 files changed, 110 insertions(+) diff --git a/modules/structs/org.go b/modules/structs/org.go index c0a545ac1c..f93b3b6493 100644 --- a/modules/structs/org.go +++ b/modules/structs/org.go @@ -57,3 +57,12 @@ type EditOrgOption struct { Visibility string `json:"visibility" binding:"In(,public,limited,private)"` RepoAdminChangeTeamAccess *bool `json:"repo_admin_change_team_access"` } + +// RenameOrgOption options when renaming an organization +type RenameOrgOption struct { + // New username for this org. This name cannot be in use yet by any other user. + // + // required: true + // unique: true + NewName string `json:"new_name" binding:"Required"` +} diff --git a/routers/api/v1/api.go b/routers/api/v1/api.go index b1a42a85e6..438db4ae71 100644 --- a/routers/api/v1/api.go +++ b/routers/api/v1/api.go @@ -1530,6 +1530,7 @@ func Routes() *web.Router { m.Combo("").Get(org.Get). Patch(reqToken(), reqOrgOwnership(), bind(api.EditOrgOption{}), org.Edit). Delete(reqToken(), reqOrgOwnership(), org.Delete) + m.Post("/rename", reqToken(), reqOrgOwnership(), bind(api.RenameOrgOption{}), org.Rename) m.Combo("/repos").Get(user.ListOrgRepos). Post(reqToken(), bind(api.CreateRepoOption{}), repo.CreateOrgRepo) m.Group("/members", func() { diff --git a/routers/api/v1/org/org.go b/routers/api/v1/org/org.go index d65f922434..aacefad647 100644 --- a/routers/api/v1/org/org.go +++ b/routers/api/v1/org/org.go @@ -315,6 +315,47 @@ func Get(ctx *context.APIContext) { ctx.JSON(http.StatusOK, org) } +func Rename(ctx *context.APIContext) { + // swagger:operation POST /orgs/{org}/rename organization renameOrg + // --- + // summary: Rename an organization + // produces: + // - application/json + // parameters: + // - name: org + // in: path + // description: existing org name + // type: string + // required: true + // - name: body + // in: body + // required: true + // schema: + // "$ref": "#/definitions/RenameOrgOption" + // responses: + // "204": + // "$ref": "#/responses/empty" + // "403": + // "$ref": "#/responses/forbidden" + // "422": + // "$ref": "#/responses/validationError" + + org := ctx.Org.Organization + form := web.GetForm(ctx).(*api.RenameOrgOption) + + if err := user_service.RenameUser(ctx, org.AsUser(), form.NewName); err != nil { + if user_model.IsErrUserAlreadyExist(err) { + ctx.Error(http.StatusUnprocessableEntity, "RenameOrg", ctx.Tr("form.username_been_taken")) + } else if db.IsErrNameReserved(err) { + ctx.Error(http.StatusUnprocessableEntity, "RenameOrg", ctx.Tr("repo.form.name_reserved", err.(db.ErrNameReserved).Name)) + } else if db.IsErrNamePatternNotAllowed(err) { + ctx.Error(http.StatusUnprocessableEntity, "RenameOrg", ctx.Tr("repo.form.name_pattern_not_allowed", err.(db.ErrNamePatternNotAllowed).Pattern)) + } else { + ctx.ServerError("RenameOrg", err) + } + } +} + // Edit change an organization's information func Edit(ctx *context.APIContext) { // swagger:operation PATCH /orgs/{org} organization orgEdit diff --git a/routers/api/v1/swagger/options.go b/routers/api/v1/swagger/options.go index 125605d98f..353d6de89b 100644 --- a/routers/api/v1/swagger/options.go +++ b/routers/api/v1/swagger/options.go @@ -208,6 +208,9 @@ type swaggerParameterBodies struct { // in:body CreateVariableOption api.CreateVariableOption + // in:body + RenameOrgOption api.RenameOrgOption + // in:body UpdateVariableOption api.UpdateVariableOption } diff --git a/templates/swagger/v1_json.tmpl b/templates/swagger/v1_json.tmpl index 8082fc594a..c58b21062d 100644 --- a/templates/swagger/v1_json.tmpl +++ b/templates/swagger/v1_json.tmpl @@ -2991,6 +2991,46 @@ } } }, + "/orgs/{org}/rename": { + "post": { + "produces": [ + "application/json" + ], + "tags": [ + "organization" + ], + "summary": "Rename an organization", + "operationId": "renameOrg", + "parameters": [ + { + "type": "string", + "description": "existing org name", + "name": "org", + "in": "path", + "required": true + }, + { + "name": "body", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/RenameOrgOption" + } + } + ], + "responses": { + "204": { + "$ref": "#/responses/empty" + }, + "403": { + "$ref": "#/responses/forbidden" + }, + "422": { + "$ref": "#/responses/validationError" + } + } + } + }, "/orgs/{org}/repos": { "get": { "produces": [ @@ -24207,6 +24247,22 @@ }, "x-go-package": "code.gitea.io/gitea/modules/structs" }, + "RenameOrgOption": { + "description": "RenameOrgOption options when renaming an organization", + "type": "object", + "required": [ + "new_name" + ], + "properties": { + "new_name": { + "description": "New username for this org. This name cannot be in use yet by any other user.", + "type": "string", + "uniqueItems": true, + "x-go-name": "NewName" + } + }, + "x-go-package": "code.gitea.io/gitea/modules/structs" + }, "RenameUserOption": { "description": "RenameUserOption options when renaming a user", "type": "object",