From 562077e3f7ce00bac386df5c43f44db53c887606 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Sun, 23 Oct 2022 12:46:37 +0800 Subject: [PATCH] Fix test --- models/git/protected_branch.go | 13 +++++++++++- models/git/protected_branch_test.go | 10 +++++++++ routers/api/v1/repo/branch.go | 32 +++++++++-------------------- 3 files changed, 32 insertions(+), 23 deletions(-) diff --git a/models/git/protected_branch.go b/models/git/protected_branch.go index f9356f414f..776bcb3785 100644 --- a/models/git/protected_branch.go +++ b/models/git/protected_branch.go @@ -22,6 +22,7 @@ import ( "code.gitea.io/gitea/modules/util" "github.com/gobwas/glob" + "github.com/gobwas/glob/syntax" ) // ProtectedBranch struct @@ -61,10 +62,20 @@ func init() { 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() { if protectBranch.globRule == nil { protectBranch.globRule = glob.MustCompile(protectBranch.RuleName, '/') - protectBranch.isPlainName = protectBranch.globRule.Match(protectBranch.RuleName) + protectBranch.isPlainName = !IsRuleNameSpecial(protectBranch.RuleName) } } diff --git a/models/git/protected_branch_test.go b/models/git/protected_branch_test.go index bd178ef092..a90a291fe3 100644 --- a/models/git/protected_branch_test.go +++ b/models/git/protected_branch_test.go @@ -52,6 +52,16 @@ func TestBranchRuleMatch(t *testing.T) { BranchName: "release/v1.16", ExpectedMatch: true, }, + { + Rule: "main", + BranchName: "main", + ExpectedMatch: true, + }, + { + Rule: "master", + BranchName: "main", + ExpectedMatch: false, + }, } for _, kase := range kases { diff --git a/routers/api/v1/repo/branch.go b/routers/api/v1/repo/branch.go index 496daf1421..4aea046fea 100644 --- a/routers/api/v1/repo/branch.go +++ b/routers/api/v1/repo/branch.go @@ -22,8 +22,6 @@ import ( "code.gitea.io/gitea/routers/api/v1/utils" pull_service "code.gitea.io/gitea/services/pull" repo_service "code.gitea.io/gitea/services/repository" - - "github.com/gobwas/glob" ) // GetBranch get a branch of a repository @@ -411,15 +409,10 @@ func CreateBranchProtection(ctx *context.APIContext) { form := web.GetForm(ctx).(*api.CreateBranchProtectionOption) repo := ctx.Repo.Repository - g, err := glob.Compile(form.RuleName, '/') - if err != nil { - ctx.Error(http.StatusBadRequest, "Create branch protection", "Branch protection rule name is not right") - return - } - isPlainRule := g.Match(form.RuleName) - isBranchName := isPlainRule - if isBranchName { - isBranchName = git.IsBranchExist(ctx.Req.Context(), ctx.Repo.Repository.RepoPath(), form.RuleName) + isPlainRule := !git_model.IsRuleNameSpecial(form.RuleName) + var isBranchExist bool + if isPlainRule { + isBranchExist = git.IsBranchExist(ctx.Req.Context(), ctx.Repo.Repository.RepoPath(), form.RuleName) } protectBranch, err := git_model.GetProtectedBranchRuleByName(ctx, repo.ID, form.RuleName) @@ -527,7 +520,7 @@ func CreateBranchProtection(ctx *context.APIContext) { return } - if isBranchName { + if isBranchExist { if err = pull_service.CheckPRsForBaseBranch(ctx.Repo.Repository, form.RuleName); err != nil { ctx.Error(http.StatusInternalServerError, "CheckPRsForBaseBranch", err) return @@ -791,18 +784,13 @@ func EditBranchProtection(ctx *context.APIContext) { return } - g, err := glob.Compile(bpName, '/') - if err != nil { - ctx.Error(http.StatusBadRequest, "Create branch protection", "Branch protection rule name is not right") - return - } - isPlainRule := g.Match(bpName) - isBranchName := isPlainRule - if isBranchName { - isBranchName = git.IsBranchExist(ctx.Req.Context(), ctx.Repo.Repository.RepoPath(), bpName) + isPlainRule := !git_model.IsRuleNameSpecial(bpName) + var isBranchExist bool + if isPlainRule { + isBranchExist = 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 { ctx.Error(http.StatusInternalServerError, "CheckPrsForBaseBranch", err) return