Commit 0455391add2032dddc7353d1b0ae36d591818d3f
1 parent
e089b11b
Exists in
spb-stable
and in
2 other branches
Improve branch-removal logic
Signed-off-by: Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
Showing
3 changed files
with
8 additions
and
9 deletions
Show diff stats
app/controllers/projects/branches_controller.rb
| ... | ... | @@ -4,8 +4,7 @@ class Projects::BranchesController < Projects::ApplicationController |
| 4 | 4 | before_filter :require_non_empty_project |
| 5 | 5 | |
| 6 | 6 | before_filter :authorize_code_access! |
| 7 | - before_filter :authorize_push!, only: [:create] | |
| 8 | - before_filter :authorize_admin_project!, only: [:destroy] | |
| 7 | + before_filter :authorize_push!, only: [:create, :destroy] | |
| 9 | 8 | |
| 10 | 9 | def index |
| 11 | 10 | @branches = Kaminari.paginate_array(@repository.branches).page(params[:page]).per(30) |
| ... | ... | @@ -22,11 +21,7 @@ class Projects::BranchesController < Projects::ApplicationController |
| 22 | 21 | end |
| 23 | 22 | |
| 24 | 23 | def destroy |
| 25 | - branch = @repository.find_branch(params[:id]) | |
| 26 | - | |
| 27 | - if branch && @repository.rm_branch(branch.name) | |
| 28 | - Event.create_ref_event(@project, current_user, branch, 'rm') | |
| 29 | - end | |
| 24 | + DeleteBranchService.new.execute(project, params[:id], current_user) | |
| 30 | 25 | |
| 31 | 26 | respond_to do |format| |
| 32 | 27 | format.html { redirect_to project_branches_path(@project) } | ... | ... |
app/services/delete_branch_service.rb
| ... | ... | @@ -8,6 +8,10 @@ class DeleteBranchService |
| 8 | 8 | return error('No such branch') |
| 9 | 9 | end |
| 10 | 10 | |
| 11 | + if branch_name == repository.root_ref | |
| 12 | + return error('Cannot remove HEAD branch') | |
| 13 | + end | |
| 14 | + | |
| 11 | 15 | # Dont allow remove of protected branch |
| 12 | 16 | if project.protected_branch?(branch_name) |
| 13 | 17 | return error('Protected branch cant be removed') | ... | ... |
app/views/projects/branches/_branch.html.haml
| ... | ... | @@ -16,8 +16,8 @@ |
| 16 | 16 | %i.icon-copy |
| 17 | 17 | Compare |
| 18 | 18 | |
| 19 | - - if can?(current_user, :admin_project, @project) && branch.name != @repository.root_ref | |
| 20 | - = link_to project_branch_path(@project, branch.name), class: 'btn btn-grouped btn-small remove-row', method: :delete, data: { confirm: 'Removed branch cannot be restored. Are you sure?'}, remote: true do | |
| 19 | + - if can_remove_branch?(@project, branch.name) | |
| 20 | + = link_to project_branch_path(@project, branch.name), class: 'btn btn-grouped btn-small btn-remove remove-row', method: :delete, data: { confirm: 'Removed branch cannot be restored. Are you sure?'}, remote: true do | |
| 21 | 21 | %i.icon-trash |
| 22 | 22 | |
| 23 | 23 | - if commit | ... | ... |