diff --git a/CHANGELOG b/CHANGELOG index 7a19af6b7655d5501a2058de3805036202a8a1a7..0a9a846745cf166f8c2bb3b2216ee337c2676750 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -147,6 +147,7 @@ v 8.12.0 (unreleased) - Refactor the triggers page and documentation !6217 - Show values of CI trigger variables only when clicked (Katarzyna Kobierska Ula Budziszewska) - Use default clone protocol on "check out, review, and merge locally" help page URL + - Let the user choose a namespace and name on GitHub imports - API for Ci Lint !5953 (Katarzyna Kobierska Urszula Budziszewska) - Allow bulk update merge requests from merge requests index page - Ensure validation messages are shown within the milestone form diff --git a/app/assets/javascripts/importer_status.js b/app/assets/javascripts/importer_status.js index 9efad1ce94386bfb078179616e2c87f3a37c3037..9fe00a0a4dbe5d107959e4bd3ea22bc2ed939ee7 100644 --- a/app/assets/javascripts/importer_status.js +++ b/app/assets/javascripts/importer_status.js @@ -14,20 +14,20 @@ $btn = $(e.currentTarget); $tr = $btn.closest('tr'); $target_field = $tr.find('.import-target'); - $namespace_input = $target_field.find('input'); + $namespace_input = $target_field.find('.js-select-namespace option:selected'); id = $tr.attr('id').replace('repo_', ''); target_namespace = null; - + new_name = null; if ($namespace_input.length > 0) { - target_namespace = $namespace_input.prop('value'); - $target_field.empty().append(target_namespace + "/" + ($target_field.data('project_name'))); + target_namespace = $namespace_input[0].innerHTML; + new_name = $target_field.find('#path').prop('value'); + $target_field.empty().append(target_namespace + "/" + new_name); } - $btn.disable().addClass('is-loading'); - return $.post(_this.import_url, { repo_id: id, - target_namespace: target_namespace + target_namespace: target_namespace, + new_name: new_name }, { dataType: 'script' }); diff --git a/app/assets/stylesheets/pages/projects.scss b/app/assets/stylesheets/pages/projects.scss index db46d8072cee3769fb223fb2dbf042be58b3d76e..986a22f4e6efd828a51b5d0a4fe5bdb986cdf0da 100644 --- a/app/assets/stylesheets/pages/projects.scss +++ b/app/assets/stylesheets/pages/projects.scss @@ -770,3 +770,9 @@ pre.light-well { } } } + +.input-group { + .project-path { + padding: 0; + } +} \ No newline at end of file diff --git a/app/controllers/import/github_controller.rb b/app/controllers/import/github_controller.rb index 8c6bdd163832c913b68a342c7714950889515d57..ee7d498c59ce1cc0fe3b5e5e3d9908867abc81e0 100644 --- a/app/controllers/import/github_controller.rb +++ b/app/controllers/import/github_controller.rb @@ -40,11 +40,12 @@ def jobs def create @repo_id = params[:repo_id].to_i repo = client.repo(@repo_id) - @project_name = repo.name - @target_namespace = find_or_create_namespace(repo.owner.login, client.user.login) + @project_name = params[:new_name].presence || repo.name + namespace_path = params[:target_namespace].presence || current_user.namespace_path + @target_namespace = find_or_create_namespace(namespace_path, current_user.namespace_path) if current_user.can?(:create_projects, @target_namespace) - @project = Gitlab::GithubImport::ProjectCreator.new(repo, @target_namespace, current_user, access_params).execute + @project = Gitlab::GithubImport::ProjectCreator.new(repo, @project_name, @target_namespace, current_user, access_params).execute else render 'unauthorized' end diff --git a/app/helpers/namespaces_helper.rb b/app/helpers/namespaces_helper.rb index 94c6b548ecd1007faef2919f615761281af1b897..af06833ef91b7c64324c94587fc6bcb159718fdf 100644 --- a/app/helpers/namespaces_helper.rb +++ b/app/helpers/namespaces_helper.rb @@ -1,6 +1,9 @@ module NamespacesHelper - def namespaces_options(selected = :current_user, display_path: false) + def namespaces_options(selected = :current_user, extra_groups = [], display_path: false) groups = current_user.owned_groups + current_user.masters_groups + + groups += process_extra_groups(extra_groups) if extra_groups.any? + users = [current_user.namespace] data_attr_group = { 'data-options-parent' => 'groups' } @@ -25,6 +28,15 @@ def namespaces_options(selected = :current_user, display_path: false) grouped_options_for_select(options, selected) end + def process_extra_groups(extra_groups) + # Remove duplicate groups - we either keep the ones that exist for the user + # (already in groups) or ignore those that do not belong to the user. + duplicated_groups = extra_groups.map { |name| Namespace.where(name: name).map(&:name) } + extra_groups = extra_groups - duplicated_groups.flatten + + extra_groups.map { |name| Group.new(name: name) } + end + def namespace_icon(namespace, size = 40) if namespace.kind_of?(Group) group_icon(namespace) diff --git a/app/views/import/github/status.html.haml b/app/views/import/github/status.html.haml index bd3be20c4f8a87e602999071de4224ec3f4fd269..8e552fbd005a33965c360d334ed867b01816f338 100644 --- a/app/views/import/github/status.html.haml +++ b/app/views/import/github/status.html.haml @@ -45,7 +45,18 @@ %td = github_project_link(repo.full_name) %td.import-target - = import_project_target(repo.owner.login, repo.name) + %fieldset.row + .input-group + .col-xs-11.col-sm-5.project-path + - if current_user.can_select_namespace? && !current_user.can_create_group? + = select_tag :namespace_id, namespaces_options(params[:namespace_id] || :current_user, display_path: true), {class: 'project-path select2 js-select-namespace', tabindex: 1} + - elsif current_user.can_create_group? + = select_tag :namespace_id, namespaces_options(params[:namespace_id] || :current_user, [repo.owner.login]), {class: 'project-path select2 js-select-namespace', tabindex: 1} + - else + = text_field_tag :path, current_user.namespace_path, class: "form-control", tabindex: 2, autofocus: true, disabled: true + + .col-xs-12.col-sm-6.project-path + = text_field_tag :path, repo.name, class: "form-control", tabindex: 2, autofocus: true, required: true %td.import-actions.job-status = button_tag class: "btn btn-import js-add-to-import" do Import diff --git a/doc/workflow/importing/img/import_projects_from_github_importer.png b/doc/workflow/importing/img/import_projects_from_github_importer.png index 2082de06f47e9a27592d8dbac4f473879b3e6481..d84c708874ab5f6d2047c1b37ac66bcaf2e01b6c 100644 Binary files a/doc/workflow/importing/img/import_projects_from_github_importer.png and b/doc/workflow/importing/img/import_projects_from_github_importer.png differ diff --git a/doc/workflow/importing/import_projects_from_github.md b/doc/workflow/importing/import_projects_from_github.md index dd38fe0bc0129cd9b494a40cbc11175624134684..c36dfdb78ecdcbd3996b7c2812a6f4f55c586dd3 100644 --- a/doc/workflow/importing/import_projects_from_github.md +++ b/doc/workflow/importing/import_projects_from_github.md @@ -106,6 +106,11 @@ If you want, you can import all your GitHub projects in one go by hitting  +--- + +You can also choose a different name for the project and a different namespace, +if you have the privileges to do so. + [gh-import]: ../../integration/github.md "GitHub integration" [new-project]: ../../gitlab-basics/create-project.md "How to create a new project in GitLab" [gh-integration]: #authorize-access-to-your-repositories-using-the-github-integration diff --git a/lib/gitlab/github_import/project_creator.rb b/lib/gitlab/github_import/project_creator.rb index e9725880c5eb2e4cc9b712cfd4a1a375f661ba5d..605abfabdabf8613b888b74faef3f95bdba45ebc 100644 --- a/lib/gitlab/github_import/project_creator.rb +++ b/lib/gitlab/github_import/project_creator.rb @@ -3,8 +3,9 @@ module GithubImport class ProjectCreator attr_reader :repo, :namespace, :current_user, :session_data - def initialize(repo, namespace, current_user, session_data) + def initialize(repo, name, namespace, current_user, session_data) @repo = repo + @name = name @namespace = namespace @current_user = current_user @session_data = session_data @@ -13,8 +14,8 @@ def initialize(repo, namespace, current_user, session_data) def execute project = ::Projects::CreateService.new( current_user, - name: repo.name, - path: repo.name, + name: @name, + path: @name, description: repo.description, namespace_id: namespace.id, visibility_level: repo.private ? Gitlab::VisibilityLevel::PRIVATE : ApplicationSetting.current.default_project_visibility, diff --git a/spec/controllers/import/github_controller_spec.rb b/spec/controllers/import/github_controller_spec.rb index ebfbf54182b487940775c579c53f8fa245daf9f4..4f96567192dccf2a60a2e8ef5465111b189a79ca 100644 --- a/spec/controllers/import/github_controller_spec.rb +++ b/spec/controllers/import/github_controller_spec.rb @@ -124,8 +124,8 @@ def assign_session_token context "when the GitHub user and GitLab user's usernames match" do it "takes the current user's namespace" do expect(Gitlab::GithubImport::ProjectCreator). - to receive(:new).with(github_repo, user.namespace, user, access_params). - and_return(double(execute: true)) + to receive(:new).with(github_repo, github_repo.name, user.namespace, user, access_params). + and_return(double(execute: true)) post :create, format: :js end @@ -136,8 +136,8 @@ def assign_session_token it "takes the current user's namespace" do expect(Gitlab::GithubImport::ProjectCreator). - to receive(:new).with(github_repo, user.namespace, user, access_params). - and_return(double(execute: true)) + to receive(:new).with(github_repo, github_repo.name, user.namespace, user, access_params). + and_return(double(execute: true)) post :create, format: :js end @@ -158,8 +158,8 @@ def assign_session_token context "when the namespace is owned by the GitLab user" do it "takes the existing namespace" do expect(Gitlab::GithubImport::ProjectCreator). - to receive(:new).with(github_repo, existing_namespace, user, access_params). - and_return(double(execute: true)) + to receive(:new).with(github_repo, github_repo.name, existing_namespace, user, access_params). + and_return(double(execute: true)) post :create, format: :js end @@ -171,9 +171,10 @@ def assign_session_token existing_namespace.save end - it "doesn't create a project" do + it "creates a project using user's namespace" do expect(Gitlab::GithubImport::ProjectCreator). - not_to receive(:new) + to receive(:new).with(github_repo, github_repo.name, user.namespace, user, access_params). + and_return(double(execute: true)) post :create, format: :js end @@ -186,15 +187,15 @@ def assign_session_token expect(Gitlab::GithubImport::ProjectCreator). to receive(:new).and_return(double(execute: true)) - expect { post :create, format: :js }.to change(Namespace, :count).by(1) + expect { post :create, target_namespace: github_repo.name, format: :js }.to change(Namespace, :count).by(1) end it "takes the new namespace" do expect(Gitlab::GithubImport::ProjectCreator). - to receive(:new).with(github_repo, an_instance_of(Group), user, access_params). + to receive(:new).with(github_repo, github_repo.name, an_instance_of(Group), user, access_params). and_return(double(execute: true)) - post :create, format: :js + post :create, target_namespace: github_repo.name, format: :js end end @@ -212,13 +213,34 @@ def assign_session_token it "takes the current user's namespace" do expect(Gitlab::GithubImport::ProjectCreator). - to receive(:new).with(github_repo, user.namespace, user, access_params). + to receive(:new).with(github_repo, github_repo.name, user.namespace, user, access_params). and_return(double(execute: true)) post :create, format: :js end end end + + context 'user has chosen a namespace and name for the project' do + let(:test_namespace) { create(:namespace, name: 'test_namespace', owner: user) } + let(:test_name) { 'test_name' } + + it 'takes the selected namespace and name' do + expect(Gitlab::GithubImport::ProjectCreator). + to receive(:new).with(github_repo, test_name, test_namespace, user, access_params). + and_return(double(execute: true)) + + post :create, { target_namespace: test_namespace.name, new_name: test_name, format: :js } + end + + it 'takes the selected name and default namespace' do + expect(Gitlab::GithubImport::ProjectCreator). + to receive(:new).with(github_repo, test_name, user.namespace, user, access_params). + and_return(double(execute: true)) + + post :create, { new_name: test_name, format: :js } + end + end end end end diff --git a/spec/lib/gitlab/github_import/project_creator_spec.rb b/spec/lib/gitlab/github_import/project_creator_spec.rb index 014ee462e5c25b2e6d699844353542d6d8da3144..ab06b7bc5bb295cf008a7acef1e1ded032e5b4bc 100644 --- a/spec/lib/gitlab/github_import/project_creator_spec.rb +++ b/spec/lib/gitlab/github_import/project_creator_spec.rb @@ -13,7 +13,7 @@ ) end - subject(:service) { described_class.new(repo, namespace, user, github_access_token: 'asdffg') } + subject(:service) { described_class.new(repo, repo.name, namespace, user, github_access_token: 'asdffg') } before do namespace.add_owner(user)