Class: DeleteBranchService

Inherits:
BaseService show all
Defined in:
app/services/delete_branch_service.rb

Instance Attribute Summary

Attributes inherited from BaseService

#current_user, #params, #project

Instance Method Summary collapse

Methods inherited from BaseService

#abilities, #can?, #deny_visibility_level, #event_service, #initialize, #log_info, #notification_service, #repository, #system_hook_service, #todo_service

Methods included from Gitlab::CurrentSettings

#current_application_settings, #fake_application_settings

Constructor Details

This class inherits a constructor from BaseService

Instance Method Details

#build_push_data(branch) ⇒ Object


49
50
51
52
# File 'app/services/delete_branch_service.rb', line 49

def build_push_data(branch)
  Gitlab::PushDataBuilder
    .build(project, current_user, branch.target, Gitlab::Git::BLANK_SHA, "#{Gitlab::Git::BRANCH_REF_PREFIX}#{branch.name}", [])
end

#error(message, return_code = 400) ⇒ Object


37
38
39
40
41
# File 'app/services/delete_branch_service.rb', line 37

def error(message, return_code = 400)
  out = super(message)
  out[:return_code] = return_code
  out
end

#execute(branch_name) ⇒ Object


4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'app/services/delete_branch_service.rb', line 4

def execute(branch_name)
  repository = project.repository
  branch = repository.find_branch(branch_name)

  # No such branch
  unless branch
    return error('No such branch', 404)
  end

  if branch_name == repository.root_ref
    return error('Cannot remove HEAD branch', 405)
  end

  # Dont allow remove of protected branch
  if project.protected_branch?(branch_name)
    return error('Protected branch cant be removed', 405)
  end

  # Dont allow user to remove branch if he is not allowed to push
  unless current_user.can?(:push_code, project)
    return error('You dont have push access to repo', 405)
  end

  if repository.rm_branch(current_user, branch_name)
    # GitPushService handles execution of services and hooks for branch pushes
    success('Branch was removed')
  else
    error('Failed to remove branch')
  end
rescue GitHooksService::PreReceiveError
  error('Branch deletion was rejected by Git hook')
end

#success(message) ⇒ Object


43
44
45
46
47
# File 'app/services/delete_branch_service.rb', line 43

def success(message)
  out = super()
  out[:message] = message
  out
end