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
|
2012-02-15 20:02:33 +00:00
|
|
|
|
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
|
2018-04-18 13:52:55 +00:00
|
|
|
if default_branch_protected? && project.empty_repo?
|
|
|
|
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)
|
2017-04-03 18:28:54 +00:00
|
|
|
return true if project.empty_repo? && default_branch_protected?
|
2017-04-03 17:59:58 +00:00
|
|
|
|
2019-03-06 12:20:27 +00:00
|
|
|
self.matching(ref_name, protected_refs: protected_refs(project)).present?
|
|
|
|
end
|
2017-11-28 15:50:44 +00:00
|
|
|
|
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
|
|
|
|
|
|
|
def self.default_branch_protected?
|
2018-02-02 18:39:55 +00:00
|
|
|
Gitlab::CurrentSettings.default_branch_protection == Gitlab::Access::PROTECTION_FULL ||
|
|
|
|
Gitlab::CurrentSettings.default_branch_protection == Gitlab::Access::PROTECTION_DEV_CAN_MERGE
|
2017-04-03 18:28:54 +00:00
|
|
|
end
|
2019-03-06 12:20:27 +00:00
|
|
|
|
|
|
|
def self.protected_refs(project)
|
|
|
|
project.protected_branches.select(:name)
|
|
|
|
end
|
2012-02-15 20:02:33 +00:00
|
|
|
end
|