Commit 88781783dd425d1ba4ff5cacf9b4cc4c23a9a35e

Authored by Dmitriy Zaporozhets
1 parent 9f80ab8e

Delete branch service with permission checks

Signed-off-by: Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
Showing 1 changed file with 42 additions and 0 deletions   Show diff stats
app/services/delete_branch_service.rb 0 → 100644
... ... @@ -0,0 +1,42 @@
  1 +class DeleteBranchService
  2 + def execute(project, branch_name, current_user)
  3 + repository = project.repository
  4 + branch = repository.find_branch(branch_name)
  5 +
  6 + # No such branch
  7 + unless branch
  8 + return error('No such branch')
  9 + end
  10 +
  11 + # Dont allow remove of protected branch
  12 + if project.protected_branch?(branch_name)
  13 + return error('Protected branch cant be removed')
  14 + end
  15 +
  16 + # Dont allow user to remove branch if he is not allowed to push
  17 + unless current_user.can?(:push_code, project)
  18 + return error('You dont have push access to repo')
  19 + end
  20 +
  21 + if repository.rm_branch(branch_name)
  22 + Event.create_ref_event(project, current_user, branch, 'rm')
  23 + success('Branch was removed')
  24 + else
  25 + return error('Failed to remove branch')
  26 + end
  27 + end
  28 +
  29 + def error(message)
  30 + {
  31 + message: message,
  32 + state: :error
  33 + }
  34 + end
  35 +
  36 + def success(message)
  37 + {
  38 + message: message,
  39 + state: :success
  40 + }
  41 + end
  42 +end
... ...