gitea源码

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // Copyright 2025 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package pull
  4. import (
  5. "context"
  6. git_model "code.gitea.io/gitea/models/git"
  7. repo_model "code.gitea.io/gitea/models/repo"
  8. "code.gitea.io/gitea/modules/gitrepo"
  9. )
  10. func CreateOrUpdateProtectedBranch(ctx context.Context, repo *repo_model.Repository,
  11. protectBranch *git_model.ProtectedBranch, whitelistOptions git_model.WhitelistOptions,
  12. ) error {
  13. err := git_model.UpdateProtectBranch(ctx, repo, protectBranch, whitelistOptions)
  14. if err != nil {
  15. return err
  16. }
  17. isPlainRule := !git_model.IsRuleNameSpecial(protectBranch.RuleName)
  18. var isBranchExist bool
  19. if isPlainRule {
  20. // TODO: read the database directly to check if the branch exists
  21. isBranchExist = gitrepo.IsBranchExist(ctx, repo, protectBranch.RuleName)
  22. }
  23. if isBranchExist {
  24. if err := CheckPRsForBaseBranch(ctx, repo, protectBranch.RuleName); err != nil {
  25. return err
  26. }
  27. } else {
  28. if !isPlainRule {
  29. // FIXME: since we only need to recheck files protected rules, we could improve this
  30. matchedBranches, err := git_model.FindAllMatchedBranches(ctx, repo.ID, protectBranch.RuleName)
  31. if err != nil {
  32. return err
  33. }
  34. for _, branchName := range matchedBranches {
  35. if err = CheckPRsForBaseBranch(ctx, repo, branchName); err != nil {
  36. return err
  37. }
  38. }
  39. }
  40. }
  41. return nil
  42. }