2018-11-18 11:00:15 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
class ProtectedBranch < ApplicationRecord
|
2017-08-17 22:00:37 +05:30
|
|
|
include ProtectedRef
|
2020-03-13 15:44:24 +05:30
|
|
|
include Gitlab::SQL::Pattern
|
2023-04-23 21:23:45 +05:30
|
|
|
include FromUnion
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2023-01-13 00:05:48 +05:30
|
|
|
belongs_to :group, foreign_key: :namespace_id, touch: true, inverse_of: :protected_branches
|
|
|
|
|
|
|
|
validate :validate_either_project_or_top_group
|
|
|
|
|
2023-03-17 16:20:25 +05:30
|
|
|
scope :requiring_code_owner_approval, -> { where(code_owner_approval_required: true) }
|
|
|
|
scope :allowing_force_push, -> { where(allow_force_push: true) }
|
|
|
|
scope :sorted_by_name, -> { order(name: :asc) }
|
2023-04-23 21:23:45 +05:30
|
|
|
scope :sorted_by_namespace_and_name, -> { order(:namespace_id, :name) }
|
|
|
|
|
|
|
|
scope :for_group, ->(group) { where(group: group) }
|
2021-04-17 20:07:23 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
protected_ref_access_levels :merge, :push
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2023-03-04 22:38:38 +05:30
|
|
|
def self.get_ids_by_name(name)
|
|
|
|
where(name: name).pluck(:id)
|
|
|
|
end
|
|
|
|
|
2018-05-09 12:01:36 +05:30
|
|
|
def self.protected_ref_accessible_to?(ref, user, project:, action:, protected_refs: nil)
|
2018-11-08 19:23:39 +05:30
|
|
|
# Maintainers, owners and admins are allowed to create the default branch
|
2020-04-08 14:13:33 +05:30
|
|
|
|
|
|
|
if project.empty_repo? && project.default_branch_protected?
|
2018-05-09 12:01:36 +05:30
|
|
|
return true if user.admin? || project.team.max_member_access(user.id) > Gitlab::Access::DEVELOPER
|
|
|
|
end
|
|
|
|
|
|
|
|
super
|
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
# Check if branch name is marked as protected in the system
|
2022-10-11 01:57:18 +05:30
|
|
|
def self.protected?(project, ref_name)
|
2020-04-08 14:13:33 +05:30
|
|
|
return true if project.empty_repo? && project.default_branch_protected?
|
2021-11-18 22:05:49 +05:30
|
|
|
return false if ref_name.blank?
|
2016-08-24 12:49:21 +05:30
|
|
|
|
2023-05-27 22:25:52 +05:30
|
|
|
ProtectedBranches::CacheService.new(project).fetch(ref_name) do # rubocop: disable CodeReuse/ServiceClass
|
2021-11-11 11:23:49 +05:30
|
|
|
self.matching(ref_name, protected_refs: protected_refs(project)).present?
|
|
|
|
end
|
2019-07-07 11:18:12 +05:30
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
def self.allow_force_push?(project, ref_name)
|
2023-05-27 22:25:52 +05:30
|
|
|
if Feature.enabled?(:group_protected_branches, project.group)
|
2023-04-23 21:23:45 +05:30
|
|
|
protected_branches = project.all_protected_branches.matching(ref_name)
|
|
|
|
|
|
|
|
project_protected_branches, group_protected_branches = protected_branches.partition(&:project_id)
|
|
|
|
|
|
|
|
# Group owner can be able to enforce the settings
|
|
|
|
return group_protected_branches.any?(&:allow_force_push) if group_protected_branches.present?
|
|
|
|
return project_protected_branches.any?(&:allow_force_push) if project_protected_branches.present?
|
|
|
|
|
|
|
|
false
|
|
|
|
else
|
|
|
|
project.protected_branches.allowing_force_push.matching(ref_name).any?
|
|
|
|
end
|
2021-04-17 20:07:23 +05:30
|
|
|
end
|
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
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
|
2016-08-24 12:49:21 +05:30
|
|
|
end
|
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
def self.protected_refs(project)
|
2023-05-27 22:25:52 +05:30
|
|
|
project.all_protected_branches
|
2019-07-07 11:18:12 +05:30
|
|
|
end
|
2019-12-21 20:55:43 +05:30
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
# overridden in EE
|
2019-12-21 20:55:43 +05:30
|
|
|
def self.branch_requires_code_owner_approval?(project, branch_name)
|
2020-11-24 15:15:51 +05:30
|
|
|
false
|
2019-12-21 20:55:43 +05:30
|
|
|
end
|
2020-03-13 15:44:24 +05:30
|
|
|
|
|
|
|
def self.by_name(query)
|
|
|
|
return none if query.blank?
|
|
|
|
|
|
|
|
where(fuzzy_arel_match(:name, query.downcase))
|
|
|
|
end
|
2021-02-22 17:27:13 +05:30
|
|
|
|
|
|
|
def allow_multiple?(type)
|
|
|
|
type == :push
|
|
|
|
end
|
2022-07-16 23:28:13 +05:30
|
|
|
|
|
|
|
def self.downcase_humanized_name
|
|
|
|
name.underscore.humanize.downcase
|
|
|
|
end
|
2022-11-25 23:54:43 +05:30
|
|
|
|
|
|
|
def default_branch?
|
|
|
|
name == project.default_branch
|
|
|
|
end
|
2023-01-13 00:05:48 +05:30
|
|
|
|
2023-04-23 21:23:45 +05:30
|
|
|
def group_level?
|
|
|
|
entity.is_a?(Group)
|
|
|
|
end
|
|
|
|
|
|
|
|
def project_level?
|
|
|
|
entity.is_a?(Project)
|
|
|
|
end
|
|
|
|
|
2023-03-17 16:20:25 +05:30
|
|
|
def entity
|
|
|
|
group || project
|
|
|
|
end
|
|
|
|
|
2023-01-13 00:05:48 +05:30
|
|
|
private
|
|
|
|
|
|
|
|
def validate_either_project_or_top_group
|
|
|
|
if !project && !group
|
|
|
|
errors.add(:base, _('must be associated with a Group or a Project'))
|
|
|
|
elsif project && group
|
|
|
|
errors.add(:base, _('cannot be associated with both a Group and a Project'))
|
2023-03-17 16:20:25 +05:30
|
|
|
elsif group && group.subgroup?
|
2023-01-13 00:05:48 +05:30
|
|
|
errors.add(:base, _('cannot be associated with a subgroup'))
|
|
|
|
end
|
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
2019-12-04 20:38:33 +05:30
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
ProtectedBranch.prepend_mod_with('ProtectedBranch')
|