Fix protected branch for API (#24013)

Fix #23998
This commit is contained in:
Lunny Xiao 2023-04-10 10:52:16 +08:00 committed by GitHub
parent ce8a6be55d
commit fd9d072af1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 8 additions and 4 deletions

View File

@ -421,6 +421,10 @@ func CreateBranchProtection(ctx *context.APIContext) {
if ruleName == "" { if ruleName == "" {
ruleName = form.BranchName //nolint 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) isPlainRule := !git_model.IsRuleNameSpecial(ruleName)
var isBranchExist bool var isBranchExist bool
@ -502,7 +506,7 @@ func CreateBranchProtection(ctx *context.APIContext) {
protectBranch = &git_model.ProtectedBranch{ protectBranch = &git_model.ProtectedBranch{
RepoID: ctx.Repo.Repository.ID, RepoID: ctx.Repo.Repository.ID,
RuleName: form.RuleName, RuleName: ruleName,
CanPush: form.EnablePush, CanPush: form.EnablePush,
EnableWhitelist: form.EnablePush && form.EnablePushWhitelist, EnableWhitelist: form.EnablePush && form.EnablePushWhitelist,
EnableMergeWhitelist: form.EnableMergeWhitelist, EnableMergeWhitelist: form.EnableMergeWhitelist,
@ -534,7 +538,7 @@ func CreateBranchProtection(ctx *context.APIContext) {
} }
if isBranchExist { 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) ctx.Error(http.StatusInternalServerError, "CheckPRsForBaseBranch", err)
return return
} }
@ -552,7 +556,7 @@ func CreateBranchProtection(ctx *context.APIContext) {
}() }()
} }
// FIXME: since we only need to recheck files protected rules, we could improve this // 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 { if err != nil {
ctx.Error(http.StatusInternalServerError, "FindAllMatchedBranches", err) ctx.Error(http.StatusInternalServerError, "FindAllMatchedBranches", err)
return return
@ -568,7 +572,7 @@ func CreateBranchProtection(ctx *context.APIContext) {
} }
// Reload from db to get all whitelists // 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 { if err != nil {
ctx.Error(http.StatusInternalServerError, "GetProtectedBranchByID", err) ctx.Error(http.StatusInternalServerError, "GetProtectedBranchByID", err)
return return