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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

288 lines
7.7 KiB
Ruby
Raw Normal View History

2018-12-05 23:21:45 +05:30
# frozen_string_literal: true
2015-04-26 12:48:37 +05:30
class Import::GithubController < Import::BaseController
2020-07-28 23:09:34 +05:30
extend ::Gitlab::Utils::Override
2019-07-07 11:18:12 +05:30
include ImportHelper
2019-12-21 20:55:43 +05:30
include ActionView::Helpers::SanitizeHelper
2019-07-07 11:18:12 +05:30
2017-08-17 22:00:37 +05:30
before_action :verify_import_enabled
2019-07-07 11:18:12 +05:30
before_action :provider_auth, only: [:status, :realtime_changes, :create]
before_action :expire_etag_cache, only: [:status, :create]
2015-04-26 12:48:37 +05:30
2020-10-24 23:57:45 +05:30
OAuthConfigMissingError = Class.new(StandardError)
rescue_from OAuthConfigMissingError, with: :missing_oauth_config
2017-08-17 22:00:37 +05:30
rescue_from Octokit::Unauthorized, with: :provider_unauthorized
2020-04-22 19:07:51 +05:30
rescue_from Octokit::TooManyRequests, with: :provider_rate_limit
2021-01-29 00:20:46 +05:30
rescue_from Gitlab::GithubImport::RateLimitError, with: :rate_limit_threshold_exceeded
2016-08-24 12:49:21 +05:30
2021-02-22 17:27:13 +05:30
PAGE_LENGTH = 25
2016-08-24 12:49:21 +05:30
def new
2019-10-12 21:52:04 +05:30
if !ci_cd_only? && github_import_configured? && logged_in_with_provider?
2017-08-17 22:00:37 +05:30
go_to_provider_for_permissions
elsif session[access_token_key]
2022-06-21 17:19:12 +05:30
redirect_to status_import_url(namespace_id: params[:namespace_id])
2016-08-24 12:49:21 +05:30
end
end
2015-04-26 12:48:37 +05:30
def callback
2022-06-21 17:19:12 +05:30
auth_state = session.delete(auth_state_key)
namespace_id = session.delete(:namespace_id)
2022-01-12 12:59:36 +05:30
if auth_state.blank? || !ActiveSupport::SecurityUtils.secure_compare(auth_state, params[:state])
provider_unauthorized
else
session[access_token_key] = get_token(params[:code])
2022-06-21 17:19:12 +05:30
redirect_to status_import_url(namespace_id: namespace_id)
2022-01-12 12:59:36 +05:30
end
2015-04-26 12:48:37 +05:30
end
2016-08-24 12:49:21 +05:30
def personal_access_token
2018-12-13 13:39:08 +05:30
session[access_token_key] = params[:personal_access_token]&.strip
2022-06-21 17:19:12 +05:30
redirect_to status_import_url(namespace_id: params[:namespace_id].presence)
2016-08-24 12:49:21 +05:30
end
2015-04-26 12:48:37 +05:30
def status
2019-07-07 11:18:12 +05:30
# Request repos to display error page if provider token is invalid
2019-12-04 20:38:33 +05:30
# Improving in https://gitlab.com/gitlab-org/gitlab-foss/issues/55585
2019-07-07 11:18:12 +05:30
client_repos
2020-07-28 23:09:34 +05:30
super
2015-04-26 12:48:37 +05:30
end
def create
2020-07-28 23:09:34 +05:30
result = Import::GithubService.new(client, current_user, import_params).execute(access_params, provider_name)
2019-03-02 22:35:43 +05:30
if result[:status] == :success
2019-07-07 11:18:12 +05:30
render json: serialized_imported_projects(result[:project])
2016-09-29 09:46:39 +05:30
else
2019-03-02 22:35:43 +05:30
render json: { errors: result[:message] }, status: result[:http_status]
2016-09-29 09:46:39 +05:30
end
2015-04-26 12:48:37 +05:30
end
2019-07-07 11:18:12 +05:30
def realtime_changes
2022-06-21 17:19:12 +05:30
Gitlab::PollingInterval.set_header(response, interval: 3_000)
render json: already_added_projects.map { |project|
{
id: project.id,
import_status: project.import_status,
stats: ::Gitlab::GithubImport::ObjectCounter.summary(project)
}
}
2019-07-07 11:18:12 +05:30
end
2020-07-28 23:09:34 +05:30
protected
2015-04-26 12:48:37 +05:30
2020-07-28 23:09:34 +05:30
override :importable_repos
def importable_repos
2021-11-11 11:23:49 +05:30
client_repos.to_a
2019-03-02 22:35:43 +05:30
end
2020-07-28 23:09:34 +05:30
override :incompatible_repos
def incompatible_repos
[]
2019-07-07 11:18:12 +05:30
end
2020-07-28 23:09:34 +05:30
override :provider_name
def provider_name
:github
2019-07-07 11:18:12 +05:30
end
2020-07-28 23:09:34 +05:30
override :provider_url
def provider_url
strong_memoize(:provider_url) do
2020-10-24 23:57:45 +05:30
oauth_config&.dig('url').presence || 'https://github.com'
2020-07-28 23:09:34 +05:30
end
2019-07-07 11:18:12 +05:30
end
2020-07-28 23:09:34 +05:30
private
def import_params
params.permit(permitted_import_params)
2019-07-07 11:18:12 +05:30
end
2020-07-28 23:09:34 +05:30
def permitted_import_params
[:repo_id, :new_name, :target_namespace]
2019-07-07 11:18:12 +05:30
end
2020-07-28 23:09:34 +05:30
def serialized_imported_projects(projects = already_added_projects)
ProjectSerializer.new.represent(projects, serializer: :import, provider_url: provider_url)
2019-07-07 11:18:12 +05:30
end
def expire_etag_cache
Gitlab::EtagCaching::Store.new.tap do |store|
store.touch(realtime_changes_path)
end
end
2015-04-26 12:48:37 +05:30
def client
2020-10-24 23:57:45 +05:30
@client ||= if Feature.enabled?(:remove_legacy_github_client)
Gitlab::GithubImport::Client.new(session[access_token_key])
else
2021-01-03 14:25:43 +05:30
Gitlab::LegacyGithubImport::Client.new(session[access_token_key], **client_options)
2020-10-24 23:57:45 +05:30
end
2015-04-26 12:48:37 +05:30
end
2019-07-07 11:18:12 +05:30
def client_repos
2020-10-24 23:57:45 +05:30
@client_repos ||= if Feature.enabled?(:remove_legacy_github_client)
2021-02-22 17:27:13 +05:30
if sanitized_filter_param
client.search_repos_by_name(sanitized_filter_param, pagination_options)[:items]
else
client.octokit.repos(nil, pagination_options)
end
2020-10-24 23:57:45 +05:30
else
filtered(client.repos)
end
end
2021-01-29 00:20:46 +05:30
def sanitized_filter_param
super
@filter = @filter&.tr(' ', '')&.tr(':', '')
2020-10-24 23:57:45 +05:30
end
def oauth_client
raise OAuthConfigMissingError unless oauth_config
@oauth_client ||= ::OAuth2::Client.new(
oauth_config.app_id,
oauth_config.app_secret,
oauth_options.merge(ssl: { verify: oauth_config['verify_ssl'] })
)
end
def oauth_config
@oauth_config ||= Gitlab::Auth::OAuth::Provider.config_for('github')
end
def oauth_options
if oauth_config
oauth_config.dig('args', 'client_options').deep_symbolize_keys
else
OmniAuth::Strategies::GitHub.default_options[:client_options].symbolize_keys
end
end
def authorize_url
2022-01-12 12:59:36 +05:30
state = SecureRandom.base64(64)
session[auth_state_key] = state
2020-10-24 23:57:45 +05:30
if Feature.enabled?(:remove_legacy_github_client)
oauth_client.auth_code.authorize_url(
redirect_uri: callback_import_url,
2022-01-12 12:59:36 +05:30
scope: 'repo, user, user:email',
state: state
2020-10-24 23:57:45 +05:30
)
else
2022-01-12 12:59:36 +05:30
client.authorize_url(callback_import_url, state)
2020-10-24 23:57:45 +05:30
end
end
def get_token(code)
if Feature.enabled?(:remove_legacy_github_client)
oauth_client.auth_code.get_token(code).token
else
client.get_token(code)
end
2019-07-07 11:18:12 +05:30
end
2017-08-17 22:00:37 +05:30
def verify_import_enabled
render_404 unless import_enabled?
2015-04-26 12:48:37 +05:30
end
2017-08-17 22:00:37 +05:30
def go_to_provider_for_permissions
2020-10-24 23:57:45 +05:30
redirect_to authorize_url
2015-04-26 12:48:37 +05:30
end
2017-08-17 22:00:37 +05:30
def import_enabled?
2020-07-28 23:09:34 +05:30
__send__("#{provider_name}_import_enabled?") # rubocop:disable GitlabSecurity/PublicSend
2015-04-26 12:48:37 +05:30
end
2019-07-07 11:18:12 +05:30
def realtime_changes_path
2020-07-28 23:09:34 +05:30
public_send("realtime_changes_import_#{provider_name}_path", format: :json) # rubocop:disable GitlabSecurity/PublicSend
2019-07-07 11:18:12 +05:30
end
2017-08-17 22:00:37 +05:30
def new_import_url
2020-07-28 23:09:34 +05:30
public_send("new_import_#{provider_name}_url", extra_import_params) # rubocop:disable GitlabSecurity/PublicSend
2015-04-26 12:48:37 +05:30
end
2015-09-25 12:07:36 +05:30
2022-06-21 17:19:12 +05:30
def status_import_url(namespace_id: nil)
public_send("status_import_#{provider_name}_url", extra_import_params.merge({ namespace_id: namespace_id })) # rubocop:disable GitlabSecurity/PublicSend
2017-08-17 22:00:37 +05:30
end
def callback_import_url
2020-07-28 23:09:34 +05:30
public_send("users_import_#{provider_name}_callback_url", extra_import_params) # rubocop:disable GitlabSecurity/PublicSend
2017-08-17 22:00:37 +05:30
end
def provider_unauthorized
session[access_token_key] = nil
redirect_to new_import_url,
2020-07-28 23:09:34 +05:30
alert: "Access denied to your #{Gitlab::ImportSources.title(provider_name.to_s)} account."
2017-08-17 22:00:37 +05:30
end
2020-04-22 19:07:51 +05:30
def provider_rate_limit(exception)
2020-05-24 23:13:21 +05:30
reset_time = Time.zone.at(exception.response_headers['x-ratelimit-reset'].to_i)
2020-04-22 19:07:51 +05:30
session[access_token_key] = nil
redirect_to new_import_url,
alert: _("GitHub API rate limit exceeded. Try again after %{reset_time}") % { reset_time: reset_time }
end
2020-10-24 23:57:45 +05:30
def missing_oauth_config
session[access_token_key] = nil
redirect_to new_import_url,
alert: _('Missing OAuth configuration for GitHub.')
end
2022-01-12 12:59:36 +05:30
def auth_state_key
:"#{provider_name}_auth_state_key"
end
2017-08-17 22:00:37 +05:30
def access_token_key
2020-07-28 23:09:34 +05:30
:"#{provider_name}_access_token"
2016-08-24 12:49:21 +05:30
end
2015-09-25 12:07:36 +05:30
def access_params
2017-08-17 22:00:37 +05:30
{ github_access_token: session[access_token_key] }
end
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2017-08-17 22:00:37 +05:30
def logged_in_with_provider?
2020-07-28 23:09:34 +05:30
current_user.identities.exists?(provider: provider_name)
2017-08-17 22:00:37 +05:30
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2017-08-17 22:00:37 +05:30
def provider_auth
2019-10-12 21:52:04 +05:30
if !ci_cd_only? && session[access_token_key].blank?
2022-06-21 17:19:12 +05:30
session[:namespace_id] = params[:namespace_id]
2017-08-17 22:00:37 +05:30
go_to_provider_for_permissions
end
end
2019-10-12 21:52:04 +05:30
def ci_cd_only?
%w[1 true].include?(params[:ci_cd_only])
end
2017-08-17 22:00:37 +05:30
def client_options
2020-04-22 19:07:51 +05:30
{ wait_for_rate_limit_reset: false }
2015-09-25 12:07:36 +05:30
end
2018-03-27 19:54:05 +05:30
def extra_import_params
{}
end
2019-12-21 20:55:43 +05:30
2021-01-29 00:20:46 +05:30
def rate_limit_threshold_exceeded
head :too_many_requests
2019-12-21 20:55:43 +05:30
end
2021-02-22 17:27:13 +05:30
def pagination_options
{
page: [1, params[:page].to_i].max,
per_page: PAGE_LENGTH
}
end
2015-04-26 12:48:37 +05:30
end
2019-12-04 20:38:33 +05:30
2021-06-08 01:23:25 +05:30
Import::GithubController.prepend_mod_with('Import::GithubController')