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
|
2016-12-15 16:31:14 +00:00
|
|
|
before_action :verify_import_enabled
|
|
|
|
before_action :provider_auth, only: [:status, :jobs, :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
|
2016-12-15 16:31:14 +00:00
|
|
|
if logged_in_with_provider?
|
|
|
|
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
|
|
|
|
|
2018-08-27 15:31:01 +00:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2014-12-31 13:07:48 +00:00
|
|
|
def status
|
2015-02-06 00:57:27 +00:00
|
|
|
@repos = client.repos
|
2018-05-02 13:35:04 +00:00
|
|
|
@already_added_projects = find_already_added_projects(provider)
|
2014-12-31 13:07:48 +00:00
|
|
|
already_added_projects_names = @already_added_projects.pluck(:import_source)
|
|
|
|
|
2016-12-15 16:31:14 +00:00
|
|
|
@repos.reject! { |repo| already_added_projects_names.include? repo.full_name }
|
2014-12-31 13:07:48 +00:00
|
|
|
end
|
2018-08-27 15:31:01 +00:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2014-12-31 13:07:48 +00:00
|
|
|
|
2015-01-20 19:52:55 +00:00
|
|
|
def jobs
|
2018-05-02 13:35:04 +00:00
|
|
|
render json: find_jobs(provider)
|
2015-01-20 19:52:55 +00:00
|
|
|
end
|
|
|
|
|
2014-12-31 13:07:48 +00:00
|
|
|
def create
|
2018-02-09 11:14:48 +00:00
|
|
|
repo = client.repo(params[:repo_id].to_i)
|
|
|
|
project_name = params[:new_name].presence || repo.name
|
2016-08-08 13:05:58 +00:00
|
|
|
namespace_path = params[:target_namespace].presence || current_user.namespace_path
|
2018-02-09 11:14:48 +00:00
|
|
|
target_namespace = find_or_create_namespace(namespace_path, current_user.namespace_path)
|
2014-12-31 13:07:48 +00:00
|
|
|
|
2018-02-09 11:14:48 +00:00
|
|
|
if can?(current_user, :create_projects, target_namespace)
|
2018-03-07 15:58:50 +00:00
|
|
|
project = Gitlab::LegacyGithubImport::ProjectCreator
|
|
|
|
.new(repo, project_name, target_namespace, current_user, access_params, type: provider)
|
|
|
|
.execute(extra_project_attrs)
|
2018-02-09 11:14:48 +00:00
|
|
|
|
|
|
|
if project.persisted?
|
|
|
|
render json: ProjectSerializer.new.represent(project)
|
|
|
|
else
|
2018-06-06 05:34:06 +00:00
|
|
|
render json: { errors: project_save_error(project) }, status: :unprocessable_entity
|
2018-02-09 11:14:48 +00:00
|
|
|
end
|
2016-08-30 17:34:37 +00:00
|
|
|
else
|
2018-02-09 11:14:48 +00:00
|
|
|
render json: { errors: 'This namespace has already been taken! Please choose another one.' }, status: :unprocessable_entity
|
2016-08-30 17:34:37 +00:00
|
|
|
end
|
2014-12-31 13:07:48 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
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
|
|
|
|
|
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
|
|
|
|
|
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
|
2018-03-07 15:58:50 +00:00
|
|
|
public_send("callback_import_#{provider}_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-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
|
2016-12-16 16:43:34 +00:00
|
|
|
if session[access_token_key].blank?
|
2016-12-15 16:31:14 +00:00
|
|
|
go_to_provider_for_permissions
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def client_options
|
|
|
|
{}
|
2015-08-07 07:06:20 +00:00
|
|
|
end
|
2018-03-07 15:58:50 +00:00
|
|
|
|
|
|
|
def extra_project_attrs
|
|
|
|
{}
|
|
|
|
end
|
|
|
|
|
|
|
|
def extra_import_params
|
|
|
|
{}
|
|
|
|
end
|
2014-12-31 13:07:48 +00:00
|
|
|
end
|