gitlab-org--gitlab-foss/lib/gitlab/access/branch_protection.rb
Yorick Peterse 52eeb56bf0
Refactor code for protecting default branches
This refactors some of the logic used for protecting default branches,
in particular Project#after_create_default_branch. The logic for this
method is moved into a separate service class. Ideally we'd get rid of
Project#after_create_default_branch entirely, but unfortunately
Project#after_import depends on it. This means it has to stick around
until we also refactor Project#after_import.

For branch protection levels we introduce
Gitlab::Access::BranchProtection, which provides a small wrapper around
Integer based branch protection levels. Using this class removes the
need for having to constantly refer to Gitlab::Access::PROTECTION_*
constants.
2019-01-16 14:25:14 +01:00

42 lines
1,006 B
Ruby

# frozen_string_literal: true
module Gitlab
module Access
# A wrapper around Integer based branch protection levels.
#
# This wrapper can be used to work with branch protection levels without
# having to directly refer to the constants. For example, instead of this:
#
# if access_level == Gitlab::Access::PROTECTION_DEV_CAN_PUSH
# ...
# end
#
# You can write this instead:
#
# protection = BranchProtection.new(access_level)
#
# if protection.developer_can_push?
# ...
# end
class BranchProtection
attr_reader :level
# @param [Integer] level The branch protection level as an Integer.
def initialize(level)
@level = level
end
def any?
level != PROTECTION_NONE
end
def developer_can_push?
level == PROTECTION_DEV_CAN_PUSH
end
def developer_can_merge?
level == PROTECTION_DEV_CAN_MERGE
end
end
end
end