API fix:modify org name
This commit is contained in:
parent
b02ab96e88
commit
dac17ab26c
|
@ -354,6 +354,7 @@ func ToDeployKey(apiLink string, key *models.DeployKey) *api.DeployKey {
|
||||||
func ToOrganization(org *models.User) *api.Organization {
|
func ToOrganization(org *models.User) *api.Organization {
|
||||||
return &api.Organization{
|
return &api.Organization{
|
||||||
ID: org.ID,
|
ID: org.ID,
|
||||||
|
Name: org.Name,
|
||||||
AvatarURL: org.AvatarLink(),
|
AvatarURL: org.AvatarLink(),
|
||||||
UserName: org.Name,
|
UserName: org.Name,
|
||||||
FullName: org.FullName,
|
FullName: org.FullName,
|
||||||
|
|
|
@ -7,6 +7,7 @@ package structs
|
||||||
// Organization represents an organization
|
// Organization represents an organization
|
||||||
type Organization struct {
|
type Organization struct {
|
||||||
ID int64 `json:"id"`
|
ID int64 `json:"id"`
|
||||||
|
Name string `json:"name"`
|
||||||
UserName string `json:"username"`
|
UserName string `json:"username"`
|
||||||
FullName string `json:"full_name"`
|
FullName string `json:"full_name"`
|
||||||
AvatarURL string `json:"avatar_url"`
|
AvatarURL string `json:"avatar_url"`
|
||||||
|
@ -35,6 +36,7 @@ type CreateOrgOption struct {
|
||||||
|
|
||||||
// EditOrgOption options for editing an organization
|
// EditOrgOption options for editing an organization
|
||||||
type EditOrgOption struct {
|
type EditOrgOption struct {
|
||||||
|
Name string `json:"name"` // 添加对name的修改,lower_name 其值跟随name变化;
|
||||||
FullName string `json:"full_name"`
|
FullName string `json:"full_name"`
|
||||||
Description string `json:"description" binding:"MaxSize(255)"`
|
Description string `json:"description" binding:"MaxSize(255)"`
|
||||||
Website string `json:"website" binding:"ValidUrl;MaxSize(255)"`
|
Website string `json:"website" binding:"ValidUrl;MaxSize(255)"`
|
||||||
|
|
|
@ -8,6 +8,7 @@ package org
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"code.gitea.io/gitea/models"
|
"code.gitea.io/gitea/models"
|
||||||
"code.gitea.io/gitea/modules/context"
|
"code.gitea.io/gitea/modules/context"
|
||||||
|
@ -272,23 +273,74 @@ func Edit(ctx *context.APIContext) {
|
||||||
// "$ref": "#/responses/Organization"
|
// "$ref": "#/responses/Organization"
|
||||||
form := web.GetForm(ctx).(*api.EditOrgOption)
|
form := web.GetForm(ctx).(*api.EditOrgOption)
|
||||||
org := ctx.Org.Organization
|
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.FullName = form.FullName
|
||||||
org.Description = form.Description
|
org.Description = form.Description
|
||||||
org.Website = form.Website
|
org.Website = form.Website
|
||||||
org.Location = form.Location
|
org.Location = form.Location
|
||||||
|
|
||||||
if form.Visibility != "" {
|
if form.Visibility != "" {
|
||||||
org.Visibility = api.VisibilityModes[form.Visibility]
|
org.Visibility = api.VisibilityModes[form.Visibility]
|
||||||
}
|
}
|
||||||
|
|
||||||
if form.RepoAdminChangeTeamAccess != nil {
|
if form.RepoAdminChangeTeamAccess != nil {
|
||||||
org.RepoAdminChangeTeamAccess = *form.RepoAdminChangeTeamAccess
|
org.RepoAdminChangeTeamAccess = *form.RepoAdminChangeTeamAccess
|
||||||
}
|
}
|
||||||
if err := models.UpdateUserCols(org,
|
|
||||||
"full_name", "description", "website", "location",
|
// Update org data, update table of the user and repository
|
||||||
"visibility", "repo_admin_change_team_access",
|
if err := models.UpdateUser(org); err != nil {
|
||||||
); err != nil {
|
ctx.Error(http.StatusInternalServerError, "UpdateUser", err)
|
||||||
ctx.Error(http.StatusInternalServerError, "EditOrganization", err)
|
|
||||||
return
|
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))
|
ctx.JSON(http.StatusOK, convert.ToOrganization(org))
|
||||||
}
|
}
|
||||||
|
|
|
@ -14859,6 +14859,10 @@
|
||||||
"type": "string",
|
"type": "string",
|
||||||
"x-go-name": "Location"
|
"x-go-name": "Location"
|
||||||
},
|
},
|
||||||
|
"name": {
|
||||||
|
"type": "string",
|
||||||
|
"x-go-name": "Name"
|
||||||
|
},
|
||||||
"repo_admin_change_team_access": {
|
"repo_admin_change_team_access": {
|
||||||
"type": "boolean",
|
"type": "boolean",
|
||||||
"x-go-name": "RepoAdminChangeTeamAccess"
|
"x-go-name": "RepoAdminChangeTeamAccess"
|
||||||
|
@ -16573,6 +16577,10 @@
|
||||||
"type": "string",
|
"type": "string",
|
||||||
"x-go-name": "Location"
|
"x-go-name": "Location"
|
||||||
},
|
},
|
||||||
|
"name": {
|
||||||
|
"type": "string",
|
||||||
|
"x-go-name": "Name"
|
||||||
|
},
|
||||||
"repo_admin_change_team_access": {
|
"repo_admin_change_team_access": {
|
||||||
"type": "boolean",
|
"type": "boolean",
|
||||||
"x-go-name": "RepoAdminChangeTeamAccess"
|
"x-go-name": "RepoAdminChangeTeamAccess"
|
||||||
|
|
Loading…
Reference in New Issue