API fix:modify org name

This commit is contained in:
hang 2021-12-09 15:04:45 +08:00
parent b02ab96e88
commit dac17ab26c
4 changed files with 68 additions and 5 deletions

View File

@ -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,

View File

@ -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)"`

View File

@ -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))
}

View File

@ -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"