Commit 712f41e1 authored by Rémy Coutable's avatar Rémy Coutable

Merge branch '37727-fix-file-delete-redirect' into 'master'

Resolve "In web editor, when delete a file, should navigate to enclosing directory"

Closes #37727

See merge request gitlab-org/gitlab-ce!21465
parents 9642a472 4a372af8
...@@ -83,7 +83,7 @@ class Projects::BlobController < Projects::ApplicationController ...@@ -83,7 +83,7 @@ class Projects::BlobController < Projects::ApplicationController
def destroy def destroy
create_commit(Files::DeleteService, success_notice: "The file has been successfully deleted.", 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_view: :show,
failure_path: project_blob_path(@project, @id)) failure_path: project_blob_path(@project, @id))
end end
...@@ -191,6 +191,15 @@ class Projects::BlobController < Projects::ApplicationController ...@@ -191,6 +191,15 @@ class Projects::BlobController < Projects::ApplicationController
end end
# rubocop: enable CodeReuse/ActiveRecord # 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 def editor_variables
@branch_name = params[:branch_name] @branch_name = params[:branch_name]
...@@ -255,9 +264,6 @@ class Projects::BlobController < Projects::ApplicationController ...@@ -255,9 +264,6 @@ class Projects::BlobController < Projects::ApplicationController
def show_json def show_json
set_last_commit_sha set_last_commit_sha
path_segments = @path.split('/')
path_segments.pop
tree_path = path_segments.join('/')
json = { json = {
id: @blob.id, id: @blob.id,
...@@ -283,4 +289,8 @@ class Projects::BlobController < Projects::ApplicationController ...@@ -283,4 +289,8 @@ class Projects::BlobController < Projects::ApplicationController
render json: json render json: json
end end
def tree_path
@path.rpartition('/').first
end
end end
---
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
...@@ -343,4 +343,76 @@ describe Projects::BlobController do ...@@ -343,4 +343,76 @@ describe Projects::BlobController do
end end
end 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 end
...@@ -31,7 +31,7 @@ describe 'Projects > Files > User deletes files', :js do ...@@ -31,7 +31,7 @@ describe 'Projects > Files > User deletes files', :js do
fill_in(:commit_message, with: 'New commit message', visible: true) fill_in(:commit_message, with: 'New commit message', visible: true)
click_button('Delete file') 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') expect(page).not_to have_content('.gitignore')
end end
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment