Skip to content

Commit 0ff31b8

Browse files
committed
Backend to sort ProtectedBranch by Priority
1 parent 3103499 commit 0ff31b8

File tree

5 files changed

+61
-1
lines changed

5 files changed

+61
-1
lines changed

models/git/protected_banch_list_test.go

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ func TestBranchRuleMatchPriority(t *testing.T) {
7575
}
7676
}
7777

78-
func TestBranchRuleSort(t *testing.T) {
78+
func TestBranchRuleSortLegacy(t *testing.T) {
7979
in := []*ProtectedBranch{{
8080
RuleName: "b",
8181
CreatedUnix: 1,
@@ -103,3 +103,37 @@ func TestBranchRuleSort(t *testing.T) {
103103
}
104104
assert.Equal(t, expect, got)
105105
}
106+
107+
func TestBranchRuleSortPriority(t *testing.T) {
108+
in := []*ProtectedBranch{{
109+
RuleName: "b",
110+
CreatedUnix: 1,
111+
Priority: 4,
112+
}, {
113+
RuleName: "b/*",
114+
CreatedUnix: 3,
115+
Priority: 2,
116+
}, {
117+
RuleName: "a/*",
118+
CreatedUnix: 2,
119+
Priority: 1,
120+
}, {
121+
RuleName: "c",
122+
CreatedUnix: 0,
123+
Priority: 0,
124+
}, {
125+
RuleName: "a",
126+
CreatedUnix: 4,
127+
Priority: 3,
128+
}}
129+
expect := []string{"c", "a/*", "b/*", "a", "b"}
130+
131+
pbr := ProtectedBranchRules(in)
132+
pbr.sort()
133+
134+
var got []string
135+
for i := range pbr {
136+
got = append(got, pbr[i].RuleName)
137+
}
138+
assert.Equal(t, expect, got)
139+
}

models/git/protected_branch.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ type ProtectedBranch struct {
3434
RepoID int64 `xorm:"UNIQUE(s)"`
3535
Repo *repo_model.Repository `xorm:"-"`
3636
RuleName string `xorm:"'branch_name' UNIQUE(s)"` // a branch name or a glob match to branch name
37+
Priority int64 `xorm:"NOT NULL DEFAULT 0"`
3738
globRule glob.Glob `xorm:"-"`
3839
isPlainName bool `xorm:"-"`
3940
CanPush bool `xorm:"NOT NULL DEFAULT false"`

models/git/protected_branch_list.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@ func (rules ProtectedBranchRules) sort() {
2828
sort.Slice(rules, func(i, j int) bool {
2929
rules[i].loadGlob()
3030
rules[j].loadGlob()
31+
32+
// if priority differ, use that to sort
33+
if rules[i].Priority != rules[j].Priority {
34+
return rules[i].Priority < rules[j].Priority
35+
}
36+
37+
// now we sort the old way
3138
if rules[i].isPlainName != rules[j].isPlainName {
3239
return rules[i].isPlainName // plain name comes first, so plain name means "less"
3340
}

models/migrations/migrations.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,8 @@ var migrations = []Migration{
603603
NewMigration("Add index for release sha1", v1_23.AddIndexForReleaseSha1),
604604
// v305 -> v306
605605
NewMigration("Add Repository Licenses", v1_23.AddRepositoryLicenses),
606+
// v306 -> v307
607+
NewMigration("Add Priority to ProtectedBranch", v1_23.AddPriorityToProtectedBranch),
606608
}
607609

608610
// GetCurrentDBVersion returns the current db version

models/migrations/v1_23/v306.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright 2024 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package v1_23 //nolint
5+
6+
import (
7+
"xorm.io/xorm"
8+
)
9+
10+
func AddPriorityToProtectedBranch(x *xorm.Engine) error {
11+
type ProtectedBranch struct {
12+
Priority int64 `xorm:"NOT NULL DEFAULT 0"`
13+
}
14+
15+
return x.Sync(new(ProtectedBranch))
16+
}

0 commit comments

Comments
 (0)