Skip to content

Commit d88958b

Browse files
GiteaBotedwardzhangedtechknowlogickwenzhuo.zhang
authored
Fix branch_protection api shows users/teams who has no readAccess (#30291) (#30544)
Backport #30291 by @edwardzhanged Add some logic in `convert.ToBranchProtection` to return only the names associated with readAccess instead of returning all names. This will ensure consistency in behavior between the frontend and backend. Fixes: #27694 Co-authored-by: Edward Zhang <[email protected]> Co-authored-by: techknowlogick <[email protected]> Co-authored-by: wenzhuo.zhang <[email protected]>
1 parent 5c55851 commit d88958b

File tree

2 files changed

+39
-25
lines changed

2 files changed

+39
-25
lines changed

routers/api/v1/repo/branch.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ func GetBranchProtection(ctx *context.APIContext) {
437437
return
438438
}
439439

440-
ctx.JSON(http.StatusOK, convert.ToBranchProtection(ctx, bp))
440+
ctx.JSON(http.StatusOK, convert.ToBranchProtection(ctx, bp, repo))
441441
}
442442

443443
// ListBranchProtections list branch protections for a repo
@@ -470,7 +470,7 @@ func ListBranchProtections(ctx *context.APIContext) {
470470
}
471471
apiBps := make([]*api.BranchProtection, len(bps))
472472
for i := range bps {
473-
apiBps[i] = convert.ToBranchProtection(ctx, bps[i])
473+
apiBps[i] = convert.ToBranchProtection(ctx, bps[i], repo)
474474
}
475475

476476
ctx.JSON(http.StatusOK, apiBps)
@@ -681,7 +681,7 @@ func CreateBranchProtection(ctx *context.APIContext) {
681681
return
682682
}
683683

684-
ctx.JSON(http.StatusCreated, convert.ToBranchProtection(ctx, bp))
684+
ctx.JSON(http.StatusCreated, convert.ToBranchProtection(ctx, bp, repo))
685685
}
686686

687687
// EditBranchProtection edits a branch protection for a repo
@@ -959,7 +959,7 @@ func EditBranchProtection(ctx *context.APIContext) {
959959
return
960960
}
961961

962-
ctx.JSON(http.StatusOK, convert.ToBranchProtection(ctx, bp))
962+
ctx.JSON(http.StatusOK, convert.ToBranchProtection(ctx, bp, repo))
963963
}
964964

965965
// DeleteBranchProtection deletes a branch protection for a repo

services/convert/convert.go

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
repo_model "code.gitea.io/gitea/models/repo"
2222
"code.gitea.io/gitea/models/unit"
2323
user_model "code.gitea.io/gitea/models/user"
24+
"code.gitea.io/gitea/modules/container"
2425
"code.gitea.io/gitea/modules/git"
2526
"code.gitea.io/gitea/modules/log"
2627
api "code.gitea.io/gitea/modules/structs"
@@ -105,33 +106,46 @@ func ToBranch(ctx context.Context, repo *repo_model.Repository, branchName strin
105106
return branch, nil
106107
}
107108

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+
}
125124
}
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)
127132
if err != nil {
128-
log.Error("GetTeamNamesByID (MergeWhitelistTeamIDs): %v", err)
133+
log.Error("GetRepoReaders: %v", err)
129134
}
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)
131141
if err != nil {
132-
log.Error("GetTeamNamesByID (ApprovalsWhitelistTeamIDs): %v", err)
142+
log.Error("Repo.Owner.TeamsWithAccessToRepo: %v", err)
133143
}
134144

145+
pushWhitelistTeams := getWhitelistEntities(teamReaders, bp.WhitelistTeamIDs)
146+
mergeWhitelistTeams := getWhitelistEntities(teamReaders, bp.MergeWhitelistTeamIDs)
147+
approvalsWhitelistTeams := getWhitelistEntities(teamReaders, bp.ApprovalsWhitelistTeamIDs)
148+
135149
branchName := ""
136150
if !git_model.IsRuleNameSpecial(bp.RuleName) {
137151
branchName = bp.RuleName

0 commit comments

Comments
 (0)