1
1
package group
2
2
3
3
import (
4
- "code.gitea.io/gitea/models/perm"
5
4
"context"
6
5
"slices"
7
6
8
7
"code.gitea.io/gitea/models/db"
9
8
group_model "code.gitea.io/gitea/models/group"
9
+ "code.gitea.io/gitea/models/perm"
10
10
repo_model "code.gitea.io/gitea/models/repo"
11
11
"code.gitea.io/gitea/models/unit"
12
12
user_model "code.gitea.io/gitea/models/user"
13
13
"code.gitea.io/gitea/modules/log"
14
+
14
15
"xorm.io/builder"
15
16
)
16
17
17
- // GroupItem - represents an item in a group, either a repository or a subgroup.
18
+ // Item - represents an item in a group, either a repository or a subgroup.
18
19
// used to display, for example, the group sidebar
19
- type GroupItem interface {
20
+ type Item interface {
20
21
Link () string
21
22
Title () string
22
- Parent () GroupItem
23
- Children (doer * user_model.User ) []GroupItem
23
+ Parent () Item
24
+ Children (doer * user_model.User ) []Item
24
25
Avatar (ctx context.Context ) string
25
26
HasChildren (doer * user_model.User ) bool
26
27
IsGroup () bool
@@ -40,15 +41,15 @@ func (g *groupItemGroup) Title() string {
40
41
return g .Group .Name
41
42
}
42
43
43
- func (g * groupItemGroup ) Parent () GroupItem {
44
+ func (g * groupItemGroup ) Parent () Item {
44
45
if g .Group .ParentGroupID == 0 {
45
46
return nil
46
47
}
47
48
group , _ := group_model .GetGroupByID (db .DefaultContext , g .Group .ParentGroupID )
48
49
return & groupItemGroup {group }
49
50
}
50
51
51
- func (g * groupItemGroup ) Children (doer * user_model.User ) (items []GroupItem ) {
52
+ func (g * groupItemGroup ) Children (doer * user_model.User ) (items []Item ) {
52
53
repos := make ([]* repo_model.Repository , 0 )
53
54
sess := db .GetEngine (db .DefaultContext )
54
55
err := sess .Table ("repository" ).
@@ -57,11 +58,11 @@ func (g *groupItemGroup) Children(doer *user_model.User) (items []GroupItem) {
57
58
Find (& repos )
58
59
if err != nil {
59
60
log .Error ("%w" , err )
60
- return make ([]GroupItem , 0 )
61
+ return make ([]Item , 0 )
61
62
}
62
63
err = g .Group .LoadAccessibleSubgroups (db .DefaultContext , false , doer )
63
64
if err != nil {
64
- return make ([]GroupItem , 0 )
65
+ return make ([]Item , 0 )
65
66
}
66
67
slices .SortStableFunc (g .Group .Subgroups , func (a , b * group_model.Group ) int {
67
68
return a .SortOrder - b .SortOrder
@@ -97,15 +98,17 @@ func (g *groupItemGroup) ID() int64 {
97
98
func (g * groupItemGroup ) Sort () int {
98
99
return g .Group .SortOrder
99
100
}
100
- func GetTopLevelGroupItemList (ctx context.Context , orgID int64 , doer * user_model.User ) (rootItems []GroupItem ) {
101
+
102
+ func GetTopLevelGroupItemList (ctx context.Context , orgID int64 , doer * user_model.User ) []Item {
103
+ var rootItems []Item
101
104
groups , err := group_model .FindGroupsByCond (ctx , & group_model.FindGroupsOptions {
102
105
ParentGroupID : 0 ,
103
106
ActorID : doer .ID ,
104
107
OwnerID : orgID ,
105
108
}, group_model .
106
109
AccessibleGroupCondition (doer , unit .TypeInvalid , perm .AccessModeRead ))
107
110
if err != nil {
108
- return
111
+ return nil
109
112
}
110
113
repos := make ([]* repo_model.Repository , 0 )
111
114
cond := builder .NewCond ().
@@ -115,7 +118,7 @@ func GetTopLevelGroupItemList(ctx context.Context, orgID int64, doer *user_model
115
118
sess := db .GetEngine (ctx )
116
119
err = sess .Table ("repository" ).Where (cond ).Find (& repos )
117
120
if err != nil {
118
- return
121
+ return nil
119
122
}
120
123
slices .SortStableFunc (groups , func (a , b * group_model.Group ) int {
121
124
return a .SortOrder - b .SortOrder
@@ -129,12 +132,12 @@ func GetTopLevelGroupItemList(ctx context.Context, orgID int64, doer *user_model
129
132
for _ , r := range repos {
130
133
rootItems = append (rootItems , & groupItemRepo {r })
131
134
}
132
- return
135
+ return rootItems
133
136
}
134
137
135
- func GroupItemHasChild ( it GroupItem , other int64 , ctx context. Context , doer * user_model.User ) bool {
138
+ func ItemHasChild ( ctx context. Context , it Item , other int64 , doer * user_model.User ) bool {
136
139
for _ , item := range it .Children (doer ) {
137
- if GroupItemHasChild ( item , other , ctx , doer ) {
140
+ if ItemHasChild ( ctx , item , other , doer ) {
138
141
return true
139
142
}
140
143
}
0 commit comments