Skip to content

Commit bf76428

Browse files
move parameters of the MoveGroup function into a struct, MoveGroupOptions
1 parent dbeea34 commit bf76428

File tree

2 files changed

+30
-16
lines changed

2 files changed

+30
-16
lines changed

services/group/group.go

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -66,33 +66,47 @@ func MoveRepositoryToGroup(ctx context.Context, repo *repo_model.Repository, new
6666
return err
6767
}
6868

69-
func MoveGroupItem(ctx context.Context, itemID, newParent int64, isGroup bool, newPos int) (err error) {
69+
type MoveGroupOptions struct {
70+
NewParent, ItemID int64
71+
IsGroup bool
72+
NewPos int
73+
}
74+
75+
func MoveGroupItem(ctx context.Context, opts MoveGroupOptions, doerID int64) (err error) {
7076
var committer db.Committer
7177
ctx, committer, err = db.TxContext(ctx)
7278
if err != nil {
7379
return err
7480
}
7581
defer committer.Close()
7682
var parentGroup *group_model.Group
77-
parentGroup, err = group_model.GetGroupByID(ctx, newParent)
83+
parentGroup, err = group_model.GetGroupByID(ctx, opts.NewParent)
7884
if err != nil {
7985
return err
8086
}
87+
canAccessNewParent, err := parentGroup.CanAccess(ctx, doerID)
88+
if err != nil {
89+
return err
90+
}
91+
if !canAccessNewParent {
92+
return fmt.Errorf("cannot access new parent group")
93+
}
94+
8195
err = parentGroup.LoadSubgroups(ctx, false)
8296
if err != nil {
8397
return err
8498
}
85-
if isGroup {
99+
if opts.IsGroup {
86100
var group *group_model.Group
87-
group, err = group_model.GetGroupByID(ctx, itemID)
101+
group, err = group_model.GetGroupByID(ctx, opts.ItemID)
88102
if err != nil {
89103
return err
90104
}
91-
if newPos < 0 {
92-
newPos = len(parentGroup.Subgroups)
105+
if opts.NewPos < 0 {
106+
opts.NewPos = len(parentGroup.Subgroups)
93107
}
94-
if group.ParentGroupID != newParent || group.SortOrder != newPos {
95-
if err = group_model.MoveGroup(ctx, group, newParent, newPos); err != nil {
108+
if group.ParentGroupID != opts.NewParent || group.SortOrder != opts.NewPos {
109+
if err = group_model.MoveGroup(ctx, group, opts.NewParent, opts.NewPos); err != nil {
96110
return err
97111
}
98112
if err = RecalculateGroupAccess(ctx, group, false); err != nil {
@@ -101,22 +115,22 @@ func MoveGroupItem(ctx context.Context, itemID, newParent int64, isGroup bool, n
101115
}
102116
} else {
103117
var repo *repo_model.Repository
104-
repo, err = repo_model.GetRepositoryByID(ctx, itemID)
118+
repo, err = repo_model.GetRepositoryByID(ctx, opts.ItemID)
105119
if err != nil {
106120
return err
107121
}
108-
if newPos < 0 {
122+
if opts.NewPos < 0 {
109123
var repoCount int64
110124
repoCount, err = repo_model.CountRepository(ctx, repo_model.SearchRepoOptions{
111-
GroupID: newParent,
125+
GroupID: opts.NewParent,
112126
})
113127
if err != nil {
114128
return err
115129
}
116-
newPos = int(repoCount)
130+
opts.NewPos = int(repoCount)
117131
}
118-
if repo.GroupID != newParent || repo.GroupSortOrder != newPos {
119-
if err = MoveRepositoryToGroup(ctx, repo, newParent, newPos); err != nil {
132+
if repo.GroupID != opts.NewParent || repo.GroupSortOrder != opts.NewPos {
133+
if err = MoveRepositoryToGroup(ctx, repo, opts.NewParent, opts.NewPos); err != nil {
120134
return err
121135
}
122136
}

services/group/group_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func TestMoveGroup(t *testing.T) {
3838
}
3939
origCount := unittest.GetCount(t, new(group_model.Group), cond.ToConds())
4040

41-
assert.NoError(t, MoveGroupItem(t.Context(), gid, 123, true, -1))
41+
assert.NoError(t, MoveGroupItem(t.Context(), MoveGroupOptions{123, gid, true, -1}, 3))
4242
unittest.AssertCountByCond(t, "repo_group", cond.ToConds(), origCount+1)
4343
}
4444
testfn(124)
@@ -53,6 +53,6 @@ func TestMoveRepo(t *testing.T) {
5353
})
5454
origCount := unittest.GetCount(t, new(repo_model.Repository), cond)
5555

56-
assert.NoError(t, MoveGroupItem(db.DefaultContext, 32, 123, false, -1))
56+
assert.NoError(t, MoveGroupItem(db.DefaultContext, MoveGroupOptions{123, 32, false, -1}, 3))
5757
unittest.AssertCountByCond(t, "repository", cond, origCount+1)
5858
}

0 commit comments

Comments
 (0)