Skip to content

Commit 2d7c1df

Browse files
lunnyLinux User
authored and
Linux User
committed
Fix protected branch for API (go-gitea#24013)
Fix go-gitea#23998
1 parent 2b10eeb commit 2d7c1df

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

routers/api/v1/repo/branch.go

+8-4
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,10 @@ func CreateBranchProtection(ctx *context.APIContext) {
421421
if ruleName == "" {
422422
ruleName = form.BranchName //nolint
423423
}
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+
}
424428

425429
isPlainRule := !git_model.IsRuleNameSpecial(ruleName)
426430
var isBranchExist bool
@@ -502,7 +506,7 @@ func CreateBranchProtection(ctx *context.APIContext) {
502506

503507
protectBranch = &git_model.ProtectedBranch{
504508
RepoID: ctx.Repo.Repository.ID,
505-
RuleName: form.RuleName,
509+
RuleName: ruleName,
506510
CanPush: form.EnablePush,
507511
EnableWhitelist: form.EnablePush && form.EnablePushWhitelist,
508512
EnableMergeWhitelist: form.EnableMergeWhitelist,
@@ -534,7 +538,7 @@ func CreateBranchProtection(ctx *context.APIContext) {
534538
}
535539

536540
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 {
538542
ctx.Error(http.StatusInternalServerError, "CheckPRsForBaseBranch", err)
539543
return
540544
}
@@ -552,7 +556,7 @@ func CreateBranchProtection(ctx *context.APIContext) {
552556
}()
553557
}
554558
// 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)
556560
if err != nil {
557561
ctx.Error(http.StatusInternalServerError, "FindAllMatchedBranches", err)
558562
return
@@ -568,7 +572,7 @@ func CreateBranchProtection(ctx *context.APIContext) {
568572
}
569573

570574
// 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)
572576
if err != nil {
573577
ctx.Error(http.StatusInternalServerError, "GetProtectedBranchByID", err)
574578
return

0 commit comments

Comments
 (0)