2015-04-26 12:48:37 +05:30
|
|
|
class Import::GithubController < Import::BaseController
|
2017-08-17 22:00:37 +05:30
|
|
|
before_action :verify_import_enabled
|
|
|
|
before_action :provider_auth, only: [:status, :jobs, :create]
|
2015-04-26 12:48:37 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
rescue_from Octokit::Unauthorized, with: :provider_unauthorized
|
2016-08-24 12:49:21 +05:30
|
|
|
|
|
|
|
def new
|
2017-08-17 22:00:37 +05:30
|
|
|
if logged_in_with_provider?
|
|
|
|
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
|
2017-08-17 22:00:37 +05:30
|
|
|
session[access_token_key] = params[:personal_access_token]
|
|
|
|
redirect_to status_import_url
|
2016-08-24 12:49:21 +05:30
|
|
|
end
|
|
|
|
|
2015-04-26 12:48:37 +05:30
|
|
|
def status
|
|
|
|
@repos = client.repos
|
2018-10-15 14:42:47 +05:30
|
|
|
@already_added_projects = find_already_added_projects(provider)
|
2015-04-26 12:48:37 +05:30
|
|
|
already_added_projects_names = @already_added_projects.pluck(:import_source)
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
@repos.reject! { |repo| already_added_projects_names.include? repo.full_name }
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def jobs
|
2018-10-15 14:42:47 +05:30
|
|
|
render json: find_jobs(provider)
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
2018-03-27 19:54:05 +05:30
|
|
|
repo = client.repo(params[:repo_id].to_i)
|
|
|
|
project_name = params[:new_name].presence || repo.name
|
2016-09-29 09:46:39 +05:30
|
|
|
namespace_path = params[:target_namespace].presence || current_user.namespace_path
|
2018-03-27 19:54:05 +05:30
|
|
|
target_namespace = find_or_create_namespace(namespace_path, current_user.namespace_path)
|
|
|
|
|
|
|
|
if can?(current_user, :create_projects, target_namespace)
|
|
|
|
project = Gitlab::LegacyGithubImport::ProjectCreator
|
|
|
|
.new(repo, project_name, target_namespace, current_user, access_params, type: provider)
|
|
|
|
.execute(extra_project_attrs)
|
|
|
|
|
|
|
|
if project.persisted?
|
|
|
|
render json: ProjectSerializer.new.represent(project)
|
|
|
|
else
|
|
|
|
render json: { errors: project.errors.full_messages }, status: :unprocessable_entity
|
|
|
|
end
|
2016-09-29 09:46:39 +05:30
|
|
|
else
|
2018-03-27 19:54:05 +05:30
|
|
|
render json: { errors: 'This namespace has already been taken! Please choose another one.' }, status: :unprocessable_entity
|
2016-09-29 09:46:39 +05:30
|
|
|
end
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
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
|
|
|
|
|
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
|
|
|
|
|
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
|
2018-03-27 19:54:05 +05:30
|
|
|
public_send("callback_import_#{provider}_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
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
# The following methods are overriden in subclasses
|
|
|
|
def provider
|
|
|
|
:github
|
|
|
|
end
|
|
|
|
|
|
|
|
def logged_in_with_provider?
|
|
|
|
current_user.identities.exists?(provider: provider)
|
|
|
|
end
|
|
|
|
|
|
|
|
def provider_auth
|
|
|
|
if session[access_token_key].blank?
|
|
|
|
go_to_provider_for_permissions
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def client_options
|
|
|
|
{}
|
2015-09-25 12:07:36 +05:30
|
|
|
end
|
2018-03-27 19:54:05 +05:30
|
|
|
|
|
|
|
def extra_project_attrs
|
|
|
|
{}
|
|
|
|
end
|
|
|
|
|
|
|
|
def extra_import_params
|
|
|
|
{}
|
|
|
|
end
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|