Fix matching algorithm
This commit is contained in:
parent
e894bdf6c4
commit
957a20b489
6 changed files with 51 additions and 27 deletions
|
@ -26,11 +26,11 @@ import (
|
|||
|
||||
// ProtectedBranch struct
|
||||
type ProtectedBranch struct {
|
||||
ID int64 `xorm:"pk autoincr"`
|
||||
RepoID int64 `xorm:"UNIQUE(s)"`
|
||||
BranchName string `xorm:"UNIQUE(s)"` // a branch name or a glob match to branch name
|
||||
globRule []glob.Glob `xorm:"-"`
|
||||
CanPush bool `xorm:"NOT NULL DEFAULT false"`
|
||||
ID int64 `xorm:"pk autoincr"`
|
||||
RepoID int64 `xorm:"UNIQUE(s)"`
|
||||
BranchName string `xorm:"UNIQUE(s)"` // a branch name or a glob match to branch name
|
||||
globRule glob.Glob `xorm:"-"`
|
||||
CanPush bool `xorm:"NOT NULL DEFAULT false"`
|
||||
EnableWhitelist bool
|
||||
WhitelistUserIDs []int64 `xorm:"JSON TEXT"`
|
||||
WhitelistTeamIDs []int64 `xorm:"JSON TEXT"`
|
||||
|
@ -66,21 +66,10 @@ func (protectBranch *ProtectedBranch) Match(branchName string) bool {
|
|||
return true
|
||||
}
|
||||
if protectBranch.globRule == nil {
|
||||
parts := strings.Split(protectBranch.BranchName, "/")
|
||||
for _, part := range parts {
|
||||
protectBranch.globRule = append(protectBranch.globRule, glob.MustCompile(part))
|
||||
}
|
||||
protectBranch.globRule = glob.MustCompile(protectBranch.BranchName, '/')
|
||||
}
|
||||
fields := strings.Split(branchName, "/")
|
||||
if len(fields) != len(protectBranch.globRule) {
|
||||
return false
|
||||
}
|
||||
for i := 0; i < len(fields); i++ {
|
||||
if !protectBranch.globRule[i].Match(fields[i]) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
|
||||
return protectBranch.globRule.Match(branchName)
|
||||
}
|
||||
|
||||
// CanUserPush returns if some user could push to this protected branch
|
||||
|
|
|
@ -18,10 +18,25 @@ func TestBranchRuleMatch(t *testing.T) {
|
|||
ExpectedMatch bool
|
||||
}{
|
||||
{
|
||||
Rule: "release/**",
|
||||
Rule: "release/*",
|
||||
BranchName: "release/v1.17",
|
||||
ExpectedMatch: true,
|
||||
},
|
||||
{
|
||||
Rule: "release/**/v1.17",
|
||||
BranchName: "release/test/v1.17",
|
||||
ExpectedMatch: true,
|
||||
},
|
||||
{
|
||||
Rule: "release/**/v1.17",
|
||||
BranchName: "release/test/1/v1.17",
|
||||
ExpectedMatch: true,
|
||||
},
|
||||
{
|
||||
Rule: "release/*/v1.17",
|
||||
BranchName: "release/test/1/v1.17",
|
||||
ExpectedMatch: false,
|
||||
},
|
||||
{
|
||||
Rule: "release/v*",
|
||||
BranchName: "release/v1.16",
|
||||
|
@ -32,6 +47,11 @@ func TestBranchRuleMatch(t *testing.T) {
|
|||
BranchName: "release/v1.16",
|
||||
ExpectedMatch: false,
|
||||
},
|
||||
{
|
||||
Rule: "**",
|
||||
BranchName: "release/v1.16",
|
||||
ExpectedMatch: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, kase := range kases {
|
||||
|
|
|
@ -2090,9 +2090,9 @@ settings.protect_unprotected_file_patterns = Unprotected file patterns (separate
|
|||
settings.protect_unprotected_file_patterns_desc = Unprotected files that are allowed to be changed directly if user has write access, bypassing push restriction. Multiple patterns can be separated using semicolon ('\;'). See <a href="https://pkg.go.dev/github.com/gobwas/glob#Compile">github.com/gobwas/glob</a> documentation for pattern syntax. Examples: <code>.drone.yml</code>, <code>/docs/**/*.txt</code>.
|
||||
settings.add_protected_branch = Enable protection
|
||||
settings.delete_protected_branch = Disable protection
|
||||
settings.update_protect_branch_success = Branch protection for branch '%s' has been updated.
|
||||
settings.remove_protected_branch_success = Branch protection for branch '%s' has been disabled.
|
||||
settings.protected_branch_deletion = Disable Branch Protection
|
||||
settings.update_protect_branch_success = Branch protection for rule '%s' has been updated.
|
||||
settings.remove_protected_branch_success = Branch protection for rule '%s' has been removed.
|
||||
settings.protected_branch_deletion = Delete Branch Protection
|
||||
settings.protected_branch_deletion_desc = Disabling branch protection allows users with write permission to push to the branch. Continue?
|
||||
settings.block_rejected_reviews = Block merge on rejected reviews
|
||||
settings.block_rejected_reviews_desc = Merging will not be possible when changes are requested by official reviewers, even if there are enough approvals.
|
||||
|
|
|
@ -291,7 +291,9 @@ func DeleteProtectedBranchRulePost(ctx *context.Context) {
|
|||
}
|
||||
|
||||
ctx.Flash.Success(ctx.Tr("repo.settings.remove_protected_branch_success", rule.BranchName))
|
||||
ctx.Redirect(fmt.Sprintf("%s/settings/branches", ctx.Repo.RepoLink))
|
||||
ctx.JSON(http.StatusOK, map[string]interface{}{
|
||||
"redirect": fmt.Sprintf("%s/settings/branches", ctx.Repo.RepoLink),
|
||||
})
|
||||
}
|
||||
|
||||
// RenameBranchPost responses for rename a branch
|
||||
|
|
|
@ -809,7 +809,7 @@ func RegisterRoutes(m *web.Route) {
|
|||
m.Get("/", repo.ProtectedBranchRules)
|
||||
m.Combo("/new").Get(repo.SettingsProtectedBranch).
|
||||
Post(bindIgnErr(forms.ProtectBranchForm{}), context.RepoMustNotBeArchived(), repo.SettingsProtectedBranchPost)
|
||||
m.Get("/{id}/delete", repo.DeleteProtectedBranchRulePost)
|
||||
m.Post("/{id}/delete", repo.DeleteProtectedBranchRulePost)
|
||||
m.Combo("/{id}").Get(repo.SettingsProtectedBranch).
|
||||
Post(bindIgnErr(forms.ProtectBranchForm{}), context.RepoMustNotBeArchived(), repo.SettingsProtectedBranchPost)
|
||||
}, repo.MustBeNotEmpty)
|
||||
|
|
|
@ -47,7 +47,7 @@
|
|||
|
||||
<div class="ui attached table segment">
|
||||
<div class="ui grid padded">
|
||||
<div class="eight wide column right">
|
||||
<div class="sixteen wide column right aligned">
|
||||
<a class="rm ui button" href="{{$.Repository.Link}}/settings/branches/new">{{$.locale.Tr "repo.settings.branches.add_new_rule"}}</a>
|
||||
</div>
|
||||
</div>
|
||||
|
@ -61,7 +61,8 @@
|
|||
<td><div class="ui basic primary label">{{.BranchName}}</div></td>
|
||||
<td class="right aligned">
|
||||
<a class="rm ui button" href="{{$.Repository.Link}}/settings/branches/{{.ID}}">{{$.locale.Tr "repo.settings.edit_protected_branch"}}</a>
|
||||
<a class="ui red button" href="{{$.Repository.Link}}/settings/branches/{{.ID}}/delete">{{$.locale.Tr "repo.settings.protected_branch.delete_rule"}}</a>
|
||||
<button class="ui red tiny button delete-button" data-url="{{$.Repository.Link}}/settings/branches/{{.ID}}/delete" data-id="{{.ID}}">
|
||||
{{$.locale.Tr "repo.settings.protected_branch.delete_rule"}}</button>
|
||||
</td>
|
||||
</tr>
|
||||
{{else}}
|
||||
|
@ -97,4 +98,16 @@
|
|||
{{end}}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="ui small basic delete modal">
|
||||
<div class="ui header">
|
||||
{{svg "octicon-trash" 16 "mr-2"}}
|
||||
{{.locale.Tr "repo.settings.protected_branch_deletion"}}
|
||||
</div>
|
||||
<div class="content">
|
||||
<p>{{.locale.Tr "repo.settings.protected_branch_deletion_desc"}}</p>
|
||||
</div>
|
||||
{{template "base/delete_modal_actions" .}}
|
||||
</div>
|
||||
|
||||
{{template "base/footer" .}}
|
||||
|
|
Loading…
Add table
Reference in a new issue