diff --git a/app/controllers/projects/blob_controller.rb b/app/controllers/projects/blob_controller.rb index 92d26a13da9..fb2cfdedd9b 100644 --- a/app/controllers/projects/blob_controller.rb +++ b/app/controllers/projects/blob_controller.rb @@ -83,7 +83,7 @@ class Projects::BlobController < Projects::ApplicationController def destroy create_commit(Files::DeleteService, success_notice: "The file has been successfully deleted.", - success_path: -> { project_tree_path(@project, @branch_name) }, + success_path: -> { after_delete_path }, failure_view: :show, failure_path: project_blob_path(@project, @id)) end @@ -191,6 +191,15 @@ class Projects::BlobController < Projects::ApplicationController end # rubocop: enable CodeReuse/ActiveRecord + def after_delete_path + branch = BranchesFinder.new(@repository, search: @ref).execute.first + if @repository.tree(branch.target, tree_path).entries.empty? + project_tree_path(@project, @ref) + else + project_tree_path(@project, File.join(@ref, tree_path)) + end + end + def editor_variables @branch_name = params[:branch_name] @@ -255,9 +264,6 @@ class Projects::BlobController < Projects::ApplicationController def show_json set_last_commit_sha - path_segments = @path.split('/') - path_segments.pop - tree_path = path_segments.join('/') json = { id: @blob.id, @@ -283,4 +289,8 @@ class Projects::BlobController < Projects::ApplicationController render json: json end + + def tree_path + @path.rpartition('/').first + end end diff --git a/changelogs/unreleased/37727-fix-file-delete-redirect.yml b/changelogs/unreleased/37727-fix-file-delete-redirect.yml new file mode 100644 index 00000000000..3fc3965f1f0 --- /dev/null +++ b/changelogs/unreleased/37727-fix-file-delete-redirect.yml @@ -0,0 +1,6 @@ +--- +title: On deletion of a file in sub directory in web IDE redirect to the sub directory + instead of project root +merge_request: 21465 +author: George Thomas @thegeorgeous +type: changed diff --git a/spec/controllers/projects/blob_controller_spec.rb b/spec/controllers/projects/blob_controller_spec.rb index 32cd7c6e70a..28f7e4634a5 100644 --- a/spec/controllers/projects/blob_controller_spec.rb +++ b/spec/controllers/projects/blob_controller_spec.rb @@ -343,4 +343,76 @@ describe Projects::BlobController do end end end + + describe 'DELETE destroy' do + let(:user) { create(:user) } + let(:project_root_path) { project_tree_path(project, 'master') } + + before do + project.add_maintainer(user) + + sign_in(user) + end + + context 'for a file in a subdirectory' do + let(:default_params) do + { + namespace_id: project.namespace, + project_id: project, + id: 'master/files/whitespace', + original_branch: 'master', + branch_name: 'master', + commit_message: 'Delete whitespace' + } + end + + let(:after_delete_path) { project_tree_path(project, 'master/files') } + + it 'redirects to the sub directory' do + delete :destroy, default_params + + expect(response).to redirect_to(after_delete_path) + end + end + + context 'if deleted file is the last one in a subdirectory' do + let(:default_params) do + { + namespace_id: project.namespace, + project_id: project, + id: 'master/bar/branch-test.txt', + original_branch: 'master', + branch_name: 'master', + commit_message: 'Delete whitespace' + } + end + + it 'redirects to the project root' do + delete :destroy, default_params + + expect(response).to redirect_to(project_root_path) + end + + context 'when deleting a file in a branch other than master' do + let(:default_params) do + { + namespace_id: project.namespace, + project_id: project, + id: 'binary-encoding/foo/bar/.gitkeep', + original_branch: 'binary-encoding', + branch_name: 'binary-encoding', + commit_message: 'Delete whitespace' + } + end + + let(:after_delete_path) { project_tree_path(project, 'binary-encoding') } + + it 'redirects to the project root of the branch' do + delete :destroy, default_params + + expect(response).to redirect_to(after_delete_path) + end + end + end + end end diff --git a/spec/features/projects/files/user_deletes_files_spec.rb b/spec/features/projects/files/user_deletes_files_spec.rb index dcb7b947c61..614b11fa5c8 100644 --- a/spec/features/projects/files/user_deletes_files_spec.rb +++ b/spec/features/projects/files/user_deletes_files_spec.rb @@ -31,7 +31,7 @@ describe 'Projects > Files > User deletes files', :js do fill_in(:commit_message, with: 'New commit message', visible: true) click_button('Delete file') - expect(current_path).to eq(project_tree_path(project, 'master')) + expect(current_path).to eq(project_tree_path(project, 'master/')) expect(page).not_to have_content('.gitignore') end end