Class: Projects::ProtectDefaultBranchService
- Inherits:
-
Object
- Object
- Projects::ProtectDefaultBranchService
- Defined in:
- app/services/projects/protect_default_branch_service.rb
Overview
Service class that can be used to execute actions necessary after creating a default branch.
Instance Attribute Summary collapse
-
#default_branch_protection ⇒ Object
readonly
Returns the value of attribute default_branch_protection.
-
#project ⇒ Object
readonly
Returns the value of attribute project.
Instance Method Summary collapse
- #create_protected_branch ⇒ Object
- #default_branch ⇒ Object
- #execute ⇒ Object
-
#initialize(project) ⇒ ProtectDefaultBranchService
constructor
A new instance of ProtectDefaultBranchService.
- #merge_access_level ⇒ Object
- #protect_branch? ⇒ Boolean
- #protect_default_branch ⇒ Object
- #protected_branch_exists? ⇒ Boolean
- #push_access_level ⇒ Object
Constructor Details
#initialize(project) ⇒ ProtectDefaultBranchService
Returns a new instance of ProtectDefaultBranchService.
10 11 12 13 14 15 |
# File 'app/services/projects/protect_default_branch_service.rb', line 10 def initialize(project) @project = project @default_branch_protection = Gitlab::Access::BranchProtection .new(project.namespace.default_branch_protection) end |
Instance Attribute Details
#default_branch_protection ⇒ Object (readonly)
Returns the value of attribute default_branch_protection.
7 8 9 |
# File 'app/services/projects/protect_default_branch_service.rb', line 7 def default_branch_protection @default_branch_protection end |
#project ⇒ Object (readonly)
Returns the value of attribute project.
7 8 9 |
# File 'app/services/projects/protect_default_branch_service.rb', line 7 def project @project end |
Instance Method Details
#create_protected_branch ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'app/services/projects/protect_default_branch_service.rb', line 28 def create_protected_branch params = { name: default_branch, push_access_levels_attributes: [{ access_level: push_access_level }], merge_access_levels_attributes: [{ access_level: merge_access_level }] } # The creator of the project is always allowed to create protected # branches, so we skip the authorization check in this service class. ProtectedBranches::CreateService .new(project, project.creator, params) .execute(skip_authorization: true) end |
#default_branch ⇒ Object
51 52 53 |
# File 'app/services/projects/protect_default_branch_service.rb', line 51 def default_branch project.default_branch end |
#execute ⇒ Object
17 18 19 |
# File 'app/services/projects/protect_default_branch_service.rb', line 17 def execute protect_default_branch if default_branch end |
#merge_access_level ⇒ Object
63 64 65 66 67 68 69 |
# File 'app/services/projects/protect_default_branch_service.rb', line 63 def merge_access_level if default_branch_protection.developer_can_merge? Gitlab::Access::DEVELOPER else Gitlab::Access::MAINTAINER end end |
#protect_branch? ⇒ Boolean
42 43 44 45 |
# File 'app/services/projects/protect_default_branch_service.rb', line 42 def protect_branch? default_branch_protection.any? && !ProtectedBranch.protected?(project, default_branch) end |
#protect_default_branch ⇒ Object
21 22 23 24 25 26 |
# File 'app/services/projects/protect_default_branch_service.rb', line 21 def protect_default_branch # Ensure HEAD points to the default branch in case it is not master project.change_head(default_branch) create_protected_branch if protect_branch? && !protected_branch_exists? end |
#protected_branch_exists? ⇒ Boolean
47 48 49 |
# File 'app/services/projects/protect_default_branch_service.rb', line 47 def protected_branch_exists? project.all_protected_branches.find_by_name(default_branch).present? end |