diff options
author | Markus Koller <mkoller@gitlab.com> | 2019-07-11 19:27:21 +0200 |
---|---|---|
committer | Markus Koller <mkoller@gitlab.com> | 2019-07-12 16:23:00 +0200 |
commit | 60a6074dc977a72e00043596623094e006c84704 (patch) | |
tree | 5a9606f23ad802e073e6974960da19220d64110a | |
parent | 21c372827eb0b536d667dd2ae16e5a4930db6300 (diff) | |
download | gitlab-ce-51952-redirect-to-webide-in-fork.tar.gz |
Open WebIDE in fork when user doesn't have access51952-redirect-to-webide-in-fork
When opening the IDE on a project where the user doesn't have push
access, we create a fork and then redirect back to the IDE. To make
sure the user can actually commit, we need to open the IDE in the forked
project rather than the upstream project.
-rw-r--r-- | app/helpers/blob_helper.rb | 11 | ||||
-rw-r--r-- | changelogs/unreleased/51952-redirect-to-webide-in-fork.yml | 5 | ||||
-rw-r--r-- | spec/features/projects/files/user_edits_files_spec.rb | 1 | ||||
-rw-r--r-- | spec/helpers/blob_helper_spec.rb | 38 |
4 files changed, 47 insertions, 8 deletions
diff --git a/app/helpers/blob_helper.rb b/app/helpers/blob_helper.rb index 0d6a6496993..4b0713001a1 100644 --- a/app/helpers/blob_helper.rb +++ b/app/helpers/blob_helper.rb @@ -18,7 +18,16 @@ module BlobHelper end def ide_edit_path(project = @project, ref = @ref, path = @path, options = {}) - segments = [ide_path, 'project', project.full_path, 'edit', ref] + project_path = + if !current_user || can?(current_user, :push_code, project) + project.full_path + else + # We currently always fork to the user's namespace + # in edit_fork_button_tag + "#{current_user.namespace.full_path}/#{project.path}" + end + + segments = [ide_path, 'project', project_path, 'edit', ref] segments.concat(['-', encode_ide_path(path)]) if path.present? File.join(segments) end diff --git a/changelogs/unreleased/51952-redirect-to-webide-in-fork.yml b/changelogs/unreleased/51952-redirect-to-webide-in-fork.yml new file mode 100644 index 00000000000..ada0efca968 --- /dev/null +++ b/changelogs/unreleased/51952-redirect-to-webide-in-fork.yml @@ -0,0 +1,5 @@ +--- +title: Open WebIDE in fork when user doesn't have access +merge_request: 30642 +author: +type: changed diff --git a/spec/features/projects/files/user_edits_files_spec.rb b/spec/features/projects/files/user_edits_files_spec.rb index e0fa9dbb5fa..e4b02408b49 100644 --- a/spec/features/projects/files/user_edits_files_spec.rb +++ b/spec/features/projects/files/user_edits_files_spec.rb @@ -162,6 +162,7 @@ describe 'Projects > Files > User edits files', :js do expect_fork_status + expect(page).to have_css('.ide-sidebar-project-title', text: "#{project2.name} #{user.namespace.full_path}/#{project2.path}") expect(page).to have_css('.ide .multi-file-tab', text: '.gitignore') end diff --git a/spec/helpers/blob_helper_spec.rb b/spec/helpers/blob_helper_spec.rb index 6808ed86c9a..1f236429347 100644 --- a/spec/helpers/blob_helper_spec.rb +++ b/spec/helpers/blob_helper_spec.rb @@ -29,14 +29,15 @@ describe BlobHelper do let(:project) { create(:project, :repository, namespace: namespace) } before do - allow(self).to receive(:current_user).and_return(nil) - allow(self).to receive(:can_collaborate_with_project?).and_return(true) + allow(helper).to receive(:current_user).and_return(nil) + allow(helper).to receive(:can?).and_return(true) + allow(helper).to receive(:can_collaborate_with_project?).and_return(true) end it 'verifies blob is text' do expect(helper).not_to receive(:blob_text_viewable?) - button = edit_blob_button(project, 'refs/heads/master', 'README.md') + button = helper.edit_blob_button(project, 'refs/heads/master', 'README.md') expect(button).to start_with('<button') end @@ -46,25 +47,25 @@ describe BlobHelper do expect(project.repository).not_to receive(:blob_at) - edit_blob_button(project, 'refs/heads/master', 'README.md', blob: blob) + helper.edit_blob_button(project, 'refs/heads/master', 'README.md', blob: blob) end it 'returns a link with the proper route' do stub_feature_flags(web_ide_default: false) - link = edit_blob_button(project, 'master', 'README.md') + link = helper.edit_blob_button(project, 'master', 'README.md') expect(Capybara.string(link).find_link('Edit')[:href]).to eq("/#{project.full_path}/edit/master/README.md") end it 'returns a link with a Web IDE route' do - link = edit_blob_button(project, 'master', 'README.md') + link = helper.edit_blob_button(project, 'master', 'README.md') expect(Capybara.string(link).find_link('Edit')[:href]).to eq("/-/ide/project/#{project.full_path}/edit/master/-/README.md") end it 'returns a link with the passed link_opts on the expected route' do stub_feature_flags(web_ide_default: false) - link = edit_blob_button(project, 'master', 'README.md', link_opts: { mr_id: 10 }) + link = helper.edit_blob_button(project, 'master', 'README.md', link_opts: { mr_id: 10 }) expect(Capybara.string(link).find_link('Edit')[:href]).to eq("/#{project.full_path}/edit/master/README.md?mr_id=10") end @@ -203,6 +204,13 @@ describe BlobHelper do describe '#ide_edit_path' do let(:project) { create(:project) } + let(:current_user) { create(:user) } + let(:can_push_code) { true } + + before do + allow(helper).to receive(:current_user).and_return(current_user) + allow(helper).to receive(:can?).and_return(can_push_code) + end around do |example| old_script_name = Rails.application.routes.default_url_options[:script_name] @@ -243,5 +251,21 @@ describe BlobHelper do expect(helper.ide_edit_path(project, "testing/slashes", "readme.md/")).to eq("/-/ide/project/#{project.namespace.path}/#{project.path}/edit/testing/slashes/-/readme.md/") end + + context 'when user is not logged in' do + let(:current_user) { nil } + + it 'returns IDE path inside the project' do + expect(helper.ide_edit_path(project, "master", "")).to eq("/-/ide/project/#{project.namespace.path}/#{project.path}/edit/master") + end + end + + context 'when user cannot push to the project' do + let(:can_push_code) { false } + + it "returns IDE path with the user's fork" do + expect(helper.ide_edit_path(project, "master", "")).to eq("/-/ide/project/#{current_user.namespace.full_path}/#{project.path}/edit/master") + end + end end end |