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

214 lines
5.8 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
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
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
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]
redirect_to status_import_url
2016-08-24 12:49:21 +05:30
end
end
2015-04-26 12:48:37 +05:30
def callback
2017-08-17 22:00:37 +05:30
session[access_token_key] = client.get_token(params[:code])
redirect_to status_import_url
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
2017-08-17 22:00:37 +05:30
redirect_to status_import_url
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
respond_to do |format|
format.json do
render json: { imported_projects: serialized_imported_projects,
provider_repos: serialized_provider_repos,
namespaces: serialized_namespaces }
end
format.html
end
2015-04-26 12:48:37 +05:30
end
def create
2019-03-02 22:35:43 +05:30
result = Import::GithubService.new(client, current_user, import_params).execute(access_params, provider)
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
Gitlab::PollingInterval.set_header(response, interval: 3_000)
2019-12-21 20:55:43 +05:30
render json: already_added_projects.to_json(only: [:id], methods: [:import_status])
2019-07-07 11:18:12 +05:30
end
2015-04-26 12:48:37 +05:30
private
2019-03-02 22:35:43 +05:30
def import_params
params.permit(permitted_import_params)
end
def permitted_import_params
[:repo_id, :new_name, :target_namespace]
end
2019-07-07 11:18:12 +05:30
def serialized_imported_projects(projects = already_added_projects)
ProjectSerializer.new.represent(projects, serializer: :import, provider_url: provider_url)
end
def serialized_provider_repos
repos = client_repos.reject { |repo| already_added_project_names.include? repo.full_name }
2020-06-23 00:09:42 +05:30
Import::ProviderRepoSerializer.new(current_user: current_user).represent(repos, provider: provider, provider_url: provider_url)
2019-07-07 11:18:12 +05:30
end
def serialized_namespaces
NamespaceSerializer.new.represent(namespaces)
end
def already_added_projects
2019-12-21 20:55:43 +05:30
@already_added_projects ||= filtered(find_already_added_projects(provider))
2019-07-07 11:18:12 +05:30
end
def already_added_project_names
@already_added_projects_names ||= already_added_projects.pluck(:import_source) # rubocop:disable CodeReuse/ActiveRecord
end
def namespaces
current_user.manageable_groups_with_routes
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
2018-03-17 18:26:18 +05:30
@client ||= Gitlab::LegacyGithubImport::Client.new(session[access_token_key], client_options)
2015-04-26 12:48:37 +05:30
end
2019-07-07 11:18:12 +05:30
def client_repos
2019-12-21 20:55:43 +05:30
@client_repos ||= filtered(client.repos)
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
redirect_to client.authorize_url(callback_import_url)
2015-04-26 12:48:37 +05:30
end
2017-08-17 22:00:37 +05:30
def import_enabled?
2018-03-17 18:26:18 +05:30
__send__("#{provider}_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
public_send("realtime_changes_import_#{provider}_path", format: :json) # rubocop:disable GitlabSecurity/PublicSend
end
2017-08-17 22:00:37 +05:30
def new_import_url
2018-03-27 19:54:05 +05:30
public_send("new_import_#{provider}_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
2017-08-17 22:00:37 +05:30
def status_import_url
2018-03-27 19:54:05 +05:30
public_send("status_import_#{provider}_url", extra_import_params) # rubocop:disable GitlabSecurity/PublicSend
2017-08-17 22:00:37 +05:30
end
def callback_import_url
2019-02-02 18:00:53 +05:30
public_send("users_import_#{provider}_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,
alert: "Access denied to your #{Gitlab::ImportSources.title(provider.to_s)} account."
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
2017-08-17 22:00:37 +05:30
def access_token_key
:"#{provider}_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-13 13:39:08 +05:30
# The following methods are overridden in subclasses
2017-08-17 22:00:37 +05:30
def provider
:github
end
2019-07-07 11:18:12 +05:30
def provider_url
strong_memoize(:provider_url) do
provider = Gitlab::Auth::OAuth::Provider.config_for('github')
provider&.dig('url').presence || 'https://github.com'
end
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?
current_user.identities.exists?(provider: provider)
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?
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
def sanitized_filter_param
@filter ||= sanitize(params[:filter])
end
def filter_attribute
:name
end
def filtered(collection)
return collection unless sanitized_filter_param
collection.select { |item| item[filter_attribute].include?(sanitized_filter_param) }
end
2015-04-26 12:48:37 +05:30
end
2019-12-04 20:38:33 +05:30
Import::GithubController.prepend_if_ee('EE::Import::GithubController')