2018-07-25 09:30:33 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-03-28 13:17:42 +00:00
|
|
|
class ProtectedBranch < ApplicationRecord
|
2017-04-03 14:17:24 +00:00
|
|
|
include ProtectedRef
|
2020-02-06 21:08:48 +00:00
|
|
|
include Gitlab::SQL::Pattern
|
2012-02-15 20:02:33 +00:00
|
|
|
|
2019-09-05 13:01:36 +00:00
|
|
|
scope :requiring_code_owner_approval,
|
|
|
|
-> { where(code_owner_approval_required: true) }
|
|
|
|
|
2021-03-11 18:09:23 +00:00
|
|
|
scope :allowing_force_push,
|
|
|
|
-> { where(allow_force_push: true) }
|
|
|
|
|
2021-10-07 12:12:09 +00:00
|
|
|
scope :get_ids_by_name, -> (name) { where(name: name).pluck(:id) }
|
|
|
|
|
2017-05-05 15:59:31 +00:00
|
|
|
protected_ref_access_levels :merge, :push
|
2017-04-03 17:59:58 +00:00
|
|
|
|
2018-04-18 13:52:55 +00:00
|
|
|
def self.protected_ref_accessible_to?(ref, user, project:, action:, protected_refs: nil)
|
2018-05-22 08:51:45 +00:00
|
|
|
# Maintainers, owners and admins are allowed to create the default branch
|
2020-03-02 12:07:57 +00:00
|
|
|
|
|
|
|
if project.empty_repo? && project.default_branch_protected?
|
2018-04-18 13:52:55 +00:00
|
|
|
return true if user.admin? || project.team.max_member_access(user.id) > Gitlab::Access::DEVELOPER
|
|
|
|
end
|
|
|
|
|
|
|
|
super
|
|
|
|
end
|
|
|
|
|
2017-04-03 17:59:58 +00:00
|
|
|
# Check if branch name is marked as protected in the system
|
|
|
|
def self.protected?(project, ref_name)
|
2020-03-02 12:07:57 +00:00
|
|
|
return true if project.empty_repo? && project.default_branch_protected?
|
2021-10-25 18:12:16 +00:00
|
|
|
return false if ref_name.blank?
|
2017-04-03 17:59:58 +00:00
|
|
|
|
2021-10-25 18:12:16 +00:00
|
|
|
Rails.cache.fetch(protected_ref_cache_key(project, ref_name)) do
|
2021-08-30 12:09:48 +00:00
|
|
|
self.matching(ref_name, protected_refs: protected_refs(project)).present?
|
|
|
|
end
|
2019-03-06 12:20:27 +00:00
|
|
|
end
|
2017-11-28 15:50:44 +00:00
|
|
|
|
2021-10-25 18:12:16 +00:00
|
|
|
def self.protected_ref_cache_key(project, ref_name)
|
|
|
|
"protected_ref-#{project.cache_key}-#{Digest::SHA1.hexdigest(ref_name)}"
|
|
|
|
end
|
|
|
|
|
2021-03-11 18:09:23 +00:00
|
|
|
def self.allow_force_push?(project, ref_name)
|
|
|
|
project.protected_branches.allowing_force_push.matching(ref_name).any?
|
|
|
|
end
|
|
|
|
|
2019-03-06 12:20:27 +00:00
|
|
|
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
|
2017-04-03 17:59:58 +00:00
|
|
|
end
|
2017-04-03 18:28:54 +00:00
|
|
|
|
2019-03-06 12:20:27 +00:00
|
|
|
def self.protected_refs(project)
|
2019-11-12 09:06:14 +00:00
|
|
|
project.protected_branches
|
2019-03-06 12:20:27 +00:00
|
|
|
end
|
2019-10-09 00:06:06 +00:00
|
|
|
|
2020-09-17 09:09:32 +00:00
|
|
|
# overridden in EE
|
2019-10-09 00:06:06 +00:00
|
|
|
def self.branch_requires_code_owner_approval?(project, branch_name)
|
2020-09-17 09:09:32 +00:00
|
|
|
false
|
2019-10-09 00:06:06 +00:00
|
|
|
end
|
2020-02-06 21:08:48 +00:00
|
|
|
|
|
|
|
def self.by_name(query)
|
|
|
|
return none if query.blank?
|
|
|
|
|
|
|
|
where(fuzzy_arel_match(:name, query.downcase))
|
|
|
|
end
|
2020-12-07 18:10:36 +00:00
|
|
|
|
|
|
|
def allow_multiple?(type)
|
|
|
|
type == :push
|
|
|
|
end
|
2012-02-15 20:02:33 +00:00
|
|
|
end
|
2019-09-13 13:26:31 +00:00
|
|
|
|
2021-05-11 21:10:21 +00:00
|
|
|
ProtectedBranch.prepend_mod_with('ProtectedBranch')
|