Skip to content

Commit f71a25a

Browse files
committed
move isSliceInt64Eq to util pkg; improve function names & typo
1 parent 3ee5d15 commit f71a25a

File tree

6 files changed

+44
-36
lines changed

6 files changed

+44
-36
lines changed

models/branches.go

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ package models
66

77
import (
88
"fmt"
9-
"sort"
109
"strings"
1110
"time"
1211

1312
"code.gitea.io/gitea/modules/base"
1413
"code.gitea.io/gitea/modules/log"
14+
"code.gitea.io/gitea/modules/util"
1515

1616
"github.com/Unknwon/com"
1717
)
@@ -94,27 +94,6 @@ func GetProtectedBranchByID(id int64) (*ProtectedBranch, error) {
9494
return rel, nil
9595
}
9696

97-
// Int64Slice attaches the methods of Interface to []int64, sorting in increasing order.
98-
type Int64Slice []int64
99-
100-
func (p Int64Slice) Len() int { return len(p) }
101-
func (p Int64Slice) Less(i, j int) bool { return p[i] < p[j] }
102-
func (p Int64Slice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
103-
104-
func isSliceInt64Eq(a, b []int64) bool {
105-
if len(a) != len(b) {
106-
return false
107-
}
108-
sort.Sort(Int64Slice(a))
109-
sort.Sort(Int64Slice(b))
110-
for i := 0; i < len(a); i++ {
111-
if a[i] != b[i] {
112-
return false
113-
}
114-
}
115-
return true
116-
}
117-
11897
// UpdateProtectBranch saves branch protection options of repository.
11998
// If ID is 0, it creates a new record. Otherwise, updates existing record.
12099
// This function also performs check if whitelist user and team's IDs have been changed
@@ -124,7 +103,7 @@ func UpdateProtectBranch(repo *Repository, protectBranch *ProtectedBranch, white
124103
return fmt.Errorf("GetOwner: %v", err)
125104
}
126105

127-
hasUsersChanged := !isSliceInt64Eq(protectBranch.WhitelistUserIDs, whitelistUserIDs)
106+
hasUsersChanged := !util.IsSliceInt64Eq(protectBranch.WhitelistUserIDs, whitelistUserIDs)
128107
if hasUsersChanged {
129108
protectBranch.WhitelistUserIDs = make([]int64, 0, len(whitelistUserIDs))
130109
for _, userID := range whitelistUserIDs {
@@ -140,11 +119,11 @@ func UpdateProtectBranch(repo *Repository, protectBranch *ProtectedBranch, white
140119
}
141120

142121
// if the repo is in an orgniziation
143-
hasTeamsChanged := !isSliceInt64Eq(protectBranch.WhitelistTeamIDs, whitelistTeamIDs)
122+
hasTeamsChanged := !util.IsSliceInt64Eq(protectBranch.WhitelistTeamIDs, whitelistTeamIDs)
144123
if hasTeamsChanged {
145-
teams, err := GetTeamsHaveAccessToRepo(repo.OwnerID, repo.ID, AccessModeWrite)
124+
teams, err := GetTeamsWithAccessToRepo(repo.OwnerID, repo.ID, AccessModeWrite)
146125
if err != nil {
147-
return fmt.Errorf("GetTeamsHaveAccessToRepo [org_id: %d, repo_id: %d]: %v", repo.OwnerID, repo.ID, err)
126+
return fmt.Errorf("GetTeamsWithAccessToRepo [org_id: %d, repo_id: %d]: %v", repo.OwnerID, repo.ID, err)
148127
}
149128
protectBranch.WhitelistTeamIDs = make([]int64, 0, len(teams))
150129
for i := range teams {

models/org.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -577,9 +577,9 @@ func (org *User) getUserTeamIDs(e Engine, userID int64) ([]int64, error) {
577577
Find(&teamIDs)
578578
}
579579

580-
// TeamsHaveAccessToRepo returns all teamsthat have given access level to the repository.
581-
func (org *User) TeamsHaveAccessToRepo(repoID int64, mode AccessMode) ([]*Team, error) {
582-
return GetTeamsHaveAccessToRepo(org.ID, repoID, mode)
580+
// TeamsWithAccessToRepo returns all teamsthat have given access level to the repository.
581+
func (org *User) TeamsWithAccessToRepo(repoID int64, mode AccessMode) ([]*Team, error) {
582+
return GetTeamsWithAccessToRepo(org.ID, repoID, mode)
583583
}
584584

585585
// GetUserTeamIDs returns of all team IDs of the organization that user is member of.

models/org_team.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -650,8 +650,8 @@ func removeTeamRepo(e Engine, teamID, repoID int64) error {
650650
return err
651651
}
652652

653-
// GetTeamsHaveAccessToRepo returns all teams in an organization that have given access level to the repository.
654-
func GetTeamsHaveAccessToRepo(orgID, repoID int64, mode AccessMode) ([]*Team, error) {
653+
// GetTeamsWithAccessToRepo returns all teams in an organization that have given access level to the repository.
654+
func GetTeamsWithAccessToRepo(orgID, repoID int64, mode AccessMode) ([]*Team, error) {
655655
teams := make([]*Team, 0, 5)
656656
return teams, x.Where("team.authorize >= ?", mode).
657657
Join("INNER", "team_repo", "team_repo.team_id = team.id").

models/repo.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -658,11 +658,11 @@ func (repo *Repository) CanEnableEditor() bool {
658658

659659
// GetWriters returns all users that have write access to the repository.
660660
func (repo *Repository) GetWriters() (_ []*User, err error) {
661-
return repo.getUsersWithAccesMode(x, AccessModeWrite)
661+
return repo.getUsersWithAccessMode(x, AccessModeWrite)
662662
}
663663

664-
// getUsersWithAccesMode returns users that have at least given access mode to the repository.
665-
func (repo *Repository) getUsersWithAccesMode(e Engine, mode AccessMode) (_ []*User, err error) {
664+
// getUsersWithAccessMode returns users that have at least given access mode to the repository.
665+
func (repo *Repository) getUsersWithAccessMode(e Engine, mode AccessMode) (_ []*User, err error) {
666666
if err = repo.getOwner(e); err != nil {
667667
return nil, err
668668
}

modules/util/compare.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright 2017 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package util
6+
7+
import "sort"
8+
9+
// Int64Slice attaches the methods of Interface to []int64, sorting in increasing order.
10+
type Int64Slice []int64
11+
12+
func (p Int64Slice) Len() int { return len(p) }
13+
func (p Int64Slice) Less(i, j int) bool { return p[i] < p[j] }
14+
func (p Int64Slice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
15+
16+
// IsSliceInt64Eq returns if the two slice has the same elements but different sequences.
17+
func IsSliceInt64Eq(a, b []int64) bool {
18+
if len(a) != len(b) {
19+
return false
20+
}
21+
sort.Sort(Int64Slice(a))
22+
sort.Sort(Int64Slice(b))
23+
for i := 0; i < len(a); i++ {
24+
if a[i] != b[i] {
25+
return false
26+
}
27+
}
28+
return true
29+
}

routers/repo/setting_protected_branch.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,9 @@ func SettingsProtectedBranch(c *context.Context) {
125125
c.Data["whitelist_users"] = strings.Join(base.Int64sToStrings(protectBranch.WhitelistUserIDs), ",")
126126

127127
if c.Repo.Owner.IsOrganization() {
128-
teams, err := c.Repo.Owner.TeamsHaveAccessToRepo(c.Repo.Repository.ID, models.AccessModeWrite)
128+
teams, err := c.Repo.Owner.TeamsWithAccessToRepo(c.Repo.Repository.ID, models.AccessModeWrite)
129129
if err != nil {
130-
c.Handle(500, "Repo.Owner.TeamsHaveAccessToRepo", err)
130+
c.Handle(500, "Repo.Owner.TeamsWithAccessToRepo", err)
131131
return
132132
}
133133
c.Data["Teams"] = teams

0 commit comments

Comments
 (0)