@@ -21,6 +21,7 @@ import (
21
21
repo_model "code.gitea.io/gitea/models/repo"
22
22
"code.gitea.io/gitea/models/unit"
23
23
user_model "code.gitea.io/gitea/models/user"
24
+ "code.gitea.io/gitea/modules/container"
24
25
"code.gitea.io/gitea/modules/git"
25
26
"code.gitea.io/gitea/modules/log"
26
27
api "code.gitea.io/gitea/modules/structs"
@@ -105,33 +106,46 @@ func ToBranch(ctx context.Context, repo *repo_model.Repository, branchName strin
105
106
return branch , nil
106
107
}
107
108
108
- // ToBranchProtection convert a ProtectedBranch to api.BranchProtection
109
- func ToBranchProtection (ctx context.Context , bp * git_model.ProtectedBranch ) * api.BranchProtection {
110
- pushWhitelistUsernames , err := user_model .GetUserNamesByIDs (ctx , bp .WhitelistUserIDs )
111
- if err != nil {
112
- log .Error ("GetUserNamesByIDs (WhitelistUserIDs): %v" , err )
113
- }
114
- mergeWhitelistUsernames , err := user_model .GetUserNamesByIDs (ctx , bp .MergeWhitelistUserIDs )
115
- if err != nil {
116
- log .Error ("GetUserNamesByIDs (MergeWhitelistUserIDs): %v" , err )
117
- }
118
- approvalsWhitelistUsernames , err := user_model .GetUserNamesByIDs (ctx , bp .ApprovalsWhitelistUserIDs )
119
- if err != nil {
120
- log .Error ("GetUserNamesByIDs (ApprovalsWhitelistUserIDs): %v" , err )
121
- }
122
- pushWhitelistTeams , err := organization .GetTeamNamesByID (ctx , bp .WhitelistTeamIDs )
123
- if err != nil {
124
- log .Error ("GetTeamNamesByID (WhitelistTeamIDs): %v" , err )
109
+ // getWhitelistEntities returns the names of the entities that are in the whitelist
110
+ func getWhitelistEntities [T * user_model.User | * organization.Team ](entities []T , whitelistIDs []int64 ) []string {
111
+ whitelistUserIDsSet := container .SetOf (whitelistIDs ... )
112
+ whitelistNames := make ([]string , 0 )
113
+ for _ , entity := range entities {
114
+ switch v := any (entity ).(type ) {
115
+ case * user_model.User :
116
+ if whitelistUserIDsSet .Contains (v .ID ) {
117
+ whitelistNames = append (whitelistNames , v .Name )
118
+ }
119
+ case * organization.Team :
120
+ if whitelistUserIDsSet .Contains (v .ID ) {
121
+ whitelistNames = append (whitelistNames , v .Name )
122
+ }
123
+ }
125
124
}
126
- mergeWhitelistTeams , err := organization .GetTeamNamesByID (ctx , bp .MergeWhitelistTeamIDs )
125
+
126
+ return whitelistNames
127
+ }
128
+
129
+ // ToBranchProtection convert a ProtectedBranch to api.BranchProtection
130
+ func ToBranchProtection (ctx context.Context , bp * git_model.ProtectedBranch , repo * repo_model.Repository ) * api.BranchProtection {
131
+ readers , err := access_model .GetRepoReaders (ctx , repo )
127
132
if err != nil {
128
- log .Error ("GetTeamNamesByID (MergeWhitelistTeamIDs) : %v" , err )
133
+ log .Error ("GetRepoReaders : %v" , err )
129
134
}
130
- approvalsWhitelistTeams , err := organization .GetTeamNamesByID (ctx , bp .ApprovalsWhitelistTeamIDs )
135
+
136
+ pushWhitelistUsernames := getWhitelistEntities (readers , bp .WhitelistUserIDs )
137
+ mergeWhitelistUsernames := getWhitelistEntities (readers , bp .MergeWhitelistUserIDs )
138
+ approvalsWhitelistUsernames := getWhitelistEntities (readers , bp .ApprovalsWhitelistUserIDs )
139
+
140
+ teamReaders , err := organization .OrgFromUser (repo .Owner ).TeamsWithAccessToRepo (ctx , repo .ID , perm .AccessModeRead )
131
141
if err != nil {
132
- log .Error ("GetTeamNamesByID (ApprovalsWhitelistTeamIDs) : %v" , err )
142
+ log .Error ("Repo.Owner.TeamsWithAccessToRepo : %v" , err )
133
143
}
134
144
145
+ pushWhitelistTeams := getWhitelistEntities (teamReaders , bp .WhitelistTeamIDs )
146
+ mergeWhitelistTeams := getWhitelistEntities (teamReaders , bp .MergeWhitelistTeamIDs )
147
+ approvalsWhitelistTeams := getWhitelistEntities (teamReaders , bp .ApprovalsWhitelistTeamIDs )
148
+
135
149
branchName := ""
136
150
if ! git_model .IsRuleNameSpecial (bp .RuleName ) {
137
151
branchName = bp .RuleName
0 commit comments