2015-04-03 09:29:27 -04:00
|
|
|
class Import::GoogleCodeController < Import::BaseController
|
2015-08-14 09:56:33 -04:00
|
|
|
before_action :verify_google_code_import_enabled
|
2015-04-16 08:03:37 -04:00
|
|
|
before_action :user_map, only: [:new_user_map, :create_user_map]
|
2015-04-03 09:29:27 -04:00
|
|
|
|
|
|
|
def new
|
|
|
|
end
|
|
|
|
|
|
|
|
def callback
|
|
|
|
dump_file = params[:dump_file]
|
|
|
|
|
|
|
|
unless dump_file.respond_to?(:read)
|
2015-10-20 03:28:28 -04:00
|
|
|
return redirect_back_or_default(options: { alert: "You need to upload a Google Takeout archive." })
|
2015-04-03 09:29:27 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
begin
|
|
|
|
dump = JSON.parse(dump_file.read)
|
|
|
|
rescue
|
2015-10-20 03:28:28 -04:00
|
|
|
return redirect_back_or_default(options: { alert: "The uploaded file is not a valid Google Takeout archive." })
|
2015-04-03 09:29:27 -04:00
|
|
|
end
|
|
|
|
|
2015-04-14 08:50:56 -04:00
|
|
|
client = Gitlab::GoogleCodeImport::Client.new(dump)
|
|
|
|
unless client.valid?
|
2015-10-20 03:28:28 -04:00
|
|
|
return redirect_back_or_default(options: { alert: "The uploaded file is not a valid Google Takeout archive." })
|
2015-04-03 09:29:27 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
session[:google_code_dump] = dump
|
2015-04-14 08:50:56 -04:00
|
|
|
|
|
|
|
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
|
|
|
|
flash.now[:alert] = "The entered user map is not a valid JSON user map."
|
|
|
|
|
|
|
|
render "new_user_map" and return
|
|
|
|
end
|
|
|
|
|
|
|
|
unless user_map.is_a?(Hash) && user_map.all? { |k, v| k.is_a?(String) && v.is_a?(String) }
|
|
|
|
flash.now[:alert] = "The entered user map is not a valid JSON user map."
|
|
|
|
|
|
|
|
render "new_user_map" and return
|
|
|
|
end
|
|
|
|
|
2015-04-17 07:32:29 -04:00
|
|
|
# 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
|
|
|
|
|
2015-04-14 08:50:56 -04:00
|
|
|
session[:google_code_user_map] = user_map
|
|
|
|
|
|
|
|
flash[:notice] = "The user map has been saved. Continue by selecting the projects you want to import."
|
|
|
|
|
2015-04-03 09:29:27 -04:00
|
|
|
redirect_to status_import_google_code_path
|
|
|
|
end
|
|
|
|
|
|
|
|
def status
|
|
|
|
unless client.valid?
|
2015-04-21 08:36:58 -04:00
|
|
|
return redirect_to new_import_google_code_path
|
2015-04-03 09:29:27 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
@repos = client.repos
|
2015-04-29 03:33:49 -04:00
|
|
|
@incompatible_repos = client.incompatible_repos
|
2015-04-03 09:29:27 -04:00
|
|
|
|
|
|
|
@already_added_projects = current_user.created_projects.where(import_type: "google_code")
|
|
|
|
already_added_projects_names = @already_added_projects.pluck(:import_source)
|
|
|
|
|
|
|
|
@repos.reject! { |repo| already_added_projects_names.include? repo.name }
|
|
|
|
end
|
|
|
|
|
|
|
|
def jobs
|
|
|
|
jobs = current_user.created_projects.where(import_type: "google_code").to_json(only: [:id, :import_status])
|
|
|
|
render json: jobs
|
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
|
|
|
@repo_id = params[:repo_id]
|
|
|
|
repo = client.repo(@repo_id)
|
|
|
|
@target_namespace = current_user.namespace
|
|
|
|
@project_name = repo.name
|
|
|
|
|
|
|
|
namespace = @target_namespace
|
|
|
|
|
2015-04-14 08:50:56 -04:00
|
|
|
user_map = session[:google_code_user_map]
|
|
|
|
|
|
|
|
@project = Gitlab::GoogleCodeImport::ProjectCreator.new(repo, namespace, current_user, user_map).execute
|
2015-04-03 09:29:27 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def client
|
|
|
|
@client ||= Gitlab::GoogleCodeImport::Client.new(session[:google_code_dump])
|
|
|
|
end
|
|
|
|
|
2015-08-14 09:56:33 -04:00
|
|
|
def verify_google_code_import_enabled
|
2015-10-09 13:07:29 -04:00
|
|
|
render_404 unless google_code_import_enabled?
|
2015-08-14 09:56:33 -04:00
|
|
|
end
|
|
|
|
|
2015-04-14 08:50:56 -04:00
|
|
|
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
|
2015-04-03 09:29:27 -04:00
|
|
|
end
|