diff --git a/options/locale/locale_en-US.json b/options/locale/locale_en-US.json index acc88e830b9..9fcbd6eeb04 100644 --- a/options/locale/locale_en-US.json +++ b/options/locale/locale_en-US.json @@ -2487,7 +2487,6 @@ "repo.settings.choose_branch": "Choose a branch…", "repo.settings.no_protected_branch": "There are no protected branches.", "repo.settings.edit_protected_branch": "Edit", - "repo.settings.protected_branch_required_rule_name": "Required rule name", "repo.settings.protected_branch_duplicate_rule_name": "Duplicate rule name", "repo.settings.protected_branch_required_approvals_min": "Required approvals cannot be negative.", "repo.settings.tags": "Tags", diff --git a/routers/web/repo/setting/protected_branch.go b/routers/web/repo/setting/protected_branch.go index beb2cc58382..7eb16a47a7a 100644 --- a/routers/web/repo/setting/protected_branch.go +++ b/routers/web/repo/setting/protected_branch.go @@ -5,9 +5,7 @@ package setting import ( "errors" - "fmt" "net/http" - "net/url" "strconv" "strings" "time" @@ -124,14 +122,15 @@ func SettingsProtectedBranch(c *context.Context) { // SettingsProtectedBranchPost updates the protected branch settings func SettingsProtectedBranchPost(ctx *context.Context) { - f := web.GetForm[*forms.ProtectBranchForm](ctx) - var protectBranch *git_model.ProtectedBranch - if f.RuleName == "" { - ctx.Flash.Error(ctx.Tr("repo.settings.protected_branch_required_rule_name")) - ctx.Redirect(ctx.Repo.RepoLink + "/settings/branches/edit") + f := context.GetFetchActionForm[*forms.ProtectBranchForm](ctx) + if f == nil { return } - + if f.RequiredApprovals < 0 { + ctx.JSONErrorWithField(ctx.Tr("repo.settings.protected_branch_required_approvals_min"), "required_approvals") + return + } + var protectBranch *git_model.ProtectedBranch var err error if f.RuleID > 0 { // If the RuleID isn't 0, it must be an edit operation. So we get rule by id. @@ -149,8 +148,7 @@ func SettingsProtectedBranchPost(ctx *context.Context) { return } if sameNameProtectBranch != nil { - ctx.Flash.Error(ctx.Tr("repo.settings.protected_branch_duplicate_rule_name")) - ctx.Redirect(fmt.Sprintf("%s/settings/branches/edit?rule_name=%s", ctx.Repo.RepoLink, protectBranch.RuleName)) + ctx.JSONErrorWithField(ctx.Tr("repo.settings.protected_branch_duplicate_rule_name"), "rule_name") return } } @@ -176,11 +174,6 @@ func SettingsProtectedBranchPost(ctx *context.Context) { var whitelistUsers, whitelistTeams, forcePushAllowlistUsers, forcePushAllowlistTeams, deletionAllowlistUsers, deletionAllowlistTeams, mergeWhitelistUsers, mergeWhitelistTeams, approvalsWhitelistUsers, approvalsWhitelistTeams, bypassAllowlistUsers, bypassAllowlistTeams []int64 protectBranch.RuleName = f.RuleName - if f.RequiredApprovals < 0 { - ctx.Flash.Error(ctx.Tr("repo.settings.protected_branch_required_approvals_min")) - ctx.Redirect(fmt.Sprintf("%s/settings/branches/edit?rule_name=%s", ctx.Repo.RepoLink, f.RuleName)) - return - } switch f.EnablePush { case "all": @@ -271,16 +264,14 @@ func SettingsProtectedBranchPost(ctx *context.Context) { continue } if _, err := glob.Compile(trimmed); err != nil { - ctx.Flash.Error(ctx.Tr("repo.settings.protect_invalid_status_check_pattern", pattern)) - ctx.Redirect(fmt.Sprintf("%s/settings/branches/edit?rule_name=%s", ctx.Repo.RepoLink, url.QueryEscape(protectBranch.RuleName))) + ctx.JSONErrorWithField(ctx.Tr("repo.settings.protect_invalid_status_check_pattern", pattern), "status_check_contexts") return } validPatterns = append(validPatterns, trimmed) } if len(validPatterns) == 0 { // if status check is enabled, patterns slice is not allowed to be empty - ctx.Flash.Error(ctx.Tr("repo.settings.protect_no_valid_status_check_patterns")) - ctx.Redirect(fmt.Sprintf("%s/settings/branches/edit?rule_name=%s", ctx.Repo.RepoLink, url.QueryEscape(protectBranch.RuleName))) + ctx.JSONErrorWithField(ctx.Tr("repo.settings.protect_no_valid_status_check_patterns"), "status_check_contexts") return } protectBranch.StatusCheckContexts = validPatterns @@ -334,7 +325,7 @@ func SettingsProtectedBranchPost(ctx *context.Context) { } ctx.Flash.Success(ctx.Tr("repo.settings.update_protect_branch_success", protectBranch.RuleName)) - ctx.Redirect(fmt.Sprintf("%s/settings/branches?rule_name=%s", ctx.Repo.RepoLink, protectBranch.RuleName)) + ctx.JSONRedirect(ctx.Repo.RepoLink + "/settings/branches") } // DeleteProtectedBranchRulePost delete protected branch rule by id diff --git a/routers/web/web.go b/routers/web/web.go index 8d7b5510597..caedfc55658 100644 --- a/routers/web/web.go +++ b/routers/web/web.go @@ -1181,7 +1181,7 @@ func registerWebRoutes(m *web.Router, webAuth *AuthMiddleware) { m.Group("/branches", func() { m.Get("/", repo_setting.ProtectedBranchRules) m.Combo("/edit").Get(repo_setting.SettingsProtectedBranch). - Post(web.Bind[*forms.ProtectBranchForm](), context.RepoMustNotBeArchived(), repo_setting.SettingsProtectedBranchPost) + Post(context.RepoMustNotBeArchived(), repo_setting.SettingsProtectedBranchPost) m.Post("/{id}/delete", repo_setting.DeleteProtectedBranchRulePost) m.Post("/priority", context.RepoMustNotBeArchived(), repo_setting.UpdateBranchProtectionPriories) }) diff --git a/templates/repo/settings/protected_branch.tmpl b/templates/repo/settings/protected_branch.tmpl index 46a57e6f7bb..6fbd8f2f4e9 100644 --- a/templates/repo/settings/protected_branch.tmpl +++ b/templates/repo/settings/protected_branch.tmpl @@ -1,6 +1,6 @@ {{template "repo/settings/layout_head" (dict "pageClass" "repository settings branches")}}