aboutsummaryrefslogtreecommitdiffstats
path: root/services
diff options
context:
space:
mode:
authorLunny Xiao <xiaolunwen@gmail.com>2025-03-05 11:15:40 -0800
committerGitHub <noreply@github.com>2025-03-05 19:15:40 +0000
commit0453177b611cab92d8ee093012c5bc33d364b41d (patch)
treec3c9716be7d59d6d8db469ff5162435ff7098bac /services
parentdf7b61ce9ad15a6d9066231b56cc935604d2c972 (diff)
downloadgitea-0453177b611cab92d8ee093012c5bc33d364b41d.tar.gz
gitea-0453177b611cab92d8ee093012c5bc33d364b41d.zip
Refactor: move part of updating protected branch logic to service layer (#33742)
Diffstat (limited to 'services')
-rw-r--r--services/forms/repo_form.go7
-rw-r--r--services/pull/protected_branch.go48
2 files changed, 48 insertions, 7 deletions
diff --git a/services/forms/repo_form.go b/services/forms/repo_form.go
index 70019f3fa9..f07186117e 100644
--- a/services/forms/repo_form.go
+++ b/services/forms/repo_form.go
@@ -170,13 +170,6 @@ func (f *RepoSettingForm) Validate(req *http.Request, errs binding.Errors) bindi
return middleware.Validate(errs, ctx.Data, f, ctx.Locale)
}
-// __________ .__
-// \______ \____________ ____ ____ | |__
-// | | _/\_ __ \__ \ / \_/ ___\| | \
-// | | \ | | \// __ \| | \ \___| Y \
-// |______ / |__| (____ /___| /\___ >___| /
-// \/ \/ \/ \/ \/
-
// ProtectBranchForm form for changing protected branch settings
type ProtectBranchForm struct {
RuleName string `binding:"Required"`
diff --git a/services/pull/protected_branch.go b/services/pull/protected_branch.go
new file mode 100644
index 0000000000..5f42629ddc
--- /dev/null
+++ b/services/pull/protected_branch.go
@@ -0,0 +1,48 @@
+// Copyright 2025 The Gitea Authors. All rights reserved.
+// SPDX-License-Identifier: MIT
+
+package pull
+
+import (
+ "context"
+
+ git_model "code.gitea.io/gitea/models/git"
+ repo_model "code.gitea.io/gitea/models/repo"
+ "code.gitea.io/gitea/modules/git"
+)
+
+func CreateOrUpdateProtectedBranch(ctx context.Context, repo *repo_model.Repository,
+ protectBranch *git_model.ProtectedBranch, whitelistOptions git_model.WhitelistOptions,
+) error {
+ err := git_model.UpdateProtectBranch(ctx, repo, protectBranch, whitelistOptions)
+ if err != nil {
+ return err
+ }
+
+ isPlainRule := !git_model.IsRuleNameSpecial(protectBranch.RuleName)
+ var isBranchExist bool
+ if isPlainRule {
+ isBranchExist = git.IsBranchExist(ctx, repo.RepoPath(), protectBranch.RuleName)
+ }
+
+ if isBranchExist {
+ if err := CheckPRsForBaseBranch(ctx, repo, protectBranch.RuleName); err != nil {
+ return err
+ }
+ } else {
+ if !isPlainRule {
+ // FIXME: since we only need to recheck files protected rules, we could improve this
+ matchedBranches, err := git_model.FindAllMatchedBranches(ctx, repo.ID, protectBranch.RuleName)
+ if err != nil {
+ return err
+ }
+ for _, branchName := range matchedBranches {
+ if err = CheckPRsForBaseBranch(ctx, repo, branchName); err != nil {
+ return err
+ }
+ }
+ }
+ }
+
+ return nil
+}