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
|
2012-02-15 15:02:33 -05:00
|
|
|
|
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
|
2018-04-18 09:52:55 -04: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 13:59:58 -04:00
|
|
|
# Check if branch name is marked as protected in the system
|
|
|
|
def self.protected?(project, ref_name)
|
2017-04-03 14:28:54 -04:00
|
|
|
return true if project.empty_repo? && 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
|
|
|
|
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
|
|
|
|
|
|
|
def self.default_branch_protected?
|
2018-02-02 13:39:55 -05:00
|
|
|
Gitlab::CurrentSettings.default_branch_protection == Gitlab::Access::PROTECTION_FULL ||
|
|
|
|
Gitlab::CurrentSettings.default_branch_protection == Gitlab::Access::PROTECTION_DEV_CAN_MERGE
|
2017-04-03 14:28:54 -04:00
|
|
|
end
|
2019-03-06 07:20:27 -05:00
|
|
|
|
|
|
|
def self.protected_refs(project)
|
|
|
|
project.protected_branches.select(:name)
|
|
|
|
end
|
2012-02-15 15:02:33 -05:00
|
|
|
end
|