2015-08-04 18:21:12 -04:00
|
|
|
class Import::FogbugzController < Import::BaseController
|
|
|
|
before_action :verify_fogbugz_import_enabled
|
|
|
|
before_action :user_map, only: [:new_user_map, :create_user_map]
|
|
|
|
|
|
|
|
rescue_from Fogbugz::AuthenticationException, with: :fogbugz_unauthorized
|
|
|
|
|
|
|
|
def new
|
|
|
|
end
|
|
|
|
|
|
|
|
def callback
|
|
|
|
begin
|
|
|
|
res = Gitlab::FogbugzImport::Client.new(import_params.symbolize_keys)
|
|
|
|
rescue
|
2015-09-15 15:23:30 -04:00
|
|
|
# If the URI is invalid various errors can occur
|
|
|
|
return redirect_to new_import_fogbugz_path, alert: 'Could not connect to FogBugz, check your URL'
|
2015-08-04 18:21:12 -04:00
|
|
|
end
|
|
|
|
session[:fogbugz_token] = res.get_token
|
|
|
|
session[:fogbugz_uri] = params[:uri]
|
|
|
|
|
|
|
|
redirect_to new_user_map_import_fogbugz_path
|
|
|
|
end
|
|
|
|
|
|
|
|
def new_user_map
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_user_map
|
|
|
|
user_map = params[:users]
|
|
|
|
|
|
|
|
unless user_map.is_a?(Hash) && user_map.all? { |k, v| !v[:name].blank? }
|
|
|
|
flash.now[:alert] = 'All users must have a name.'
|
|
|
|
|
2017-02-21 17:31:14 -05:00
|
|
|
return render 'new_user_map'
|
2015-08-04 18:21:12 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
session[:fogbugz_user_map] = user_map
|
|
|
|
|
|
|
|
flash[:notice] = 'The user map has been saved. Continue by selecting the projects you want to import.'
|
|
|
|
|
|
|
|
redirect_to status_import_fogbugz_path
|
|
|
|
end
|
|
|
|
|
|
|
|
def status
|
|
|
|
unless client.valid?
|
|
|
|
return redirect_to new_import_fogbugz_path
|
|
|
|
end
|
|
|
|
|
|
|
|
@repos = client.repos
|
|
|
|
|
2018-05-02 09:35:04 -04:00
|
|
|
@already_added_projects = find_already_added_projects('fogbugz')
|
2015-08-04 18:21:12 -04:00
|
|
|
already_added_projects_names = @already_added_projects.pluck(:import_source)
|
|
|
|
|
|
|
|
@repos.reject! { |repo| already_added_projects_names.include? repo.name }
|
|
|
|
end
|
|
|
|
|
|
|
|
def jobs
|
2018-05-02 09:35:04 -04:00
|
|
|
render json: find_jobs('fogbugz')
|
2015-08-04 18:21:12 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
2018-02-09 06:14:48 -05:00
|
|
|
repo = client.repo(params[:repo_id])
|
2015-08-04 18:21:12 -04:00
|
|
|
fb_session = { uri: session[:fogbugz_uri], token: session[:fogbugz_token] }
|
|
|
|
umap = session[:fogbugz_user_map] || client.user_map
|
|
|
|
|
2018-02-09 06:14:48 -05:00
|
|
|
project = Gitlab::FogbugzImport::ProjectCreator.new(repo, fb_session, current_user.namespace, current_user, umap).execute
|
|
|
|
|
|
|
|
if project.persisted?
|
|
|
|
render json: ProjectSerializer.new.represent(project)
|
|
|
|
else
|
2018-06-06 01:34:06 -04:00
|
|
|
render json: { errors: project_save_error(project) }, status: :unprocessable_entity
|
2018-02-09 06:14:48 -05:00
|
|
|
end
|
2015-08-04 18:21:12 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def client
|
|
|
|
@client ||= Gitlab::FogbugzImport::Client.new(token: session[:fogbugz_token], uri: session[:fogbugz_uri])
|
|
|
|
end
|
|
|
|
|
|
|
|
def user_map
|
|
|
|
@user_map ||= begin
|
|
|
|
user_map = client.user_map
|
|
|
|
|
|
|
|
stored_user_map = session[:fogbugz_user_map]
|
|
|
|
user_map.update(stored_user_map) if stored_user_map
|
|
|
|
|
|
|
|
user_map
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def fogbugz_unauthorized(exception)
|
2015-09-15 15:23:30 -04:00
|
|
|
redirect_to new_import_fogbugz_path, alert: exception.message
|
2015-08-04 18:21:12 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def import_params
|
|
|
|
params.permit(:uri, :email, :password)
|
|
|
|
end
|
|
|
|
|
|
|
|
def verify_fogbugz_import_enabled
|
2015-10-09 13:07:29 -04:00
|
|
|
render_404 unless fogbugz_import_enabled?
|
2015-08-04 18:21:12 -04:00
|
|
|
end
|
|
|
|
end
|