You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

protected_branch_list.go 2.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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/util"
  9. "github.com/gobwas/glob"
  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 rules[i].isPlainName != rules[j].isPlainName {
  25. return rules[i].isPlainName // plain name comes first, so plain name means "less"
  26. }
  27. return rules[i].CreatedUnix < rules[j].CreatedUnix
  28. })
  29. }
  30. // FindRepoProtectedBranchRules load all repository's protected rules
  31. func FindRepoProtectedBranchRules(ctx context.Context, repoID int64) (ProtectedBranchRules, error) {
  32. var rules ProtectedBranchRules
  33. err := db.GetEngine(ctx).Where("repo_id = ?", repoID).Asc("created_unix").Find(&rules)
  34. if err != nil {
  35. return nil, err
  36. }
  37. rules.sort() // to make non-glob rules have higher priority, and for same glob/non-glob rules, first created rules have higher priority
  38. return rules, nil
  39. }
  40. // FindAllMatchedBranches find all matched branches
  41. func FindAllMatchedBranches(ctx context.Context, repoID int64, ruleName string) ([]string, error) {
  42. results := make([]string, 0, 10)
  43. for page := 1; ; page++ {
  44. brancheNames, err := FindBranchNames(ctx, FindBranchOptions{
  45. ListOptions: db.ListOptions{
  46. PageSize: 100,
  47. Page: page,
  48. },
  49. RepoID: repoID,
  50. IsDeletedBranch: util.OptionalBoolFalse,
  51. })
  52. if err != nil {
  53. return nil, err
  54. }
  55. rule := glob.MustCompile(ruleName)
  56. for _, branch := range brancheNames {
  57. if rule.Match(branch) {
  58. results = append(results, branch)
  59. }
  60. }
  61. if len(brancheNames) < 100 {
  62. break
  63. }
  64. }
  65. return results, nil
  66. }
  67. // GetFirstMatchProtectedBranchRule returns the first matched rules
  68. func GetFirstMatchProtectedBranchRule(ctx context.Context, repoID int64, branchName string) (*ProtectedBranch, error) {
  69. rules, err := FindRepoProtectedBranchRules(ctx, repoID)
  70. if err != nil {
  71. return nil, err
  72. }
  73. return rules.GetFirstMatched(branchName), nil
  74. }
  75. // IsBranchProtected checks if branch is protected
  76. func IsBranchProtected(ctx context.Context, repoID int64, branchName string) (bool, error) {
  77. rule, err := GetFirstMatchProtectedBranchRule(ctx, repoID, branchName)
  78. if err != nil {
  79. return false, err
  80. }
  81. return rule != nil, nil
  82. }