diff --git a/app/controllers/concerns/creates_commit.rb b/app/controllers/concerns/creates_commit.rb index 5009bf7ff0ced6c14456637c42c01de856e06bad..5389cba957797ca2e7800b2fe205a936038d52af 100644 --- a/app/controllers/concerns/creates_commit.rb +++ b/app/controllers/concerns/creates_commit.rb @@ -138,7 +138,8 @@ def create_merge_request? # Even if the field is set, if we're checking the same branch # as the target branch in the same project, # we don't want to create a merge request. - params[:create_merge_request].present? && + # FIXME: We should use either 1 or true, not both. + ActiveModel::Type::Boolean.new.cast(params[:create_merge_request]) && (@different_project || @start_branch != @branch_name) # rubocop:disable Gitlab/ModuleWithInstanceVariables end diff --git a/spec/controllers/projects/tree_controller_spec.rb b/spec/controllers/projects/tree_controller_spec.rb index 4e00d58bf17dbca22c3f95b12ab77df0e0384b4c..1244668c659e2f3bbf5f6b3b3454a025f8c65ac1 100644 --- a/spec/controllers/projects/tree_controller_spec.rb +++ b/spec/controllers/projects/tree_controller_spec.rb @@ -294,6 +294,8 @@ end describe '#create_dir' do + let(:create_merge_request) { nil } + render_views before do @@ -303,18 +305,57 @@ id: 'master', dir_name: path, branch_name: branch_name, - commit_message: 'Test commit message' + commit_message: 'Test commit message', + create_merge_request: create_merge_request } end context 'successful creation' do let(:path) { 'files/new_dir' } - let(:branch_name) { 'master-test' } + let(:branch_name) { "main-test-#{SecureRandom.hex}" } - it 'redirects to the new directory' do - expect(subject) - .to redirect_to("/#{project.full_path}/-/tree/#{branch_name}/#{path}") - expect(flash[:notice]).to eq('The directory has been successfully created.') + context 'when not creating a new MR' do + let(:create_merge_request) { 'false' } + + it 'redirects to the new directory' do + expect(subject) + .to redirect_to("/#{project.full_path}/-/tree/#{branch_name}/#{path}") + expect(flash[:notice]).to eq('The directory has been successfully created.') + end + end + + context 'when creating a new MR' do + shared_examples 'a new MR from branch redirection' do + it 'redirects to the new MR page' do + expect(subject) + .to redirect_to("/#{project.full_path}/-/merge_requests/new?merge_request%5Bsource_branch%5D=#{branch_name}&merge_request%5Btarget_branch%5D=master&merge_request%5Btarget_project_id%5D=#{project.id}") + expect(flash[:notice]).to eq('The directory has been successfully created. You can now submit a merge request to get this change into the original branch.') + end + end + + context "and the passed create_merge_request value is true" do + it_behaves_like 'a new MR from branch redirection' do + let(:create_merge_request) { true } + end + end + + context "and the passed create_merge_request value is 'true'" do + it_behaves_like 'a new MR from branch redirection' do + let(:create_merge_request) { 'true' } + end + end + + context "and the passed create_merge_request value is '1'" do + it_behaves_like 'a new MR from branch redirection' do + let(:create_merge_request) { '1' } + end + end + + context "and the passed create_merge_request value is 1" do + it_behaves_like 'a new MR from branch redirection' do + let(:create_merge_request) { 1 } + end + end end end diff --git a/spec/features/projects/files/user_creates_files_spec.rb b/spec/features/projects/files/user_creates_files_spec.rb index edc504240a7d87526a5b0597bdb12fca173fe9f4..c104f43c4af9b0021d2fe798dc6361880ed6cd52 100644 --- a/spec/features/projects/files/user_creates_files_spec.rb +++ b/spec/features/projects/files/user_creates_files_spec.rb @@ -56,7 +56,13 @@ end context 'with committing a new file' do + let(:file_name) { 'a_file.md' } + let(:file_content) { 'some file content' } + let(:can_submit_mr_content) { 'You can now submit a merge request to get this change into the original branch.' } + context 'when an user has write access' do + let(:branch_name) { 'new_branch_name' } + before do visit(project_tree_path_root_ref) @@ -94,28 +100,30 @@ def submit_new_file(options) expect(page).to have_content 'Path cannot include directory traversal' end - it 'creates and commit a new file' do - editor_set_value('*.rbca') - fill_in(:file_name, with: 'not_a_file.md') + it 'creates and commits a new file' do + editor_set_value(file_content) + fill_in(:file_name, with: file_name) fill_in(:commit_message, with: 'New commit message', visible: true) + click_button('Commit changes') - new_file_path = project_blob_path(project, 'master/not_a_file.md') + new_file_path = project_blob_path(project, "master/#{file_name}") expect(page).to have_current_path(new_file_path, ignore_query: true) wait_for_requests - expect(page).to have_content('*.rbca') + expect(page).to have_content(file_content) end - it 'creates and commit a new file with new lines at the end of file' do + it 'creates and commits a new file with new lines at the end of file' do editor_set_value('Sample\n\n\n') - fill_in(:file_name, with: 'not_a_file.md') + fill_in(:file_name, with: file_name) fill_in(:commit_message, with: 'New commit message', visible: true) + click_button('Commit changes') - new_file_path = project_blob_path(project, 'master/not_a_file.md') + new_file_path = project_blob_path(project, "master/#{file_name}") expect(page).to have_current_path(new_file_path, ignore_query: true) @@ -124,38 +132,64 @@ def submit_new_file(options) expect(find('.monaco-editor')).to have_content('Sample\n\n\n') end - it 'creates and commit a new file with a directory name' do + it 'creates and commits a new file with a directory name' do fill_in(:file_name, with: 'foo/bar/baz.txt') expect(page).to have_selector('.file-editor') - editor_set_value('*.rbca') + editor_set_value(file_content) fill_in(:commit_message, with: 'New commit message', visible: true) + click_button('Commit changes') expect(page).to have_current_path(project_blob_path(project, 'master/foo/bar/baz.txt'), ignore_query: true) wait_for_requests - expect(page).to have_content('*.rbca') + expect(page).to have_content(file_content) end - it 'creates and commit a new file specifying a new branch' do - expect(page).to have_selector('.file-editor') + context 'when not creating a new MR' do + it 'creates and commits a new file specifying a new branch' do + expect(page).to have_selector('.file-editor') - editor_set_value('*.rbca') - fill_in(:file_name, with: 'not_a_file.md') - fill_in(:commit_message, with: 'New commit message', visible: true) - fill_in(:branch_name, with: 'new_branch_name', visible: true) - click_button('Commit changes') + editor_set_value(file_content) + fill_in(:file_name, with: file_name) + fill_in(:commit_message, with: 'New commit message', visible: true) + fill_in(:branch_name, with: branch_name, visible: true) + find_field('Start a new merge request with these changes').uncheck - expect(page).to have_current_path(project_new_merge_request_path(project), ignore_query: true) + click_button('Commit changes') - click_link('Changes') + new_file_path = project_blob_path(project, "#{branch_name}/#{file_name}") - wait_for_requests + expect(page).to have_current_path(new_file_path) + + wait_for_requests + + expect(page).not_to have_content(can_submit_mr_content) + end + end + + context 'when creating a new MR' do + it 'creates and commits a new file specifying a new branch and creates an MR' do + expect(page).to have_selector('.file-editor') + + editor_set_value(file_content) + fill_in(:file_name, with: file_name) + fill_in(:commit_message, with: 'New commit message', visible: true) + fill_in(:branch_name, with: branch_name, visible: true) + + click_button('Commit changes') + + expect(page).to have_current_path(project_new_merge_request_path(project), ignore_query: true) + + click_link('Changes') + + wait_for_requests - expect(page).to have_content('*.rbca') + expect(page).to have_content(can_submit_mr_content) + end end end @@ -174,12 +208,12 @@ def submit_new_file(options) expect(page).to have_content(message) end - it 'creates and commit new file in forked project' do + it 'creates and commits a new file in forked project' do expect(page).to have_selector('.file-editor') - editor_set_value('*.rbca') + editor_set_value(file_content) - fill_in(:file_name, with: 'not_a_file.md') + fill_in(:file_name, with: file_name) fill_in(:commit_message, with: 'New commit message', visible: true) click_button('Commit changes')