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

112 lines
2.9 KiB
Ruby
Raw Normal View History

2018-12-05 23:21:45 +05:30
# frozen_string_literal: true
2017-08-17 22:00:37 +05:30
class Import::GiteaController < Import::GithubController
2019-07-07 11:18:12 +05:30
extend ::Gitlab::Utils::Override
2020-04-22 19:07:51 +05:30
before_action :verify_blocked_uri, only: :status
2017-08-17 22:00:37 +05:30
def new
2019-07-07 11:18:12 +05:30
if session[access_token_key].present? && provider_url.present?
2022-08-13 15:12:31 +05:30
redirect_to status_import_url
2017-08-17 22:00:37 +05:30
end
end
def personal_access_token
session[host_key] = params[host_key]
super
end
def status
2023-03-04 22:38:38 +05:30
# Request repos to display error page if provider token is invalid
# Improving in https://gitlab.com/gitlab-org/gitlab/-/issues/25859
client_repos
respond_to do |format|
format.json do
render json: { imported_projects: serialized_imported_projects,
provider_repos: serialized_provider_repos,
incompatible_repos: serialized_incompatible_repos }
end
format.html do
if params[:namespace_id].present?
@namespace = Namespace.find_by_id(params[:namespace_id])
2023-05-27 22:25:52 +05:30
render_404 unless current_user.can?(:import_projects, @namespace)
2023-03-04 22:38:38 +05:30
end
end
end
2017-08-17 22:00:37 +05:30
end
2020-07-28 23:09:34 +05:30
protected
2017-08-17 22:00:37 +05:30
2020-07-28 23:09:34 +05:30
override :provider_name
def provider_name
:gitea
2017-08-17 22:00:37 +05:30
end
2020-07-28 23:09:34 +05:30
private
def host_key
:"#{provider_name}_host_url"
2017-08-17 22:00:37 +05:30
end
2019-07-07 11:18:12 +05:30
override :provider_url
def provider_url
session[host_key]
end
2017-08-17 22:00:37 +05:30
# Gitea is not yet an OAuth provider
# See https://github.com/go-gitea/gitea/issues/27
2019-07-07 11:18:12 +05:30
override :logged_in_with_provider?
2017-08-17 22:00:37 +05:30
def logged_in_with_provider?
false
end
2019-07-07 11:18:12 +05:30
override :provider_auth
2017-08-17 22:00:37 +05:30
def provider_auth
2019-07-07 11:18:12 +05:30
if session[access_token_key].blank? || provider_url.blank?
2017-08-17 22:00:37 +05:30
redirect_to new_import_gitea_url,
2019-07-07 11:18:12 +05:30
alert: _('You need to specify both an Access Token and a Host URL.')
2017-08-17 22:00:37 +05:30
end
end
2023-05-27 22:25:52 +05:30
override :serialized_imported_projects
def serialized_imported_projects(projects = already_added_projects)
ProjectSerializer.new.represent(projects, serializer: :import, provider_url: provider_url)
end
2017-08-17 22:00:37 +05:30
2020-10-24 23:57:45 +05:30
override :client_repos
def client_repos
@client_repos ||= filtered(client.repos)
end
def client
2022-07-16 23:28:13 +05:30
@client ||= Gitlab::LegacyGithubImport::Client.new(session[access_token_key], **client_options)
2020-10-24 23:57:45 +05:30
end
2019-07-07 11:18:12 +05:30
override :client_options
2017-08-17 22:00:37 +05:30
def client_options
2021-09-30 23:02:18 +05:30
verified_url, provider_hostname = verify_blocked_uri
{ host: verified_url.scheme == 'https' ? provider_url : verified_url.to_s, api_version: 'v1', hostname: provider_hostname }
2017-08-17 22:00:37 +05:30
end
2020-04-08 14:13:33 +05:30
2020-04-22 19:07:51 +05:30
def verify_blocked_uri
2021-09-30 23:02:18 +05:30
@verified_url_and_hostname ||= Gitlab::UrlBlocker.validate!(
2020-04-08 14:13:33 +05:30
provider_url,
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-04-08 14:13:33 +05:30
)
2020-04-22 19:07:51 +05:30
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-04-08 14:13:33 +05:30
end
def allow_local_requests?
Gitlab::CurrentSettings.allow_local_requests_from_web_hooks_and_services?
end
2017-08-17 22:00:37 +05:30
end