2020-09-29 14:09:52 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class Import::BulkImportsController < ApplicationController
|
2020-12-21 13:10:26 -05:00
|
|
|
include ActionView::Helpers::SanitizeHelper
|
|
|
|
|
2020-09-29 14:09:52 -04:00
|
|
|
before_action :ensure_group_import_enabled
|
|
|
|
before_action :verify_blocked_uri, only: :status
|
|
|
|
|
2020-10-05 08:08:47 -04:00
|
|
|
feature_category :importers
|
|
|
|
|
2021-01-11 13:10:43 -05:00
|
|
|
POLLING_INTERVAL = 3_000
|
|
|
|
|
2021-06-25 08:07:44 -04:00
|
|
|
rescue_from BulkImports::Error, 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
|
2021-10-13 08:12:20 -04:00
|
|
|
data = ::BulkImports::GetImportableDataService.new(params, query_params, credentials).execute
|
2021-01-29 10:09:40 -05:00
|
|
|
|
|
|
|
pagination_headers.each do |header|
|
2021-10-13 08:12:20 -04:00
|
|
|
response.set_header(header, data[:response].headers[header])
|
2021-01-29 10:09:40 -05:00
|
|
|
end
|
|
|
|
|
2021-10-13 08:12:20 -04:00
|
|
|
json_response = { importable_data: serialized_data(data[:response].parsed_response) }
|
|
|
|
json_response[:version_validation] = data[:version_validation]
|
|
|
|
|
|
|
|
render json: json_response
|
2020-10-05 11:08:56 -04:00
|
|
|
end
|
2020-12-15 07:10:17 -05:00
|
|
|
format.html do
|
|
|
|
@source_url = session[url_key]
|
|
|
|
end
|
2020-10-05 11:08:56 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-10-23 11:08:42 -04:00
|
|
|
def create
|
2021-12-14 07:13:33 -05:00
|
|
|
responses = create_params.map { |entry| ::BulkImports::CreateService.new(current_user, entry, credentials).execute }
|
2021-02-22 13:10:55 -05:00
|
|
|
|
2021-12-14 07:13:33 -05:00
|
|
|
render json: responses.map { |response| { success: response.success?, id: response.payload[:id], message: response.message } }
|
2020-10-23 11:08:42 -04:00
|
|
|
end
|
|
|
|
|
2021-01-11 13:10:43 -05:00
|
|
|
def realtime_changes
|
|
|
|
Gitlab::PollingInterval.set_header(response, interval: POLLING_INTERVAL)
|
|
|
|
|
|
|
|
render json: current_user_bulk_imports.to_json(only: [:id], methods: [:status_name])
|
|
|
|
end
|
|
|
|
|
2020-09-29 14:09:52 -04:00
|
|
|
private
|
|
|
|
|
2021-01-29 10:09:40 -05:00
|
|
|
def pagination_headers
|
|
|
|
%w[x-next-page x-page x-per-page x-prev-page x-total x-total-pages]
|
|
|
|
end
|
|
|
|
|
|
|
|
def serialized_data(data)
|
|
|
|
serializer.represent(data, {}, Import::BulkImportEntity)
|
2020-10-05 11:08:56 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def serializer
|
|
|
|
@serializer ||= BaseSerializer.new(current_user: current_user)
|
|
|
|
end
|
|
|
|
|
2020-12-21 13:10:26 -05:00
|
|
|
# Default query string params used to fetch groups from GitLab source instance
|
|
|
|
#
|
|
|
|
# top_level_only: fetch only top level groups (subgroups are fetched during import itself)
|
|
|
|
# min_access_level: fetch only groups user has maintainer or above permissions
|
|
|
|
# search: optional search param to search user's groups by a keyword
|
|
|
|
def query_params
|
|
|
|
query_params = {
|
|
|
|
top_level_only: true,
|
2021-06-04 11:10:25 -04:00
|
|
|
min_access_level: Gitlab::Access::OWNER
|
2020-12-21 13:10:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
query_params[:search] = sanitized_filter_param if sanitized_filter_param
|
|
|
|
query_params
|
2020-10-05 11:08:56 -04:00
|
|
|
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
|
2020-12-15 04:10:00 -05:00
|
|
|
params.permit(bulk_import: bulk_import_params)[:bulk_import]
|
2020-10-23 11:08:42 -04:00
|
|
|
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
|
2021-08-31 08:11:07 -04:00
|
|
|
render_404 unless Feature.enabled?(:bulk_import, default_enabled: :yaml)
|
2020-09-29 14:09:52 -04:00
|
|
|
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
|
|
|
|
2021-03-02 16:11:07 -05:00
|
|
|
redirect_to new_group_path(anchor: 'import-group-pane'), alert: _('Specified URL cannot be used: "%{reason}"') % { reason: e.message }
|
2020-09-29 14:09:52 -04:00
|
|
|
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
|
2021-03-02 16:11:07 -05:00
|
|
|
redirect_to new_group_path(anchor: 'import-group-pane')
|
2020-10-05 11:08:56 -04:00
|
|
|
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],
|
2020-12-15 04:10:00 -05:00
|
|
|
access_token: session[access_token_key]
|
2020-10-23 11:08:42 -04:00
|
|
|
}
|
|
|
|
end
|
2020-12-21 13:10:26 -05:00
|
|
|
|
|
|
|
def sanitized_filter_param
|
|
|
|
@filter ||= sanitize(params[:filter])&.downcase
|
|
|
|
end
|
2021-01-11 13:10:43 -05:00
|
|
|
|
|
|
|
def current_user_bulk_imports
|
|
|
|
current_user.bulk_imports.gitlab
|
|
|
|
end
|
2020-09-29 14:09:52 -04:00
|
|
|
end
|