2018-09-23 15:44:14 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-02-05 13:31:36 -05:00
|
|
|
class Import::BaseController < ApplicationController
|
2020-06-15 11:08:20 -04:00
|
|
|
include ActionView::Helpers::SanitizeHelper
|
|
|
|
|
2021-12-10 10:10:24 -05:00
|
|
|
before_action -> { check_rate_limit!(:project_import, scope: [current_user, :project_import], redirect_back: true) }, only: [:create]
|
2020-10-05 08:08:47 -04:00
|
|
|
feature_category :importers
|
2022-04-27 14:10:15 -04:00
|
|
|
urgency :low
|
2020-01-23 19:08:51 -05:00
|
|
|
|
2020-06-15 11:08:20 -04:00
|
|
|
def status
|
|
|
|
respond_to do |format|
|
|
|
|
format.json do
|
|
|
|
render json: { imported_projects: serialized_imported_projects,
|
|
|
|
provider_repos: serialized_provider_repos,
|
2021-08-17 14:10:35 -04:00
|
|
|
incompatible_repos: serialized_incompatible_repos }
|
2020-06-15 11:08:20 -04:00
|
|
|
end
|
2022-04-15 05:08:25 -04:00
|
|
|
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
|
2020-06-15 11:08:20 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def realtime_changes
|
|
|
|
Gitlab::PollingInterval.set_header(response, interval: 3_000)
|
|
|
|
|
|
|
|
render json: already_added_projects.to_json(only: [:id], methods: [:import_status])
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
def importable_repos
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
|
|
|
|
def incompatible_repos
|
2020-07-16 20:09:37 -04:00
|
|
|
raise NotImplementedError
|
2020-06-15 11:08:20 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def provider_name
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
|
|
|
|
def provider_url
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
|
2020-08-18 20:10:34 -04:00
|
|
|
def extra_representation_opts
|
|
|
|
{}
|
|
|
|
end
|
|
|
|
|
2015-02-05 13:31:36 -05:00
|
|
|
private
|
|
|
|
|
2020-06-15 11:08:20 -04:00
|
|
|
def sanitized_filter_param
|
2020-11-04 10:08:41 -05:00
|
|
|
@filter ||= sanitize(params[:filter])&.downcase
|
2020-06-15 11:08:20 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def filtered(collection)
|
|
|
|
return collection unless sanitized_filter_param
|
|
|
|
|
2020-11-04 10:08:41 -05:00
|
|
|
collection.select { |item| item[:name].to_s.downcase.include?(sanitized_filter_param) }
|
2020-06-15 11:08:20 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def serialized_provider_repos
|
2020-08-18 20:10:34 -04:00
|
|
|
Import::ProviderRepoSerializer.new(current_user: current_user).represent(importable_repos, provider: provider_name, provider_url: provider_url, **extra_representation_opts)
|
2020-06-15 11:08:20 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def serialized_incompatible_repos
|
2020-08-18 20:10:34 -04:00
|
|
|
Import::ProviderRepoSerializer.new(current_user: current_user).represent(incompatible_repos, provider: provider_name, provider_url: provider_url, **extra_representation_opts)
|
2020-06-15 11:08:20 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def serialized_imported_projects
|
|
|
|
ProjectSerializer.new.represent(already_added_projects, serializer: :import, provider_url: provider_url)
|
|
|
|
end
|
|
|
|
|
|
|
|
def already_added_projects
|
2022-03-31 14:08:39 -04:00
|
|
|
@already_added_projects ||= find_already_added_projects(provider_name)
|
2020-06-15 11:08:20 -04:00
|
|
|
end
|
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2018-05-02 09:35:04 -04:00
|
|
|
def find_already_added_projects(import_type)
|
2020-01-24 07:09:01 -05:00
|
|
|
current_user.created_projects.where(import_type: import_type).with_import_state
|
2018-05-02 09:35:04 -04:00
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2018-05-02 09:35:04 -04:00
|
|
|
|
2019-01-17 05:37:08 -05:00
|
|
|
# deprecated: being replaced by app/services/import/base_service.rb
|
2017-04-05 06:09:31 -04:00
|
|
|
def find_or_create_namespace(names, owner)
|
|
|
|
names = params[:target_namespace].presence || names
|
|
|
|
|
2018-01-26 10:39:10 -05:00
|
|
|
return current_user.namespace if names == owner
|
|
|
|
|
|
|
|
group = Groups::NestedCreateService.new(current_user, group_path: names).execute
|
2017-04-05 06:09:31 -04:00
|
|
|
|
2018-01-26 10:39:10 -05:00
|
|
|
group.errors.any? ? current_user.namespace : group
|
2021-04-26 08:09:44 -04:00
|
|
|
rescue StandardError => e
|
2018-01-26 10:39:10 -05:00
|
|
|
Gitlab::AppLogger.error(e)
|
2017-04-05 06:09:31 -04:00
|
|
|
|
2018-01-26 10:39:10 -05:00
|
|
|
current_user.namespace
|
2015-02-05 13:31:36 -05:00
|
|
|
end
|
2018-06-06 01:34:06 -04:00
|
|
|
|
2019-01-17 05:37:08 -05:00
|
|
|
# deprecated: being replaced by app/services/import/base_service.rb
|
2018-06-06 01:34:06 -04:00
|
|
|
def project_save_error(project)
|
2018-06-06 04:56:50 -04:00
|
|
|
project.errors.full_messages.join(', ')
|
2018-06-06 01:34:06 -04:00
|
|
|
end
|
2015-02-05 13:31:36 -05:00
|
|
|
end
|