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

133 lines
3.5 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
2020-03-13 15:44:24 +05:30
before_action :import_rate_limit, only: [:create]
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,
incompatible_repos: serialized_incompatible_repos,
namespaces: serialized_namespaces }
end
format.html
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
[]
end
def provider_name
raise NotImplementedError
end
def provider_url
raise NotImplementedError
end
2015-04-26 12:48:37 +05:30
private
2020-06-23 00:09:42 +05:30
def filter_attribute
:name
end
def sanitized_filter_param
@filter ||= sanitize(params[:filter])
end
def filtered(collection)
return collection unless sanitized_filter_param
collection.select { |item| item[filter_attribute].include?(sanitized_filter_param) }
end
def serialized_provider_repos
Import::ProviderRepoSerializer.new(current_user: current_user).represent(importable_repos, provider: provider_name, provider_url: provider_url)
end
def serialized_incompatible_repos
Import::ProviderRepoSerializer.new(current_user: current_user).represent(incompatible_repos, provider: provider_name, provider_url: provider_url)
end
def serialized_imported_projects
ProjectSerializer.new.represent(already_added_projects, serializer: :import, provider_url: provider_url)
end
def already_added_projects
@already_added_projects ||= filtered(find_already_added_projects(provider_name))
end
def serialized_namespaces
NamespaceSerializer.new.represent(namespaces)
end
def namespaces
current_user.manageable_groups_with_routes
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
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2018-10-15 14:42:47 +05:30
def find_jobs(import_type)
current_user.created_projects
2020-03-13 15:44:24 +05:30
.with_import_state
2018-11-08 19:23:39 +05:30
.where(import_type: import_type)
.to_json(only: [:id], methods: [:import_status])
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
rescue => e
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
2020-03-13 15:44:24 +05:30
def import_rate_limit
key = "project_import".to_sym
if rate_limiter.throttled?(key, scope: [current_user, key])
rate_limiter.log_request(request, "#{key}_request_limit".to_sym, current_user)
redirect_back_or_default(options: { alert: _('This endpoint has been requested too many times. Try again later.') })
end
end
def rate_limiter
::Gitlab::ApplicationRateLimiter
end
2015-04-26 12:48:37 +05:30
end