summaryrefslogtreecommitdiff
path: root/app/services/delete_branch_service.rb
diff options
context:
space:
mode:
authorDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>2014-05-22 14:23:20 +0300
committerDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>2014-05-22 14:23:20 +0300
commit88781783dd425d1ba4ff5cacf9b4cc4c23a9a35e (patch)
tree8fa95e919e5e2626fc04392e9e3b75890b9533a2 /app/services/delete_branch_service.rb
parent9f80ab8e75181534cb21809258337f081beaf918 (diff)
downloadgitlab-ce-88781783dd425d1ba4ff5cacf9b4cc4c23a9a35e.tar.gz
Delete branch service with permission checks
Signed-off-by: Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
Diffstat (limited to 'app/services/delete_branch_service.rb')
-rw-r--r--app/services/delete_branch_service.rb42
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