Class: Gitlab::Access::BranchProtection
- Inherits:
-
Object
- Object
- Gitlab::Access::BranchProtection
- Defined in:
- lib/gitlab/access/branch_protection.rb
Overview
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
Instance Attribute Summary collapse
-
#level ⇒ Object
readonly
Returns the value of attribute level.
Class Method Summary collapse
- .protected_after_initial_push ⇒ Object
- .protected_against_developer_pushes ⇒ Object
- .protected_fully ⇒ Object
- .protection_none ⇒ Object
- .protection_partial ⇒ Object
Instance Method Summary collapse
- #any? ⇒ Boolean
- #developer_can_initial_push? ⇒ Boolean
- #developer_can_merge? ⇒ Boolean
- #developer_can_push? ⇒ Boolean
- #fully_protected? ⇒ Boolean
-
#initialize(level) ⇒ BranchProtection
constructor
A new instance of BranchProtection.
- #to_hash ⇒ Object
Constructor Details
#initialize(level) ⇒ BranchProtection
Returns a new instance of BranchProtection.
25 26 27 |
# File 'lib/gitlab/access/branch_protection.rb', line 25 def initialize(level) @level = level end |
Instance Attribute Details
#level ⇒ Object (readonly)
Returns the value of attribute level.
22 23 24 |
# File 'lib/gitlab/access/branch_protection.rb', line 22 def level @level end |
Class Method Details
.protected_after_initial_push ⇒ Object
96 97 98 99 100 101 102 103 |
# File 'lib/gitlab/access/branch_protection.rb', line 96 def protected_after_initial_push { allowed_to_push: [{ 'access_level' => Gitlab::Access::MAINTAINER }], allowed_to_merge: [{ 'access_level' => Gitlab::Access::DEVELOPER }], allow_force_push: true, developer_can_initial_push: true } end |
.protected_against_developer_pushes ⇒ Object
88 89 90 91 92 93 94 |
# File 'lib/gitlab/access/branch_protection.rb', line 88 def protected_against_developer_pushes { allowed_to_push: [{ 'access_level' => Gitlab::Access::MAINTAINER }], allowed_to_merge: [{ 'access_level' => Gitlab::Access::DEVELOPER }], allow_force_push: true } end |
.protected_fully ⇒ Object
80 81 82 83 84 85 86 |
# File 'lib/gitlab/access/branch_protection.rb', line 80 def protected_fully { allowed_to_push: [{ 'access_level' => Gitlab::Access::MAINTAINER }], allowed_to_merge: [{ 'access_level' => Gitlab::Access::MAINTAINER }], allow_force_push: false } end |
.protection_none ⇒ Object
68 69 70 71 72 73 74 |
# File 'lib/gitlab/access/branch_protection.rb', line 68 def protection_none { allowed_to_push: [{ 'access_level' => Gitlab::Access::DEVELOPER }], allowed_to_merge: [{ 'access_level' => Gitlab::Access::DEVELOPER }], allow_force_push: true } end |
.protection_partial ⇒ Object
76 77 78 |
# File 'lib/gitlab/access/branch_protection.rb', line 76 def protection_partial protection_none.merge(allow_force_push: false) end |
Instance Method Details
#any? ⇒ Boolean
29 30 31 |
# File 'lib/gitlab/access/branch_protection.rb', line 29 def any? level != PROTECTION_NONE end |
#developer_can_initial_push? ⇒ Boolean
37 38 39 |
# File 'lib/gitlab/access/branch_protection.rb', line 37 def developer_can_initial_push? level == PROTECTION_DEV_CAN_INITIAL_PUSH end |
#developer_can_merge? ⇒ Boolean
41 42 43 |
# File 'lib/gitlab/access/branch_protection.rb', line 41 def developer_can_merge? level == PROTECTION_DEV_CAN_MERGE end |
#developer_can_push? ⇒ Boolean
33 34 35 |
# File 'lib/gitlab/access/branch_protection.rb', line 33 def developer_can_push? level == PROTECTION_DEV_CAN_PUSH end |
#fully_protected? ⇒ Boolean
45 46 47 |
# File 'lib/gitlab/access/branch_protection.rb', line 45 def fully_protected? level == PROTECTION_FULL end |
#to_hash ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/gitlab/access/branch_protection.rb', line 49 def to_hash # translate the original integer values into a json payload # that matches the protected branches API: # https://docs.gitlab.com/ee/api/protected_branches.html#update-a-protected-branch case level when PROTECTION_NONE self.class.protection_none when PROTECTION_DEV_CAN_PUSH self.class.protection_partial when PROTECTION_FULL self.class.protected_fully when PROTECTION_DEV_CAN_MERGE self.class.protected_against_developer_pushes when PROTECTION_DEV_CAN_INITIAL_PUSH self.class.protected_after_initial_push end end |