summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLunny Xiao <xiaolunwen@gmail.com>2023-04-10 10:52:16 +0800
committerGitHub <noreply@github.com>2023-04-09 22:52:16 -0400
commitfd9d072af1ea141c96bb1cf363caf96e685217e6 (patch)
treeb36a89ef0d3d8ba60154a806029daf4cabcef945
parentce8a6be55d408414f3900bd0cddbc95b9dd2408e (diff)
downloadgitea-fd9d072af1ea141c96bb1cf363caf96e685217e6.tar.gz
gitea-fd9d072af1ea141c96bb1cf363caf96e685217e6.zip
Fix protected branch for API (#24013)
Fix #23998
-rw-r--r--routers/api/v1/repo/branch.go12
1 files changed, 8 insertions, 4 deletions
diff --git a/routers/api/v1/repo/branch.go b/routers/api/v1/repo/branch.go
index dff47fbcf1..1ae6193a8a 100644
--- a/routers/api/v1/repo/branch.go
+++ b/routers/api/v1/repo/branch.go
@@ -421,6 +421,10 @@ func CreateBranchProtection(ctx *context.APIContext) {
if ruleName == "" {
ruleName = form.BranchName //nolint
}
+ if len(ruleName) == 0 {
+ ctx.Error(http.StatusBadRequest, "both rule_name and branch_name are empty", "both rule_name and branch_name are empty")
+ return
+ }
isPlainRule := !git_model.IsRuleNameSpecial(ruleName)
var isBranchExist bool
@@ -502,7 +506,7 @@ func CreateBranchProtection(ctx *context.APIContext) {
protectBranch = &git_model.ProtectedBranch{
RepoID: ctx.Repo.Repository.ID,
- RuleName: form.RuleName,
+ RuleName: ruleName,
CanPush: form.EnablePush,
EnableWhitelist: form.EnablePush && form.EnablePushWhitelist,
EnableMergeWhitelist: form.EnableMergeWhitelist,
@@ -534,7 +538,7 @@ func CreateBranchProtection(ctx *context.APIContext) {
}
if isBranchExist {
- if err = pull_service.CheckPRsForBaseBranch(ctx.Repo.Repository, form.RuleName); err != nil {
+ if err = pull_service.CheckPRsForBaseBranch(ctx.Repo.Repository, ruleName); err != nil {
ctx.Error(http.StatusInternalServerError, "CheckPRsForBaseBranch", err)
return
}
@@ -552,7 +556,7 @@ func CreateBranchProtection(ctx *context.APIContext) {
}()
}
// FIXME: since we only need to recheck files protected rules, we could improve this
- matchedBranches, err := git_model.FindAllMatchedBranches(ctx, ctx.Repo.GitRepo, form.RuleName)
+ matchedBranches, err := git_model.FindAllMatchedBranches(ctx, ctx.Repo.GitRepo, ruleName)
if err != nil {
ctx.Error(http.StatusInternalServerError, "FindAllMatchedBranches", err)
return
@@ -568,7 +572,7 @@ func CreateBranchProtection(ctx *context.APIContext) {
}
// Reload from db to get all whitelists
- bp, err := git_model.GetProtectedBranchRuleByName(ctx, ctx.Repo.Repository.ID, form.RuleName)
+ bp, err := git_model.GetProtectedBranchRuleByName(ctx, ctx.Repo.Repository.ID, ruleName)
if err != nil {
ctx.Error(http.StatusInternalServerError, "GetProtectedBranchByID", err)
return