2018-09-23 19:44:14 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-08-04 22:21:12 +00:00
|
|
|
class Import::FogbugzController < Import::BaseController
|
2020-06-15 15:08:20 +00:00
|
|
|
extend ::Gitlab::Utils::Override
|
|
|
|
|
2015-08-04 22:21:12 +00:00
|
|
|
before_action :verify_fogbugz_import_enabled
|
|
|
|
before_action :user_map, only: [:new_user_map, :create_user_map]
|
2020-03-26 18:08:03 +00:00
|
|
|
before_action :verify_blocked_uri, only: :callback
|
2015-08-04 22:21:12 +00:00
|
|
|
|
|
|
|
rescue_from Fogbugz::AuthenticationException, with: :fogbugz_unauthorized
|
|
|
|
|
|
|
|
def new
|
|
|
|
end
|
|
|
|
|
|
|
|
def callback
|
|
|
|
begin
|
2019-06-08 05:54:51 +00:00
|
|
|
res = Gitlab::FogbugzImport::Client.new(import_params.to_h.symbolize_keys)
|
2021-04-26 12:09:44 +00:00
|
|
|
rescue StandardError
|
2015-09-15 19:23:30 +00:00
|
|
|
# If the URI is invalid various errors can occur
|
2022-06-29 12:09:26 +00:00
|
|
|
return redirect_to new_import_fogbugz_path(namespace_id: params[:namespace_id]), alert: _('Could not connect to FogBugz, check your URL')
|
2015-08-04 22:21:12 +00:00
|
|
|
end
|
|
|
|
session[:fogbugz_token] = res.get_token
|
|
|
|
session[:fogbugz_uri] = params[:uri]
|
|
|
|
|
2022-06-28 15:09:17 +00:00
|
|
|
redirect_to new_user_map_import_fogbugz_path(namespace_id: params[:namespace_id])
|
2015-08-04 22:21:12 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def new_user_map
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_user_map
|
2019-06-08 05:54:51 +00:00
|
|
|
user_map = user_map_params.to_h[:users]
|
2015-08-04 22:21:12 +00:00
|
|
|
|
|
|
|
unless user_map.is_a?(Hash) && user_map.all? { |k, v| !v[:name].blank? }
|
2019-03-27 16:52:52 +00:00
|
|
|
flash.now[:alert] = _('All users must have a name.')
|
2015-08-04 22:21:12 +00:00
|
|
|
|
2017-02-21 22:31:14 +00:00
|
|
|
return render 'new_user_map'
|
2015-08-04 22:21:12 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
session[:fogbugz_user_map] = user_map
|
|
|
|
|
2019-03-27 16:52:52 +00:00
|
|
|
flash[:notice] = _('The user map has been saved. Continue by selecting the projects you want to import.')
|
2015-08-04 22:21:12 +00:00
|
|
|
|
2022-06-28 15:09:17 +00:00
|
|
|
redirect_to status_import_fogbugz_path(namespace_id: params[:namespace_id])
|
2015-08-04 22:21:12 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def status
|
|
|
|
unless client.valid?
|
2022-06-28 15:09:17 +00:00
|
|
|
return redirect_to new_import_fogbugz_path(namespace_id: params[:namespace_id])
|
2015-08-04 22:21:12 +00:00
|
|
|
end
|
|
|
|
|
2020-07-17 00:09:37 +00:00
|
|
|
super
|
2015-08-04 22:21:12 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
2022-05-18 21:07:37 +00:00
|
|
|
credentials = { uri: session[:fogbugz_uri], token: session[:fogbugz_token] }
|
|
|
|
|
2015-08-04 22:21:12 +00:00
|
|
|
umap = session[:fogbugz_user_map] || client.user_map
|
|
|
|
|
2022-05-18 21:07:37 +00:00
|
|
|
result = Import::FogbugzService.new(client, current_user, params.merge(umap: umap)).execute(credentials)
|
2018-02-09 11:14:48 +00:00
|
|
|
|
2022-05-18 21:07:37 +00:00
|
|
|
if result[:status] == :success
|
|
|
|
render json: ProjectSerializer.new.represent(result[:project], serializer: :import)
|
2018-02-09 11:14:48 +00:00
|
|
|
else
|
2022-05-18 21:07:37 +00:00
|
|
|
render json: { errors: result[:message] }, status: result[:http_status]
|
2018-02-09 11:14:48 +00:00
|
|
|
end
|
2015-08-04 22:21:12 +00:00
|
|
|
end
|
|
|
|
|
2020-06-15 15:08:20 +00:00
|
|
|
protected
|
|
|
|
|
|
|
|
override :importable_repos
|
|
|
|
def importable_repos
|
2021-08-20 15:10:24 +00:00
|
|
|
client.repos
|
2020-06-15 15:08:20 +00:00
|
|
|
end
|
|
|
|
|
2020-07-17 00:09:37 +00:00
|
|
|
override :incompatible_repos
|
|
|
|
def incompatible_repos
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
|
2020-06-15 15:08:20 +00:00
|
|
|
override :provider_name
|
|
|
|
def provider_name
|
|
|
|
:fogbugz
|
|
|
|
end
|
|
|
|
|
|
|
|
override :provider_url
|
|
|
|
def provider_url
|
|
|
|
session[:fogbugz_uri]
|
|
|
|
end
|
|
|
|
|
2015-08-04 22:21:12 +00:00
|
|
|
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)
|
2022-06-28 15:09:17 +00:00
|
|
|
redirect_to new_import_fogbugz_path(namespace_id: params[:namespace_id]), alert: exception.message
|
2015-08-04 22:21:12 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def import_params
|
|
|
|
params.permit(:uri, :email, :password)
|
|
|
|
end
|
|
|
|
|
2019-06-08 05:54:51 +00:00
|
|
|
def user_map_params
|
|
|
|
params.permit(users: %w(name email gitlab_user))
|
|
|
|
end
|
|
|
|
|
2015-08-04 22:21:12 +00:00
|
|
|
def verify_fogbugz_import_enabled
|
2015-10-09 17:07:29 +00:00
|
|
|
render_404 unless fogbugz_import_enabled?
|
2015-08-04 22:21:12 +00:00
|
|
|
end
|
2020-03-26 18:08:03 +00:00
|
|
|
|
|
|
|
def verify_blocked_uri
|
|
|
|
Gitlab::UrlBlocker.validate!(
|
|
|
|
params[:uri],
|
2020-12-10 00:09:50 +00:00
|
|
|
allow_localhost: allow_local_requests?,
|
|
|
|
allow_local_network: allow_local_requests?,
|
|
|
|
schemes: %w(http https)
|
2020-03-26 18:08:03 +00:00
|
|
|
)
|
|
|
|
rescue Gitlab::UrlBlocker::BlockedUrlError => e
|
|
|
|
redirect_to new_import_fogbugz_url, alert: _('Specified URL cannot be used: "%{reason}"') % { reason: e.message }
|
|
|
|
end
|
|
|
|
|
|
|
|
def allow_local_requests?
|
|
|
|
Gitlab::CurrentSettings.allow_local_requests_from_web_hooks_and_services?
|
|
|
|
end
|
2015-08-04 22:21:12 +00:00
|
|
|
end
|