diff --git a/modules/convert/convert.go b/modules/convert/convert.go index b5d3afdc7..63182e0cd 100644 --- a/modules/convert/convert.go +++ b/modules/convert/convert.go @@ -354,6 +354,7 @@ func ToDeployKey(apiLink string, key *models.DeployKey) *api.DeployKey { func ToOrganization(org *models.User) *api.Organization { return &api.Organization{ ID: org.ID, + Name: org.Name, AvatarURL: org.AvatarLink(), UserName: org.Name, FullName: org.FullName, diff --git a/modules/structs/org.go b/modules/structs/org.go index 38c6c6d6d..b02736524 100644 --- a/modules/structs/org.go +++ b/modules/structs/org.go @@ -7,6 +7,7 @@ package structs // Organization represents an organization type Organization struct { ID int64 `json:"id"` + Name string `json:"name"` UserName string `json:"username"` FullName string `json:"full_name"` AvatarURL string `json:"avatar_url"` @@ -35,6 +36,7 @@ type CreateOrgOption struct { // EditOrgOption options for editing an organization type EditOrgOption struct { + Name string `json:"name"` // 添加对name的修改,lower_name 其值跟随name变化; FullName string `json:"full_name"` Description string `json:"description" binding:"MaxSize(255)"` Website string `json:"website" binding:"ValidUrl;MaxSize(255)"` diff --git a/routers/api/v1/org/org.go b/routers/api/v1/org/org.go index 4e153bad7..370a62fd5 100644 --- a/routers/api/v1/org/org.go +++ b/routers/api/v1/org/org.go @@ -8,6 +8,7 @@ package org import ( "fmt" "net/http" + "strings" "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/context" @@ -272,23 +273,74 @@ func Edit(ctx *context.APIContext) { // "$ref": "#/responses/Organization" form := web.GetForm(ctx).(*api.EditOrgOption) org := ctx.Org.Organization + + if org.LowerName != strings.ToLower(form.Name) { + if len(form.Name) > 40 { + ctx.Error(http.StatusBadRequest, "EditOrganization", "name长度不能超过40个字符") + return + } + isExist, err := models.IsUserExist(org.ID, form.Name) + if err != nil { + ctx.Error(http.StatusConflict, "IsUserExist", "name already used") + return + } else if isExist { + ctx.Error(http.StatusConflict, "IsUserExist", "name already used") + return + } + + if err := models.ChangeUserName(org, form.Name); err != nil { + if err == models.ErrUserNameIllegal { + ctx.Error(http.StatusBadRequest, "ErrUserNameIllegal", "only allowed '-' '_' '.' characters") + return + } else { + ctx.Error(http.StatusBadRequest, "ChangeUserName", "only allowed '-' '_' '.' characters") + return + } + } + org.Name = form.Name + org.LowerName = strings.ToLower(org.Name) + } + org.FullName = form.FullName org.Description = form.Description org.Website = form.Website org.Location = form.Location + if form.Visibility != "" { org.Visibility = api.VisibilityModes[form.Visibility] } + if form.RepoAdminChangeTeamAccess != nil { org.RepoAdminChangeTeamAccess = *form.RepoAdminChangeTeamAccess } - if err := models.UpdateUserCols(org, - "full_name", "description", "website", "location", - "visibility", "repo_admin_change_team_access", - ); err != nil { - ctx.Error(http.StatusInternalServerError, "EditOrganization", err) + + // Update org data, update table of the user and repository + if err := models.UpdateUser(org); err != nil { + ctx.Error(http.StatusInternalServerError, "UpdateUser", err) return } + visibilityChanged := form.Visibility != org.Visibility.String() + // Update forks visiblity + if visibilityChanged { + if err := org.GetRepositories(models.ListOptions{Page: 1, PageSize: org.NumRepos}); err != nil { + ctx.Error(http.StatusInternalServerError, "GetRepositories", err) + return + } + for _, repo := range org.Repos { + if err := models.UpdateRepository(repo, true); err != nil { + ctx.Error(http.StatusInternalServerError, "UpdateRepository", err) + return + } + } + } + + // if err := models.UpdateUserCols(org, + // "full_name", "description", "website", "location", + // "visibility", "repo_admin_change_team_access", + // ); err != nil { + // ctx.Error(http.StatusInternalServerError, "EditOrganization", err) + // return + // } ctx.JSON(http.StatusOK, convert.ToOrganization(org)) } diff --git a/templates/swagger/v1_json.tmpl b/templates/swagger/v1_json.tmpl index 929ba3f4e..037f4f6e7 100644 --- a/templates/swagger/v1_json.tmpl +++ b/templates/swagger/v1_json.tmpl @@ -14859,6 +14859,10 @@ "type": "string", "x-go-name": "Location" }, + "name": { + "type": "string", + "x-go-name": "Name" + }, "repo_admin_change_team_access": { "type": "boolean", "x-go-name": "RepoAdminChangeTeamAccess" @@ -16573,6 +16577,10 @@ "type": "string", "x-go-name": "Location" }, + "name": { + "type": "string", + "x-go-name": "Name" + }, "repo_admin_change_team_access": { "type": "boolean", "x-go-name": "RepoAdminChangeTeamAccess"