This commit is contained in:
Lunny Xiao 2022-10-23 12:46:37 +08:00
parent c0989ed9ac
commit 562077e3f7
No known key found for this signature in database
GPG key ID: C3B7C91B632F738A
3 changed files with 32 additions and 23 deletions

View file

@ -22,6 +22,7 @@ import (
"code.gitea.io/gitea/modules/util" "code.gitea.io/gitea/modules/util"
"github.com/gobwas/glob" "github.com/gobwas/glob"
"github.com/gobwas/glob/syntax"
) )
// ProtectedBranch struct // ProtectedBranch struct
@ -61,10 +62,20 @@ func init() {
db.RegisterModel(new(ProtectedBranch)) db.RegisterModel(new(ProtectedBranch))
} }
// IsRuleNameSpecial return true if it contains special charactor
func IsRuleNameSpecial(ruleName string) bool {
for i := 0; i < len(ruleName); i++ {
if syntax.Special(ruleName[i]) {
return true
}
}
return false
}
func (protectBranch *ProtectedBranch) loadGlob() { func (protectBranch *ProtectedBranch) loadGlob() {
if protectBranch.globRule == nil { if protectBranch.globRule == nil {
protectBranch.globRule = glob.MustCompile(protectBranch.RuleName, '/') protectBranch.globRule = glob.MustCompile(protectBranch.RuleName, '/')
protectBranch.isPlainName = protectBranch.globRule.Match(protectBranch.RuleName) protectBranch.isPlainName = !IsRuleNameSpecial(protectBranch.RuleName)
} }
} }

View file

@ -52,6 +52,16 @@ func TestBranchRuleMatch(t *testing.T) {
BranchName: "release/v1.16", BranchName: "release/v1.16",
ExpectedMatch: true, ExpectedMatch: true,
}, },
{
Rule: "main",
BranchName: "main",
ExpectedMatch: true,
},
{
Rule: "master",
BranchName: "main",
ExpectedMatch: false,
},
} }
for _, kase := range kases { for _, kase := range kases {

View file

@ -22,8 +22,6 @@ import (
"code.gitea.io/gitea/routers/api/v1/utils" "code.gitea.io/gitea/routers/api/v1/utils"
pull_service "code.gitea.io/gitea/services/pull" pull_service "code.gitea.io/gitea/services/pull"
repo_service "code.gitea.io/gitea/services/repository" repo_service "code.gitea.io/gitea/services/repository"
"github.com/gobwas/glob"
) )
// GetBranch get a branch of a repository // GetBranch get a branch of a repository
@ -411,15 +409,10 @@ func CreateBranchProtection(ctx *context.APIContext) {
form := web.GetForm(ctx).(*api.CreateBranchProtectionOption) form := web.GetForm(ctx).(*api.CreateBranchProtectionOption)
repo := ctx.Repo.Repository repo := ctx.Repo.Repository
g, err := glob.Compile(form.RuleName, '/') isPlainRule := !git_model.IsRuleNameSpecial(form.RuleName)
if err != nil { var isBranchExist bool
ctx.Error(http.StatusBadRequest, "Create branch protection", "Branch protection rule name is not right") if isPlainRule {
return isBranchExist = git.IsBranchExist(ctx.Req.Context(), ctx.Repo.Repository.RepoPath(), form.RuleName)
}
isPlainRule := g.Match(form.RuleName)
isBranchName := isPlainRule
if isBranchName {
isBranchName = git.IsBranchExist(ctx.Req.Context(), ctx.Repo.Repository.RepoPath(), form.RuleName)
} }
protectBranch, err := git_model.GetProtectedBranchRuleByName(ctx, repo.ID, form.RuleName) protectBranch, err := git_model.GetProtectedBranchRuleByName(ctx, repo.ID, form.RuleName)
@ -527,7 +520,7 @@ func CreateBranchProtection(ctx *context.APIContext) {
return return
} }
if isBranchName { if isBranchExist {
if err = pull_service.CheckPRsForBaseBranch(ctx.Repo.Repository, form.RuleName); err != nil { if err = pull_service.CheckPRsForBaseBranch(ctx.Repo.Repository, form.RuleName); err != nil {
ctx.Error(http.StatusInternalServerError, "CheckPRsForBaseBranch", err) ctx.Error(http.StatusInternalServerError, "CheckPRsForBaseBranch", err)
return return
@ -791,18 +784,13 @@ func EditBranchProtection(ctx *context.APIContext) {
return return
} }
g, err := glob.Compile(bpName, '/') isPlainRule := !git_model.IsRuleNameSpecial(bpName)
if err != nil { var isBranchExist bool
ctx.Error(http.StatusBadRequest, "Create branch protection", "Branch protection rule name is not right") if isPlainRule {
return isBranchExist = git.IsBranchExist(ctx.Req.Context(), ctx.Repo.Repository.RepoPath(), bpName)
}
isPlainRule := g.Match(bpName)
isBranchName := isPlainRule
if isBranchName {
isBranchName = git.IsBranchExist(ctx.Req.Context(), ctx.Repo.Repository.RepoPath(), bpName)
} }
if isBranchName { if isBranchExist {
if err = pull_service.CheckPRsForBaseBranch(ctx.Repo.Repository, bpName); err != nil { if err = pull_service.CheckPRsForBaseBranch(ctx.Repo.Repository, bpName); err != nil {
ctx.Error(http.StatusInternalServerError, "CheckPrsForBaseBranch", err) ctx.Error(http.StatusInternalServerError, "CheckPrsForBaseBranch", err)
return return