debian-mirror-gitlab/app/controllers/import/base_controller.rb

109 lines
3.1 KiB
Ruby
Raw Normal View History

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