@@ -421,6 +421,10 @@ func CreateBranchProtection(ctx *context.APIContext) {
421
421
if ruleName == "" {
422
422
ruleName = form .BranchName //nolint
423
423
}
424
+ if len (ruleName ) == 0 {
425
+ ctx .Error (http .StatusBadRequest , "both rule_name and branch_name are empty" , "both rule_name and branch_name are empty" )
426
+ return
427
+ }
424
428
425
429
isPlainRule := ! git_model .IsRuleNameSpecial (ruleName )
426
430
var isBranchExist bool
@@ -502,7 +506,7 @@ func CreateBranchProtection(ctx *context.APIContext) {
502
506
503
507
protectBranch = & git_model.ProtectedBranch {
504
508
RepoID : ctx .Repo .Repository .ID ,
505
- RuleName : form . RuleName ,
509
+ RuleName : ruleName ,
506
510
CanPush : form .EnablePush ,
507
511
EnableWhitelist : form .EnablePush && form .EnablePushWhitelist ,
508
512
EnableMergeWhitelist : form .EnableMergeWhitelist ,
@@ -534,7 +538,7 @@ func CreateBranchProtection(ctx *context.APIContext) {
534
538
}
535
539
536
540
if isBranchExist {
537
- if err = pull_service .CheckPRsForBaseBranch (ctx .Repo .Repository , form . RuleName ); err != nil {
541
+ if err = pull_service .CheckPRsForBaseBranch (ctx .Repo .Repository , ruleName ); err != nil {
538
542
ctx .Error (http .StatusInternalServerError , "CheckPRsForBaseBranch" , err )
539
543
return
540
544
}
@@ -552,7 +556,7 @@ func CreateBranchProtection(ctx *context.APIContext) {
552
556
}()
553
557
}
554
558
// FIXME: since we only need to recheck files protected rules, we could improve this
555
- matchedBranches , err := git_model .FindAllMatchedBranches (ctx , ctx .Repo .GitRepo , form . RuleName )
559
+ matchedBranches , err := git_model .FindAllMatchedBranches (ctx , ctx .Repo .GitRepo , ruleName )
556
560
if err != nil {
557
561
ctx .Error (http .StatusInternalServerError , "FindAllMatchedBranches" , err )
558
562
return
@@ -568,7 +572,7 @@ func CreateBranchProtection(ctx *context.APIContext) {
568
572
}
569
573
570
574
// Reload from db to get all whitelists
571
- bp , err := git_model .GetProtectedBranchRuleByName (ctx , ctx .Repo .Repository .ID , form . RuleName )
575
+ bp , err := git_model .GetProtectedBranchRuleByName (ctx , ctx .Repo .Repository .ID , ruleName )
572
576
if err != nil {
573
577
ctx .Error (http .StatusInternalServerError , "GetProtectedBranchByID" , err )
574
578
return
0 commit comments