2015-02-05 18:31:36 +00:00
|
|
|
class Import::GithubController < Import::BaseController
|
2015-04-16 12:03:37 +00:00
|
|
|
before_action :verify_github_import_enabled
|
2016-06-27 11:42:22 +00:00
|
|
|
before_action :github_auth, only: [:status, :jobs, :create]
|
2014-12-31 13:07:48 +00:00
|
|
|
|
|
|
|
rescue_from Octokit::Unauthorized, with: :github_unauthorized
|
2015-01-13 19:44:17 +00:00
|
|
|
|
2016-05-02 15:22:38 +00:00
|
|
|
helper_method :logged_in_with_github?
|
|
|
|
|
|
|
|
def new
|
|
|
|
if logged_in_with_github?
|
|
|
|
go_to_github_for_permissions
|
|
|
|
elsif session[:github_access_token]
|
|
|
|
redirect_to status_import_github_url
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-12-31 13:07:48 +00:00
|
|
|
def callback
|
2015-08-07 07:06:20 +00:00
|
|
|
session[:github_access_token] = client.get_token(params[:code])
|
2015-02-03 01:01:07 +00:00
|
|
|
redirect_to status_import_github_url
|
2014-12-31 13:07:48 +00:00
|
|
|
end
|
|
|
|
|
2016-05-02 15:22:38 +00:00
|
|
|
def personal_access_token
|
|
|
|
session[:github_access_token] = params[:personal_access_token]
|
|
|
|
redirect_to status_import_github_url
|
|
|
|
end
|
|
|
|
|
2014-12-31 13:07:48 +00:00
|
|
|
def status
|
2015-02-06 00:57:27 +00:00
|
|
|
@repos = client.repos
|
2014-12-31 13:07:48 +00:00
|
|
|
@already_added_projects = current_user.created_projects.where(import_type: "github")
|
|
|
|
already_added_projects_names = @already_added_projects.pluck(:import_source)
|
|
|
|
|
2015-02-03 02:25:33 +00:00
|
|
|
@repos.reject!{ |repo| already_added_projects_names.include? repo.full_name }
|
2014-12-31 13:07:48 +00:00
|
|
|
end
|
|
|
|
|
2015-01-20 19:52:55 +00:00
|
|
|
def jobs
|
2015-02-03 03:30:09 +00:00
|
|
|
jobs = current_user.created_projects.where(import_type: "github").to_json(only: [:id, :import_status])
|
2015-01-20 19:52:55 +00:00
|
|
|
render json: jobs
|
|
|
|
end
|
|
|
|
|
2014-12-31 13:07:48 +00:00
|
|
|
def create
|
|
|
|
@repo_id = params[:repo_id].to_i
|
2015-02-06 00:57:27 +00:00
|
|
|
repo = client.repo(@repo_id)
|
2016-08-08 13:05:58 +00: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 13:07:48 +00:00
|
|
|
|
2016-08-30 17:34:37 +00:00
|
|
|
if current_user.can?(:create_projects, @target_namespace)
|
2016-08-08 13:05:58 +00:00
|
|
|
@project = Gitlab::GithubImport::ProjectCreator.new(repo, @project_name, @target_namespace, current_user, access_params).execute
|
2016-08-30 17:34:37 +00:00
|
|
|
else
|
|
|
|
render 'unauthorized'
|
|
|
|
end
|
2014-12-31 13:07:48 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def client
|
2015-08-07 07:06:20 +00:00
|
|
|
@client ||= Gitlab::GithubImport::Client.new(session[:github_access_token])
|
2014-12-31 13:07:48 +00:00
|
|
|
end
|
|
|
|
|
2015-02-17 21:52:32 +00:00
|
|
|
def verify_github_import_enabled
|
2015-10-09 17:07:29 +00:00
|
|
|
render_404 unless github_import_enabled?
|
2015-02-17 21:52:32 +00:00
|
|
|
end
|
|
|
|
|
2014-12-31 13:07:48 +00:00
|
|
|
def github_auth
|
2015-08-07 07:06:20 +00:00
|
|
|
if session[:github_access_token].blank?
|
2015-01-18 15:29:37 +00:00
|
|
|
go_to_github_for_permissions
|
2014-12-31 13:07:48 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-01-18 15:29:37 +00:00
|
|
|
def go_to_github_for_permissions
|
2015-02-06 00:57:27 +00:00
|
|
|
redirect_to client.authorize_url(callback_import_github_url)
|
2014-12-31 13:07:48 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def github_unauthorized
|
2016-05-02 15:22:38 +00:00
|
|
|
session[:github_access_token] = nil
|
|
|
|
redirect_to new_import_github_url,
|
|
|
|
alert: 'Access denied to your GitHub account.'
|
2014-12-31 13:07:48 +00:00
|
|
|
end
|
2015-08-07 07:06:20 +00:00
|
|
|
|
2016-05-02 15:22:38 +00:00
|
|
|
def logged_in_with_github?
|
|
|
|
current_user.identities.exists?(provider: 'github')
|
|
|
|
end
|
2015-08-07 07:06:20 +00:00
|
|
|
|
|
|
|
def access_params
|
|
|
|
{ github_access_token: session[:github_access_token] }
|
|
|
|
end
|
2014-12-31 13:07:48 +00:00
|
|
|
end
|