2018-12-05 23:21:45 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-04-26 12:48:37 +05:30
|
|
|
class Import::GoogleCodeController < Import::BaseController
|
2015-09-25 12:07:36 +05:30
|
|
|
before_action :verify_google_code_import_enabled
|
2015-09-11 14:41:01 +05:30
|
|
|
before_action :user_map, only: [:new_user_map, :create_user_map]
|
2015-04-26 12:48:37 +05:30
|
|
|
|
|
|
|
def new
|
|
|
|
end
|
|
|
|
|
|
|
|
def callback
|
|
|
|
dump_file = params[:dump_file]
|
|
|
|
|
|
|
|
unless dump_file.respond_to?(:read)
|
2019-05-18 00:54:41 +05:30
|
|
|
return redirect_back_or_default(options: { alert: _("You need to upload a Google Takeout archive.") })
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
begin
|
|
|
|
dump = JSON.parse(dump_file.read)
|
|
|
|
rescue
|
2019-05-18 00:54:41 +05:30
|
|
|
return redirect_back_or_default(options: { alert: _("The uploaded file is not a valid Google Takeout archive.") })
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
client = Gitlab::GoogleCodeImport::Client.new(dump)
|
|
|
|
unless client.valid?
|
2019-05-18 00:54:41 +05:30
|
|
|
return redirect_back_or_default(options: { alert: _("The uploaded file is not a valid Google Takeout archive.") })
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
session[:google_code_dump] = dump
|
|
|
|
|
|
|
|
if params[:create_user_map] == "1"
|
|
|
|
redirect_to new_user_map_import_google_code_path
|
|
|
|
else
|
|
|
|
redirect_to status_import_google_code_path
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def new_user_map
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_user_map
|
|
|
|
user_map_json = params[:user_map]
|
|
|
|
user_map_json = "{}" if user_map_json.blank?
|
|
|
|
|
|
|
|
begin
|
|
|
|
user_map = JSON.parse(user_map_json)
|
|
|
|
rescue
|
2019-05-18 00:54:41 +05:30
|
|
|
flash.now[:alert] = _("The entered user map is not a valid JSON user map.")
|
2015-04-26 12:48:37 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
return render "new_user_map"
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
unless user_map.is_a?(Hash) && user_map.all? { |k, v| k.is_a?(String) && v.is_a?(String) }
|
2019-05-18 00:54:41 +05:30
|
|
|
flash.now[:alert] = _("The entered user map is not a valid JSON user map.")
|
2015-04-26 12:48:37 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
return render "new_user_map"
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
# This is the default, so let's not save it into the database.
|
|
|
|
user_map.reject! do |key, value|
|
|
|
|
value == Gitlab::GoogleCodeImport::Client.mask_email(key)
|
|
|
|
end
|
|
|
|
|
|
|
|
session[:google_code_user_map] = user_map
|
|
|
|
|
2019-05-18 00:54:41 +05:30
|
|
|
flash[:notice] = _("The user map has been saved. Continue by selecting the projects you want to import.")
|
2015-04-26 12:48:37 +05:30
|
|
|
|
|
|
|
redirect_to status_import_google_code_path
|
|
|
|
end
|
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2015-04-26 12:48:37 +05:30
|
|
|
def status
|
|
|
|
unless client.valid?
|
2015-09-11 14:41:01 +05:30
|
|
|
return redirect_to new_import_google_code_path
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
@repos = client.repos
|
2015-09-11 14:41:01 +05:30
|
|
|
@incompatible_repos = client.incompatible_repos
|
2015-04-26 12:48:37 +05:30
|
|
|
|
2018-10-15 14:42:47 +05:30
|
|
|
@already_added_projects = find_already_added_projects('google_code')
|
2015-04-26 12:48:37 +05:30
|
|
|
already_added_projects_names = @already_added_projects.pluck(:import_source)
|
|
|
|
|
|
|
|
@repos.reject! { |repo| already_added_projects_names.include? repo.name }
|
|
|
|
end
|
2018-12-05 23:21:45 +05:30
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2015-04-26 12:48:37 +05:30
|
|
|
|
|
|
|
def jobs
|
2018-10-15 14:42:47 +05:30
|
|
|
render json: find_jobs('google_code')
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
2018-03-27 19:54:05 +05:30
|
|
|
repo = client.repo(params[:repo_id])
|
2015-04-26 12:48:37 +05:30
|
|
|
user_map = session[:google_code_user_map]
|
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
project = Gitlab::GoogleCodeImport::ProjectCreator.new(repo, current_user.namespace, current_user, user_map).execute
|
|
|
|
|
|
|
|
if project.persisted?
|
|
|
|
render json: ProjectSerializer.new.represent(project)
|
|
|
|
else
|
2018-11-08 19:23:39 +05:30
|
|
|
render json: { errors: project_save_error(project) }, status: :unprocessable_entity
|
2018-03-27 19:54:05 +05:30
|
|
|
end
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def client
|
|
|
|
@client ||= Gitlab::GoogleCodeImport::Client.new(session[:google_code_dump])
|
|
|
|
end
|
|
|
|
|
2015-09-25 12:07:36 +05:30
|
|
|
def verify_google_code_import_enabled
|
2015-10-24 18:46:33 +05:30
|
|
|
render_404 unless google_code_import_enabled?
|
2015-09-25 12:07:36 +05:30
|
|
|
end
|
|
|
|
|
2015-04-26 12:48:37 +05:30
|
|
|
def user_map
|
|
|
|
@user_map ||= begin
|
|
|
|
user_map = client.user_map
|
|
|
|
|
|
|
|
stored_user_map = session[:google_code_user_map]
|
|
|
|
user_map.update(stored_user_map) if stored_user_map
|
|
|
|
|
|
|
|
Hash[user_map.sort]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|