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