diff options
author | Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com> | 2014-05-22 15:23:20 +0400 |
---|---|---|
committer | Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com> | 2014-05-22 15:23:20 +0400 |
commit | 88781783dd425d1ba4ff5cacf9b4cc4c23a9a35e (patch) | |
tree | 8fa95e919e5e2626fc04392e9e3b75890b9533a2 /app/services | |
parent | 9f80ab8e75181534cb21809258337f081beaf918 (diff) |
Delete branch service with permission checks
Signed-off-by: Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
Diffstat (limited to 'app/services')
-rw-r--r-- | app/services/delete_branch_service.rb | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/app/services/delete_branch_service.rb b/app/services/delete_branch_service.rb new file mode 100644 index 00000000000..9f48ab4b60d --- /dev/null +++ b/app/services/delete_branch_service.rb @@ -0,0 +1,42 @@ +class DeleteBranchService + def execute(project, branch_name, current_user) + repository = project.repository + branch = repository.find_branch(branch_name) + + # No such branch + unless branch + return error('No such branch') + end + + # Dont allow remove of protected branch + if project.protected_branch?(branch_name) + return error('Protected branch cant be removed') + 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') + end + + if repository.rm_branch(branch_name) + Event.create_ref_event(project, current_user, branch, 'rm') + success('Branch was removed') + else + return error('Failed to remove branch') + end + end + + def error(message) + { + message: message, + state: :error + } + end + + def success(message) + { + message: message, + state: :success + } + end +end |