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

139 lines
3.5 KiB
Ruby
Raw Normal View History

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