gitea源码

protected_branch_list.go 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // Copyright 2022 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package git
  4. import (
  5. "context"
  6. "sort"
  7. "code.gitea.io/gitea/models/db"
  8. "code.gitea.io/gitea/modules/glob"
  9. "code.gitea.io/gitea/modules/optional"
  10. )
  11. type ProtectedBranchRules []*ProtectedBranch
  12. func (rules ProtectedBranchRules) GetFirstMatched(branchName string) *ProtectedBranch {
  13. for _, rule := range rules {
  14. if rule.Match(branchName) {
  15. return rule
  16. }
  17. }
  18. return nil
  19. }
  20. func (rules ProtectedBranchRules) sort() {
  21. sort.Slice(rules, func(i, j int) bool {
  22. rules[i].loadGlob()
  23. rules[j].loadGlob()
  24. // if priority differ, use that to sort
  25. if rules[i].Priority != rules[j].Priority {
  26. return rules[i].Priority < rules[j].Priority
  27. }
  28. // now we sort the old way
  29. if rules[i].isPlainName != rules[j].isPlainName {
  30. return rules[i].isPlainName // plain name comes first, so plain name means "less"
  31. }
  32. return rules[i].CreatedUnix < rules[j].CreatedUnix
  33. })
  34. }
  35. // FindRepoProtectedBranchRules load all repository's protected rules
  36. func FindRepoProtectedBranchRules(ctx context.Context, repoID int64) (ProtectedBranchRules, error) {
  37. var rules ProtectedBranchRules
  38. err := db.GetEngine(ctx).Where("repo_id = ?", repoID).Asc("created_unix").Find(&rules)
  39. if err != nil {
  40. return nil, err
  41. }
  42. rules.sort() // to make non-glob rules have higher priority, and for same glob/non-glob rules, first created rules have higher priority
  43. return rules, nil
  44. }
  45. // FindAllMatchedBranches find all matched branches
  46. func FindAllMatchedBranches(ctx context.Context, repoID int64, ruleName string) ([]string, error) {
  47. results := make([]string, 0, 10)
  48. for page := 1; ; page++ {
  49. brancheNames, err := FindBranchNames(ctx, FindBranchOptions{
  50. ListOptions: db.ListOptions{
  51. PageSize: 100,
  52. Page: page,
  53. },
  54. RepoID: repoID,
  55. IsDeletedBranch: optional.Some(false),
  56. })
  57. if err != nil {
  58. return nil, err
  59. }
  60. rule := glob.MustCompile(ruleName)
  61. for _, branch := range brancheNames {
  62. if rule.Match(branch) {
  63. results = append(results, branch)
  64. }
  65. }
  66. if len(brancheNames) < 100 {
  67. break
  68. }
  69. }
  70. return results, nil
  71. }
  72. // GetFirstMatchProtectedBranchRule returns the first matched rules
  73. func GetFirstMatchProtectedBranchRule(ctx context.Context, repoID int64, branchName string) (*ProtectedBranch, error) {
  74. rules, err := FindRepoProtectedBranchRules(ctx, repoID)
  75. if err != nil {
  76. return nil, err
  77. }
  78. return rules.GetFirstMatched(branchName), nil
  79. }
  80. // IsBranchProtected checks if branch is protected
  81. func IsBranchProtected(ctx context.Context, repoID int64, branchName string) (bool, error) {
  82. rule, err := GetFirstMatchProtectedBranchRule(ctx, repoID, branchName)
  83. if err != nil {
  84. return false, err
  85. }
  86. return rule != nil, nil
  87. }