2018-09-23 15:44:14 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-12-15 11:31:14 -05:00
|
|
|
class Import::GiteaController < Import::GithubController
|
2018-11-07 11:44:21 -05:00
|
|
|
extend ::Gitlab::Utils::Override
|
|
|
|
|
2020-04-06 20:09:33 -04:00
|
|
|
before_action :verify_blocked_uri, only: :status
|
|
|
|
|
2016-12-15 11:31:14 -05:00
|
|
|
def new
|
2018-11-07 11:44:21 -05:00
|
|
|
if session[access_token_key].present? && provider_url.present?
|
2022-06-22 02:08:52 -04:00
|
|
|
redirect_to status_import_url
|
2016-12-15 11:31:14 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def personal_access_token
|
2016-12-16 11:43:34 -05:00
|
|
|
session[host_key] = params[host_key]
|
2016-12-15 11:31:14 -05:00
|
|
|
super
|
|
|
|
end
|
|
|
|
|
2022-05-04 08:07:48 -04:00
|
|
|
# We need to re-expose controller's internal method 'status' as action.
|
|
|
|
# rubocop:disable Lint/UselessMethodDefinition
|
2016-12-15 11:31:14 -05:00
|
|
|
def status
|
2020-04-06 20:09:33 -04:00
|
|
|
super
|
2016-12-15 11:31:14 -05:00
|
|
|
end
|
2022-05-04 08:07:48 -04:00
|
|
|
# rubocop:enable Lint/UselessMethodDefinition
|
2016-12-15 11:31:14 -05:00
|
|
|
|
2020-07-16 20:09:37 -04:00
|
|
|
protected
|
2016-12-15 11:31:14 -05:00
|
|
|
|
2020-07-16 20:09:37 -04:00
|
|
|
override :provider_name
|
|
|
|
def provider_name
|
|
|
|
:gitea
|
2016-12-16 11:43:34 -05:00
|
|
|
end
|
|
|
|
|
2020-07-16 20:09:37 -04:00
|
|
|
private
|
|
|
|
|
|
|
|
def host_key
|
|
|
|
:"#{provider_name}_host_url"
|
2016-12-15 11:31:14 -05:00
|
|
|
end
|
|
|
|
|
2018-11-07 11:44:21 -05:00
|
|
|
override :provider_url
|
|
|
|
def provider_url
|
|
|
|
session[host_key]
|
|
|
|
end
|
|
|
|
|
2016-12-15 11:31:14 -05:00
|
|
|
# Gitea is not yet an OAuth provider
|
|
|
|
# See https://github.com/go-gitea/gitea/issues/27
|
2018-11-07 11:44:21 -05:00
|
|
|
override :logged_in_with_provider?
|
2016-12-15 11:31:14 -05:00
|
|
|
def logged_in_with_provider?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2018-11-07 11:44:21 -05:00
|
|
|
override :provider_auth
|
2016-12-15 11:31:14 -05:00
|
|
|
def provider_auth
|
2018-11-07 11:44:21 -05:00
|
|
|
if session[access_token_key].blank? || provider_url.blank?
|
2016-12-15 11:31:14 -05:00
|
|
|
redirect_to new_import_gitea_url,
|
2019-03-27 12:52:52 -04:00
|
|
|
alert: _('You need to specify both an Access Token and a Host URL.')
|
2016-12-15 11:31:14 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-08-11 08:09:55 -04:00
|
|
|
override :client_repos
|
|
|
|
def client_repos
|
|
|
|
@client_repos ||= filtered(client.repos)
|
|
|
|
end
|
|
|
|
|
|
|
|
override :client
|
|
|
|
def client
|
2022-04-26 17:08:42 -04:00
|
|
|
@client ||= Gitlab::LegacyGithubImport::Client.new(session[access_token_key], **client_options)
|
2020-08-11 08:09:55 -04:00
|
|
|
end
|
|
|
|
|
2018-11-07 11:44:21 -05:00
|
|
|
override :client_options
|
2016-12-15 11:31:14 -05:00
|
|
|
def client_options
|
2021-09-30 14:11:31 -04:00
|
|
|
verified_url, provider_hostname = verify_blocked_uri
|
|
|
|
|
|
|
|
{ host: verified_url.scheme == 'https' ? provider_url : verified_url.to_s, api_version: 'v1', hostname: provider_hostname }
|
2016-12-15 11:31:14 -05:00
|
|
|
end
|
2020-03-05 10:07:52 -05:00
|
|
|
|
2020-04-06 20:09:33 -04:00
|
|
|
def verify_blocked_uri
|
2021-09-30 14:11:31 -04:00
|
|
|
@verified_url_and_hostname ||= Gitlab::UrlBlocker.validate!(
|
2020-03-05 10:07:52 -05:00
|
|
|
provider_url,
|
2020-12-09 19:09:50 -05:00
|
|
|
allow_localhost: allow_local_requests?,
|
|
|
|
allow_local_network: allow_local_requests?,
|
|
|
|
schemes: %w(http https)
|
2020-03-05 10:07:52 -05:00
|
|
|
)
|
2020-04-06 20:09:33 -04:00
|
|
|
rescue Gitlab::UrlBlocker::BlockedUrlError => e
|
|
|
|
session[access_token_key] = nil
|
|
|
|
|
|
|
|
redirect_to new_import_url, alert: _('Specified URL cannot be used: "%{reason}"') % { reason: e.message }
|
2020-03-05 10:07:52 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def allow_local_requests?
|
|
|
|
Gitlab::CurrentSettings.allow_local_requests_from_web_hooks_and_services?
|
|
|
|
end
|
2016-12-15 11:31:14 -05:00
|
|
|
end
|