Class: Projects::ProtectDefaultBranchService

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

Instance Method Summary collapse

Constructor Details

#initialize(project) ⇒ ProtectDefaultBranchService

Returns a new instance of ProtectDefaultBranchService.

Parameters:


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

#projectObject (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_branchObject


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_branchObject


47
48
49
# File 'app/services/projects/protect_default_branch_service.rb', line 47

def default_branch
  project.default_branch
end

#executeObject


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_levelObject


59
60
61
62
63
64
65
# File 'app/services/projects/protect_default_branch_service.rb', line 59

def merge_access_level
  if default_branch_protection.developer_can_merge?
    Gitlab::Access::DEVELOPER
  else
    Gitlab::Access::MAINTAINER
  end
end

#protect_branch?Boolean

Returns:

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


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?
end

#push_access_levelObject


51
52
53
54
55
56
57
# File 'app/services/projects/protect_default_branch_service.rb', line 51

def push_access_level
  if default_branch_protection.developer_can_push?
    Gitlab::Access::DEVELOPER
  else
    Gitlab::Access::MAINTAINER
  end
end