Rule Sort

This commit is contained in:
Lunny Xiao 2022-10-20 13:39:58 +08:00
parent 1837aae03b
commit a5cc6d3bfc
No known key found for this signature in database
GPG key ID: C3B7C91B632F738A
2 changed files with 31 additions and 5 deletions

View file

@ -30,6 +30,7 @@ type ProtectedBranch struct {
RepoID int64 `xorm:"UNIQUE(s)"` RepoID int64 `xorm:"UNIQUE(s)"`
BranchName string `xorm:"UNIQUE(s)"` // a branch name or a glob match to branch name BranchName string `xorm:"UNIQUE(s)"` // a branch name or a glob match to branch name
globRule glob.Glob `xorm:"-"` globRule glob.Glob `xorm:"-"`
isPlainName bool `xorm:"-"`
CanPush bool `xorm:"NOT NULL DEFAULT false"` CanPush bool `xorm:"NOT NULL DEFAULT false"`
EnableWhitelist bool EnableWhitelist bool
WhitelistUserIDs []int64 `xorm:"JSON TEXT"` WhitelistUserIDs []int64 `xorm:"JSON TEXT"`
@ -60,14 +61,19 @@ func init() {
db.RegisterModel(new(ProtectedBranch)) db.RegisterModel(new(ProtectedBranch))
} }
func (protectBranch *ProtectedBranch) loadGlob() {
if protectBranch.globRule == nil {
protectBranch.globRule = glob.MustCompile(protectBranch.BranchName, '/')
protectBranch.isPlainName = protectBranch.globRule.Match(protectBranch.BranchName)
}
}
// Match tests if branchName matches the rule // Match tests if branchName matches the rule
func (protectBranch *ProtectedBranch) Match(branchName string) bool { func (protectBranch *ProtectedBranch) Match(branchName string) bool {
if strings.EqualFold(protectBranch.BranchName, branchName) { if strings.EqualFold(protectBranch.BranchName, branchName) {
return true return true
} }
if protectBranch.globRule == nil { protectBranch.loadGlob()
protectBranch.globRule = glob.MustCompile(protectBranch.BranchName, '/')
}
return protectBranch.globRule.Match(branchName) return protectBranch.globRule.Match(branchName)
} }

View file

@ -6,6 +6,7 @@ package git
import ( import (
"context" "context"
"sort"
"code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/git"
@ -24,11 +25,30 @@ func (rules ProtectedBranchRules) GetFirstMatched(branchName string) *ProtectedB
return nil return nil
} }
func (rules ProtectedBranchRules) Sort() {
sort.Slice(rules, func(i, j int) bool {
rules[i].loadGlob()
rules[j].loadGlob()
if rules[i].isPlainName {
if !rules[j].isPlainName {
return true
}
} else if rules[j].isPlainName {
return true
}
return rules[i].CreatedUnix < rules[j].CreatedUnix
})
}
// FindRepoProtectedBranchRules load all repository's protected rules // FindRepoProtectedBranchRules load all repository's protected rules
func FindRepoProtectedBranchRules(ctx context.Context, repoID int64) (ProtectedBranchRules, error) { func FindRepoProtectedBranchRules(ctx context.Context, repoID int64) (ProtectedBranchRules, error) {
var rules []*ProtectedBranch var rules ProtectedBranchRules
err := db.GetEngine(ctx).Where("repo_id = ?", repoID).Asc("created_unix").Find(&rules) err := db.GetEngine(ctx).Where("repo_id = ?", repoID).Asc("created_unix").Find(&rules)
return rules, err if err != nil {
return nil, err
}
rules.Sort()
return rules, nil
} }
// FindAllMatchedBranches find all matched branches // FindAllMatchedBranches find all matched branches