2018-09-23 15:44:14 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-02-05 13:31:36 -05:00
|
|
|
class Import::GithubController < Import::BaseController
|
2018-11-07 11:44:21 -05:00
|
|
|
include ImportHelper
|
2019-09-18 11:06:03 -04:00
|
|
|
include ActionView::Helpers::SanitizeHelper
|
2018-11-07 11:44:21 -05:00
|
|
|
|
2016-12-15 11:31:14 -05:00
|
|
|
before_action :verify_import_enabled
|
2018-11-07 11:44:21 -05:00
|
|
|
before_action :provider_auth, only: [:status, :realtime_changes, :create]
|
|
|
|
before_action :expire_etag_cache, only: [:status, :create]
|
2014-12-31 08:07:48 -05:00
|
|
|
|
2016-12-15 11:31:14 -05:00
|
|
|
rescue_from Octokit::Unauthorized, with: :provider_unauthorized
|
2016-05-02 11:22:38 -04:00
|
|
|
|
|
|
|
def new
|
2019-07-15 13:15:10 -04:00
|
|
|
if !ci_cd_only? && github_import_configured? && logged_in_with_provider?
|
2016-12-15 11:31:14 -05:00
|
|
|
go_to_provider_for_permissions
|
2016-12-16 11:43:34 -05:00
|
|
|
elsif session[access_token_key]
|
2016-12-15 11:31:14 -05:00
|
|
|
redirect_to status_import_url
|
2016-05-02 11:22:38 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-12-31 08:07:48 -05:00
|
|
|
def callback
|
2016-12-16 11:43:34 -05:00
|
|
|
session[access_token_key] = client.get_token(params[:code])
|
2016-12-15 11:31:14 -05:00
|
|
|
redirect_to status_import_url
|
2014-12-31 08:07:48 -05:00
|
|
|
end
|
|
|
|
|
2016-05-02 11:22:38 -04:00
|
|
|
def personal_access_token
|
2018-10-17 16:02:40 -04:00
|
|
|
session[access_token_key] = params[:personal_access_token]&.strip
|
2016-12-15 11:31:14 -05:00
|
|
|
redirect_to status_import_url
|
2016-05-02 11:22:38 -04:00
|
|
|
end
|
|
|
|
|
2014-12-31 08:07:48 -05:00
|
|
|
def status
|
2018-11-07 11:44:21 -05:00
|
|
|
# Request repos to display error page if provider token is invalid
|
2019-09-18 10:02:45 -04:00
|
|
|
# Improving in https://gitlab.com/gitlab-org/gitlab-foss/issues/55585
|
2018-11-07 11:44:21 -05:00
|
|
|
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-01-20 14:52:55 -05:00
|
|
|
end
|
|
|
|
|
2014-12-31 08:07:48 -05:00
|
|
|
def create
|
2019-01-17 05:37:08 -05:00
|
|
|
result = Import::GithubService.new(client, current_user, import_params).execute(access_params, provider)
|
|
|
|
|
|
|
|
if result[:status] == :success
|
2018-11-07 11:44:21 -05:00
|
|
|
render json: serialized_imported_projects(result[:project])
|
2016-08-30 13:34:37 -04:00
|
|
|
else
|
2019-01-17 05:37:08 -05:00
|
|
|
render json: { errors: result[:message] }, status: result[:http_status]
|
2016-08-30 13:34:37 -04:00
|
|
|
end
|
2014-12-31 08:07:48 -05:00
|
|
|
end
|
|
|
|
|
2018-11-07 11:44:21 -05:00
|
|
|
def realtime_changes
|
|
|
|
Gitlab::PollingInterval.set_header(response, interval: 3_000)
|
|
|
|
|
2019-09-18 11:06:03 -04:00
|
|
|
render json: already_added_projects.to_json(only: [:id], methods: [:import_status])
|
2018-11-07 11:44:21 -05:00
|
|
|
end
|
|
|
|
|
2014-12-31 08:07:48 -05:00
|
|
|
private
|
|
|
|
|
2019-01-17 05:37:08 -05:00
|
|
|
def import_params
|
2019-01-17 08:21:37 -05:00
|
|
|
params.permit(permitted_import_params)
|
|
|
|
end
|
|
|
|
|
|
|
|
def permitted_import_params
|
|
|
|
[:repo_id, :new_name, :target_namespace]
|
2019-01-17 05:37:08 -05:00
|
|
|
end
|
|
|
|
|
2018-11-07 11:44:21 -05:00
|
|
|
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 }
|
|
|
|
ProviderRepoSerializer.new(current_user: current_user).represent(repos, provider: provider, provider_url: provider_url)
|
|
|
|
end
|
|
|
|
|
|
|
|
def serialized_namespaces
|
|
|
|
NamespaceSerializer.new.represent(namespaces)
|
|
|
|
end
|
|
|
|
|
|
|
|
def already_added_projects
|
2019-09-18 11:06:03 -04:00
|
|
|
@already_added_projects ||= filtered(find_already_added_projects(provider))
|
2018-11-07 11:44:21 -05:00
|
|
|
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
|
|
|
|
|
2014-12-31 08:07:48 -05:00
|
|
|
def client
|
2017-10-13 12:50:36 -04:00
|
|
|
@client ||= Gitlab::LegacyGithubImport::Client.new(session[access_token_key], client_options)
|
2014-12-31 08:07:48 -05:00
|
|
|
end
|
|
|
|
|
2018-11-07 11:44:21 -05:00
|
|
|
def client_repos
|
2019-09-18 11:06:03 -04:00
|
|
|
@client_repos ||= filtered(client.repos)
|
2018-11-07 11:44:21 -05:00
|
|
|
end
|
|
|
|
|
2016-12-15 11:31:14 -05:00
|
|
|
def verify_import_enabled
|
|
|
|
render_404 unless import_enabled?
|
2015-02-17 16:52:32 -05:00
|
|
|
end
|
|
|
|
|
2016-12-15 11:31:14 -05:00
|
|
|
def go_to_provider_for_permissions
|
|
|
|
redirect_to client.authorize_url(callback_import_url)
|
|
|
|
end
|
|
|
|
|
|
|
|
def import_enabled?
|
2017-08-10 12:39:26 -04:00
|
|
|
__send__("#{provider}_import_enabled?") # rubocop:disable GitlabSecurity/PublicSend
|
2014-12-31 08:07:48 -05:00
|
|
|
end
|
|
|
|
|
2018-11-07 11:44:21 -05:00
|
|
|
def realtime_changes_path
|
|
|
|
public_send("realtime_changes_import_#{provider}_path", format: :json) # rubocop:disable GitlabSecurity/PublicSend
|
|
|
|
end
|
|
|
|
|
2016-12-15 11:31:14 -05:00
|
|
|
def new_import_url
|
2018-03-07 10:58:50 -05:00
|
|
|
public_send("new_import_#{provider}_url", extra_import_params) # rubocop:disable GitlabSecurity/PublicSend
|
2014-12-31 08:07:48 -05:00
|
|
|
end
|
|
|
|
|
2016-12-15 11:31:14 -05:00
|
|
|
def status_import_url
|
2018-03-07 10:58:50 -05:00
|
|
|
public_send("status_import_#{provider}_url", extra_import_params) # rubocop:disable GitlabSecurity/PublicSend
|
2014-12-31 08:07:48 -05:00
|
|
|
end
|
2015-08-07 03:06:20 -04:00
|
|
|
|
2016-12-15 11:31:14 -05:00
|
|
|
def callback_import_url
|
2019-01-22 12:38:08 -05:00
|
|
|
public_send("users_import_#{provider}_callback_url", extra_import_params) # rubocop:disable GitlabSecurity/PublicSend
|
2016-12-15 11:31:14 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def provider_unauthorized
|
2016-12-16 11:43:34 -05:00
|
|
|
session[access_token_key] = nil
|
2016-12-15 11:31:14 -05:00
|
|
|
redirect_to new_import_url,
|
2016-12-16 03:15:30 -05:00
|
|
|
alert: "Access denied to your #{Gitlab::ImportSources.title(provider.to_s)} account."
|
2016-05-02 11:22:38 -04:00
|
|
|
end
|
2015-08-07 03:06:20 -04:00
|
|
|
|
2016-12-16 11:43:34 -05:00
|
|
|
def access_token_key
|
|
|
|
:"#{provider}_access_token"
|
|
|
|
end
|
|
|
|
|
2015-08-07 03:06:20 -04:00
|
|
|
def access_params
|
2016-12-16 11:43:34 -05:00
|
|
|
{ github_access_token: session[access_token_key] }
|
2016-12-15 11:31:14 -05:00
|
|
|
end
|
|
|
|
|
2018-10-30 06:53:01 -04:00
|
|
|
# The following methods are overridden in subclasses
|
2016-12-15 11:31:14 -05:00
|
|
|
def provider
|
|
|
|
:github
|
|
|
|
end
|
|
|
|
|
2018-11-07 11:44:21 -05:00
|
|
|
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-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2016-12-15 11:31:14 -05:00
|
|
|
def logged_in_with_provider?
|
|
|
|
current_user.identities.exists?(provider: provider)
|
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2016-12-15 11:31:14 -05:00
|
|
|
|
|
|
|
def provider_auth
|
2019-07-15 13:15:10 -04:00
|
|
|
if !ci_cd_only? && session[access_token_key].blank?
|
2016-12-15 11:31:14 -05:00
|
|
|
go_to_provider_for_permissions
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-07-15 13:15:10 -04:00
|
|
|
def ci_cd_only?
|
|
|
|
%w[1 true].include?(params[:ci_cd_only])
|
|
|
|
end
|
|
|
|
|
2016-12-15 11:31:14 -05:00
|
|
|
def client_options
|
|
|
|
{}
|
2015-08-07 03:06:20 -04:00
|
|
|
end
|
2018-03-07 10:58:50 -05:00
|
|
|
|
|
|
|
def extra_import_params
|
|
|
|
{}
|
|
|
|
end
|
2019-09-18 11:06:03 -04:00
|
|
|
|
|
|
|
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
|
2014-12-31 08:07:48 -05:00
|
|
|
end
|
2019-09-13 09:26:31 -04:00
|
|
|
|
|
|
|
Import::GithubController.prepend_if_ee('EE::Import::GithubController')
|