diff --git a/models/group/group.go b/models/group/group.go index 4ddc1f4d88..115a455bcf 100644 --- a/models/group/group.go +++ b/models/group/group.go @@ -14,6 +14,7 @@ import ( "code.gitea.io/gitea/models/perm" "code.gitea.io/gitea/models/unit" user_model "code.gitea.io/gitea/models/user" + "code.gitea.io/gitea/modules/container" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/optional" "code.gitea.io/gitea/modules/setting" @@ -434,12 +435,18 @@ func MoveGroup(ctx context.Context, group *Group, newParent int64, newSortOrder if err = ng.LoadSubgroups(ctx, false); err != nil { return err } - siblings = append(append(ng.Subgroups[0:min(newSortOrder, len(ng.Subgroups))], group), ng.Subgroups[newSortOrder:]...) + filtered := container.Filter(ng.Subgroups, func(e *Group) bool { + return e.ID != group.ID + }) + siblings = append(append(filtered[0:min(newSortOrder, len(ng.Subgroups))], group), filtered[newSortOrder:]...) } else if newParent <= 0 { tmpSiblings, err = FindGroups(ctx, &FindGroupsOptions{ OwnerID: group.OwnerID, ParentGroupID: 0, }) + tmpSiblings = container.Filter(tmpSiblings, func(e *Group) bool { + return group.ID != e.ID + }) tmpSiblings2 := make(RepoGroupList, newSortOrder) copy(tmpSiblings2, tmpSiblings[0:newSortOrder]) tmpSiblings2 = append(tmpSiblings2, group) @@ -463,6 +470,7 @@ func MoveGroup(ctx context.Context, group *Group, newParent int64, newSortOrder group.ParentGroupID = newParent group.SortOrder = newSortOrder for i, gg := range siblings { + log.Info("ITEM %+v", gg) gg.SortOrder = i if _, err = sess.Table(group.TableName()). ID(gg.ID). diff --git a/modules/container/filter.go b/modules/container/filter.go index 3e27552f1e..6dac7c97f2 100644 --- a/modules/container/filter.go +++ b/modules/container/filter.go @@ -20,6 +20,16 @@ func FilterSlice[E any, T comparable](s []E, include func(E) (T, bool)) []T { return slices.Clip(filtered) } +func Filter[E any](s []E, include func(E) bool) []E { + filtered := make([]E, 0, len(s)) // slice will be clipped before returning + for i := range s { + if ok := include(s[i]); ok { + filtered = append(filtered, s[i]) + } + } + return slices.Clip(filtered) +} + func DedupeBy[E any, I comparable](s []E, id func(E) I) []E { filtered := make([]E, 0, len(s)) // slice will be clipped before returning seen := make(map[I]bool, len(s))