Fix matching algorithm
This commit is contained in:
parent
e894bdf6c4
commit
957a20b489
6 changed files with 51 additions and 27 deletions
|
@ -29,7 +29,7 @@ type ProtectedBranch struct {
|
||||||
ID int64 `xorm:"pk autoincr"`
|
ID int64 `xorm:"pk autoincr"`
|
||||||
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:"-"`
|
||||||
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"`
|
||||||
|
@ -66,21 +66,10 @@ func (protectBranch *ProtectedBranch) Match(branchName string) bool {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
if protectBranch.globRule == nil {
|
if protectBranch.globRule == nil {
|
||||||
parts := strings.Split(protectBranch.BranchName, "/")
|
protectBranch.globRule = glob.MustCompile(protectBranch.BranchName, '/')
|
||||||
for _, part := range parts {
|
|
||||||
protectBranch.globRule = append(protectBranch.globRule, glob.MustCompile(part))
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
fields := strings.Split(branchName, "/")
|
return protectBranch.globRule.Match(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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// CanUserPush returns if some user could push to this protected branch
|
// CanUserPush returns if some user could push to this protected branch
|
||||||
|
|
|
@ -18,10 +18,25 @@ func TestBranchRuleMatch(t *testing.T) {
|
||||||
ExpectedMatch bool
|
ExpectedMatch bool
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
Rule: "release/**",
|
Rule: "release/*",
|
||||||
BranchName: "release/v1.17",
|
BranchName: "release/v1.17",
|
||||||
ExpectedMatch: true,
|
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*",
|
Rule: "release/v*",
|
||||||
BranchName: "release/v1.16",
|
BranchName: "release/v1.16",
|
||||||
|
@ -32,6 +47,11 @@ func TestBranchRuleMatch(t *testing.T) {
|
||||||
BranchName: "release/v1.16",
|
BranchName: "release/v1.16",
|
||||||
ExpectedMatch: false,
|
ExpectedMatch: false,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
Rule: "**",
|
||||||
|
BranchName: "release/v1.16",
|
||||||
|
ExpectedMatch: true,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, kase := range kases {
|
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.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.add_protected_branch = Enable protection
|
||||||
settings.delete_protected_branch = Disable protection
|
settings.delete_protected_branch = Disable protection
|
||||||
settings.update_protect_branch_success = Branch protection for branch '%s' has been updated.
|
settings.update_protect_branch_success = Branch protection for rule '%s' has been updated.
|
||||||
settings.remove_protected_branch_success = Branch protection for branch '%s' has been disabled.
|
settings.remove_protected_branch_success = Branch protection for rule '%s' has been removed.
|
||||||
settings.protected_branch_deletion = Disable Branch Protection
|
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.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 = 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.
|
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.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
|
// RenameBranchPost responses for rename a branch
|
||||||
|
|
|
@ -809,7 +809,7 @@ func RegisterRoutes(m *web.Route) {
|
||||||
m.Get("/", repo.ProtectedBranchRules)
|
m.Get("/", repo.ProtectedBranchRules)
|
||||||
m.Combo("/new").Get(repo.SettingsProtectedBranch).
|
m.Combo("/new").Get(repo.SettingsProtectedBranch).
|
||||||
Post(bindIgnErr(forms.ProtectBranchForm{}), context.RepoMustNotBeArchived(), repo.SettingsProtectedBranchPost)
|
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).
|
m.Combo("/{id}").Get(repo.SettingsProtectedBranch).
|
||||||
Post(bindIgnErr(forms.ProtectBranchForm{}), context.RepoMustNotBeArchived(), repo.SettingsProtectedBranchPost)
|
Post(bindIgnErr(forms.ProtectBranchForm{}), context.RepoMustNotBeArchived(), repo.SettingsProtectedBranchPost)
|
||||||
}, repo.MustBeNotEmpty)
|
}, repo.MustBeNotEmpty)
|
||||||
|
|
|
@ -47,7 +47,7 @@
|
||||||
|
|
||||||
<div class="ui attached table segment">
|
<div class="ui attached table segment">
|
||||||
<div class="ui grid padded">
|
<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>
|
<a class="rm ui button" href="{{$.Repository.Link}}/settings/branches/new">{{$.locale.Tr "repo.settings.branches.add_new_rule"}}</a>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
@ -61,7 +61,8 @@
|
||||||
<td><div class="ui basic primary label">{{.BranchName}}</div></td>
|
<td><div class="ui basic primary label">{{.BranchName}}</div></td>
|
||||||
<td class="right aligned">
|
<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="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>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
{{else}}
|
{{else}}
|
||||||
|
@ -97,4 +98,16 @@
|
||||||
{{end}}
|
{{end}}
|
||||||
</div>
|
</div>
|
||||||
</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" .}}
|
{{template "base/footer" .}}
|
||||||
|
|
Loading…
Add table
Reference in a new issue