Skip to content

Commit d65b457

Browse files
authored
Merge branch 'main' into fix-regression-of-29430
2 parents 30e20f7 + 5d43801 commit d65b457

File tree

2 files changed

+43
-8
lines changed

2 files changed

+43
-8
lines changed

models/git/protected_branch.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,20 @@ func IsRuleNameSpecial(ruleName string) bool {
8484
}
8585

8686
func (protectBranch *ProtectedBranch) loadGlob() {
87-
if protectBranch.globRule == nil {
88-
var err error
89-
protectBranch.globRule, err = glob.Compile(protectBranch.RuleName, '/')
90-
if err != nil {
91-
log.Warn("Invalid glob rule for ProtectedBranch[%d]: %s %v", protectBranch.ID, protectBranch.RuleName, err)
92-
protectBranch.globRule = glob.MustCompile(glob.QuoteMeta(protectBranch.RuleName), '/')
93-
}
94-
protectBranch.isPlainName = !IsRuleNameSpecial(protectBranch.RuleName)
87+
if protectBranch.isPlainName || protectBranch.globRule != nil {
88+
return
89+
}
90+
// detect if it is not glob
91+
if !IsRuleNameSpecial(protectBranch.RuleName) {
92+
protectBranch.isPlainName = true
93+
return
94+
}
95+
// now we load the glob
96+
var err error
97+
protectBranch.globRule, err = glob.Compile(protectBranch.RuleName, '/')
98+
if err != nil {
99+
log.Warn("Invalid glob rule for ProtectedBranch[%d]: %s %v", protectBranch.ID, protectBranch.RuleName, err)
100+
protectBranch.globRule = glob.MustCompile(glob.QuoteMeta(protectBranch.RuleName), '/')
95101
}
96102
}
97103

models/git/protected_banch_list_test.go renamed to models/git/protected_branch_list_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,32 @@ func TestBranchRuleMatchPriority(t *testing.T) {
7474
}
7575
}
7676
}
77+
78+
func TestBranchRuleSort(t *testing.T) {
79+
in := []*ProtectedBranch{{
80+
RuleName: "b",
81+
CreatedUnix: 1,
82+
}, {
83+
RuleName: "b/*",
84+
CreatedUnix: 3,
85+
}, {
86+
RuleName: "a/*",
87+
CreatedUnix: 2,
88+
}, {
89+
RuleName: "c",
90+
CreatedUnix: 0,
91+
}, {
92+
RuleName: "a",
93+
CreatedUnix: 4,
94+
}}
95+
expect := []string{"c", "b", "a", "a/*", "b/*"}
96+
97+
pbr := ProtectedBranchRules(in)
98+
pbr.sort()
99+
100+
var got []string
101+
for i := range pbr {
102+
got = append(got, pbr[i].RuleName)
103+
}
104+
assert.Equal(t, expect, got)
105+
}

0 commit comments

Comments
 (0)