Commit 2dcd7598 authored by Mark Chao's avatar Mark Chao

Merge branch...

Merge branch '353936-github-importer-default-to-the-current-group-when-importing-from-github' into 'master'

GitHub Importer - Default to the current group when importing from GitHub

See merge request gitlab-org/gitlab!84323
parents 4e64b89b 5fd18ea7
...@@ -16,12 +16,13 @@ export function initStoreFromElement(element) { ...@@ -16,12 +16,13 @@ export function initStoreFromElement(element) {
jobsPath, jobsPath,
importPath, importPath,
namespacesPath, namespacesPath,
defaultTargetNamespace,
paginatable, paginatable,
} = element.dataset; } = element.dataset;
return createStore({ return createStore({
initialState: { initialState: {
defaultTargetNamespace: gon.current_username, defaultTargetNamespace,
ciCdOnly: parseBoolean(ciCdOnly), ciCdOnly: parseBoolean(ciCdOnly),
canSelectNamespace: parseBoolean(canSelectNamespace), canSelectNamespace: parseBoolean(canSelectNamespace),
provider, provider,
......
...@@ -13,7 +13,13 @@ class Import::BaseController < ApplicationController ...@@ -13,7 +13,13 @@ class Import::BaseController < ApplicationController
provider_repos: serialized_provider_repos, provider_repos: serialized_provider_repos,
incompatible_repos: serialized_incompatible_repos } incompatible_repos: serialized_incompatible_repos }
end end
format.html format.html do
if params[:namespace_id]&.present?
@namespace = Namespace.find_by_id(params[:namespace_id])
render_404 unless current_user.can?(:create_projects, @namespace)
end
end
end end
end end
......
...@@ -23,24 +23,25 @@ class Import::GithubController < Import::BaseController ...@@ -23,24 +23,25 @@ class Import::GithubController < Import::BaseController
if !ci_cd_only? && github_import_configured? && logged_in_with_provider? if !ci_cd_only? && github_import_configured? && logged_in_with_provider?
go_to_provider_for_permissions go_to_provider_for_permissions
elsif session[access_token_key] elsif session[access_token_key]
redirect_to status_import_url redirect_to status_import_url(namespace_id: params[:namespace_id])
end end
end end
def callback def callback
auth_state = session[auth_state_key] auth_state = session.delete(auth_state_key)
session[auth_state_key] = nil namespace_id = session.delete(:namespace_id)
if auth_state.blank? || !ActiveSupport::SecurityUtils.secure_compare(auth_state, params[:state]) if auth_state.blank? || !ActiveSupport::SecurityUtils.secure_compare(auth_state, params[:state])
provider_unauthorized provider_unauthorized
else else
session[access_token_key] = get_token(params[:code]) session[access_token_key] = get_token(params[:code])
redirect_to status_import_url redirect_to status_import_url(namespace_id: namespace_id)
end end
end end
def personal_access_token def personal_access_token
session[access_token_key] = params[:personal_access_token]&.strip session[access_token_key] = params[:personal_access_token]&.strip
redirect_to status_import_url redirect_to status_import_url(namespace_id: params[:namespace_id].presence)
end end
def status def status
...@@ -209,8 +210,8 @@ class Import::GithubController < Import::BaseController ...@@ -209,8 +210,8 @@ class Import::GithubController < Import::BaseController
public_send("new_import_#{provider_name}_url", extra_import_params) # rubocop:disable GitlabSecurity/PublicSend public_send("new_import_#{provider_name}_url", extra_import_params) # rubocop:disable GitlabSecurity/PublicSend
end end
def status_import_url def status_import_url(namespace_id: nil)
public_send("status_import_#{provider_name}_url", extra_import_params) # rubocop:disable GitlabSecurity/PublicSend public_send("status_import_#{provider_name}_url", extra_import_params.merge({ namespace_id: namespace_id })) # rubocop:disable GitlabSecurity/PublicSend
end end
def callback_import_url def callback_import_url
...@@ -256,6 +257,7 @@ class Import::GithubController < Import::BaseController ...@@ -256,6 +257,7 @@ class Import::GithubController < Import::BaseController
def provider_auth def provider_auth
if !ci_cd_only? && session[access_token_key].blank? if !ci_cd_only? && session[access_token_key].blank?
session[:namespace_id] = params[:namespace_id]
go_to_provider_for_permissions go_to_provider_for_permissions
end end
end end
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
- extra_data = local_assigns.fetch(:extra_data, {}) - extra_data = local_assigns.fetch(:extra_data, {})
- filterable = local_assigns.fetch(:filterable, true) - filterable = local_assigns.fetch(:filterable, true)
- paginatable = local_assigns.fetch(:paginatable, false) - paginatable = local_assigns.fetch(:paginatable, false)
- default_namespace_path = (local_assigns[:default_namespace] || current_user.namespace).full_path
- provider_title = Gitlab::ImportSources.title(provider) - provider_title = Gitlab::ImportSources.title(provider)
- header_title _("New project"), new_project_path - header_title _("New project"), new_project_path
...@@ -14,6 +15,7 @@ ...@@ -14,6 +15,7 @@
namespaces_path: import_available_namespaces_path, namespaces_path: import_available_namespaces_path,
repos_path: url_for([:status, :import, provider, format: :json]), repos_path: url_for([:status, :import, provider, format: :json]),
jobs_path: url_for([:realtime_changes, :import, provider, format: :json]), jobs_path: url_for([:realtime_changes, :import, provider, format: :json]),
default_target_namespace: default_namespace_path,
import_path: url_for([:import, provider, format: :json]), import_path: url_for([:import, provider, format: :json]),
filterable: filterable.to_s, filterable: filterable.to_s,
paginatable: paginatable.to_s }.merge(extra_data) } paginatable: paginatable.to_s }.merge(extra_data) }
...@@ -10,7 +10,7 @@ ...@@ -10,7 +10,7 @@
= import_github_authorize_message = import_github_authorize_message
- if github_import_configured? && !has_ci_cd_only_params? - if github_import_configured? && !has_ci_cd_only_params?
= link_to status_import_github_path, class: 'gl-button btn btn-confirm' do = link_to status_import_github_path(namespace_id: params[:namespace_id]), class: 'gl-button btn btn-confirm' do
= sprite_icon('github', css_class: 'gl-mr-2') = sprite_icon('github', css_class: 'gl-mr-2')
= title = title
...@@ -23,6 +23,7 @@ ...@@ -23,6 +23,7 @@
= form_tag personal_access_token_import_github_path, method: :post do = form_tag personal_access_token_import_github_path, method: :post do
.form-group .form-group
%label.label-bold= _('Personal Access Token') %label.label-bold= _('Personal Access Token')
= hidden_field_tag(:namespace_id, params[:namespace_id])
= text_field_tag :personal_access_token, '', class: 'form-control gl-form-input', placeholder: _('e.g. %{token}') % { token: '8d3f016698e...' }, data: { qa_selector: 'personal_access_token_field' } = text_field_tag :personal_access_token, '', class: 'form-control gl-form-input', placeholder: _('e.g. %{token}') % { token: '8d3f016698e...' }, data: { qa_selector: 'personal_access_token_field' }
%span.form-text.text-muted %span.form-text.text-muted
= import_github_personal_access_token_message = import_github_personal_access_token_message
......
...@@ -7,4 +7,4 @@ ...@@ -7,4 +7,4 @@
- paginatable = Feature.enabled?(:remove_legacy_github_client) - paginatable = Feature.enabled?(:remove_legacy_github_client)
= render 'import/githubish_status', provider: 'github', paginatable: paginatable = render 'import/githubish_status', provider: 'github', paginatable: paginatable, default_namespace: @namespace
- active_tab = local_assigns.fetch(:active_tab, 'blank') - active_tab = local_assigns.fetch(:active_tab, 'blank')
- track_label = local_assigns.fetch(:track_label, 'import_project') - track_label = local_assigns.fetch(:track_label, 'import_project')
- namespace_id = local_assigns.fetch(:destination_namespace_id, nil)
.project-import .project-import
.form-group.import-btn-container.clearfix .form-group.import-btn-container.clearfix
...@@ -16,7 +17,7 @@ ...@@ -16,7 +17,7 @@
- if github_import_enabled? - if github_import_enabled?
%div %div
= link_to new_import_github_path, class: 'gl-button btn-default btn js-import-github js-import-project-btn', data: { platform: 'github', **tracking_attrs_data(track_label, 'click_button', 'github') } do = link_to new_import_github_path(namespace_id: namespace_id), class: 'gl-button btn-default btn js-import-github js-import-project-btn', data: { platform: 'github', **tracking_attrs_data(track_label, 'click_button', 'github') } do
.gl-button-icon .gl-button-icon
= sprite_icon('github') = sprite_icon('github')
GitHub GitHub
......
...@@ -110,7 +110,7 @@ ...@@ -110,7 +110,7 @@
= _('You can always change your URL later') = _('You can always change your URL later')
.js-import-project-buttons .js-import-project-buttons
= render 'projects/import_project_pane' = render 'projects/import_project_pane', destination_namespace_id: @namespace&.id
- else - else
.nothing-here-block .nothing-here-block
%h4= s_('ProjectsNew|No import options available') %h4= s_('ProjectsNew|No import options available')
......
...@@ -82,11 +82,33 @@ RSpec.describe Import::GithubController do ...@@ -82,11 +82,33 @@ RSpec.describe Import::GithubController do
expect(controller).to redirect_to(new_import_url) expect(controller).to redirect_to(new_import_url)
expect(flash[:alert]).to eq('Access denied to your GitHub account.') expect(flash[:alert]).to eq('Access denied to your GitHub account.')
end end
it "includes namespace_id from session if it is present" do
namespace_id = 1
session[:namespace_id] = 1
get :callback, params: { state: valid_auth_state }
expect(controller).to redirect_to(status_import_github_url(namespace_id: namespace_id))
end
end end
end end
describe "POST personal_access_token" do describe "POST personal_access_token" do
it_behaves_like 'a GitHub-ish import controller: POST personal_access_token' it_behaves_like 'a GitHub-ish import controller: POST personal_access_token'
it 'passes namespace_id param as query param if it was present' do
namespace_id = 5
status_import_url = public_send("status_import_#{provider}_url", { namespace_id: namespace_id })
allow_next_instance_of(Gitlab::LegacyGithubImport::Client) do |client|
allow(client).to receive(:user).and_return(true)
end
post :personal_access_token, params: { personal_access_token: 'some-token', namespace_id: 5 }
expect(controller).to redirect_to(status_import_url)
end
end end
describe "GET status" do describe "GET status" do
......
...@@ -184,6 +184,41 @@ RSpec.shared_examples 'a GitHub-ish import controller: GET status' do ...@@ -184,6 +184,41 @@ RSpec.shared_examples 'a GitHub-ish import controller: GET status' do
expect(json_response.dig("provider_repos").count).to eq(1) expect(json_response.dig("provider_repos").count).to eq(1)
end end
end end
context 'when namespace_id query param is provided' do
let_it_be(:current_user) { create(:user) }
let(:namespace) { create(:namespace) }
before do
allow(controller).to receive(:current_user).and_return(current_user)
end
context 'when user is allowed to create projects in this namespace' do
before do
allow(current_user).to receive(:can?).and_return(true)
end
it 'provides namespace to the template' do
get :status, params: { namespace_id: namespace.id }, format: :html
expect(response).to have_gitlab_http_status :ok
expect(assigns(:namespace)).to eq(namespace)
end
end
context 'when user is not allowed to create projects in this namespace' do
before do
allow(current_user).to receive(:can?).and_return(false)
end
it 'renders 404' do
get :status, params: { namespace_id: namespace.id }, format: :html
expect(response).to have_gitlab_http_status :not_found
end
end
end
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