2020-09-29 14:09:52 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class Import::BulkImportsController < ApplicationController
|
|
|
|
before_action :ensure_group_import_enabled
|
|
|
|
before_action :verify_blocked_uri, only: :status
|
|
|
|
|
2020-10-05 08:08:47 -04:00
|
|
|
feature_category :importers
|
|
|
|
|
2020-10-23 11:08:42 -04:00
|
|
|
rescue_from BulkImports::Clients::Http::ConnectionError, with: :bulk_import_connection_error
|
2020-10-05 11:08:56 -04:00
|
|
|
|
2020-09-29 14:09:52 -04:00
|
|
|
def configure
|
2020-10-23 11:08:42 -04:00
|
|
|
session[access_token_key] = configure_params[access_token_key]&.strip
|
|
|
|
session[url_key] = configure_params[url_key]
|
2020-09-29 14:09:52 -04:00
|
|
|
|
2020-10-23 11:08:42 -04:00
|
|
|
redirect_to status_import_bulk_imports_url
|
2020-09-29 14:09:52 -04:00
|
|
|
end
|
|
|
|
|
2020-10-05 11:08:56 -04:00
|
|
|
def status
|
|
|
|
respond_to do |format|
|
|
|
|
format.json do
|
|
|
|
render json: { importable_data: serialized_importable_data }
|
|
|
|
end
|
|
|
|
|
|
|
|
format.html
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-10-23 11:08:42 -04:00
|
|
|
def create
|
|
|
|
BulkImportService.new(current_user, create_params, credentials).execute
|
|
|
|
|
|
|
|
render json: :ok
|
|
|
|
end
|
|
|
|
|
2020-09-29 14:09:52 -04:00
|
|
|
private
|
|
|
|
|
2020-10-05 11:08:56 -04:00
|
|
|
def serialized_importable_data
|
|
|
|
serializer.represent(importable_data, {}, Import::BulkImportEntity)
|
|
|
|
end
|
|
|
|
|
|
|
|
def serializer
|
|
|
|
@serializer ||= BaseSerializer.new(current_user: current_user)
|
|
|
|
end
|
|
|
|
|
|
|
|
def importable_data
|
2020-11-06 04:08:56 -05:00
|
|
|
client.get('groups', top_level_only: true).parsed_response
|
2020-10-05 11:08:56 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def client
|
2020-10-23 11:08:42 -04:00
|
|
|
@client ||= BulkImports::Clients::Http.new(
|
2020-10-05 11:08:56 -04:00
|
|
|
uri: session[url_key],
|
|
|
|
token: session[access_token_key]
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2020-10-23 11:08:42 -04:00
|
|
|
def configure_params
|
2020-09-29 14:09:52 -04:00
|
|
|
params.permit(access_token_key, url_key)
|
|
|
|
end
|
|
|
|
|
2020-10-23 11:08:42 -04:00
|
|
|
def create_params
|
|
|
|
params.permit(:bulk_import, [*bulk_import_params])
|
|
|
|
end
|
|
|
|
|
|
|
|
def bulk_import_params
|
|
|
|
%i[
|
|
|
|
source_type
|
|
|
|
source_full_path
|
|
|
|
destination_name
|
|
|
|
destination_namespace
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
2020-09-29 14:09:52 -04:00
|
|
|
def ensure_group_import_enabled
|
|
|
|
render_404 unless Feature.enabled?(:bulk_import)
|
|
|
|
end
|
|
|
|
|
|
|
|
def access_token_key
|
|
|
|
:bulk_import_gitlab_access_token
|
|
|
|
end
|
|
|
|
|
|
|
|
def url_key
|
|
|
|
:bulk_import_gitlab_url
|
|
|
|
end
|
|
|
|
|
|
|
|
def verify_blocked_uri
|
|
|
|
Gitlab::UrlBlocker.validate!(
|
|
|
|
session[url_key],
|
2020-12-09 19:09:50 -05:00
|
|
|
allow_localhost: allow_local_requests?,
|
|
|
|
allow_local_network: allow_local_requests?,
|
|
|
|
schemes: %w(http https)
|
2020-09-29 14:09:52 -04:00
|
|
|
)
|
|
|
|
rescue Gitlab::UrlBlocker::BlockedUrlError => e
|
2020-10-05 11:08:56 -04:00
|
|
|
clear_session_data
|
2020-09-29 14:09:52 -04:00
|
|
|
|
|
|
|
redirect_to new_group_path, alert: _('Specified URL cannot be used: "%{reason}"') % { reason: e.message }
|
|
|
|
end
|
|
|
|
|
|
|
|
def allow_local_requests?
|
|
|
|
Gitlab::CurrentSettings.allow_local_requests_from_web_hooks_and_services?
|
|
|
|
end
|
2020-10-05 11:08:56 -04:00
|
|
|
|
|
|
|
def bulk_import_connection_error(error)
|
|
|
|
clear_session_data
|
|
|
|
|
|
|
|
error_message = _("Unable to connect to server: %{error}") % { error: error }
|
|
|
|
flash[:alert] = error_message
|
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
format.json do
|
|
|
|
render json: {
|
|
|
|
error: {
|
|
|
|
message: error_message,
|
|
|
|
redirect: new_group_path
|
|
|
|
}
|
|
|
|
}, status: :unprocessable_entity
|
|
|
|
end
|
|
|
|
format.html do
|
|
|
|
redirect_to new_group_path
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def clear_session_data
|
|
|
|
session[url_key] = nil
|
|
|
|
session[access_token_key] = nil
|
|
|
|
end
|
2020-10-23 11:08:42 -04:00
|
|
|
|
|
|
|
def credentials
|
|
|
|
{
|
|
|
|
url: session[url_key],
|
|
|
|
access_token: [access_token_key]
|
|
|
|
}
|
|
|
|
end
|
2020-09-29 14:09:52 -04:00
|
|
|
end
|