Class: Gitlab::Access::BranchProtection

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(level) ⇒ BranchProtection

Returns a new instance of BranchProtection.

Parameters:

  • level (Integer)

    The branch protection level as an Integer.



25
26
27
# File 'lib/gitlab/access/branch_protection.rb', line 25

def initialize(level)
  @level = level
end

Instance Attribute Details

#levelObject (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_pushObject



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_pushesObject



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_fullyObject



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_noneObject



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_partialObject



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

Returns:

  • (Boolean)


29
30
31
# File 'lib/gitlab/access/branch_protection.rb', line 29

def any?
  level != PROTECTION_NONE
end

#developer_can_initial_push?Boolean

Returns:

  • (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

Returns:

  • (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

Returns:

  • (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

Returns:

  • (Boolean)


45
46
47
# File 'lib/gitlab/access/branch_protection.rb', line 45

def fully_protected?
  level == PROTECTION_FULL
end

#to_hashObject



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