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

151 lines
3.8 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
2019-07-07 11:18:12 +05:30
return redirect_to new_import_fogbugz_path, alert: _('Could not connect to FogBugz, check your URL')
2015-09-25 12:07:36 +05:30
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
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
redirect_to status_import_fogbugz_path
end
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2015-09-25 12:07:36 +05:30
def status
unless client.valid?
return redirect_to new_import_fogbugz_path
end
2020-07-28 23:09:34 +05:30
super
2015-09-25 12:07:36 +05:30
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2015-09-25 12:07:36 +05:30
2020-06-23 00:09:42 +05:30
def realtime_changes
super
end
2015-09-25 12:07:36 +05:30
def create
2018-03-27 19:54:05 +05:30
repo = client.repo(params[:repo_id])
2015-09-25 12:07:36 +05:30
fb_session = { uri: session[:fogbugz_uri], token: session[:fogbugz_token] }
umap = session[:fogbugz_user_map] || client.user_map
2018-03-27 19:54:05 +05:30
project = Gitlab::FogbugzImport::ProjectCreator.new(repo, fb_session, current_user.namespace, current_user, umap).execute
if project.persisted?
2020-06-23 00:09:42 +05:30
render json: ProjectSerializer.new.represent(project, serializer: :import)
2018-03-27 19:54:05 +05:30
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-09-25 12:07:36 +05:30
end
2020-06-23 00:09:42 +05:30
protected
# rubocop: disable CodeReuse/ActiveRecord
override :importable_repos
def importable_repos
repos = client.repos
already_added_projects_names = already_added_projects.map(&:import_source)
repos.reject { |repo| already_added_projects_names.include? repo.name }
end
# rubocop: enable CodeReuse/ActiveRecord
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)
redirect_to new_import_fogbugz_path, alert: exception.message
end
def import_params
params.permit(:uri, :email, :password)
end
2019-07-31 22:56:46 +05:30
def user_map_params
params.permit(users: %w(name email gitlab_user))
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?,
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