2015-02-05 13:31:36 -05:00
|
|
|
class Import::GithubController < Import::BaseController
|
2016-12-15 11:31:14 -05:00
|
|
|
before_action :verify_import_enabled
|
|
|
|
before_action :provider_auth, only: [:status, :jobs, :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
|
2016-12-15 11:31:14 -05:00
|
|
|
if logged_in_with_provider?
|
|
|
|
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
|
2016-12-16 11:43:34 -05:00
|
|
|
session[access_token_key] = params[:personal_access_token]
|
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
|
2015-02-05 19:57:27 -05:00
|
|
|
@repos = client.repos
|
2016-12-15 11:31:14 -05:00
|
|
|
@already_added_projects = current_user.created_projects.where(import_type: provider)
|
2014-12-31 08:07:48 -05:00
|
|
|
already_added_projects_names = @already_added_projects.pluck(:import_source)
|
|
|
|
|
2016-12-15 11:31:14 -05:00
|
|
|
@repos.reject! { |repo| already_added_projects_names.include? repo.full_name }
|
2014-12-31 08:07:48 -05:00
|
|
|
end
|
|
|
|
|
2015-01-20 14:52:55 -05:00
|
|
|
def jobs
|
2016-12-15 11:31:14 -05:00
|
|
|
jobs = current_user.created_projects.where(import_type: provider).to_json(only: [:id, :import_status])
|
2015-01-20 14:52:55 -05:00
|
|
|
render json: jobs
|
|
|
|
end
|
|
|
|
|
2014-12-31 08:07:48 -05:00
|
|
|
def create
|
|
|
|
@repo_id = params[:repo_id].to_i
|
2015-02-05 19:57:27 -05:00
|
|
|
repo = client.repo(@repo_id)
|
2016-08-08 09:05:58 -04:00
|
|
|
@project_name = params[:new_name].presence || repo.name
|
|
|
|
namespace_path = params[:target_namespace].presence || current_user.namespace_path
|
|
|
|
@target_namespace = find_or_create_namespace(namespace_path, current_user.namespace_path)
|
2014-12-31 08:07:48 -05:00
|
|
|
|
2016-12-15 11:31:14 -05:00
|
|
|
if can?(current_user, :create_projects, @target_namespace)
|
|
|
|
@project = Gitlab::GithubImport::ProjectCreator.new(repo, @project_name, @target_namespace, current_user, access_params, type: provider).execute
|
2016-08-30 13:34:37 -04:00
|
|
|
else
|
|
|
|
render 'unauthorized'
|
|
|
|
end
|
2014-12-31 08:07:48 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def client
|
2016-12-16 11:43:34 -05:00
|
|
|
@client ||= Gitlab::GithubImport::Client.new(session[access_token_key], client_options)
|
2014-12-31 08:07:48 -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
|
|
|
|
|
2016-12-15 11:31:14 -05:00
|
|
|
def new_import_url
|
2017-08-03 22:20:34 -04:00
|
|
|
public_send("new_import_#{provider}_url") # 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
|
2017-08-03 22:20:34 -04:00
|
|
|
public_send("status_import_#{provider}_url") # 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
|
2017-08-03 22:20:34 -04:00
|
|
|
public_send("callback_import_#{provider}_url") # 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
|
|
|
|
|
|
|
|
# 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
|
2016-12-16 11:43:34 -05:00
|
|
|
if session[access_token_key].blank?
|
2016-12-15 11:31:14 -05:00
|
|
|
go_to_provider_for_permissions
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def client_options
|
|
|
|
{}
|
2015-08-07 03:06:20 -04:00
|
|
|
end
|
2014-12-31 08:07:48 -05:00
|
|
|
end
|