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