Fix test
This commit is contained in:
parent
c0989ed9ac
commit
562077e3f7
3 changed files with 32 additions and 23 deletions
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Add table
Reference in a new issue