diff --git a/app/helpers/namespaces_helper.rb b/app/helpers/namespaces_helper.rb index 19aa630007d..1503aea4aa2 100644 --- a/app/helpers/namespaces_helper.rb +++ b/app/helpers/namespaces_helper.rb @@ -9,6 +9,7 @@ module NamespacesHelper .includes(:route) .order('routes.path') users = [current_user.namespace] + selected_option = nil unless extra_group.nil? || extra_group.is_a?(Group) extra_group = Group.find(extra_group) if Namespace.find(extra_group).kind == 'group' @@ -18,7 +19,11 @@ module NamespacesHelper # Avoid duplicate groups if one already exists by that name existing_group = Group.find_by(name: extra_group.name) extra_group = existing_group if existing_group - groups |= [extra_group] if Ability.allowed?(current_user, :read_group, extra_group) + + if Ability.allowed?(current_user, :read_group, extra_group) + groups |= [extra_group] + selected_option = extra_group.id if selected == :extra_group + end end options = [] @@ -27,12 +32,12 @@ module NamespacesHelper unless groups_only options << options_for_group(users, display_path: display_path, type: 'user') - if selected == :current_user && current_user.namespace - selected = current_user.namespace.id + if (selected == :current_user || selected_option.nil?) && current_user.namespace + selected_option = current_user.namespace.id end end - grouped_options_for_select(options, selected) + grouped_options_for_select(options, selected_option) end def namespace_icon(namespace, size = 40) diff --git a/app/views/import/bitbucket_server/status.html.haml b/app/views/import/bitbucket_server/status.html.haml index b7a73753b3c..5d78e9cfd1d 100644 --- a/app/views/import/bitbucket_server/status.html.haml +++ b/app/views/import/bitbucket_server/status.html.haml @@ -58,7 +58,7 @@ .input-group .project-path.input-group-prepend - if current_user.can_select_namespace? - - selected = params[:namespace_id] || :current_user + - selected = params[:namespace_id] || :extra_group - opts = current_user.can_create_group? ? { extra_group: Group.new(name: repo.project_key, path: repo.project_key) } : {} = select_tag :namespace_id, namespaces_options(selected, opts.merge({ display_path: true })), { class: 'input-group-text select2 js-select-namespace', tabindex: 1 } - else diff --git a/spec/helpers/namespaces_helper_spec.rb b/spec/helpers/namespaces_helper_spec.rb index b12b2587510..78bc5ff0055 100644 --- a/spec/helpers/namespaces_helper_spec.rb +++ b/spec/helpers/namespaces_helper_spec.rb @@ -40,6 +40,25 @@ describe NamespacesHelper do expect(options).to include(admin_group.name) end + it 'selects extra_group' do + allow(helper).to receive(:current_user).and_return(admin) + + options = helper.namespaces_options(:extra_group, display_path: true, extra_group: user_group) + + expect(options).to include("selected=\"selected\" value=\"#{user_group.id}\"") + expect(options).to include(admin_group.name) + end + + it 'falls back to current user selection' do + allow(helper).to receive(:current_user).and_return(user) + + options = helper.namespaces_options(:extra_group, display_path: true, extra_group: build(:group, name: admin_group.name)) + + expect(options).to include(user_group.name) + expect(options).not_to include(admin_group.name) + expect(options).to include("selected=\"selected\" value=\"#{user.namespace.id}\"") + end + it 'returns only groups if groups_only option is true' do allow(helper).to receive(:current_user).and_return(user)