From f35f801ff2ad463fd82fd9fc1cde3bfdf644c991 Mon Sep 17 00:00:00 2001 From: edwardzhang <13917322147@163.com> Date: Fri, 5 Apr 2024 18:54:37 +0800 Subject: [PATCH 1/4] make branch_protection api return readAccess users --- models/organization/org.go | 5 +++ models/organization/team_repo.go | 12 ++++++ models/perm/access/repo_permission.go | 26 +++++++++++++ routers/api/v1/repo/branch.go | 8 ++-- services/convert/convert.go | 55 ++++++++++++++++++++------- 5 files changed, 89 insertions(+), 17 deletions(-) diff --git a/models/organization/org.go b/models/organization/org.go index ba0fd756e38f6..6cba572e52514 100644 --- a/models/organization/org.go +++ b/models/organization/org.go @@ -658,6 +658,11 @@ func (org *Organization) TeamsWithAccessToRepo(ctx context.Context, repoID int64 return GetTeamsWithAccessToRepo(ctx, org.ID, repoID, mode) } +// TeamsWithAccessToRepoFromIDs returns teams that have given access level to the repository based on the given teamIDs. +func (org *Organization) TeamsWithAccessToRepoFromIDs(ctx context.Context, repoID int64, mode perm.AccessMode, teamIDs []int64) ([]*Team, error) { + return GetTeamsWithAccessToRepoFromIDs(ctx, org.ID, repoID, mode, teamIDs) +} + // GetUserTeamIDs returns of all team IDs of the organization that user is member of. func (org *Organization) GetUserTeamIDs(ctx context.Context, userID int64) ([]int64, error) { return org.getUserTeamIDs(ctx, userID) diff --git a/models/organization/team_repo.go b/models/organization/team_repo.go index 1184e39263635..e4db38c11bda7 100644 --- a/models/organization/team_repo.go +++ b/models/organization/team_repo.go @@ -83,3 +83,15 @@ func GetTeamsWithAccessToRepo(ctx context.Context, orgID, repoID int64, mode per OrderBy("name"). Find(&teams) } + +// GetTeamsWithAccessToRepoFromIDs returns teams in an organization that have given access level to the repository based on given teamIDs. +func GetTeamsWithAccessToRepoFromIDs(ctx context.Context, orgID, repoID int64, mode perm.AccessMode, teamIDs []int64) ([]*Team, error) { + teams := make([]*Team, 0) + return teams, db.GetEngine(ctx).Where("team.authorize >= ?", mode). + In("team.id", teamIDs). + Join("INNER", "team_repo", "team_repo.team_id = team.id"). + And("team_repo.org_id = ?", orgID). + And("team_repo.repo_id = ?", repoID). + OrderBy("name"). + Find(&teams) +} diff --git a/models/perm/access/repo_permission.go b/models/perm/access/repo_permission.go index 4175cb9b92661..9e3b88ec82db3 100644 --- a/models/perm/access/repo_permission.go +++ b/models/perm/access/repo_permission.go @@ -398,6 +398,32 @@ func GetRepoReaders(ctx context.Context, repo *repo_model.Repository) (_ []*user return getUsersWithAccessMode(ctx, repo, perm_model.AccessModeRead) } +// getUsersWithAccessModeFromIDs returns users that have at least given access mode to the repository based on given userIDs. +func getUsersWithAccessModeFromIDs(ctx context.Context, repo *repo_model.Repository, mode perm_model.AccessMode, userIDs []int64) (_ []*user_model.User, err error) { + e := db.GetEngine(ctx) + accesses := make([]*Access, 0, 10) + if err = e.Where("repo_id = ? AND mode >= ?", repo.ID, mode).In("user_id", userIDs).Find(&accesses); err != nil { + return nil, err + } + users := make([]*user_model.User, 0) + if len(accesses) > 0 { + userIDs := make([]int64, len(accesses)) + for i := 0; i < len(accesses); i++ { + userIDs[i] = accesses[i].UserID + } + if err = e.In("id", userIDs).Find(&users); err != nil { + return nil, err + } + } + + return users, nil +} + +// GetRepoReadersFromIDs returns the users who has explicit read access or higher to the repository based on given userIDs. +func GetRepoReadersFromIDs(ctx context.Context, repo *repo_model.Repository, userIDs []int64) (_ []*user_model.User, err error) { + return getUsersWithAccessModeFromIDs(ctx, repo, perm_model.AccessModeRead, userIDs) +} + // GetRepoWriters returns all users that have write access to the repository. func GetRepoWriters(ctx context.Context, repo *repo_model.Repository) (_ []*user_model.User, err error) { return getUsersWithAccessMode(ctx, repo, perm_model.AccessModeWrite) diff --git a/routers/api/v1/repo/branch.go b/routers/api/v1/repo/branch.go index 5e6b6a86586bc..baab486e522fc 100644 --- a/routers/api/v1/repo/branch.go +++ b/routers/api/v1/repo/branch.go @@ -437,7 +437,7 @@ func GetBranchProtection(ctx *context.APIContext) { return } - ctx.JSON(http.StatusOK, convert.ToBranchProtection(ctx, bp)) + ctx.JSON(http.StatusOK, convert.ToBranchProtection(ctx, bp, repo)) } // ListBranchProtections list branch protections for a repo @@ -470,7 +470,7 @@ func ListBranchProtections(ctx *context.APIContext) { } apiBps := make([]*api.BranchProtection, len(bps)) for i := range bps { - apiBps[i] = convert.ToBranchProtection(ctx, bps[i]) + apiBps[i] = convert.ToBranchProtection(ctx, bps[i], repo) } ctx.JSON(http.StatusOK, apiBps) @@ -681,7 +681,7 @@ func CreateBranchProtection(ctx *context.APIContext) { return } - ctx.JSON(http.StatusCreated, convert.ToBranchProtection(ctx, bp)) + ctx.JSON(http.StatusCreated, convert.ToBranchProtection(ctx, bp, repo)) } // EditBranchProtection edits a branch protection for a repo @@ -959,7 +959,7 @@ func EditBranchProtection(ctx *context.APIContext) { return } - ctx.JSON(http.StatusOK, convert.ToBranchProtection(ctx, bp)) + ctx.JSON(http.StatusOK, convert.ToBranchProtection(ctx, bp, repo)) } // DeleteBranchProtection deletes a branch protection for a repo diff --git a/services/convert/convert.go b/services/convert/convert.go index ca3ec32a4042f..b0819b66e4762 100644 --- a/services/convert/convert.go +++ b/services/convert/convert.go @@ -106,30 +106,59 @@ func ToBranch(ctx context.Context, repo *repo_model.Repository, branchName strin } // ToBranchProtection convert a ProtectedBranch to api.BranchProtection -func ToBranchProtection(ctx context.Context, bp *git_model.ProtectedBranch) *api.BranchProtection { - pushWhitelistUsernames, err := user_model.GetUserNamesByIDs(ctx, bp.WhitelistUserIDs) +func ToBranchProtection(ctx context.Context, bp *git_model.ProtectedBranch, repo *repo_model.Repository) *api.BranchProtection { + pushWhitelistUsernamesRaw, err := access_model.GetRepoReadersFromIDs(ctx, repo, bp.WhitelistUserIDs) if err != nil { - log.Error("GetUserNamesByIDs (WhitelistUserIDs): %v", err) + log.Error("GetRepoReadersFromIDs (WhitelistUserIDs): %v", err) } - mergeWhitelistUsernames, err := user_model.GetUserNamesByIDs(ctx, bp.MergeWhitelistUserIDs) + var pushWhitelistUsernames []string + for _, username := range pushWhitelistUsernamesRaw { + pushWhitelistUsernames = append(pushWhitelistUsernames, username.Name) + } + + mergeWhitelistUsernamesRaw, err := access_model.GetRepoReadersFromIDs(ctx, repo, bp.MergeWhitelistUserIDs) if err != nil { - log.Error("GetUserNamesByIDs (MergeWhitelistUserIDs): %v", err) + log.Error("GetRepoReadersFromIDs (MergeWhitelistUserIDs): %v", err) + } + var mergeWhitelistUsernames []string + for _, username := range mergeWhitelistUsernamesRaw { + mergeWhitelistUsernames = append(mergeWhitelistUsernames, username.Name) } - approvalsWhitelistUsernames, err := user_model.GetUserNamesByIDs(ctx, bp.ApprovalsWhitelistUserIDs) + + approvalsWhitelistUsernamesRaw, err := access_model.GetRepoReadersFromIDs(ctx, repo, bp.ApprovalsWhitelistUserIDs) if err != nil { - log.Error("GetUserNamesByIDs (ApprovalsWhitelistUserIDs): %v", err) + log.Error("GetRepoReadersFromIDs (ApprovalsWhitelistUserIDs): %v", err) + } + var approvalsWhitelistUsernames []string + for _, username := range approvalsWhitelistUsernamesRaw { + approvalsWhitelistUsernames = append(approvalsWhitelistUsernames, username.Name) } - pushWhitelistTeams, err := organization.GetTeamNamesByID(ctx, bp.WhitelistTeamIDs) + + pushWhitelistTeamsRaw, err := organization.OrgFromUser(repo.Owner).TeamsWithAccessToRepoFromIDs(ctx, repo.ID, perm.AccessModeRead, bp.WhitelistTeamIDs) if err != nil { - log.Error("GetTeamNamesByID (WhitelistTeamIDs): %v", err) + log.Error("TeamsWithAccessToRepoFromIDs (WhitelistTeamIDs): %v", err) } - mergeWhitelistTeams, err := organization.GetTeamNamesByID(ctx, bp.MergeWhitelistTeamIDs) + var pushWhitelistTeams []string + for _, team := range pushWhitelistTeamsRaw { + pushWhitelistTeams = append(pushWhitelistTeams, team.Name) + } + + mergeWhitelistTeamsRaw, err := organization.OrgFromUser(repo.Owner).TeamsWithAccessToRepoFromIDs(ctx, repo.ID, perm.AccessModeRead, bp.MergeWhitelistTeamIDs) if err != nil { - log.Error("GetTeamNamesByID (MergeWhitelistTeamIDs): %v", err) + log.Error("TeamsWithAccessToRepoFromIDs (MergeWhitelistTeamIDs): %v", err) + } + var mergeWhitelistTeams []string + for _, team := range mergeWhitelistTeamsRaw { + mergeWhitelistTeams = append(mergeWhitelistTeams, team.Name) } - approvalsWhitelistTeams, err := organization.GetTeamNamesByID(ctx, bp.ApprovalsWhitelistTeamIDs) + + approvalsWhitelistTeamsRaw, err := organization.OrgFromUser(repo.Owner).TeamsWithAccessToRepoFromIDs(ctx, repo.ID, perm.AccessModeRead, bp.ApprovalsWhitelistTeamIDs) if err != nil { - log.Error("GetTeamNamesByID (ApprovalsWhitelistTeamIDs): %v", err) + log.Error("TeamsWithAccessToRepoFromIDs (ApprovalsWhitelistTeamIDs): %v", err) + } + var approvalsWhitelistTeams []string + for _, team := range approvalsWhitelistTeamsRaw { + approvalsWhitelistTeams = append(approvalsWhitelistTeams, team.Name) } branchName := "" From 980e1c9501a69ec69d36466dcafbb03c2f0e6d89 Mon Sep 17 00:00:00 2001 From: edwardzhang <13917322147@163.com> Date: Sun, 14 Apr 2024 15:39:56 +0800 Subject: [PATCH 2/4] retrive whiteliseUsers by filtering from GetRepoReaders --- models/organization/org.go | 5 -- models/organization/team_repo.go | 12 ---- models/perm/access/repo_permission.go | 26 --------- services/convert/convert.go | 82 +++++++++++++-------------- 4 files changed, 40 insertions(+), 85 deletions(-) diff --git a/models/organization/org.go b/models/organization/org.go index 6cba572e52514..ba0fd756e38f6 100644 --- a/models/organization/org.go +++ b/models/organization/org.go @@ -658,11 +658,6 @@ func (org *Organization) TeamsWithAccessToRepo(ctx context.Context, repoID int64 return GetTeamsWithAccessToRepo(ctx, org.ID, repoID, mode) } -// TeamsWithAccessToRepoFromIDs returns teams that have given access level to the repository based on the given teamIDs. -func (org *Organization) TeamsWithAccessToRepoFromIDs(ctx context.Context, repoID int64, mode perm.AccessMode, teamIDs []int64) ([]*Team, error) { - return GetTeamsWithAccessToRepoFromIDs(ctx, org.ID, repoID, mode, teamIDs) -} - // GetUserTeamIDs returns of all team IDs of the organization that user is member of. func (org *Organization) GetUserTeamIDs(ctx context.Context, userID int64) ([]int64, error) { return org.getUserTeamIDs(ctx, userID) diff --git a/models/organization/team_repo.go b/models/organization/team_repo.go index e4db38c11bda7..1184e39263635 100644 --- a/models/organization/team_repo.go +++ b/models/organization/team_repo.go @@ -83,15 +83,3 @@ func GetTeamsWithAccessToRepo(ctx context.Context, orgID, repoID int64, mode per OrderBy("name"). Find(&teams) } - -// GetTeamsWithAccessToRepoFromIDs returns teams in an organization that have given access level to the repository based on given teamIDs. -func GetTeamsWithAccessToRepoFromIDs(ctx context.Context, orgID, repoID int64, mode perm.AccessMode, teamIDs []int64) ([]*Team, error) { - teams := make([]*Team, 0) - return teams, db.GetEngine(ctx).Where("team.authorize >= ?", mode). - In("team.id", teamIDs). - Join("INNER", "team_repo", "team_repo.team_id = team.id"). - And("team_repo.org_id = ?", orgID). - And("team_repo.repo_id = ?", repoID). - OrderBy("name"). - Find(&teams) -} diff --git a/models/perm/access/repo_permission.go b/models/perm/access/repo_permission.go index 9e3b88ec82db3..4175cb9b92661 100644 --- a/models/perm/access/repo_permission.go +++ b/models/perm/access/repo_permission.go @@ -398,32 +398,6 @@ func GetRepoReaders(ctx context.Context, repo *repo_model.Repository) (_ []*user return getUsersWithAccessMode(ctx, repo, perm_model.AccessModeRead) } -// getUsersWithAccessModeFromIDs returns users that have at least given access mode to the repository based on given userIDs. -func getUsersWithAccessModeFromIDs(ctx context.Context, repo *repo_model.Repository, mode perm_model.AccessMode, userIDs []int64) (_ []*user_model.User, err error) { - e := db.GetEngine(ctx) - accesses := make([]*Access, 0, 10) - if err = e.Where("repo_id = ? AND mode >= ?", repo.ID, mode).In("user_id", userIDs).Find(&accesses); err != nil { - return nil, err - } - users := make([]*user_model.User, 0) - if len(accesses) > 0 { - userIDs := make([]int64, len(accesses)) - for i := 0; i < len(accesses); i++ { - userIDs[i] = accesses[i].UserID - } - if err = e.In("id", userIDs).Find(&users); err != nil { - return nil, err - } - } - - return users, nil -} - -// GetRepoReadersFromIDs returns the users who has explicit read access or higher to the repository based on given userIDs. -func GetRepoReadersFromIDs(ctx context.Context, repo *repo_model.Repository, userIDs []int64) (_ []*user_model.User, err error) { - return getUsersWithAccessModeFromIDs(ctx, repo, perm_model.AccessModeRead, userIDs) -} - // GetRepoWriters returns all users that have write access to the repository. func GetRepoWriters(ctx context.Context, repo *repo_model.Repository) (_ []*user_model.User, err error) { return getUsersWithAccessMode(ctx, repo, perm_model.AccessModeWrite) diff --git a/services/convert/convert.go b/services/convert/convert.go index b0819b66e4762..e14fa48da5ed1 100644 --- a/services/convert/convert.go +++ b/services/convert/convert.go @@ -105,61 +105,59 @@ func ToBranch(ctx context.Context, repo *repo_model.Repository, branchName strin return branch, nil } +// getWhitelistEntities returns the names of the entities that are in the whitelist +func getWhitelistEntities(entities []interface{}, whitelistIDs []int64) []string { + whitelistIDsMap := make(map[int64]struct{}) + for _, id := range whitelistIDs { + whitelistIDsMap[id] = struct{}{} + } + + whitelistNames := make([]string, 0) + for _, entity := range entities { + switch v := entity.(type) { + case *user_model.User: + if _, ok := whitelistIDsMap[v.ID]; ok { + whitelistNames = append(whitelistNames, v.Name) + } + case *organization.Team: + if _, ok := whitelistIDsMap[v.ID]; ok { + whitelistNames = append(whitelistNames, v.Name) + } + } + } + + return whitelistNames +} + // ToBranchProtection convert a ProtectedBranch to api.BranchProtection func ToBranchProtection(ctx context.Context, bp *git_model.ProtectedBranch, repo *repo_model.Repository) *api.BranchProtection { - pushWhitelistUsernamesRaw, err := access_model.GetRepoReadersFromIDs(ctx, repo, bp.WhitelistUserIDs) + readers, err := access_model.GetRepoReaders(ctx, repo) if err != nil { - log.Error("GetRepoReadersFromIDs (WhitelistUserIDs): %v", err) - } - var pushWhitelistUsernames []string - for _, username := range pushWhitelistUsernamesRaw { - pushWhitelistUsernames = append(pushWhitelistUsernames, username.Name) + log.Error("GetRepoReaders: %v", err) } - mergeWhitelistUsernamesRaw, err := access_model.GetRepoReadersFromIDs(ctx, repo, bp.MergeWhitelistUserIDs) - if err != nil { - log.Error("GetRepoReadersFromIDs (MergeWhitelistUserIDs): %v", err) - } - var mergeWhitelistUsernames []string - for _, username := range mergeWhitelistUsernamesRaw { - mergeWhitelistUsernames = append(mergeWhitelistUsernames, username.Name) + readersInterface := make([]interface{}, len(readers)) + for i, v := range readers { + readersInterface[i] = v } - approvalsWhitelistUsernamesRaw, err := access_model.GetRepoReadersFromIDs(ctx, repo, bp.ApprovalsWhitelistUserIDs) - if err != nil { - log.Error("GetRepoReadersFromIDs (ApprovalsWhitelistUserIDs): %v", err) - } - var approvalsWhitelistUsernames []string - for _, username := range approvalsWhitelistUsernamesRaw { - approvalsWhitelistUsernames = append(approvalsWhitelistUsernames, username.Name) - } + pushWhitelistUsernames := getWhitelistEntities(readersInterface, bp.WhitelistUserIDs) + mergeWhitelistUsernames := getWhitelistEntities(readersInterface, bp.MergeWhitelistUserIDs) + approvalsWhitelistUsernames := getWhitelistEntities(readersInterface, bp.ApprovalsWhitelistUserIDs) - pushWhitelistTeamsRaw, err := organization.OrgFromUser(repo.Owner).TeamsWithAccessToRepoFromIDs(ctx, repo.ID, perm.AccessModeRead, bp.WhitelistTeamIDs) + teamReaders, err := organization.OrgFromUser(repo.Owner).TeamsWithAccessToRepo(ctx, repo.ID, perm.AccessModeRead) if err != nil { - log.Error("TeamsWithAccessToRepoFromIDs (WhitelistTeamIDs): %v", err) - } - var pushWhitelistTeams []string - for _, team := range pushWhitelistTeamsRaw { - pushWhitelistTeams = append(pushWhitelistTeams, team.Name) + log.Error("Repo.Owner.TeamsWithAccessToRepo: %v", err) } - mergeWhitelistTeamsRaw, err := organization.OrgFromUser(repo.Owner).TeamsWithAccessToRepoFromIDs(ctx, repo.ID, perm.AccessModeRead, bp.MergeWhitelistTeamIDs) - if err != nil { - log.Error("TeamsWithAccessToRepoFromIDs (MergeWhitelistTeamIDs): %v", err) - } - var mergeWhitelistTeams []string - for _, team := range mergeWhitelistTeamsRaw { - mergeWhitelistTeams = append(mergeWhitelistTeams, team.Name) + teamReadersInterface := make([]interface{}, len(teamReaders)) + for i, v := range teamReaders { + teamReadersInterface[i] = v } - approvalsWhitelistTeamsRaw, err := organization.OrgFromUser(repo.Owner).TeamsWithAccessToRepoFromIDs(ctx, repo.ID, perm.AccessModeRead, bp.ApprovalsWhitelistTeamIDs) - if err != nil { - log.Error("TeamsWithAccessToRepoFromIDs (ApprovalsWhitelistTeamIDs): %v", err) - } - var approvalsWhitelistTeams []string - for _, team := range approvalsWhitelistTeamsRaw { - approvalsWhitelistTeams = append(approvalsWhitelistTeams, team.Name) - } + pushWhitelistTeams := getWhitelistEntities(teamReadersInterface, bp.WhitelistTeamIDs) + mergeWhitelistTeams := getWhitelistEntities(teamReadersInterface, bp.MergeWhitelistTeamIDs) + approvalsWhitelistTeams := getWhitelistEntities(teamReadersInterface, bp.ApprovalsWhitelistTeamIDs) branchName := "" if !git_model.IsRuleNameSpecial(bp.RuleName) { From f8af94da11609b3426a7a268bbc47b71929bba98 Mon Sep 17 00:00:00 2001 From: edwardzhang <13917322147@163.com> Date: Sun, 14 Apr 2024 16:01:52 +0800 Subject: [PATCH 3/4] fix fmt error {}interface to any --- services/convert/convert.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/services/convert/convert.go b/services/convert/convert.go index e14fa48da5ed1..4e6030a005406 100644 --- a/services/convert/convert.go +++ b/services/convert/convert.go @@ -106,7 +106,7 @@ func ToBranch(ctx context.Context, repo *repo_model.Repository, branchName strin } // getWhitelistEntities returns the names of the entities that are in the whitelist -func getWhitelistEntities(entities []interface{}, whitelistIDs []int64) []string { +func getWhitelistEntities(entities []any, whitelistIDs []int64) []string { whitelistIDsMap := make(map[int64]struct{}) for _, id := range whitelistIDs { whitelistIDsMap[id] = struct{}{} @@ -136,7 +136,7 @@ func ToBranchProtection(ctx context.Context, bp *git_model.ProtectedBranch, repo log.Error("GetRepoReaders: %v", err) } - readersInterface := make([]interface{}, len(readers)) + readersInterface := make([]any, len(readers)) for i, v := range readers { readersInterface[i] = v } @@ -150,7 +150,7 @@ func ToBranchProtection(ctx context.Context, bp *git_model.ProtectedBranch, repo log.Error("Repo.Owner.TeamsWithAccessToRepo: %v", err) } - teamReadersInterface := make([]interface{}, len(teamReaders)) + teamReadersInterface := make([]any, len(teamReaders)) for i, v := range teamReaders { teamReadersInterface[i] = v } From db6a0d3196ccc80163d2d0d4f72e0d9eb436b3a4 Mon Sep 17 00:00:00 2001 From: "wenzhuo.zhang" Date: Tue, 16 Apr 2024 13:44:35 +0800 Subject: [PATCH 4/4] Use generic to restrict getWhitelistEntities --- services/convert/convert.go | 37 ++++++++++++------------------------- 1 file changed, 12 insertions(+), 25 deletions(-) diff --git a/services/convert/convert.go b/services/convert/convert.go index 4e6030a005406..5df0303646b1c 100644 --- a/services/convert/convert.go +++ b/services/convert/convert.go @@ -21,6 +21,7 @@ import ( repo_model "code.gitea.io/gitea/models/repo" "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/git" "code.gitea.io/gitea/modules/log" api "code.gitea.io/gitea/modules/structs" @@ -106,21 +107,17 @@ func ToBranch(ctx context.Context, repo *repo_model.Repository, branchName strin } // getWhitelistEntities returns the names of the entities that are in the whitelist -func getWhitelistEntities(entities []any, whitelistIDs []int64) []string { - whitelistIDsMap := make(map[int64]struct{}) - for _, id := range whitelistIDs { - whitelistIDsMap[id] = struct{}{} - } - +func getWhitelistEntities[T *user_model.User | *organization.Team](entities []T, whitelistIDs []int64) []string { + whitelistUserIDsSet := container.SetOf(whitelistIDs...) whitelistNames := make([]string, 0) for _, entity := range entities { - switch v := entity.(type) { + switch v := any(entity).(type) { case *user_model.User: - if _, ok := whitelistIDsMap[v.ID]; ok { + if whitelistUserIDsSet.Contains(v.ID) { whitelistNames = append(whitelistNames, v.Name) } case *organization.Team: - if _, ok := whitelistIDsMap[v.ID]; ok { + if whitelistUserIDsSet.Contains(v.ID) { whitelistNames = append(whitelistNames, v.Name) } } @@ -136,28 +133,18 @@ func ToBranchProtection(ctx context.Context, bp *git_model.ProtectedBranch, repo log.Error("GetRepoReaders: %v", err) } - readersInterface := make([]any, len(readers)) - for i, v := range readers { - readersInterface[i] = v - } - - pushWhitelistUsernames := getWhitelistEntities(readersInterface, bp.WhitelistUserIDs) - mergeWhitelistUsernames := getWhitelistEntities(readersInterface, bp.MergeWhitelistUserIDs) - approvalsWhitelistUsernames := getWhitelistEntities(readersInterface, bp.ApprovalsWhitelistUserIDs) + pushWhitelistUsernames := getWhitelistEntities(readers, bp.WhitelistUserIDs) + mergeWhitelistUsernames := getWhitelistEntities(readers, bp.MergeWhitelistUserIDs) + approvalsWhitelistUsernames := getWhitelistEntities(readers, bp.ApprovalsWhitelistUserIDs) teamReaders, err := organization.OrgFromUser(repo.Owner).TeamsWithAccessToRepo(ctx, repo.ID, perm.AccessModeRead) if err != nil { log.Error("Repo.Owner.TeamsWithAccessToRepo: %v", err) } - teamReadersInterface := make([]any, len(teamReaders)) - for i, v := range teamReaders { - teamReadersInterface[i] = v - } - - pushWhitelistTeams := getWhitelistEntities(teamReadersInterface, bp.WhitelistTeamIDs) - mergeWhitelistTeams := getWhitelistEntities(teamReadersInterface, bp.MergeWhitelistTeamIDs) - approvalsWhitelistTeams := getWhitelistEntities(teamReadersInterface, bp.ApprovalsWhitelistTeamIDs) + pushWhitelistTeams := getWhitelistEntities(teamReaders, bp.WhitelistTeamIDs) + mergeWhitelistTeams := getWhitelistEntities(teamReaders, bp.MergeWhitelistTeamIDs) + approvalsWhitelistTeams := getWhitelistEntities(teamReaders, bp.ApprovalsWhitelistTeamIDs) branchName := "" if !git_model.IsRuleNameSpecial(bp.RuleName) {