Class: ProtectedBranch

Inherits:
ApplicationRecord show all
Includes:
Gitlab::SQL::Pattern, ProtectedRef
Defined in:
app/models/protected_branch.rb

Defined Under Namespace

Classes: MergeAccessLevel, PushAccessLevel

Constant Summary

Constants included from Gitlab::SQL::Pattern

Gitlab::SQL::Pattern::MIN_CHARS_FOR_PARTIAL_MATCHING, Gitlab::SQL::Pattern::REGEX_QUOTED_WORD

Class Method Summary collapse

Methods included from ProtectedRef

#commit

Methods inherited from ApplicationRecord

at_most, id_in, id_not_in, iid_in, pluck_primary_key, primary_key_in, safe_ensure_unique, safe_find_or_create_by, safe_find_or_create_by!, underscore, without_order

Class Method Details

.any_protected?(project, ref_names) ⇒ Boolean

Returns:

  • (Boolean)

29
30
31
32
33
34
35
# File 'app/models/protected_branch.rb', line 29

def self.any_protected?(project, ref_names)
  protected_refs(project).any? do |protected_ref|
    ref_names.any? do |ref_name|
      protected_ref.matches?(ref_name)
    end
  end
end

.branch_requires_code_owner_approval?(project, branch_name) ⇒ Boolean

Returns:

  • (Boolean)

41
42
43
44
# File 'app/models/protected_branch.rb', line 41

def self.branch_requires_code_owner_approval?(project, branch_name)
  # NOOP
  #
end

.by_name(query) ⇒ Object


46
47
48
49
50
# File 'app/models/protected_branch.rb', line 46

def self.by_name(query)
  return none if query.blank?

  where(fuzzy_arel_match(:name, query.downcase))
end

.protected?(project, ref_name) ⇒ Boolean

Check if branch name is marked as protected in the system

Returns:

  • (Boolean)

23
24
25
26
27
# File 'app/models/protected_branch.rb', line 23

def self.protected?(project, ref_name)
  return true if project.empty_repo? && project.default_branch_protected?

  self.matching(ref_name, protected_refs: protected_refs(project)).present?
end

.protected_ref_accessible_to?(ref, user, project:, action:, protected_refs: nil) ⇒ Boolean

Returns:

  • (Boolean)

12
13
14
15
16
17
18
19
20
# File 'app/models/protected_branch.rb', line 12

def self.protected_ref_accessible_to?(ref, user, project:, action:, protected_refs: nil)
  # Maintainers, owners and admins are allowed to create the default branch

  if project.empty_repo? && project.default_branch_protected?
    return true if user.admin? || project.team.max_member_access(user.id) > Gitlab::Access::DEVELOPER
  end

  super
end

.protected_refs(project) ⇒ Object


37
38
39
# File 'app/models/protected_branch.rb', line 37

def self.protected_refs(project)
  project.protected_branches
end