mirror of
https://github.com/go-gitea/gitea.git
synced 2026-05-12 11:13:21 +02:00
fix: ensure group service populates the sort order of newly created groups
This commit is contained in:
parent
496140d537
commit
92df65f9c5
@ -27,13 +27,14 @@ func NewGroup(ctx context.Context, g *group_model.Group) error {
|
||||
if len(g.Name) == 0 {
|
||||
return util.NewInvalidArgumentErrorf("empty group name")
|
||||
}
|
||||
has, err := db.ExistByID[user_model.User](ctx, g.OwnerID)
|
||||
owner, has, err := db.GetByID[user_model.User](ctx, g.OwnerID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !has {
|
||||
return organization.ErrOrgNotExist{ID: g.OwnerID}
|
||||
}
|
||||
g.OwnerName = owner.Name
|
||||
g.LowerName = strings.ToLower(g.Name)
|
||||
ctx, committer, err := db.TxContext(ctx)
|
||||
if err != nil {
|
||||
@ -41,6 +42,35 @@ func NewGroup(ctx context.Context, g *group_model.Group) error {
|
||||
}
|
||||
defer committer.Close()
|
||||
|
||||
if g.ParentGroupID > 0 {
|
||||
ngrp, err := group_model.GetGroupByID(ctx, g.ParentGroupID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err = ngrp.LoadSubgroups(ctx, false); err != nil {
|
||||
return err
|
||||
}
|
||||
g.SortOrder = len(ngrp.Subgroups)
|
||||
gidChain, err := group_model.GetParentGroupIDChain(ctx, g.ParentGroupID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(gidChain) >= 20 {
|
||||
return group_model.ErrGroupTooDeep{
|
||||
ID: g.ParentGroupID,
|
||||
}
|
||||
}
|
||||
} else {
|
||||
siblings, err := group_model.FindGroups(ctx, &group_model.FindGroupsOptions{
|
||||
ParentGroupID: 0,
|
||||
OwnerID: g.OwnerID,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
g.SortOrder = len(siblings)
|
||||
}
|
||||
|
||||
if err = db.Insert(ctx, g); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user